@stacksjs/arrays 0.75.422 → 0.75.423
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/helpers.d.ts +1 -1
- package/dist/math.d.ts +16 -9
- package/dist/math.js +1 -1
- package/package.json +1 -1
package/dist/helpers.d.ts
CHANGED
|
@@ -136,7 +136,7 @@ export declare function at<T>(array: readonly T[], index: number): T;
|
|
|
136
136
|
* move([1, 2, 3, 4], 0, 2) // [2, 3, 1, 4]
|
|
137
137
|
* move([1, 2, 3, 4], 0, -1) // [2, 3, 4, 1]
|
|
138
138
|
* move([1, 2, 3, 4], -1, 0) // [4, 1, 2, 3]
|
|
139
|
-
* move([1, 2, 3, 4], -1, -2) // [1,
|
|
139
|
+
* move([1, 2, 3, 4], -1, -2) // [1, 2, 4, 3]
|
|
140
140
|
* move([1, 2, 3, 4], 1, 1) // [1, 2, 3, 4]
|
|
141
141
|
* ```
|
|
142
142
|
*/
|
package/dist/math.d.ts
CHANGED
|
@@ -126,41 +126,48 @@ export declare function variance(array: number[]): number;
|
|
|
126
126
|
*/
|
|
127
127
|
export declare function standardDeviation(array: number[]): number;
|
|
128
128
|
/**
|
|
129
|
-
* Returns the z-score of a number in an array of numbers
|
|
129
|
+
* Returns the z-score of a number in an array of numbers: how many
|
|
130
|
+
* (population) standard deviations it lies from the array's mean.
|
|
130
131
|
* @param array
|
|
131
132
|
* @param num
|
|
132
133
|
* @category Array
|
|
133
134
|
* @example
|
|
134
135
|
* ```ts
|
|
135
|
-
* zScore([1, 2, 3, 4], 2) // 0
|
|
136
|
-
* zScore([1, 2, 3, 4], 3) // 0.
|
|
136
|
+
* zScore([1, 2, 3, 4], 2) // -0.4472135954999579
|
|
137
|
+
* zScore([1, 2, 3, 4], 3) // 0.4472135954999579
|
|
137
138
|
* ```
|
|
138
139
|
* @see https://en.wikipedia.org/wiki/Standard_score
|
|
139
140
|
*/
|
|
140
141
|
export declare function zScore(array: number[], num: number): number;
|
|
141
142
|
/**
|
|
142
|
-
* Returns the percentile
|
|
143
|
+
* Returns the value at the `num`-th percentile (0 to 100) of an array of
|
|
144
|
+
* numbers, interpolating linearly between the two nearest ranks (the method
|
|
145
|
+
* numpy uses by default). Throws for an empty array or a percentile outside
|
|
146
|
+
* 0 to 100, which used to come back as a made-up 0 or an extrapolation.
|
|
143
147
|
* @param array
|
|
144
148
|
* @param num
|
|
145
149
|
* @category Array
|
|
146
150
|
* @example
|
|
147
151
|
* ```ts
|
|
148
|
-
* percentile([1, 2, 3, 4],
|
|
149
|
-
* percentile([1, 2, 3, 4],
|
|
152
|
+
* percentile([1, 2, 3, 4], 50) // 2.5
|
|
153
|
+
* percentile([1, 2, 3, 4], 75) // 3.25
|
|
150
154
|
* ```
|
|
151
155
|
* @see https://en.wikipedia.org/wiki/Percentile
|
|
152
156
|
*/
|
|
153
157
|
export declare function percentile(array: number[], num: number): number;
|
|
154
158
|
/**
|
|
155
|
-
* Returns the interquartile range of an array of numbers
|
|
159
|
+
* Returns the interquartile range of an array of numbers: the median of the
|
|
160
|
+
* upper half minus the median of the lower half, leaving out the middle value
|
|
161
|
+
* of an odd-length array. The array is sorted first; the halves used to be
|
|
162
|
+
* taken in the order given, so `[4, 1, 3, 2]` answered 0 and `[1, 2, 3, 4]` 2.
|
|
156
163
|
* @param array
|
|
157
164
|
* @category Array
|
|
158
165
|
* @example
|
|
159
166
|
* ```ts
|
|
160
|
-
* interquartileRange([1, 2, 3, 4]) //
|
|
167
|
+
* interquartileRange([1, 2, 3, 4]) // 2
|
|
168
|
+
* interquartileRange([1, 2, 3, 4, 5, 6, 7, 8]) // 4
|
|
161
169
|
* ```
|
|
162
170
|
* @see https://en.wikipedia.org/wiki/Interquartile_range
|
|
163
|
-
* @see https://stackoverflow.com/questions/48719873/how-to-calculate-interquartile-range-in-javascript
|
|
164
171
|
*/
|
|
165
172
|
export declare function interquartileRange(array: number[]): number;
|
|
166
173
|
/**
|
package/dist/math.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export function average(arr){if(arr.length===0)throw Error("Cannot compute average of an empty array");return sum(arr)/arr.length}export function avg(arr){return average(arr)}export function median(arr){if(arr.length===0)throw Error("Cannot compute median of an empty array");const sorted=[...arr].sort((a,b)=>a-b),mid=Math.floor(sorted.length/2);let medianValue;if(sorted.length%2!==0)medianValue=sorted[mid];else medianValue=(sorted[mid]+sorted[mid-1])/2;return medianValue}export function mode(arr){if(arr.length===0)throw Error("Cannot compute mode of an empty array");const counts=new Map;for(const v of arr)counts.set(v,(counts.get(v)||0)+1);let maxCount=0,result=arr[0];for(const[value,count]of counts)if(count>maxCount){maxCount=count;result=value}return result}export function sum(array){return array.reduce((acc,cur)=>acc+cur,0)}export function product(array){let acc=1;for(const cur of array){acc*=cur;if(Number.isFinite(acc)&&Math.abs(acc)>Number.MAX_SAFE_INTEGER)throw RangeError(`[arrays.product] Result exceeds Number.MAX_SAFE_INTEGER (${Number.MAX_SAFE_INTEGER}); use BigInt for large products.`)}return acc}export function min(array){if(array.length===0)throw Error("Cannot compute min of an empty array");return Math.min(...array)}export function max(array){if(array.length===0)throw Error("Cannot compute max of an empty array");return Math.max(...array)}export function range(array){return max(array)-min(array)}export function variance(array){const mean=average(array);return average(array.map((num)=>(num-mean)**2))}export function standardDeviation(array){return Math.sqrt(variance(array))}export function zScore(array,num){return(num-average(array))/standardDeviation(array)}export function percentile(array,num){const sorted=[...array].sort((a,b)=>a-b),index=num/100*(sorted.length-1),lower=Math.floor(index),upper=Math.ceil(index),weight=index-lower;if(upper===lower)return sorted[index]??0;return(1-weight)*(sorted[lower]??0)+weight*(sorted[upper]??0)}export function interquartileRange(array){const q1=median(
|
|
1
|
+
export function average(arr){if(arr.length===0)throw Error("Cannot compute average of an empty array");return sum(arr)/arr.length}export function avg(arr){return average(arr)}export function median(arr){if(arr.length===0)throw Error("Cannot compute median of an empty array");const sorted=[...arr].sort((a,b)=>a-b),mid=Math.floor(sorted.length/2);let medianValue;if(sorted.length%2!==0)medianValue=sorted[mid];else medianValue=(sorted[mid]+sorted[mid-1])/2;return medianValue}export function mode(arr){if(arr.length===0)throw Error("Cannot compute mode of an empty array");const counts=new Map;for(const v of arr)counts.set(v,(counts.get(v)||0)+1);let maxCount=0,result=arr[0];for(const[value,count]of counts)if(count>maxCount){maxCount=count;result=value}return result}export function sum(array){return array.reduce((acc,cur)=>acc+cur,0)}export function product(array){let acc=1;for(const cur of array){acc*=cur;if(Number.isFinite(acc)&&Math.abs(acc)>Number.MAX_SAFE_INTEGER)throw RangeError(`[arrays.product] Result exceeds Number.MAX_SAFE_INTEGER (${Number.MAX_SAFE_INTEGER}); use BigInt for large products.`)}return acc}export function min(array){if(array.length===0)throw Error("Cannot compute min of an empty array");return Math.min(...array)}export function max(array){if(array.length===0)throw Error("Cannot compute max of an empty array");return Math.max(...array)}export function range(array){return max(array)-min(array)}export function variance(array){const mean=average(array);return average(array.map((num)=>(num-mean)**2))}export function standardDeviation(array){return Math.sqrt(variance(array))}export function zScore(array,num){return(num-average(array))/standardDeviation(array)}export function percentile(array,num){if(array.length===0)throw Error("Cannot compute a percentile of an empty array");if(!Number.isFinite(num)||num<0||num>100)throw RangeError(`A percentile is between 0 and 100, got ${num}`);const sorted=[...array].sort((a,b)=>a-b),index=num/100*(sorted.length-1),lower=Math.floor(index),upper=Math.ceil(index),weight=index-lower;if(upper===lower)return sorted[index]??0;return(1-weight)*(sorted[lower]??0)+weight*(sorted[upper]??0)}export function interquartileRange(array){if(array.length===0)throw Error("Cannot compute the interquartile range of an empty array");if(array.length===1)return 0;const sorted=[...array].sort((a,b)=>a-b),q1=median(sorted.slice(0,Math.floor(sorted.length/2)));return median(sorted.slice(Math.ceil(sorted.length/2)))-q1}export function covariance(array1,array2){if(array1.length!==array2.length)throw Error("Arrays must have the same length");const mean1=average(array1),mean2=average(array2);return average(array1.map((num1,i)=>(num1-mean1)*(array2[i]-mean2)))}
|