diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md b/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md new file mode 100644 index 000000000000..c65abc49c7c3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md @@ -0,0 +1,148 @@ + + +# incrnanewvariance + +> Compute an [exponentially weighted variance][exponentially weighted variance] incrementally, ignoring `NaN` values. + +
+ +The [exponentially weighted variance][exponentially weighted variance] is defined as + + + +```math +S_n = \begin{cases} 0 & \textrm{if}\ n = 0 \\ (1 - \alpha) (S_{n-1} + \alpha(x_n - \mu_{n-1})^2) & \textrm{if}\ n > 0 \end{cases} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); +``` + +#### incrnanewvariance( alpha ) + +Returns an accumulator function which incrementally computes an [exponentially weighted variance][exponentially weighted variance], ignoring `NaN` values. + +```javascript +var accumulator = incrnanewvariance( 0.5 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated exponentially weighted variance. If not provided an input value `x`, the accumulator function returns the current exponentially weighted variance. + +```javascript +var accumulator = incrnanewvariance( 0.5 ); + +var v = accumulator(); +// returns null + +v = accumulator( 2.0 ); +// returns 0 + +v = accumulator( 4.0 ); +// returns 1 + +v = accumulator( NaN ); +// returns 1 + +v = accumulator( 5.0 ); +// returns 1.5 + +v = accumulator(); +// returns 1.5 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. 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 randu = require( '@stdlib/random/base/randu' ); +var incrnanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator with smoothing factor α = 0.5: +accumulator = incrnanewvariance( 0.5 ); + +// For each simulated datum, update the exponentially weighted variance... +for ( i = 0; i < 100; i++ ) { + // With a 20% chance, provide NaN to test that invalid inputs are ignored: + v = ( randu() < 0.2 ) ? NaN : randu() * 100.0; + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js new file mode 100644 index 000000000000..31302d039195 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 randu = require( '@stdlib/random/base/randu' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var nanewvariance = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = nanewvariance( 0.5 ); + 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( format( '%s::accumulator', pkg ), function benchmark( b ) { + var acc; + var v; + var i; + acc = nanewvariance( 0.5 ); + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu() ); + 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/nanewvariance/docs/img/equation_exponentially_weighted_variance.svg b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/img/equation_exponentially_weighted_variance.svg new file mode 100644 index 000000000000..ec759179e837 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/img/equation_exponentially_weighted_variance.svg @@ -0,0 +1,89 @@ + +upper S Subscript n Baseline equals StartLayout Enlarged left-brace 1st Row 1st Column 0 2nd Column if n equals 0 2nd Row 1st Column left-parenthesis 1 minus alpha right-parenthesis left-parenthesis upper S Subscript n minus 1 Baseline plus alpha left-parenthesis x Subscript n Baseline minus mu Subscript n minus 1 Baseline right-parenthesis squared right-parenthesis 2nd Column if n greater-than 0 EndLayout + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/repl.txt new file mode 100644 index 000000000000..8a7fe4f004c9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( α ) + Returns an accumulator function which incrementally computes an + exponentially weighted variance, where α is a smoothing factor between 0 and + 1. This accumulator ignores any input values that are `NaN`. + + If provided a value, the accumulator function returns an updated variance. + If not provided a value, the accumulator function returns the current + variance. + + If provided `NaN` or a value which, when used in computations, results in + `NaN`, that input is ignored and the accumulated variance remains unchanged. + + Parameters + ---------- + α: number + Smoothing factor (value between 0 and 1). + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 0.5 ); + > var v = accumulator() + null + > v = accumulator( 2.0 ) + 0 + > v = accumulator( 4.0 ) + 1 + > v = accumulator( NaN ) + 1 + > v = accumulator( 5.0 ) + 1.5 + > v = accumulator() + 1.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts new file mode 100644 index 000000000000..d352ce759321 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts @@ -0,0 +1,69 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 a value, the accumulator function returns an updated variance. If not provided a value, the accumulator function returns the current variance. +* +* ## Notes +* +* - If provided `NaN`, the input is ignored and the accumulated value remains unchanged. +* +* @param x - value +* @returns variance or null +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes an exponentially weighted variance, ignoring `NaN` values. +* +* @param alpha - smoothing factor +* @throws must provide a nonnegative number +* @throws must be on the interval `[0,1]` +* @returns accumulator function +* +* @example +* var accumulator = incrnanewvariance( 0.5 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 0 +* +* v = accumulator( 4.0 ); +* // returns 1.0 +* +* v = accumulator( NaN ); +* // returns 1.0 +* +* v = accumulator( 5.0 ); +* // returns 1.5 +* +* v = accumulator(); +* // returns 1.5 +*/ +declare function incrnanewvariance( alpha: number ): accumulator; + + +// EXPORTS // + +export = incrnanewvariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts new file mode 100644 index 000000000000..ae83d18dc3d5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 nanewvariance = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + nanewvariance( 0.5 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided an argument that is not a number... +{ + nanewvariance( '5' ); // $ExpectError + nanewvariance( true ); // $ExpectError + nanewvariance( false ); // $ExpectError + nanewvariance( null ); // $ExpectError + nanewvariance( undefined ); // $ExpectError + nanewvariance( [] ); // $ExpectError + nanewvariance( {} ); // $ExpectError + nanewvariance( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = nanewvariance( 0.5 ); + acc(); // $ExpectType number | null + acc( 3.14 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = nanewvariance( 0.5 ); + acc( '5' ); // $ExpectError + acc( true ); // $ExpectError + acc( false ); // $ExpectError + acc( null ); // $ExpectError + acc( [] ); // $ExpectError + acc( {} ); // $ExpectError + acc( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js new file mode 100644 index 000000000000..e690b056e08d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var nanewvariance = require( './..' ); + +var accumulator; +var s2; +var v; +var i; + +// Initialize an accumulator: +accumulator = nanewvariance( 0.5 ); + +// For each simulated datum, update the exponentially weighted variance... +console.log( '\nValue\tVariance\n' ); +for ( i = 0; i < 100; i++ ) { + v = ( randu() < 0.2 ) ? NaN : randu() * 100.0; + s2 = accumulator( v ); + console.log( '%s\t%s', ( isnan( v ) ) ? 'NaN' : v.toFixed( 4 ), ( s2 === null ) ? 'null' : s2.toFixed( 4 ) ); +} +console.log( '\nFinal variance: %s\n', ( accumulator() === null ) ? 'null' : accumulator().toFixed( 4 ) ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js new file mode 100644 index 000000000000..2470b910ecf1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 an exponentially weighted variance incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanewvariance +* +* @example +* var incrnanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); +* +* var accumulator = incrnanewvariance( 0.5 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 0 +* +* v = accumulator( 4.0 ); +* // returns 1.0 +* +* v = accumulator( NaN ); +* // returns 1.0 +* +* v = accumulator( 5.0 ); +* // returns 1.5 +* +* v = accumulator(); +* // returns 1.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js new file mode 100644 index 000000000000..8df03ffe3dfc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js @@ -0,0 +1,83 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var increwvariance = require( '@stdlib/stats/incr/ewvariance' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes an exponentially weighted variance, ignoring `NaN` values. +* +* @param {NonNegativeNumber} alpha - smoothing factor +* @throws {TypeError} must provide a nonnegative number +* @throws {RangeError} must be on the interval `[0,1]` +* @returns {Function} accumulator function +* +* @example +* var incrnanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); +* +* var accumulator = incrnanewvariance( 0.5 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 0 +* +* v = accumulator( 4.0 ); +* // returns 1.0 +* +* v = accumulator( NaN ); +* // returns 1.0 +* +* v = accumulator( 5.0 ); +* // returns 1.5 +* +* v = accumulator(); +* // returns 1.5 +*/ +function incrnanewvariance( alpha ) { + var acc = increwvariance( alpha ); + return accumulator; + + /** + * Updates and returns the exponentially weighted variance when provided a numeric input. + * If no input is provided, returns the current exponentially weighted variance. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} updated exponentially weighted variance or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return acc(); + } + return acc( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanewvariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json b/lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json new file mode 100644 index 000000000000..fb7d368454f2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/stats/incr/nanewvariance", + "version": "0.0.0", + "description": "Compute an exponentially weighted variance incrementally, ignoring NaN values.", + "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", + "weighted variance", + "ewvariance", + "variance", + "exponential", + "smoothing factor", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js new file mode 100644 index 000000000000..87bb602bec78 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 nanewvariance = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = nanewvariance( 0.5 ); + t.strictEqual( acc(), null, 'returns null initially' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes an exponentially weighted variance', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + expected = [ + 0.0, // after 1st input + 0.25, // after 2nd input + 0.1875, // after 3rd input + 0.859375, // after 4th input + 0.43359375, // after 5th input + 0.4365234375 // after 6th input + ]; + + actual = []; + acc = nanewvariance( 0.5 ); + for ( i = 0; i < data.length; i++ ) { + actual.push( acc( data[ i ] ) ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'if not provided an input value, accumulator returns current variance', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 1.0 ]; + acc = nanewvariance( 0.5 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.strictEqual( acc(), 0.6875, 'returns expected variance' ); + t.end(); +}); + +tape( 'the accumulator function skips NaN inputs', function test( t ) { + var acc = nanewvariance( 0.5 ); + var v; + + v = acc( 2.0 ); + t.strictEqual( v, 0.0, 'returns 0.0 with one valid value' ); + + v = acc( NaN ); + t.strictEqual( v, 0.0, 'still returns 0.0 after NaN input' ); + + v = acc( 4.0 ); + t.strictEqual( v, 1.0, 'returns correct variance after second valid input' ); + + t.end(); +});