diff --git a/lib/node_modules/@stdlib/stats/incr/nanvariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanvariance/benchmark/benchmark.js new file mode 100644 index 000000000000..18e2d9e1264e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvariance/benchmark/benchmark.js @@ -0,0 +1,87 @@ +/** +* @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 incrnanvariance = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanvariance(); + 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 = incrnanvariance(); + 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 = incrnanvariance( 3.14 ); + 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/nanvariance/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanvariance/docs/repl.txt new file mode 100644 index 000000000000..779aba1c5ef5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvariance/docs/repl.txt @@ -0,0 +1,31 @@ +{{alias}}( [mean] ) + Returns an accumulator function which incrementally computes an unbiased + sample variance, ignoring NaN values. + If provided a value, the accumulator function returns an updated unbiased + sample variance. If not provided a value, the accumulator function returns + the current unbiased sample variance. + If provided `NaN`, the `NaN` value is ignored and the current accumulated + variance is returned. + Parameters + ---------- + mean: number (optional) + Known mean. + Returns + ------- + acc: Function + Accumulator function. + Examples + -------- + > var accumulator = {{alias}}(); + > var s2 = accumulator() + null + > s2 = accumulator( 2.0 ) + 0.0 + > s2 = accumulator( NaN ) + 0.0 + > s2 = accumulator( -5.0 ) + 24.5 + > s2 = accumulator() + 24.5 + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/incr/nanvariance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanvariance/docs/types/index.d.ts new file mode 100644 index 000000000000..b180d8f54d5a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvariance/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @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 `NaN` value is ignored and the current accumulated variance is returned. +* +* @param x - value +* @returns unbiased sample variance +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes an unbiased sample variance, ignoring NaN values. +* +* @param mu - known mean +* @returns accumulator function +* +* @example +* var accumulator = incrnanvariance(); +* +* 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(); +* // returns 24.5 +*/ +declare function incrnanvariance( mu?: number ): accumulator; + + +// EXPORTS // + +export = incrnanvariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nanvariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanvariance/docs/types/test.ts new file mode 100644 index 000000000000..1f8360addce5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvariance/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @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 incrnanvariance = require( './index' ); + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanvariance(); // $ExpectType accumulator + incrnanvariance( 0.0 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided invalid arguments... +{ + incrnanvariance( '5' ); // $ExpectError + incrnanvariance( true ); // $ExpectError + incrnanvariance( false ); // $ExpectError + incrnanvariance( null ); // $ExpectError + incrnanvariance( [] ); // $ExpectError + incrnanvariance( {} ); // $ExpectError + incrnanvariance( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanvariance(); + 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 = incrnanvariance(); + 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/nanvariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanvariance/examples/index.js new file mode 100644 index 000000000000..433604403d90 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvariance/examples/index.js @@ -0,0 +1,59 @@ +/** +* @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 incrnanvariance = require( './../lib' ); + +var accumulator; +var s2; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanvariance(); + +// For each simulated datum, update the unbiased sample variance... +console.log( '\nValue\tVariance\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, s2 ); +} +console.log( '\nFinal variance: %d\n', accumulator() ); + +// Initialize an accumulator with a known mean: +accumulator = incrnanvariance( 50.0 ); + +// For each simulated datum, update the unbiased sample variance... +console.log( '\nValue\tVariance\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, s2 ); +} +console.log( '\nFinal variance: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanvariance/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanvariance/lib/index.js new file mode 100644 index 000000000000..1506d394ab42 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvariance/lib/index.js @@ -0,0 +1,74 @@ +/** +* @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 unbiased sample variance incrementally, ignoring NaN values. +* +* @module @stdlib/stats/incr/nanvariance +* +* @example +* var incrnanvariance = require( '@stdlib/stats/incr/nanvariance' ); +* +* var accumulator = incrnanvariance(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 0.0 +* +* v = accumulator( NaN ); +* // returns 0.0 +* +* v = accumulator( -5.0 ); +* // returns 24.5 +* +* v = accumulator(); +* // returns 24.5 +* +* @example +* var incrnanvariance = require( '@stdlib/stats/incr/nanvariance' ); +* +* var accumulator = incrnanvariance( 3.14 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns ~1.2996 +* +* v = accumulator( NaN ); +* // returns 1.2996 +* +* v = accumulator( -5.0 ); +* // returns 24.4402 +* +* v = accumulator(); +* // returns 24.4402 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanvariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanvariance/lib/main.js new file mode 100644 index 000000000000..fe18bca5b695 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvariance/lib/main.js @@ -0,0 +1,100 @@ +/** +* @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 incrvariance = require( '@stdlib/stats/incr/variance' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes an unbiased sample variance, ignoring NaN values. +* +* @param {number} [mean] - mean value +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanvariance(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 0.0 +* +* v = accumulator( NaN ); +* // returns 0.0 +* +* v = accumulator( -5.0 ); +* // returns 24.5 +* +* v = accumulator(); +* // returns 24.5 +* +* @example +* var accumulator = incrnanvariance( 3.14 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 1.2996 +* +* v = accumulator( NaN ); +* // returns 1.2996 +* +* v = accumulator( -5.0 ); +* // returns 33.7796 +* +* v = accumulator(); +* // returns 33.7796 +*/ +function incrnanvariance( mean ) { + var variance; + + if ( arguments.length === 0 ) { + variance = incrvariance(); + } else { + variance = incrvariance( mean ); + } + + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated unbiased sample variance. If not provided a value, the accumulator function returns the current unbiased sample variance. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} unbiased sample variance or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return variance(); + } + return variance( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanvariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nanvariance/package.json b/lib/node_modules/@stdlib/stats/incr/nanvariance/package.json new file mode 100644 index 000000000000..d30abf070896 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvariance/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/stats/incr/nanvariance", + "version": "0.0.0", + "description": "Compute an 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 variance", + "unbiased", + "var", + "dispersion", + "standard deviation", + "stdev", + "central tendency", + "incremental", + "accumulator", + "nan", + "missing values" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanvariance/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanvariance/test/test.js new file mode 100644 index 000000000000..0120f21ddb75 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvariance/test/test.js @@ -0,0 +1,243 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrnanvariance = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanvariance, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanvariance(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known mean)', function test( t ) { + t.equal( typeof incrnanvariance( 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() { + incrnanvariance( value ); + }; + } +}); + +tape( 'the accumulator function incrementally computes an unbiased sample variance, ignoring NaN values', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + data = [ 2.0, 3.0, NaN, 4.0, 3.0, NaN, 4.0 ]; + + expected = [ 0, 0.5, 0.5, 1, 0.6666666666666666, 0.6666666666666666, 0.7 ]; + + acc = incrnanvariance(); + + actual = []; + for ( i = 0; i < data.length; i++ ) { + actual.push( acc( data[ i ] ) ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes an unbiased sample variance (known mean), ignoring NaN values', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + data = [ 2.0, 3.0, NaN, 2.0, 4.0, NaN, 3.0, 4.0 ]; + + expected = [ 1, 0.5, 0.5, 0.6666666666666666, 0.75, 0.75, 0.6, 0.6666666666666666 ]; + + acc = incrnanvariance( 3.0 ); + + actual = []; + for ( i = 0; i < data.length; i++ ) { + actual.push( 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 unbiased sample variance', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 1.0 ]; + acc = incrnanvariance(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 1.0, 'returns the current accumulated unbiased sample variance' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current unbiased sample variance (known mean)', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 1.0 ]; + acc = incrnanvariance( 2.0 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 0.6666666666666666, 'returns the current accumulated unbiased sample variance' ); + t.end(); +}); + +tape( 'the sample variance is `null` until at least 1 datum has been provided (unknown mean)', function test( t ) { + var acc; + var s2; + + acc = incrnanvariance(); + + s2 = acc(); + t.equal( s2, null, 'returns null' ); + + s2 = acc( 3.0 ); + t.notEqual( s2, null, 'does not return null' ); + + s2 = acc(); + t.notEqual( s2, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the sample variance is `null` until at least 1 datum has been provided (known mean)', function test( t ) { + var acc; + var s2; + + acc = incrnanvariance( 3.0 ); + + s2 = acc(); + t.equal( s2, null, 'returns null' ); + + s2 = acc( 3.0 ); + t.notEqual( s2, null, 'does not return null' ); + + s2 = acc(); + t.notEqual( s2, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the sample variance is `0` until at least 2 datums have been provided (unknown mean)', function test( t ) { + var acc; + var s2; + + acc = incrnanvariance(); + + s2 = acc( 2.0 ); + t.equal( s2, 0.0, 'returns 0' ); + + s2 = acc(); + t.equal( s2, 0.0, 'returns 0' ); + + s2 = acc( 3.0 ); + t.notEqual( s2, 0.0, 'does not return 0' ); + + s2 = acc(); + t.notEqual( s2, 0.0, 'does not return 0' ); + + t.end(); +}); + +tape( 'if provided a `NaN`, the accumulator function ignores the value', function test( t ) { + var data; + var acc; + var v; + var i; + + data = [ NaN, 2.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]; + acc = incrnanvariance(); + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + if ( i === 0 ) { + t.equal( v, null, 'returns null' ); + } else { + t.equal( isnan( v ), false, 'does not return NaN' ); + } + if ( i > 0 ) { + t.equal( isnan( acc() ), false, 'does not return NaN' ); + } + } + t.equal( isnan( acc() ), false, 'does not return NaN' ); + t.end(); +}); + +tape( 'if provided a `NaN`, the accumulator function ignores the value (known mean)', function test( t ) { + var data; + var acc; + var v; + var i; + + data = [ NaN, 2.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]; + acc = incrnanvariance( 3.14 ); + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + if ( i === 0 ) { + t.equal( v, null, 'returns null' ); + } else { + t.equal( isnan( v ), false, 'does not return NaN' ); + } + if ( i > 0 ) { + t.equal( isnan( acc() ), false, 'does not return NaN' ); + } + } + t.equal( isnan( acc() ), false, 'does not return NaN' ); + t.end(); +});