@naturalcycles/js-lib 15.60.0 → 15.61.0
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/math/math.util.d.ts +13 -0
- package/dist/math/math.util.js +24 -0
- package/package.json +1 -1
- package/src/math/math.util.ts +26 -0
package/dist/math/math.util.d.ts
CHANGED
|
@@ -11,6 +11,19 @@ export declare function _average(values: number[]): number;
|
|
|
11
11
|
* Same as _average, but safely returns null if input array is empty or nullish.
|
|
12
12
|
*/
|
|
13
13
|
export declare function _averageOrNull(values: number[] | undefined | null): number | null;
|
|
14
|
+
/**
|
|
15
|
+
* @returns Population standard deviation of the array of numbers
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
*
|
|
19
|
+
* _stdDev([2, 4, 4, 4, 5, 5, 7, 9])
|
|
20
|
+
* // 2
|
|
21
|
+
*/
|
|
22
|
+
export declare function _stdDev(values: number[]): number;
|
|
23
|
+
/**
|
|
24
|
+
* Same as _stdDev, but safely returns null if input array is empty or nullish.
|
|
25
|
+
*/
|
|
26
|
+
export declare function _stdDevOrNull(values: number[] | undefined | null): number | null;
|
|
14
27
|
/**
|
|
15
28
|
* valuesArray and weightsArray length is expected to be the same.
|
|
16
29
|
*/
|
package/dist/math/math.util.js
CHANGED
|
@@ -21,6 +21,30 @@ export function _average(values) {
|
|
|
21
21
|
export function _averageOrNull(values) {
|
|
22
22
|
return values?.length ? _average(values) : null;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* @returns Population standard deviation of the array of numbers
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
*
|
|
29
|
+
* _stdDev([2, 4, 4, 4, 5, 5, 7, 9])
|
|
30
|
+
* // 2
|
|
31
|
+
*/
|
|
32
|
+
export function _stdDev(values) {
|
|
33
|
+
_assert(values.length, '_stdDev is called on empty array');
|
|
34
|
+
const avg = _average(values);
|
|
35
|
+
let sumSquaredDiff = 0;
|
|
36
|
+
for (const n of values) {
|
|
37
|
+
const diff = n - avg;
|
|
38
|
+
sumSquaredDiff += diff * diff;
|
|
39
|
+
}
|
|
40
|
+
return Math.sqrt(sumSquaredDiff / values.length);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Same as _stdDev, but safely returns null if input array is empty or nullish.
|
|
44
|
+
*/
|
|
45
|
+
export function _stdDevOrNull(values) {
|
|
46
|
+
return values?.length ? _stdDev(values) : null;
|
|
47
|
+
}
|
|
24
48
|
/**
|
|
25
49
|
* valuesArray and weightsArray length is expected to be the same.
|
|
26
50
|
*/
|
package/package.json
CHANGED
package/src/math/math.util.ts
CHANGED
|
@@ -23,6 +23,32 @@ export function _averageOrNull(values: number[] | undefined | null): number | nu
|
|
|
23
23
|
return values?.length ? _average(values) : null
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* @returns Population standard deviation of the array of numbers
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
*
|
|
31
|
+
* _stdDev([2, 4, 4, 4, 5, 5, 7, 9])
|
|
32
|
+
* // 2
|
|
33
|
+
*/
|
|
34
|
+
export function _stdDev(values: number[]): number {
|
|
35
|
+
_assert(values.length, '_stdDev is called on empty array')
|
|
36
|
+
const avg = _average(values)
|
|
37
|
+
let sumSquaredDiff = 0
|
|
38
|
+
for (const n of values) {
|
|
39
|
+
const diff = n - avg
|
|
40
|
+
sumSquaredDiff += diff * diff
|
|
41
|
+
}
|
|
42
|
+
return Math.sqrt(sumSquaredDiff / values.length)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Same as _stdDev, but safely returns null if input array is empty or nullish.
|
|
47
|
+
*/
|
|
48
|
+
export function _stdDevOrNull(values: number[] | undefined | null): number | null {
|
|
49
|
+
return values?.length ? _stdDev(values) : null
|
|
50
|
+
}
|
|
51
|
+
|
|
26
52
|
/**
|
|
27
53
|
* valuesArray and weightsArray length is expected to be the same.
|
|
28
54
|
*/
|