diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/README.md b/lib/node_modules/@stdlib/stats/incr/wstdev/README.md new file mode 100644 index 000000000000..a24859fa00ff --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/README.md @@ -0,0 +1,173 @@ + + +# incrwstdev + +> Compute a [weighted sample standard deviation][weighted-sample-stdev] incrementally. + +
+ +The [weighted sample standard deviation][weighted-sample-stdev] is defined as + + + +```math +s = \sqrt{\frac{\displaystyle\sum_{i=0}^{n-1} w_{i} \left( x_{i} - \bar{x} \right)^2}{\displaystyle\sum_{i=0}^{n-1} w_{i}}} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrwstdev = require( '@stdlib/stats/incr/wstdev' ); +``` + +#### incrwstdev() + +Returns an accumulator `function` which incrementally computes a [weighted sample standard deviation][weighted-sample-stdev]. + +```javascript +var accumulator = incrwstdev(); +``` + +#### accumulator( \[x, w] ) + +If provided an input value `x` and a weight `w`, the accumulator function returns an updated weighted sample standard deviation. If not provided any input values, the accumulator function returns the current weighted sample standard deviation. + +```javascript +var accumulator = incrwstdev(); + +var s = accumulator( 2.0, 1.0 ); +// returns 0.0 + +s = accumulator( 3.0, 2.0 ); +// returns ~0.471 + +s = accumulator( 2.0, 0.1 ); +// returns ~0.478 + +s = accumulator(); +// returns ~0.478 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/base/uniform' ); +var incrwstdev = require( '@stdlib/stats/incr/wstdev' ); + +var accumulator; +var v; +var w; +var i; + +// Initialize an accumulator: +accumulator = incrwstdev(); + +// For each simulated datum, update the weighted sample standard deviation... +for ( i = 0; i < 100; i++ ) { + v = uniform( 0.0, 100.0 ); + w = uniform( 0.0, 100.0 ); + accumulator( v, w ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/wstdev/benchmark/benchmark.js new file mode 100644 index 000000000000..bfde44f09956 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/benchmark/benchmark.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Float64Array = require( '@stdlib/array/float64' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var pkg = require( './../package.json' ).name; +var incrwstdev = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrwstdev(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator', function benchmark( b ) { + var acc; + var len; + var x; + var v; + var i; + + acc = incrwstdev(); + len = 100; + x = new Float64Array( len ); + for ( i = 0; i < len; i++ ) { + x[ i ] = uniform( 0.0, 1.0 ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( x[ i % len ], 1.0 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator,known_mean', function benchmark( b ) { + var acc; + var len; + var x; + var v; + var i; + + acc = incrwstdev( 3.0 ); + len = 100; + x = new Float64Array( len ); + for ( i = 0; i < len; i++ ) { + x[ i ] = uniform( 0.0, 1.0 ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( x[ i % len ], 1.0 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/docs/img/equation_weighted_sample_standard_deviation.svg b/lib/node_modules/@stdlib/stats/incr/wstdev/docs/img/equation_weighted_sample_standard_deviation.svg new file mode 100644 index 000000000000..b8cd2b76c494 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/docs/img/equation_weighted_sample_standard_deviation.svg @@ -0,0 +1,89 @@ + +s equals StartRoot StartFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts w Subscript i Baseline left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis squared Over sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts w Subscript i Baseline EndFraction EndRoot + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/wstdev/docs/repl.txt new file mode 100644 index 000000000000..d09d84cf54c9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/docs/repl.txt @@ -0,0 +1,44 @@ + +{{alias}}( [mean] ) + Returns an accumulator function which incrementally computes a weighted + sample standard deviation. + + If provided arguments, the accumulator function returns an updated weighted + sample standard deviation. If not provided arguments, the accumulator + function returns the current weighted sample standard deviation. + + If provided `NaN` or a value which, when used in computations, results in + `NaN`, the accumulated value is `NaN` for all future invocations. + + The accumulator function accepts two arguments: + + - x: value. + - w: weight. + + Parameters + ---------- + mean: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var s = accumulator() + null + > s = accumulator( 2.0, 1.0 ) + 0.0 + > s = accumulator( 3.0, 2.0 ) + ~0.471 + > s = accumulator( 2.0, 0.1 ) + ~0.478 + > s = accumulator() + ~0.478 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/wstdev/docs/types/index.d.ts new file mode 100644 index 000000000000..7733a565c25d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/docs/types/index.d.ts @@ -0,0 +1,68 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided both arguments, returns an updated weighted sample standard deviation; otherwise, returns the current weighted sample standard deviation. +* +* ## Notes +* +* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations. +* +* @param x - value +* @param {NonNegativeNumber} [w] - weight +* @throws {TypeError} second argument must be a nonnegative number +* @returns weighted sample standard deviation +*/ +type accumulator = ( x?: number, w?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a weighted sample standard deviation. +* +* @returns accumulator function +* +* @example +* var accumulator = incrwstdev(); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* s = accumulator( 3.0, 2.0 ); +* // returns ~0.471 +* +* s = accumulator( 2.0, 0.1 ); +* // returns ~0.478 +* +* s = accumulator(); +* // returns ~0.478 +* +* @example +* var accumulator = incrwstdev( 3.0 ); +*/ +declare function incrwstdev(): accumulator; + + +// EXPORTS // + +export = incrwstdev; diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/wstdev/docs/types/test.ts new file mode 100644 index 000000000000..d9980f114fc5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrwstdev = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrwstdev(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided arguments... +{ + incrwstdev( '5' ); // $ExpectError + incrwstdev( 5 ); // $ExpectError + incrwstdev( true ); // $ExpectError + incrwstdev( false ); // $ExpectError + incrwstdev( null ); // $ExpectError + incrwstdev( undefined ); // $ExpectError + incrwstdev( [] ); // $ExpectError + incrwstdev( {} ); // $ExpectError + incrwstdev( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrwstdev(); + + acc(); // $ExpectType number | null + acc( 3.14, 1.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrwstdev(); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/examples/index.js b/lib/node_modules/@stdlib/stats/incr/wstdev/examples/index.js new file mode 100644 index 000000000000..fb7cf6433ef2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/examples/index.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var uniform = require( '@stdlib/random/base/uniform' ); +var incrwstdev = require( './../lib' ); + +var accumulator; +var s; +var x; +var w; +var i; + +// Initialize an accumulator: +accumulator = incrwstdev(); + +// For each simulated datum, update the weighted sample standard deviation... +console.log( '\nValue\tWeight\tSigma\n' ); +for ( i = 0; i < 100; i++ ) { + x = uniform( 0.0, 100.0 ); + w = uniform( 0.0, 100.0 ); + s = accumulator( x, w ); + console.log( '%d\t%d\t%d', x.toFixed( 4 ), w.toFixed( 4 ), s.toFixed( 4 ) ); +} +console.log( '\nFinal weighted sample standard deviation: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/lib/index.js b/lib/node_modules/@stdlib/stats/incr/wstdev/lib/index.js new file mode 100644 index 000000000000..fcb9fc092d2f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute a weighted sample standard deviation incrementally. +* +* @module @stdlib/stats/incr/wstdev +* +* @example +* var incrwstdev = require( '@stdlib/stats/incr/wstdev' ); +* +* var accumulator = incrwstdev(); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* s = accumulator( 3.0, 2.0 ); +* // returns ~0.471 +* +* s = accumulator( 2.0, 0.1 ); +* // returns ~0.478 +* +* s = accumulator(); +* // returns ~0.478 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/lib/main.js b/lib/node_modules/@stdlib/stats/incr/wstdev/lib/main.js new file mode 100644 index 000000000000..b9f442e69caf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/lib/main.js @@ -0,0 +1,206 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ).isPrimitive; +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a weighted sample standard deviation. +* +* ## Method +* +* - The weighted sample standard deviation is defined as +* +* ```tex +* \sigma = \sqrt{\frac{\sum_{i=0}^{n-1} w_i (x_i - \mu)^2}{\sum_{i=0}^{n-1} w_i}} +* ``` +* +* where \\( w_i \\) are the weights and \\( \mu \\) is the weighted arithmetic mean. +* +* - To derive an incremental formula for computing a weighted sample standard deviation, let: +* +* ```tex +* W_n = \sum_{i=1}^{n} w_i +* ``` +* +* - Accordingly: +* +* ```tex +* \begin{align*} +* \sigma^2 &= \frac{1}{W_n} \sum_{i=1}^{n} w_i (x_i - \mu)^2 \\ +* &= \frac{1}{W_n} \sum_{i=1}^{n} w_i x_i^2 - \mu^2 +* \end{align*} +* ``` +* +* - Let: +* +* ```tex +* M2_n = W_n \sigma^2_n = \sum_{i=1}^{n} w_i x_i^2 - W_n \mu_n^2 +* ``` +* +* - To compute the incremental update for \\(M2_n\\): +* +* ```tex +* \begin{align*} +* M2_n - M2_{n-1} &= \sum_{i=1}^{n} w_i x_i^2 - W_n \mu_n^2 - \sum_{i=1}^{n-1} w_i x_i^2 + W_{n-1} \mu_{n-1}^2 \\ +* &= w_n x_n^2 - W_n \mu_n^2 + W_{n-1} \mu_{n-1}^2 \\ +* &= w_n x_n^2 - W_n \mu_n^2 + (W_n - w_n) \mu_{n-1}^2 \\ +* &= w_n (x_n^2 - \mu_{n-1}^2) + W_n (\mu_{n-1}^2 - \mu_n^2) +* \end{align*} +* ``` +* +* - Simplify further: +* +* ```tex +* \begin{align*} +* M2_n - M2_{n-1} &= w_n (x_n - \mu_{n-1}) (x_n - \mu_n) \\ +* M2_n &= M2_{n-1} + w_n (x_n - \mu_{n-1}) (x_n - \mu_n) +* \end{align*} +* ``` +* +* - Finally, compute the weighted sample standard deviation: +* +* ```tex +* \sigma_n = \sqrt{\frac{M2_n}{W_n}} +* ``` +* +* - Incrementally updating the weighted sample mean: +* +* ```tex +* \begin{align*} +* \mu_n &= \mu_{n-1} + \frac{w_n}{W_n} (x_n - \mu_{n-1}) +* \end{align*} +* ``` +* +* @param {number} [mean] - mean value +* @throws {TypeError} must provide a number primitive +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrwstdev(); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* s = accumulator( 3.0, 2.0 ); +* // returns ~0.471 +* +* s = accumulator( 2.0, 0.1 ); +* // returns ~0.478 +* +* s = accumulator(); +* // returns ~0.478 +* +* @example +* var accumulator = incrwstdev( 3.0 ); +*/ +function incrwstdev( mean ) { + var delta; + var wsum; + var FLG; + var M2; + var mu; + + wsum = 0.0; + M2 = 0.0; + if ( arguments.length ) { + if ( !isNumber( mean ) ) { + throw new TypeError( format( 'invalid argument. Must provide a number. Value: `%s`.', mean ) ); + } + mu = mean; + return accumulator2; + } + mu = 0.0; + return accumulator1; + + /** + * If provided a value, the accumulator function returns an updated weighted sample standard deviation. If not provided a value, the accumulator function returns the current weighted sample standard deviation. + * + * @private + * @param {number} [x] - value + * @param {NonNegativeNumber} [w] - weight + * @throws {TypeError} second argument must be a nonnegative number + * @returns {(number|null)} weighted sample standard deviation or null + */ + function accumulator1( x, w ) { + if ( arguments.length === 0 ) { + if ( FLG === void 0 ) { + return null; + } + return sqrt( M2 / wsum ); + } + if ( !isNonNegativeNumber( w ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative number. Value: `%s`.', w ) ); + } + if ( w === 0.0 ) { + return ( FLG === void 0 ) ? null : sqrt( M2 / wsum ); + } + FLG = true; + delta = x - mu; + wsum += w; + mu += ( w / wsum ) * delta; + M2 += w * delta * ( x - mu ); + return sqrt( M2 / wsum ); + } + + /** + * If provided a value, the accumulator function returns an updated weighted sample standard deviation. If not provided a value, the accumulator function returns the current weighted sample standard deviation. + * + * @private + * @param {number} [x] - value + * @param {NonNegativeNumber} [w] - weight + * @throws {TypeError} second argument must be a nonnegative number + * @returns {(number|null)} weighted sample standard deviation or null + */ + function accumulator2( x, w ) { + if ( arguments.length === 0 ) { + if ( FLG === void 0 ) { + return null; + } + return sqrt( M2 / wsum ); + } + if ( !isNonNegativeNumber( w ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative number. Value: `%s`.', w ) ); + } + if ( w === 0.0 ) { + return ( FLG === void 0 ) ? null : sqrt( M2 / wsum ); + } + FLG = true; + delta = x - mu; + wsum += w; + M2 += w * delta * delta; + return sqrt( M2 / wsum ); + } +} + + +// EXPORTS // + +module.exports = incrwstdev; diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/package.json b/lib/node_modules/@stdlib/stats/incr/wstdev/package.json new file mode 100644 index 000000000000..10ffc3184538 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/incr/wstdev", + "version": "0.0.0", + "description": "Compute a weighted sample standard deviation incrementally.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "average", + "avg", + "mean", + "arithmetic mean", + "central tendency", + "incremental", + "accumulator", + "weighted" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/wstdev/test/test.js b/lib/node_modules/@stdlib/stats/incr/wstdev/test/test.js new file mode 100644 index 000000000000..7964be5943a5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wstdev/test/test.js @@ -0,0 +1,208 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var incrwstdev = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrwstdev, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrwstdev(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known mean)', function test( t ) { + t.equal( typeof incrwstdev( 3.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a non-numeric value', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrwstdev( value ); + }; + } +}); + +tape( 'the accumulator function ignores zero weights and only updates the weighted sample standard deviation after encountering a non-zero weight', function test( t ) { + var acc = incrwstdev(); + t.equal( acc(), null, 'returns expected value' ); + t.equal( acc( 2.0, 0.0 ), null, 'returns expected value' ); + t.equal( acc( 2.0, 2.0 ), 0.0, 'returns expected value' ); + t.equal( acc( 14.0, 2.0 ), 6.0, 'returns expected value' ); + t.equal( acc( 25.0, 0.0 ), 6.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function throws an error if provided a second argument which is not a nonnegative number', function test( t ) { + var values; + var acc; + var i; + + values = [ + '5', + -5.0, + NaN, + undefined, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + acc = incrwstdev(); + acc( 2.0, value ); + }; + } +}); + +tape( 'the accumulator function incrementally computes a weighted sample standard deviation', function test( t ) { + var wMeanPrev; + var expected; + var wVarSum; + var actual; + var delta; + var dataX; + var dataW; + var xwSum; + var wMean; + var wSum; + var acc; + var tol; + var N; + var x; + var w; + var i; + + dataX = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + dataW = [ 1.0, 2.0, 0.1, 1.8, 9.9, 3.6 ]; + N = dataX.length; + + acc = incrwstdev(); + + wMeanPrev = 0.0; + wVarSum = 0.0; + xwSum = 0.0; + wMean = 0.0; + wSum = 0.0; + for ( i = 0; i < N; i++ ) { + x = dataX[ i ]; + w = dataW[ i ]; + xwSum += x * w; + wSum += w; + wMean = xwSum / wSum; + wVarSum += w * (x - wMeanPrev) * (x - wMean); + wMeanPrev = wMean; + expected = sqrt( wVarSum / wSum ); + actual = acc( x, w ); + delta = abs( actual - expected ); + tol = EPS * abs( expected ); + t.ok( delta <= tol, 'within tolerance. x: ' + x + '. Value: ' + actual + '. Expected: ' + expected + '. Tolerance: ' + tol + '.' ); + } + t.end(); +}); + +tape( 'if not provided arguments, the accumulator function returns the current weighted sample standard deviation', function test( t ) { + var expected; + var actual; + var delta; + var dataX; + var dataW; + var tol; + var acc; + var i; + + expected = 2.0; + dataX = [ 2.0, 4.0, 6.0, 8.0 ]; + dataW = [ 1.0, 2.0, 3.0, 4.0 ]; + acc = incrwstdev(); + for ( i = 0; i < dataX.length; i++ ) { + acc( dataX[ i ], dataW[ i ] ); + } + actual = acc(); + delta = abs( actual - expected ); + tol = EPS * abs( expected ); + t.ok( delta <= tol, 'returns the current accumulated weighted sample standard deviation within tolerance' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current weighted sample standard deviation (known mean)', function test( t ) { + var dataX; + var dataW; + var acc; + var i; + + dataX = [ 2.0, 4.0, 6.0, 8.0 ]; + dataW = [ 1.0, 2.0, 3.0, 4.0 ]; + acc = incrwstdev( 6.0 ); + for ( i = 0; i < dataX.length; i++ ) { + acc( dataX[ i ], dataW[ i ] ); + } + t.equal( acc(), 2.0, 'returns the current accumulated weighted sample standard deviation' ); + t.end(); +}); + +tape( 'if provided `NaN` for a value, the accumulator function returns `NaN`', function test( t ) { + var acc = incrwstdev(); + t.equal( isnan( acc( NaN, 1.0 ) ), true, 'returns NaN' ); + t.equal( isnan( acc( NaN, 1.0 ) ), true, 'returns NaN' ); + t.end(); +});