diff --git a/lib/node_modules/@stdlib/stats/incr/nanewstdev/README.md b/lib/node_modules/@stdlib/stats/incr/nanewstdev/README.md new file mode 100644 index 000000000000..1727feca5703 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/README.md @@ -0,0 +1,171 @@ + + +# incrnanewstdev + +> Compute an [exponentially weighted standard deviation][moving-average] incrementally, ignoring `NaN` values. + +
+ +An [exponentially weighted variance][moving-average] can be defined recursively 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} +``` + + + + + +where `μ` is the [exponentially weighted mean][@stdlib/stats/incr/ewmean]. The [exponentially weighted standard deviation][moving-average] is the square root of the [exponentially weighted variance][moving-average]. + +
+ + + +
+ +## Usage + +```javascript +var incrnanewstdev = require( '@stdlib/stats/incr/nanewstdev' ); +``` + +#### incrnanewstdev( alpha ) + +Returns an accumulator `function` which incrementally computes an [exponentially weighted standard deviation][moving-average], where `alpha` is a smoothing factor between `0` and `1`, ignoring `NaN` values. + +```javascript +var accumulator = incrnanewstdev( 0.5 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated standard deviation. If not provided an input value `x`, the accumulator function returns the current standard deviation. + +```javascript +var accumulator = incrnanewstdev( 0.5 ); + +var s = accumulator(); +// returns null + +s = accumulator( 2.0 ); +// returns 0.0 + +s = accumulator( 1.0 ); +// returns 0.5 + +s = accumulator( NaN ); +// returns 0.5 + +s = accumulator( 3.0 ); +// returns ~0.83 + +s = accumulator(); +// returns ~0.83 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked.If non-numeric inputs are possible, but 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrnanewstdev = require( '@stdlib/stats/incr/nanewstdev' ); + +var accumulator; +var s; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanewstdev( 0.5 ); + +// For each simulated datum, update the exponentially weighted standard deviation... +for ( i = 0; i < 100; i++ ) { + v = (randu() < 0.1) ? NaN : randu() * 100.0; + s = accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanewstdev/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanewstdev/benchmark/benchmark.js new file mode 100644 index 000000000000..7a33014e0702 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/benchmark/benchmark.js @@ -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. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var incrnanewstdev = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanewstdev( 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( pkg+'::accumulator', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanewstdev( 0.5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( i ); + 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/nanewstdev/docs/img/equation_exponentially_weighted_variance.svg b/lib/node_modules/@stdlib/stats/incr/nanewstdev/docs/img/equation_exponentially_weighted_variance.svg new file mode 100644 index 000000000000..ec759179e837 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/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/nanewstdev/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanewstdev/docs/repl.txt new file mode 100644 index 000000000000..963c62d036c8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}( α ) + Returns an accumulator function which incrementally computes an + exponentially weighted standard deviation, where α is a smoothing factor + between 0 and 1, ignoring `NaN` values. + + If provided a value, the accumulator function returns an updated standard + deviation. If not provided a value, the accumulator function returns the + current standard deviation. + + Parameters + ---------- + α: number + Smoothing factor (value between 0 and 1). + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 0.5 ); + > var s = accumulator() + null + > s = accumulator( 2.0 ) + 0.0 + > s = accumulator( NaN ) + 0.0 + > s = accumulator( -5.0 ) + 3.5 + > s = accumulator() + 3.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanewstdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanewstdev/docs/types/index.d.ts new file mode 100644 index 000000000000..d9aaefb7cffd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/docs/types/index.d.ts @@ -0,0 +1,61 @@ +/* +* @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 a value, the accumulator function returns an updated standard deviation. If not provided a value, the accumulator function returns the current standard deviation. +* +* @param x - value +* @returns standard deviation or null +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes an exponentially weighted standard deviation, ignoring `NaN` values. +* +* @param alpha - smoothing factor +* @throws must be on the interval `[0,1]` +* @returns accumulator function +* +* @example +* var accumulator = incrnanewstdev( 0.5 ); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0 ); +* // returns 0.0 +* +* s = accumulator( NaN ); +* // returns 0.0 +* +* s = accumulator( -5.0 ); +* // returns 3.5 +* +* s = accumulator(); +* // returns 3.5 +*/ +declare function incrnanewstdev( alpha: number ): accumulator; + + +// EXPORTS // + +export = incrnanewstdev; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewstdev/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanewstdev/docs/types/test.ts new file mode 100644 index 000000000000..304949f455f7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @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 incrnanewstdev = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanewstdev( 0.5 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided an argument that is not a number... +{ + incrnanewstdev( '5' ); // $ExpectError + incrnanewstdev( true ); // $ExpectError + incrnanewstdev( false ); // $ExpectError + incrnanewstdev( null ); // $ExpectError + incrnanewstdev( undefined ); // $ExpectError + incrnanewstdev( [] ); // $ExpectError + incrnanewstdev( {} ); // $ExpectError + incrnanewstdev( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanewstdev( 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 = incrnanewstdev( 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/nanewstdev/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanewstdev/examples/index.js new file mode 100644 index 000000000000..99e8b99aca7f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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 randu = require( '@stdlib/random/base/randu' ); +var incrnanewstdev = require( './../lib' ); + +// Initialize an accumulator: +var accumulator = incrnanewstdev( 0.5 ); + +// For each simulated datum, update the exponentially weighted standard deviation... +console.log( '\nValue\tStDev\n' ); +var s; +var v; +var i; +for ( i = 0; i < 100; i++ ) { + v = ( randu() < 0.1 ) ? NaN : randu() * 100.0; // 10% chance of a NaN + s = accumulator( v ); + console.log( '%d\t%d', v.toFixed( 4 ), ( s === null ) ? NaN : s.toFixed( 4 ) ); +} +console.log( '\nFinal StDev: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanewstdev/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanewstdev/lib/index.js new file mode 100644 index 000000000000..e8c0b6b737af --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/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 an exponentially weighted standard deviation incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanewstdev +* +* @example +* var increwstdev = require( '@stdlib/stats/incr/nanewstdev' ); +* +* var accumulator = increwstdev(); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0 ); +* // returns 0.0 +* +* s = accumulator( NaN ); +* // returns 0.0 +* +* s = accumulator( -5.0 ); +* // returns 3.5 +* +* s = accumulator(); +* // returns 3.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewstdev/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanewstdev/lib/main.js new file mode 100644 index 000000000000..a7de9ec2809a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/lib/main.js @@ -0,0 +1,79 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var increwstdev = require( '@stdlib/stats/incr/ewstdev' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes an exponentially weighted standard deviation, 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 accumulator = incrnanewstdev( 0.5 ); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0 ); +* // returns 0.0 +* +* s = accumulator( NaN ); +* // returns 0.0 +* +* s = accumulator( -5.0 ); +* // returns 3.5 +* +* s = accumulator(); +* // returns 3.5 +*/ +function incrnanewstdev( alpha ) { + var ewstdev; + + ewstdev = increwstdev(alpha); + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated standard deviation. If not provided a value, the accumulator function returns the current standard deviation. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} standard deviation or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return ewstdev(); + } + return ewstdev( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanewstdev; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewstdev/package.json b/lib/node_modules/@stdlib/stats/incr/nanewstdev/package.json new file mode 100644 index 000000000000..4bfef3d5ca93 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/stats/incr/nanewstdev", + "version": "0.0.0", + "description": "Compute an exponentially weighted standard deviation 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", + "stdev", + "std", + "variance", + "var", + "standard", + "deviation", + "dispersion", + "weighted", + "exponential", + "emsd", + "emv", + "ewmv", + "ewmsd", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanewstdev/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanewstdev/test/test.js new file mode 100644 index 000000000000..1aed109b702a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewstdev/test/test.js @@ -0,0 +1,158 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var incrnanewstdev = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanewstdev, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a nonnegative number', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 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() { + incrnanewstdev( value ); + }; + } +}); + +tape( 'the function throws an error if provided a nonnegative number which does not reside on the interval `[0,1]`', function test( t ) { + var values; + var i; + + values = [ + 1.5, + 3.14, + 1.0001, + 1.0e300 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanewstdev( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanewstdev( 0.5 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = incrnanewstdev( 0.5 ); + t.equal( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes an exponentially weighted standard deviation', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ + 2.0, + NaN, + 3.0, + 2.0, + 4.0, + NaN, + 3.0, + 4.0 + ]; + N = data.length; + + // Note: manually computed using the recurrence relation S_n = (1-α)(S_{n-1} + α(x_n - mu_{n-1})^2) = S_n = (1-α)S_{n-1} + α(1-α)(x_n - mu_{n-1})^2 + expected = [ + 0.0, // m = 2.0, s2 = 0.0 + 0.0, // Ignore NaN values + sqrt( (0.5*0.0) + (0.25*pow( 3.0-2.0, 2 )) ), // m = 2.5, s2 = 0.25 + sqrt( (0.5*0.25) + (0.25*pow( 2.0-2.5, 2 )) ), // m = 2.25, s2 = 0.1875 + sqrt( (0.5*0.1875) + (0.25*pow( 4.0-2.25, 2 )) ), // m = 3.125, s2 = 0.859375 + sqrt( (0.5*0.1875) + (0.25*pow( 4.0-2.25, 2 )) ), // Ignore NaN values + sqrt( (0.5*0.859375) + (0.25*pow( 3.0-3.125, 2 )) ), // m = 3.0625, s2 = 0.43359375 + sqrt( (0.5*0.43359375) + (0.25*pow( 4.0-3.0625, 2 )) ) // m = 3.53125, s2 = 0.4365234375 + ]; + actual = []; + + acc = incrnanewstdev( 0.5 ); + + for ( i = 0; i < N; i++ ) { + actual.push( acc( data[ i ] ) ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current standard deviation', function test( t ) { + var data; + var acc; + var i; + + data = [ + 2.0, // m = 2.0, s2 = 0.0 + 3.0, // m = 2.5, s2 = 0.25 + NaN, // Ignore NaN values + 1.0 // m = 1.75, s2 = 0.6875 + ]; + acc = incrnanewstdev( 0.5 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), sqrt( 0.6875 ), 'returns expected value' ); + t.end(); +});