From 7b2f24282dda0bd794e03916382f524c0ad4550f Mon Sep 17 00:00:00 2001 From: gopi kishan Date: Tue, 18 Mar 2025 08:39:34 +0530 Subject: [PATCH 01/10] feat: add stats/incr/nanmvariance --- .../@stdlib/stats/incr/nanmvariance/README.md | 170 +++++++++ .../incr/nanmvariance/benchmark/benchmark.js | 113 ++++++ .../img/equation_unbiased_sample_variance.svg | 61 ++++ .../stats/incr/nanmvariance/docs/repl.txt | 51 +++ .../incr/nanmvariance/docs/types/index.d.ts | 77 ++++ .../incr/nanmvariance/docs/types/test.ts | 80 +++++ .../stats/incr/nanmvariance/examples/index.js | 43 +++ .../stats/incr/nanmvariance/lib/index.js | 61 ++++ .../stats/incr/nanmvariance/lib/main.js | 235 ++++++++++++ .../stats/incr/nanmvariance/package.json | 76 ++++ .../stats/incr/nanmvariance/test/test.js | 339 ++++++++++++++++++ 11 files changed, 1306 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/img/equation_unbiased_sample_variance.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md b/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md new file mode 100644 index 000000000000..06c0ad978b8f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md @@ -0,0 +1,170 @@ +# nanmvariance + +> Compute a moving [unbiased sample variance][sample-variance] incrementally, ignoring `NaN` values. + +
+ +For a window of size `W`, the [unbiased sample variance][sample-variance] is defined as + + + +```math +s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2 +``` + + + + + +where `n` is the number of non-`NaN` values in the window, and `\bar{x}` is the arithmetic mean of the non-`NaN` values. + +
+ + + +
+ +## Usage + +```javascript +var incrnanmvariance = require( '@stdlib/stats/incr/nanmvariance' ); +``` + +#### incrnanmvariance( window\[, mean] ) + +Returns an accumulator `function` which incrementally computes a moving [unbiased sample variance][sample-variance], ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving [unbiased sample variance][sample-variance]. + +```javascript +var accumulator = incrnanmvariance( 3 ); +``` + +If the mean is already known, provide a `mean` argument. + +```javascript +var accumulator = incrnanmvariance( 3, 5.0 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated [unbiased sample variance][sample-variance]. If not provided an input value `x`, the accumulator function returns the current [unbiased sample variance][sample-variance]. + +```javascript +var accumulator = incrnanmvariance( 3 ); + +var s2 = accumulator(); +// returns null + +// Fill the window... +s2 = accumulator( 2.0 ); // [2.0] +// returns 0.0 + +s2 = accumulator( NaN ); // [2.0, NaN] +// returns 0.0 + +s2 = accumulator( -5.0 ); // [2.0, NaN, -5.0] +// returns 24.5 + +// Window begins sliding... +s2 = accumulator( 3.0 ); // [NaN, -5.0, 3.0] +// returns 32.0 + +s2 = accumulator( NaN ); // [-5.0, 3.0, NaN] +// returns 32.0 + +s2 = accumulator(); +// returns 32.0 +``` + +
+ + + +
+ +## 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. +- NaN input values are ignored. If the window contains only NaN values, the variance is calculated as if the window were empty. +- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided non-NaN values. +- The implementation uses [Welford's algorithm][welford-algorithm]. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanmvariance = require( '@stdlib/stats/incr/nanmvariance' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmvariance( 5 ); + +// For each simulated datum, update the moving unbiased sample variance... +console.log( '\nValue\tSample Variance\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + console.log( '%d\t%d', v.toFixed( 4 ), accumulator( v ).toFixed( 4 ) ); +} +console.log( '\nFinal variance: %d\n', accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/benchmark/benchmark.js new file mode 100644 index 000000000000..b9942a1c2a44 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/benchmark/benchmark.js @@ -0,0 +1,113 @@ +/** +* @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 randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var incrnanmvariance = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmvariance( (i%5)+1 ); + 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 = incrnanmvariance( 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(); +}); + +bench( pkg+'::accumulator,NaN', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanmvariance( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( NaN ); + 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 v; + var i; + + acc = incrnanmvariance( 5, 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/nanmvariance/docs/img/equation_unbiased_sample_variance.svg b/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/img/equation_unbiased_sample_variance.svg new file mode 100644 index 000000000000..f4722f986a70 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/img/equation_unbiased_sample_variance.svg @@ -0,0 +1,61 @@ + +s squared equals StartFraction 1 Over upper W minus 1 EndFraction sigma-summation Underscript i equals 0 Overscript upper W minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis squared + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/repl.txt new file mode 100644 index 000000000000..53a1168e5604 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/repl.txt @@ -0,0 +1,51 @@ +{{alias}}( W[, mean] ) + Returns an accumulator function which incrementally computes a moving + unbiased sample variance, ignoring NaN values. + + The `W` parameter defines the number of values over which to compute the + moving unbiased sample variance. + + If provided a value, the accumulator function returns an updated moving + unbiased sample variance. If not provided a value, the accumulator function + returns the current moving unbiased sample variance. + + NaN input values are ignored. If the window contains only NaN values, the + variance is calculated as if the window were empty. + + As `W` values are needed to fill the window buffer, the first `W-1` returned + values are calculated from smaller sample sizes. Until the window is full, + each returned value is calculated from all provided non-NaN values. + + Parameters + ---------- + W: integer + Window size. + + mean: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 3 ); + > var s2 = accumulator() + null + > s2 = accumulator( 2.0 ) + 0.0 + > s2 = accumulator( NaN ) + 0.0 + > s2 = accumulator( -5.0 ) + 24.5 + > s2 = accumulator( 3.0 ) + 19.0 + > s2 = accumulator( NaN ) + 19.0 + > s2 = accumulator() + 19.0 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/types/index.d.ts new file mode 100644 index 000000000000..58ce6ae03a8e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/types/index.d.ts @@ -0,0 +1,77 @@ +/* +* @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, returns an updated unbiased sample variance; otherwise, returns the current unbiased sample variance. +* +* ## Notes +* +* - If provided `NaN`, the value is ignored. +* - If the window contains only `NaN` values, the returned value is calculated as if the window were empty. +* +* @param x - value +* @returns unbiased sample variance or null +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a moving unbiased sample variance, ignoring NaN values. +* +* @param W - window size +* @param mean - mean value +* @throws first argument must be a positive integer +* @throws second argument must be a number +* @returns accumulator function +* +* @example +* var accumulator = incrnanmvariance( 3 ); +* +* var s2 = accumulator(); +* // returns null +* +* s2 = accumulator( 2.0 ); +* // returns 0.0 +* +* s2 = accumulator( NaN ); +* // returns 0.0 +* +* s2 = accumulator( -5.0 ); +* // returns 24.5 +* +* s2 = accumulator( 3.0 ); +* // returns 19.0 +* +* s2 = accumulator( NaN ); +* // returns 19.0 +* +* s2 = accumulator(); +* // returns 19.0 +* +* @example +* var accumulator = incrnanmvariance( 3, -2.0 ); +*/ +declare function incrnanmvariance( W: number, mean?: number ): accumulator; + + +// EXPORTS // + +export = incrnanmvariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/types/test.ts new file mode 100644 index 000000000000..892fd279deeb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/docs/types/test.ts @@ -0,0 +1,80 @@ +/* +* @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 incrnanmvariance from './index'; + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmvariance( 3 ); // $ExpectType accumulator + incrnanmvariance( 3, 0.5 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + incrnanmvariance( '5' ); // $ExpectError + incrnanmvariance( true ); // $ExpectError + incrnanmvariance( false ); // $ExpectError + incrnanmvariance( null ); // $ExpectError + incrnanmvariance( undefined ); // $ExpectError + incrnanmvariance( [] ); // $ExpectError + incrnanmvariance( {} ); // $ExpectError + incrnanmvariance( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + incrnanmvariance( 3, '5' ); // $ExpectError + incrnanmvariance( 3, true ); // $ExpectError + incrnanmvariance( 3, false ); // $ExpectError + incrnanmvariance( 3, null ); // $ExpectError + incrnanmvariance( 3, undefined ); // $ExpectError + incrnanmvariance( 3, [] ); // $ExpectError + incrnanmvariance( 3, {} ); // $ExpectError + incrnanmvariance( 3, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + incrnanmvariance(); // $ExpectError + incrnanmvariance( 3, 0.5, 78 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmvariance( 3 ); + + acc(); // $ExpectType number | null + acc( 3.14 ); // $ExpectType number | null + acc( NaN ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanmvariance( 3 ); + + 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/nanmvariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js new file mode 100644 index 000000000000..14e6aad075bd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js @@ -0,0 +1,43 @@ +/** +* @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 incrnanmvariance = require( './../lib' ); + +var accumulator; +var s2; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmvariance( 5 ); + +// For each simulated datum, update the moving unbiased sample variance... +console.log( '\nValue\tSample Variance\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + s2 = accumulator( v ); + console.log( '%d\t%d', v.toFixed( 4 ), s2 === null ? 'null' : s2.toFixed( 4 ) ); +} +console.log( '\nFinal variance: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/index.js new file mode 100644 index 000000000000..e942bb4b3f81 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/index.js @@ -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. +*/ + +'use strict'; + +/** +* Compute a moving unbiased sample variance incrementally, ignoring NaN values. +* +* @module @stdlib/stats/incr/nanmvariance +* +* @example +* var incrnanmvariance = require( '@stdlib/stats/incr/nanmvariance' ); +* +* var accumulator = incrnanmvariance( 3 ); +* +* var s2 = accumulator(); +* // returns null +* +* s2 = accumulator( 2.0 ); +* // returns 0.0 +* +* s2 = accumulator( NaN ); +* // returns 0.0 +* +* s2 = accumulator( -5.0 ); +* // returns 24.5 +* +* s2 = accumulator( 3.0 ); +* // returns 19.0 +* +* s2 = accumulator( NaN ); +* // returns 19.0 +* +* s2 = accumulator(); +* // returns 19.0 +*/ + + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js new file mode 100644 index 000000000000..1d40f2a55a1d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js @@ -0,0 +1,235 @@ +/** +* @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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a moving unbiased sample variance, ignoring NaN values. +* +* @param {PositiveInteger} W - window size +* @param {number} [mean] - mean value +* @throws {TypeError} first argument must be a positive integer +* @throws {TypeError} second argument must be a number +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmvariance( 3 ); +* +* var s2 = accumulator(); +* // returns null +* +* s2 = accumulator( 2.0 ); +* // returns 0.0 +* +* s2 = accumulator( NaN ); +* // returns 0.0 +* +* s2 = accumulator( -5.0 ); +* // returns 24.5 +* +* s2 = accumulator( 3.0 ); +* // returns 19.0 +* +* s2 = accumulator( NaN ); +* // returns 19.0 +* +* s2 = accumulator(); +* // returns 19.0 +* +* @example +* var accumulator = incrnanmvariance( 3, -2.0 ); +*/ +function incrnanmvariance( W, mean ) { + var delta; + var buf; + var tmp; + var M2; + var mu; + var d1; + var d2; + var N; + var n; + var i; + + if ( !isPositiveInteger( W ) ) { + throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) ); + } + + buf = new Array( W ); + n = W - 1; + M2 = 0.0; + i = -1; + N = 0; + + if ( arguments.length > 1 ) { + 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 unbiased sample variance, ignoring NaN values. If not provided a value, the accumulator function returns the current unbiased sample variance. + * + * @private + * @param {number} [x] - input value + * @returns {(number|null)} unbiased sample variance or null + */ + function accumulator1( x ) { + var k; + var v; + + if ( arguments.length === 0 ) { + if ( N === 0 ) { + return null; + } + if ( N === 1 ) { + return 0.0; + } + if ( N < W ) { + return M2 / (N-1); + } + return M2 / n; + } + + // If incoming value is NaN, ignore it: + if ( isnan( x ) ) { + return ( N === 0 ) ? null : ( N === 1 ) ? 0.0 : ( N < W ) ? M2 / (N-1) : M2 / n; + } + + // Update the index for managing the circular buffer: + i = (i+1) % W; + + // Case: initial window... + if ( N < W ) { + buf[ i ] = x; // update buffer + N += 1; + delta = x - mu; + mu += delta / N; + M2 += delta * (x - mu); + if ( N === 1 ) { + return 0.0; + } + return M2 / (N-1); + } + + // Case: N = W = 1 + else if ( N === 1 ) { + M2 = 0.0; + return M2; + } + + // Case: outgoing value is NaN, which we need to simply replace without affecting the variance + else if ( isnan( buf[ i ] ) ) { + buf[ i ] = x; + // When replacing NaN with a value, we need to update M2 with the squared difference from mean + M2 += (x - mu) * (x - mu); + return M2 / n; + } + + // Case: standard update when neither incoming nor outgoing values are NaN + else { + tmp = buf[ i ]; + delta = x - tmp; + d1 = tmp - mu; + mu += delta / N; + d2 = x - mu; + M2 += delta * (d1 + d2); + } + + buf[ i ] = x; + return M2 / n; + } + + /** + * If provided a value, the accumulator function returns an updated unbiased sample variance, ignoring NaN values. If not provided a value, the accumulator function returns the current unbiased sample variance. + * + * @private + * @param {number} [x] - input value + * @returns {(number|null)} unbiased sample variance or null + */ + function accumulator2( x ) { + var k; + + if ( arguments.length === 0 ) { + if ( N === 0 ) { + return null; + } + if ( N < W ) { + return M2 / N; + } + return M2 / W; + } + + // If incoming value is NaN, ignore it: + if ( isnan( x ) ) { + return ( N === 0 ) ? null : ( N < W ) ? M2 / N : M2 / W; + } + + // Update the index for managing the circular buffer: + i = (i+1) % W; + + // Case: initial window... + if ( N < W ) { + buf[ i ] = x; // update buffer + N += 1; + delta = x - mu; + M2 += delta * delta; + return M2 / N; + } + + // Case: outgoing value is NaN, which we need to simply replace without affecting the variance + else if ( isnan( buf[ i ] ) ) { + buf[ i ] = x; + // Add the squared difference between the new value and mean + M2 += (x - mu) * (x - mu); + return M2 / W; + } + + // Case: standard update when neither incoming nor outgoing values are NaN + else { + tmp = buf[ i ]; + // Remove the contribution of the outgoing value + M2 -= (tmp - mu) * (tmp - mu); + // Add the contribution of the incoming value + M2 += (x - mu) * (x - mu); + } + + buf[ i ] = x; + return M2 / W; + } +} + + +// EXPORTS // + +module.exports = incrnanmvariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/package.json b/lib/node_modules/@stdlib/stats/incr/nanmvariance/package.json new file mode 100644 index 000000000000..76e5961d8d8d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/stats/incr/nanmvariance", + "version": "0.0.0", + "description": "Compute a moving unbiased sample 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", + "variance", + "sample", + "sample variance", + "unbiased", + "stdev", + "standard", + "deviation", + "dispersion", + "incremental", + "accumulator", + "moving variance", + "sliding window", + "sliding", + "window", + "moving", + "nan", + "ignoring nan" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js new file mode 100644 index 000000000000..a9dcecd7f41b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js @@ -0,0 +1,339 @@ +/** +* @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 abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var incrnanmvariance = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmvariance, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a positive integer for the window size', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + null, + void 0, + NaN, + [], + {}, + 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() { + incrnanmvariance( value ); + }; + } +}); + +tape( 'the function throws an error if not provided a number as the mean 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() { + incrnanmvariance( 3, value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmvariance( 3 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known mean)', function test( t ) { + t.equal( typeof incrnanmvariance( 3, 3.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = incrnanmvariance( 3 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'the initial accumulated value is `null` (known mean)', function test( t ) { + var acc = incrnanmvariance( 3, 3.0 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a moving variance (known mean)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, 3.0, 4.0, NaN, 5.0, 6.0, 7.0, 8.0, NaN, 10.0 ]; + N = data.length; + + acc = incrnanmvariance( 3, 4.5 ); + + expected = [ 6.25, 4.25, 2.9166666666666665, 2.9166666666666665, 0.9166666666666666, 0.9166666666666666, 2.9166666666666665, 6.916666666666667, 6.916666666666667, 16.25 ]; + + actual = new Array( N ); + for ( i = 0; i < N; i++ ) { + actual[ i ] = acc( data[ i ] ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + + // Test when a NaN is added to a full window and then is replaced: + acc = incrnanmvariance( 3, 4.5 ); + for ( i = 0; i < 3; i++ ) { + acc( 4.5 ); // fill with mean value to get zero variance + } + + // Add NaN (shouldn't change variance): + t.equal( acc( NaN ), 0.0, 'returns expected value' ); + + // Replace NaN with a value (should affect variance): + t.equal( acc( 10.0 ), 10.083333333333334, 'returns expected value' ); + + t.end(); +}); + +tape( 'the accumulator function incrementally computes a moving variance (unknown mean)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, 3.0, 4.0, NaN, 5.0, 6.0, 7.0, 8.0, NaN, 10.0 ]; + N = data.length; + + acc = incrnanmvariance( 3 ); + + expected = [ + 0.0, // only one value + 0.5, // only two values + 1.0, // three values: full window + 1.0, // NaN is ignored, previous values persist + 1.0, // full window + 1.0, // full window + 1.0, // full window + 1.0, // full window + 1.0, // NaN is ignored, previous values persist + 2.333333333333332 // full window: 8.0, NaN, 10.0 (NaN ignored) + ]; + + actual = new Array( N ); + for ( i = 0; i < N; i++ ) { + actual[ i ] = acc( data[ i ] ); + // Account for floating-point errors: + if ( abs( actual[i] - expected[i] ) < EPS ) { + actual[ i ] = expected[ i ]; + } + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + + // Test when a NaN is added to a full window and then is replaced: + acc = incrnanmvariance( 3 ); + for ( i = 0; i < 3; i++ ) { + acc( 4.0 ); // Fill with identical values + } + + // Should have zero variance: + t.equal( acc(), 0.0, 'returns expected value' ); + + // Add NaN (shouldn't change variance): + t.equal( acc( NaN ), 0.0, 'returns expected value' ); + + // Replace NaN with a value (should affect variance): + t.equal( acc( 10.0 ), 12.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current variance', function test( t ) { + var acc = incrnanmvariance( 3 ); + var v; + + v = acc(); + t.equal( v, null, 'returns null' ); + + acc( 2.0 ); + acc( 3.0 ); + acc( 7.0 ); + + v = acc(); + t.equal( v, 7.0, 'returns the current variance' ); + + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmvariance( 3 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'if only one value has been provided, the accumulator function returns a variance equal to 0', function test( t ) { + var acc = incrnanmvariance( 3 ); + acc( 2.0 ); + t.equal( acc(), 0.0, 'returns 0' ); + t.end(); +}); + +tape( 'if only one value has been provided, the accumulator function returns a variance equal to 0 (known mean)', function test( t ) { + var acc = incrnanmvariance( 3, 3.0 ); + acc( 2.0 ); + t.equal( acc(), (2.0-3.0)*(2.0-3.0), 'returns (2.0-3.0)^2' ); + t.end(); +}); + +tape( 'if the window size is one, the accumulator function always returns a variance of 0 (unknown mean)', function test( t ) { + var acc = incrnanmvariance( 1 ); + + acc( 2.0 ); + t.equal( acc(), 0.0, 'returns 0' ); + + acc( 3.0 ); + t.equal( acc(), 0.0, 'returns 0' ); + + acc( 4.0 ); + t.equal( acc(), 0.0, 'returns 0' ); + + acc( NaN ); + t.equal( acc(), 0.0, 'returns 0' ); + + acc( 5.0 ); + t.equal( acc(), 0.0, 'returns 0' ); + + t.end(); +}); + +tape( 'if the window size is one, the accumulator function always returns the squared difference between the current value and the mean (known mean)', function test( t ) { + var acc = incrnanmvariance( 1, 3.0 ); + + acc( 2.0 ); + t.equal( acc(), 1.0, 'returns (2.0-3.0)^2 = 1.0' ); + + acc( 3.0 ); + t.equal( acc(), 0.0, 'returns (3.0-3.0)^2 = 0.0' ); + + acc( 4.0 ); + t.equal( acc(), 1.0, 'returns (4.0-3.0)^2 = 1.0' ); + + acc( NaN ); + t.equal( acc(), 1.0, 'returns (4.0-3.0)^2 = 1.0 (NaN ignored)' ); + + acc( 5.0 ); + t.equal( acc(), 4.0, 'returns (5.0-3.0)^2 = 4.0' ); + + t.end(); +}); + +tape( 'the accumulator function correctly handles NaN values (unknown mean)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ NaN, NaN, NaN, NaN, NaN ]; + N = data.length; + + expected = [ null ]; + for ( i = 1; i < N; i++ ) { + expected.push( null ); + } + + acc = incrnanmvariance( 3 ); + + actual = new Array( N ); + for ( i = 0; i < N; i++ ) { + actual[ i ] = acc( data[ i ] ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.equal( acc(), null, 'returns null' ); + + t.end(); +}); + +tape( 'the accumulator function correctly handles NaN values (known mean)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ NaN, NaN, NaN, NaN, NaN ]; + N = data.length; + + expected = [ null ]; + for ( i = 1; i < N; i++ ) { + expected.push( null ); + } + + acc = incrnanmvariance( 3, 3.0 ); + + actual = new Array( N ); + for ( i = 0; i < N; i++ ) { + actual[ i ] = acc( data[ i ] ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.equal( acc(), null, 'returns null' ); + + t.end(); +}); From d4d2f018ed49876320faeabed0b143d3f1b579b4 Mon Sep 17 00:00:00 2001 From: gopi kishan Date: Tue, 18 Mar 2025 09:08:38 +0530 Subject: [PATCH 02/10] solving eslint issue in README.js --- .../@stdlib/stats/incr/nanmvariance/README.md | 6 +- .../stats/incr/nanmvariance/lib/main.js | 72 +++++++++++-------- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md b/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md index 06c0ad978b8f..6e4841e2f106 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md @@ -69,13 +69,13 @@ s2 = accumulator( -5.0 ); // [2.0, NaN, -5.0] // Window begins sliding... s2 = accumulator( 3.0 ); // [NaN, -5.0, 3.0] -// returns 32.0 +// returns 19.0 s2 = accumulator( NaN ); // [-5.0, 3.0, NaN] -// returns 32.0 +// returns 19.0 s2 = accumulator(); -// returns 32.0 +// returns 19.0 ``` diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js index 1d40f2a55a1d..11df86e69f96 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js @@ -80,7 +80,7 @@ function incrnanmvariance( W, mean ) { throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) ); } - buf = new Array( W ); + buf = []; n = W - 1; M2 = 0.0; i = -1; @@ -105,9 +105,6 @@ function incrnanmvariance( W, mean ) { * @returns {(number|null)} unbiased sample variance or null */ function accumulator1( x ) { - var k; - var v; - if ( arguments.length === 0 ) { if ( N === 0 ) { return null; @@ -116,60 +113,72 @@ function incrnanmvariance( W, mean ) { return 0.0; } if ( N < W ) { - return M2 / (N-1); + return M2 / ( N - 1 ); } return M2 / n; } - // If incoming value is NaN, ignore it: if ( isnan( x ) ) { - return ( N === 0 ) ? null : ( N === 1 ) ? 0.0 : ( N < W ) ? M2 / (N-1) : M2 / n; + if ( N === 0 ) { + return null; + } + if ( N === 1 ) { + return 0.0; + } + if ( N < W ) { + return M2 / ( N - 1 ); + } + return M2 / n; } + // Update the index for managing the circular buffer: - i = (i+1) % W; + i = ( i+1 ) % W; // Case: initial window... if ( N < W ) { - buf[ i ] = x; // update buffer + if ( N < W ) { + buf.push( x ); + } else { + buf[ i ] = x; + } N += 1; delta = x - mu; mu += delta / N; - M2 += delta * (x - mu); + M2 += delta * ( x - mu ); if ( N === 1 ) { return 0.0; } - return M2 / (N-1); + return M2 / ( N-1 ); } // Case: N = W = 1 - else if ( N === 1 ) { + if (N === 1) { M2 = 0.0; return M2; } // Case: outgoing value is NaN, which we need to simply replace without affecting the variance - else if ( isnan( buf[ i ] ) ) { - buf[ i ] = x; - // When replacing NaN with a value, we need to update M2 with the squared difference from mean + if (isnan(buf[i])) { + buf[i] = x; + + // When replacing NaN with a value, update M2 with the squared difference from the mean M2 += (x - mu) * (x - mu); return M2 / n; } // Case: standard update when neither incoming nor outgoing values are NaN - else { - tmp = buf[ i ]; - delta = x - tmp; - d1 = tmp - mu; - mu += delta / N; - d2 = x - mu; - M2 += delta * (d1 + d2); - } - - buf[ i ] = x; + tmp = buf[i]; + delta = x - tmp; + d1 = tmp - mu; + mu += delta / N; + d2 = x - mu; + M2 += delta * (d1 + d2); + + buf[i] = x; return M2 / n; } - + /** * If provided a value, the accumulator function returns an updated unbiased sample variance, ignoring NaN values. If not provided a value, the accumulator function returns the current unbiased sample variance. * @@ -178,8 +187,6 @@ function incrnanmvariance( W, mean ) { * @returns {(number|null)} unbiased sample variance or null */ function accumulator2( x ) { - var k; - if ( arguments.length === 0 ) { if ( N === 0 ) { return null; @@ -200,7 +207,11 @@ function incrnanmvariance( W, mean ) { // Case: initial window... if ( N < W ) { - buf[ i ] = x; // update buffer + if ( N < W ) { + buf.push( x ); + } else { + buf[ i ] = x; + } N += 1; delta = x - mu; M2 += delta * delta; @@ -210,6 +221,7 @@ function incrnanmvariance( W, mean ) { // Case: outgoing value is NaN, which we need to simply replace without affecting the variance else if ( isnan( buf[ i ] ) ) { buf[ i ] = x; + // Add the squared difference between the new value and mean M2 += (x - mu) * (x - mu); return M2 / W; @@ -218,8 +230,10 @@ function incrnanmvariance( W, mean ) { // Case: standard update when neither incoming nor outgoing values are NaN else { tmp = buf[ i ]; + // Remove the contribution of the outgoing value M2 -= (tmp - mu) * (tmp - mu); + // Add the contribution of the incoming value M2 += (x - mu) * (x - mu); } From 2a4c9677ecbb84bee23077f6fa4f859fd4459fa4 Mon Sep 17 00:00:00 2001 From: gopi kishan Date: Tue, 18 Mar 2025 09:18:23 +0530 Subject: [PATCH 03/10] solving eslint issue in main.js --- .../@stdlib/stats/incr/nanmvariance/README.md | 20 +++++++++++++++ .../stats/incr/nanmvariance/lib/main.js | 25 +++++++++++-------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md b/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md index 6e4841e2f106..bbd26e113aee 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/README.md @@ -1,3 +1,23 @@ + + # nanmvariance > Compute a moving [unbiased sample variance][sample-variance] incrementally, ignoring `NaN` values. diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js index 11df86e69f96..8e3d70ee78a6 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js @@ -131,7 +131,6 @@ function incrnanmvariance( W, mean ) { return M2 / n; } - // Update the index for managing the circular buffer: i = ( i+1 ) % W; @@ -178,7 +177,7 @@ function incrnanmvariance( W, mean ) { buf[i] = x; return M2 / n; } - + /** * If provided a value, the accumulator function returns an updated unbiased sample variance, ignoring NaN values. If not provided a value, the accumulator function returns the current unbiased sample variance. * @@ -199,7 +198,13 @@ function incrnanmvariance( W, mean ) { // If incoming value is NaN, ignore it: if ( isnan( x ) ) { - return ( N === 0 ) ? null : ( N < W ) ? M2 / N : M2 / W; + if ( N === 0 ) { + return null; + } + if (N < W) { + return M2 / N; + } + return M2 / W; } // Update the index for managing the circular buffer: @@ -219,7 +224,7 @@ function incrnanmvariance( W, mean ) { } // Case: outgoing value is NaN, which we need to simply replace without affecting the variance - else if ( isnan( buf[ i ] ) ) { + if ( isnan( buf[ i ] ) ) { buf[ i ] = x; // Add the squared difference between the new value and mean @@ -228,15 +233,13 @@ function incrnanmvariance( W, mean ) { } // Case: standard update when neither incoming nor outgoing values are NaN - else { - tmp = buf[ i ]; + tmp = buf[ i ]; - // Remove the contribution of the outgoing value - M2 -= (tmp - mu) * (tmp - mu); + // Remove the contribution of the outgoing value + M2 -= ( tmp - mu ) * ( tmp - mu ); - // Add the contribution of the incoming value - M2 += (x - mu) * (x - mu); - } + // Add the contribution of the incoming value + M2 += ( x - mu ) * ( x - mu ); buf[ i ] = x; return M2 / W; From 71f2ef7fc09c6c58ef19da1a342451122cc6ac96 Mon Sep 17 00:00:00 2001 From: gopi kishan Date: Tue, 18 Mar 2025 09:21:45 +0530 Subject: [PATCH 04/10] solving eslint issue in main.js --- .../@stdlib/stats/incr/nanmvariance/examples/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js index 14e6aad075bd..8c011a1748d1 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js @@ -38,6 +38,6 @@ for ( i = 0; i < 100; i++ ) { v = randu() * 100.0; } s2 = accumulator( v ); - console.log( '%d\t%d', v.toFixed( 4 ), s2 === null ? 'null' : s2.toFixed( 4 ) ); + console.log( '%d\t%d', v.toFixed( 4 ), (s2 === null ? 'null' : s2.toFixed( 4 )) ); } console.log( '\nFinal variance: %d\n', accumulator() ); From 8e829648be74222c7e68cbb7dafdf7fe3142d4ed Mon Sep 17 00:00:00 2001 From: gopi kishan Date: Tue, 18 Mar 2025 09:26:12 +0530 Subject: [PATCH 05/10] solving eslint issue in index.js --- .../@stdlib/stats/incr/nanmvariance/examples/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js index 8c011a1748d1..779a8d9c2787 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js @@ -38,6 +38,6 @@ for ( i = 0; i < 100; i++ ) { v = randu() * 100.0; } s2 = accumulator( v ); - console.log( '%d\t%d', v.toFixed( 4 ), (s2 === null ? 'null' : s2.toFixed( 4 )) ); + console.log( '%d\t%s', v.toFixed( 4 ), s2 !== null ? s2.toFixed( 4 ) : 'null' ); } console.log( '\nFinal variance: %d\n', accumulator() ); From 7456b017094d4bbdd36f5519ac16e1e2d236542c Mon Sep 17 00:00:00 2001 From: gopi kishan Date: Tue, 18 Mar 2025 09:30:52 +0530 Subject: [PATCH 06/10] solving eslint issue in index.js --- .../@stdlib/stats/incr/nanmvariance/examples/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js index 779a8d9c2787..a6ce1c005675 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/examples/index.js @@ -38,6 +38,6 @@ for ( i = 0; i < 100; i++ ) { v = randu() * 100.0; } s2 = accumulator( v ); - console.log( '%d\t%s', v.toFixed( 4 ), s2 !== null ? s2.toFixed( 4 ) : 'null' ); + console.log('%s\t%s', v.toString(), (s2 === null) ? 'null' : s2.toFixed(4)); } console.log( '\nFinal variance: %d\n', accumulator() ); From 5b31f4702067c9760545e4b9fbfd369e383caaaa Mon Sep 17 00:00:00 2001 From: gopi kishan Date: Tue, 18 Mar 2025 09:37:01 +0530 Subject: [PATCH 07/10] solving eslint issue in test.js --- .../stats/incr/nanmvariance/test/test.js | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js index a9dcecd7f41b..908e3f320f90 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js @@ -126,11 +126,22 @@ tape( 'the accumulator function incrementally computes a moving variance (known acc = incrnanmvariance( 3, 4.5 ); - expected = [ 6.25, 4.25, 2.9166666666666665, 2.9166666666666665, 0.9166666666666666, 0.9166666666666666, 2.9166666666666665, 6.916666666666667, 6.916666666666667, 16.25 ]; + expected = [ + 6.25, + 4.25, + 2.9166666666666665, + 2.9166666666666665, + 0.9166666666666666, + 0.9166666666666666, + 2.9166666666666665, + 6.916666666666667, + 6.916666666666667, + 16.25 + ]; - actual = new Array( N ); + actual = []; for ( i = 0; i < N; i++ ) { - actual[ i ] = acc( data[ i ] ); + actual.push( acc( data[ i ] ) ); } t.deepEqual( actual, expected, 'returns expected incremental results' ); @@ -175,9 +186,9 @@ tape( 'the accumulator function incrementally computes a moving variance (unknow 2.333333333333332 // full window: 8.0, NaN, 10.0 (NaN ignored) ]; - actual = new Array( N ); + actual = []; for ( i = 0; i < N; i++ ) { - actual[ i ] = acc( data[ i ] ); + actual.push( acc( data[ i ] ) ); // Account for floating-point errors: if ( abs( actual[i] - expected[i] ) < EPS ) { actual[ i ] = expected[ i ]; @@ -300,9 +311,9 @@ tape( 'the accumulator function correctly handles NaN values (unknown mean)', fu acc = incrnanmvariance( 3 ); - actual = new Array( N ); + actual = []; for ( i = 0; i < N; i++ ) { - actual[ i ] = acc( data[ i ] ); + actual.push( acc( data[ i ] ) ); } t.deepEqual( actual, expected, 'returns expected incremental results' ); t.equal( acc(), null, 'returns null' ); @@ -328,9 +339,9 @@ tape( 'the accumulator function correctly handles NaN values (known mean)', func acc = incrnanmvariance( 3, 3.0 ); - actual = new Array( N ); + actual = []; for ( i = 0; i < N; i++ ) { - actual[ i ] = acc( data[ i ] ); + actual.push( acc( data[ i ] ) ); } t.deepEqual( actual, expected, 'returns expected incremental results' ); t.equal( acc(), null, 'returns null' ); From a8df4bb32067a12764a7a24f5a92c482bb1391a4 Mon Sep 17 00:00:00 2001 From: gopi kishan Date: Tue, 18 Mar 2025 09:39:21 +0530 Subject: [PATCH 08/10] solving eslint issue in test.js --- lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js index 908e3f320f90..78e5f5bbbb9a 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js @@ -189,6 +189,7 @@ tape( 'the accumulator function incrementally computes a moving variance (unknow actual = []; for ( i = 0; i < N; i++ ) { actual.push( acc( data[ i ] ) ); + // Account for floating-point errors: if ( abs( actual[i] - expected[i] ) < EPS ) { actual[ i ] = expected[ i ]; From 9f18b242bbc840b77d47b61f5177017878f82b06 Mon Sep 17 00:00:00 2001 From: gopi kishan Date: Tue, 18 Mar 2025 10:03:32 +0530 Subject: [PATCH 09/10] solving eslint issue in test.js --- lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js index 78e5f5bbbb9a..d5783e4ed5b3 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/test/test.js @@ -189,7 +189,7 @@ tape( 'the accumulator function incrementally computes a moving variance (unknow actual = []; for ( i = 0; i < N; i++ ) { actual.push( acc( data[ i ] ) ); - + // Account for floating-point errors: if ( abs( actual[i] - expected[i] ) < EPS ) { actual[ i ] = expected[ i ]; From c4cc10d4ac83b831f55968118527c8d28a0cd57e Mon Sep 17 00:00:00 2001 From: gopi kishan Date: Tue, 18 Mar 2025 13:13:46 +0530 Subject: [PATCH 10/10] Refactor incrmvariance Implementation for NaN Handling --- .../stats/incr/nanmvariance/lib/main.js | 177 +----------------- 1 file changed, 10 insertions(+), 167 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js index 8e3d70ee78a6..173282630f3e 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvariance/lib/main.js @@ -20,10 +20,8 @@ // MODULES // -var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; -var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var format = require( '@stdlib/string/format' ); +var incrmvariance = require( '@stdlib/stats/incr/mvariance' ); // MAIN // @@ -65,37 +63,13 @@ var format = require( '@stdlib/string/format' ); * var accumulator = incrnanmvariance( 3, -2.0 ); */ function incrnanmvariance( W, mean ) { - var delta; - var buf; - var tmp; - var M2; - var mu; - var d1; - var d2; - var N; - var n; - var i; - - if ( !isPositiveInteger( W ) ) { - throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) ); - } - - buf = []; - n = W - 1; - M2 = 0.0; - i = -1; - N = 0; - + var acc; if ( arguments.length > 1 ) { - if ( !isNumber( mean ) ) { - throw new TypeError( format( 'invalid argument. Must provide a number. Value: `%s`.', mean ) ); - } - mu = mean; - return accumulator2; + acc = incrmvariance( W, mean ); + } else { + acc = incrmvariance( W ); } - - mu = 0.0; - return accumulator1; + return accumulator; /** * If provided a value, the accumulator function returns an updated unbiased sample variance, ignoring NaN values. If not provided a value, the accumulator function returns the current unbiased sample variance. @@ -104,145 +78,14 @@ function incrnanmvariance( W, mean ) { * @param {number} [x] - input value * @returns {(number|null)} unbiased sample variance or null */ - function accumulator1( x ) { + function accumulator( x ) { if ( arguments.length === 0 ) { - if ( N === 0 ) { - return null; - } - if ( N === 1 ) { - return 0.0; - } - if ( N < W ) { - return M2 / ( N - 1 ); - } - return M2 / n; + return acc(); } - if ( isnan( x ) ) { - if ( N === 0 ) { - return null; - } - if ( N === 1 ) { - return 0.0; - } - if ( N < W ) { - return M2 / ( N - 1 ); - } - return M2 / n; + return acc(); } - - // Update the index for managing the circular buffer: - i = ( i+1 ) % W; - - // Case: initial window... - if ( N < W ) { - if ( N < W ) { - buf.push( x ); - } else { - buf[ i ] = x; - } - N += 1; - delta = x - mu; - mu += delta / N; - M2 += delta * ( x - mu ); - if ( N === 1 ) { - return 0.0; - } - return M2 / ( N-1 ); - } - - // Case: N = W = 1 - if (N === 1) { - M2 = 0.0; - return M2; - } - - // Case: outgoing value is NaN, which we need to simply replace without affecting the variance - if (isnan(buf[i])) { - buf[i] = x; - - // When replacing NaN with a value, update M2 with the squared difference from the mean - M2 += (x - mu) * (x - mu); - return M2 / n; - } - - // Case: standard update when neither incoming nor outgoing values are NaN - tmp = buf[i]; - delta = x - tmp; - d1 = tmp - mu; - mu += delta / N; - d2 = x - mu; - M2 += delta * (d1 + d2); - - buf[i] = x; - return M2 / n; - } - - /** - * If provided a value, the accumulator function returns an updated unbiased sample variance, ignoring NaN values. If not provided a value, the accumulator function returns the current unbiased sample variance. - * - * @private - * @param {number} [x] - input value - * @returns {(number|null)} unbiased sample variance or null - */ - function accumulator2( x ) { - if ( arguments.length === 0 ) { - if ( N === 0 ) { - return null; - } - if ( N < W ) { - return M2 / N; - } - return M2 / W; - } - - // If incoming value is NaN, ignore it: - if ( isnan( x ) ) { - if ( N === 0 ) { - return null; - } - if (N < W) { - return M2 / N; - } - return M2 / W; - } - - // Update the index for managing the circular buffer: - i = (i+1) % W; - - // Case: initial window... - if ( N < W ) { - if ( N < W ) { - buf.push( x ); - } else { - buf[ i ] = x; - } - N += 1; - delta = x - mu; - M2 += delta * delta; - return M2 / N; - } - - // Case: outgoing value is NaN, which we need to simply replace without affecting the variance - if ( isnan( buf[ i ] ) ) { - buf[ i ] = x; - - // Add the squared difference between the new value and mean - M2 += (x - mu) * (x - mu); - return M2 / W; - } - - // Case: standard update when neither incoming nor outgoing values are NaN - tmp = buf[ i ]; - - // Remove the contribution of the outgoing value - M2 -= ( tmp - mu ) * ( tmp - mu ); - - // Add the contribution of the incoming value - M2 += ( x - mu ) * ( x - mu ); - - buf[ i ] = x; - return M2 / W; + return acc( x ); } }