From 05d2e1d50196f6a51994f5c1fba65becd1afe9b6 Mon Sep 17 00:00:00 2001 From: Girish Garg Date: Sun, 16 Mar 2025 10:47:40 +0530 Subject: [PATCH 1/2] feat: add incremental corrected sample standard deviation function that ignores NaN values --- .../@stdlib/stats/incr/nanapcorr/lib/main.js | 0 .../@stdlib/stats/incr/nanstdev/README.md | 184 ++++++++++++++++++ .../incr/nanstdev/benchmark/benchmark.js | 90 +++++++++ ...on_corrected_sample_standard_deviation.svg | 1 + .../@stdlib/stats/incr/nanstdev/docs/repl.txt | 38 ++++ .../stats/incr/nanstdev/docs/types/index.d.ts | 64 ++++++ .../stats/incr/nanstdev/docs/types/test.ts | 60 ++++++ .../stats/incr/nanstdev/examples/index.js | 45 +++++ .../@stdlib/stats/incr/nanstdev/lib/index.js | 54 +++++ .../@stdlib/stats/incr/nanstdev/lib/main.js | 132 +++++++++++++ .../@stdlib/stats/incr/nanstdev/package.json | 69 +++++++ .../@stdlib/stats/incr/nanstdev/test/test.js | 169 ++++++++++++++++ 12 files changed, 906 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md b/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md new file mode 100644 index 000000000000..7eba670a88e5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md @@ -0,0 +1,184 @@ + + +# incrstdev + +> Compute a [corrected sample standard deviation][sample-stdev] incrementally. + +
+ +The [corrected sample standard deviation][sample-stdev] is defined as + + + +```math +s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrstdev = require( '@stdlib/stats/incr/nanstdev' ); +``` + +#### incrstdev( \[mean] ) + +Returns an accumulator `function` which incrementally computes a [corrected sample standard deviation][sample-stdev]. + +```javascript +var accumulator = incrnanstdev(); +``` + +If the mean is already known, provide a `mean` argument. + +```javascript +var accumulator = incrnanstdev( 3.0 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated [corrected sample standard deviation][sample-stdev]. If not provided an input value `x`, the accumulator function returns the current [corrected sample standard deviation][sample-stdev]. + +```javascript +var accumulator = incrnanstdev(); + +var s = accumulator( 2.0 ); +// returns 0.0 + +s = accumulator( 1.0 ); // => sqrt(((2-1.5)^2+(1-1.5)^2) / (2-1)) +// returns ~0.7071 + +s = accumulator( NaN ); // Ignored +// return ~0.7071 + +s = accumulator( 3.0 ); // => sqrt(((2-2)^2+(1-2)^2+(3-2)^2) / (3-1)) +// returns 1.0 + +s = accumulator(); +// returns 1.0 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, It will be Ignored Automatically without any errors 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 incrnanstdev = require( './../lib' ); + +var accumulator; +var s; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanstdev(); + +// For each simulated datum, update the corrected sample standard deviation... +for ( i = 0; i < 100; i++ ) { + v = randu() * 100.0; + + // Introduce NaN values randomly to test the accumulator + if ( randu() < 0.1 ) { // 10% chance to insert NaN + v = NaN; + } + + s = accumulator( v ); +} +console.log( '\nFinal standard deviation: %d\n', accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js new file mode 100644 index 000000000000..60338c5db765 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js @@ -0,0 +1,90 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 incrnanstdev = require( './../lib' ); + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanstdev(); + 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 = incrnanstdev(); + + 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,known_mean', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanstdev( 3.0 ); + + 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/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg new file mode 100644 index 000000000000..809fe66eaf09 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/repl.txt new file mode 100644 index 000000000000..c893f55dac2a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( [mean] ) + Returns an accumulator function which incrementally computes a corrected + sample standard deviation, ignoring `NaN` values. + + If provided a value, the accumulator function returns an updated corrected + sample standard deviation. If not provided a value, the accumulator function + returns the current corrected sample standard deviation. + + If provided `NaN` or a value which, when used in computations, results in + `NaN`, the value is ignored and does not affect future calculations. + + Parameters + ---------- + mean: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var s = accumulator() + null + > s = accumulator( 2.0 ) + 0.0 + > s = accumulator( NaN ) + 0.0 + > s = accumulator( -5.0 ) + ~4.95 + > s = accumulator() + ~4.95 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts new file mode 100644 index 000000000000..04e19ba51d42 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 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 corrected sample standard deviation; otherwise, returns the current corrected sample standard deviation. +* +* ## Notes +* +* - If provided `NaN` or a value which, when used in computations, the value is ignored and does not affect future calculations. +* +* @param x - value +* @returns corrected sample standard deviation +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a corrected sample standard deviation. +* +* @param mu - known mean +* @returns accumulator function +* +* @example +* var accumulator = incrstdev(); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0 ); +* // returns 0.0 +* +* s = accumulator( NaN ); +* // returns 0.0 +* +* s = accumulator( -5.0 ); +* // returns ~4.95 +* +* s = accumulator(); +* // returns ~4.95 +*/ +declare function incrnanstdev( mu?: number ): accumulator; + + +// EXPORTS // + +export = incrnanstdev; diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts new file mode 100644 index 000000000000..d4e366a6da05 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 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 incrnanstdev = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanstdev(); // $ExpectType accumulator + incrnanstdev( 0.0 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided invalid arguments... +{ + incrnanstdev( '5' ); // $ExpectError + incrnanstdev( true ); // $ExpectError + incrnanstdev( false ); // $ExpectError + incrnanstdev( null ); // $ExpectError + incrnanstdev( [] ); // $ExpectError + incrnanstdev( {} ); // $ExpectError + incrnanstdev( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanstdev(); + + 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 = incrnanstdev(); + + 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/nanstdev/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js new file mode 100644 index 000000000000..8e67b391cae7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 incrnanstdev = require( './../lib' ); + +var accumulator; +var s; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanstdev(); + +// For each simulated datum, update the corrected sample standard deviation... +console.log( '\nValue\tSigma\n' ); +for ( i = 0; i < 100; i++ ) { + v = randu() * 100.0; + + // Introduce NaN values randomly to test the accumulator + if ( randu() < 0.1 ) { // 10% chance to insert NaN + v = NaN; + } + + s = accumulator( v ); + console.log( '%d\t%d', isNaN(v) ? 'NaN' : v.toFixed( 4 ), s.toFixed( 4 )); +} +console.log( '\nFinal standard deviation: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js new file mode 100644 index 000000000000..fc47e4a11360 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 corrected sample standard deviation incrementally with ignoring NaN values. +* +* @module @stdlib/stats/incr/nanstdev +* +* @example +* var incrnanstdev = require( '@stdlib/stats/incr/nanstdev' ); +* +* var accumulator = incrnanstdev(); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0 ); +* // returns 0.0 +* +* s = accumulator( NaN ); // NaN is ignored +* // returns 0.0 +* +* s = accumulator( -5.0 ); +* // returns ~4.95 +* +* s = accumulator(); +* // returns ~4.95 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js new file mode 100644 index 000000000000..f4b06a540f48 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js @@ -0,0 +1,132 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a corrected sample standard deviation, +* **ignoring NaN values**. +* +* This implementation is based on Welford's algorithm. +* +* ## Key Differences from `incrstdev` +* - **Ignores NaN values** instead of propagating them. +* +* @param {number} [mean] - mean value +* @throws {TypeError} must provide a number primitive +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanstdev(); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0 ); +* // returns 0.0 +* +* s = accumulator( NaN ); // NaN is ignored +* // returns 0.0 +* +* s = accumulator( -5.0 ); +* // returns ~4.95 +* +* s = accumulator(); +* // returns ~4.95 +*/ +function incrnanstdev( mean ) { + var delta; + var mu; + var M2; + var N; + + M2 = 0.0; + N = 0; + if ( arguments.length ) { + if ( !isNumber( mean ) ) { + throw new TypeError( format( 'invalid argument. Must provide a number. Value: `%s`.', mean ) ); + } + mu = mean; + return accumulator2; + } + mu = 0.0; + return accumulator1; + + /** + * If provided a value, the accumulator function returns an updated corrected sample standard deviation. If not provided a value, the accumulator function returns the current corrected sample standard deviation which ignores NaN values. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} corrected sample standard deviation or null + */ + function accumulator1( x ) { + if ( arguments.length === 0 ) { + if ( N === 0 ) { + return null; + } + if ( N === 1 ) { + return 0.0; + } + return sqrt( M2 / (N-1) ); + } + if ( isnan( x ) ) { // Skip NaN values + return sqrt( M2 / (N > 1 ? (N - 1) : 1) ); + } + N += 1; + delta = x - mu; + mu += delta / N; + M2 += delta * ( x - mu ); + return sqrt( M2 / (N > 1 ? (N - 1) : 1) ); + } + + /** + * If provided a value, the accumulator function returns an updated corrected sample standard deviation. If not provided a value, the accumulator function returns the current corrected sample standard deviation. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} corrected sample standard deviation or null + */ + function accumulator2( x ) { + if ( arguments.length === 0 ) { + if ( N === 0 ) { + return null; + } + return sqrt( M2 / N ); + } + if ( isnan( x ) ) { // Skip NaN values + return sqrt( M2 / (N || 1) ); + } + N += 1; + delta = x - mu; + M2 += delta * delta; + return sqrt( M2 / N ); + } +} + +// EXPORTS // +module.exports = incrnanstdev; diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json b/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json new file mode 100644 index 000000000000..29025dcbcc86 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/stats/incr/nanstdev", + "version": "0.0.0", + "description": "Compute a corrected sample standard deviation incrementally ignoring NaN.", + "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 variance", + "var", + "dispersion", + "standard deviation", + "stdev", + "corrected", + "central tendency", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js new file mode 100644 index 000000000000..11e3671bc045 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js @@ -0,0 +1,169 @@ +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var incrnanstdev = require( './../lib' ); + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanstdev, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanstdev(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known mean)', function test( t ) { + t.equal( typeof incrnanstdev( 3.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a non-numeric value', function test( t ) { + var values; + var i; + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanstdev( value ); + }; + } +}); + +tape( 'the accumulator function incrementally computes a corrected sample standard deviation while ignoring NaNs', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + data = [ 2.0, NaN, 3.0, 2.0, 4.0, NaN, 3.0, 4.0 ]; + + expected = [ + 0.0, + 0.0, + sqrt( 0.5 ), + sqrt( 0.33333333333333337 ), + sqrt( 0.9166666666666666 ), + sqrt( 0.9166666666666666 ), + sqrt( 0.7 ), + sqrt( 0.8 ) + ]; + + acc = incrnanstdev(); + actual = new Array( data.length ); + for ( i = 0; i < data.length; i++ ) { + actual[i] = acc( data[i] ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current corrected sample standard deviation', function test( t ) { + var data = [ NaN, 2.0, 3.0, NaN, 1.0 ]; + var acc = incrnanstdev(); + for ( var i = 0; i < data.length; i++ ) { + acc( data[i] ); + } + t.equal( acc(), 1.0, 'returns the current accumulated corrected sample standard deviation' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current corrected sample sample deviation (known mean)', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, NaN, 1.0 ]; + acc = incrnanstdev( 2.0 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), sqrt( 0.6666666666666666 ), 'returns the current accumulated corrected sample standard deviation' ); + t.end(); +}); + +tape( 'the corrected sample standard deviation is `null` until at least 1 datum has been provided (unknown mean)', function test( t ) { + var acc; + var s; + + acc = incrnanstdev(); + + s = acc(); + t.equal( s, null, 'returns null' ); + + s = acc( 3.0 ); + t.notEqual( s, null, 'does not return null' ); + + s = acc(); + t.notEqual( s, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the corrected sample standard deviation is `null` until at least 1 datum has been provided (known mean)', function test( t ) { + var acc; + var s; + + acc = incrnanstdev( 3.0 ); + + s = acc(); + t.equal( s, null, 'returns null' ); + + s = acc( 3.0 ); + t.notEqual( s, null, 'does not return null' ); + + s = acc(); + t.notEqual( s, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the corrected sample standard deviation is `0` until at least 2 datums have been provided (unknown mean)', function test( t ) { + var acc; + var s; + + acc = incrnanstdev(); + + s = acc( 2.0 ); + t.equal( s, 0.0, 'returns 0' ); + + s = acc(); + t.equal( s, 0.0, 'returns 0' ); + + s = acc( 3.0 ); + t.notEqual( s, 0.0, 'does not return 0' ); + + s = acc(); + t.notEqual( s, 0.0, 'does not return 0' ); + + t.end(); +}); + +tape( 'if provided only NaNs, the accumulator function returns `null`', function test( t ) { + var acc = incrnanstdev(); + acc( NaN ); + acc( NaN ); + acc( NaN ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); From 99679a96c834f2d53c5a613d44cfcccdfdf8b15b Mon Sep 17 00:00:00 2001 From: Girish Garg Date: Sun, 16 Mar 2025 11:12:12 +0530 Subject: [PATCH 2/2] feat: add incremental corrected sample standard deviation function that ignores NaN values --- lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js deleted file mode 100644 index e69de29bb2d1..000000000000