diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/README.md b/lib/node_modules/@stdlib/stats/incr/nancovariance/README.md new file mode 100644 index 000000000000..7d5db2b95ea0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/README.md @@ -0,0 +1,113 @@ + + +# incrnancovariance + +> Compute a sample covariance incrementally, ignoring `NaN` values. + +
+ +## Usage + +```javascript +var nancovariance = require( '@stdlib/stats/incr/nancovariance' ); +``` + +#### incrnancovariance + +Returns an accumulator function which incrementally computes a sample covariance, ignoring any input pairs that contain NaN values. The accumulator returns null until at least two valid data pairs have been provided. + +```javascript +var accumulator = nancovariance(); +``` + +#### accumulator(`[x, y]`) + +If provided a pair of numeric input values `[x, y]`, the accumulator updates and returns the sample covariance. If not provided any input values, the accumulator returns the current sample covariance. + +```javascript +var accumulator = nancovariance(); + +var cov = accumulator(); +// returns null + +cov = accumulator( 2.0, 1.0 ); +// returns null + +cov = accumulator( NaN, 1.0 ); +// returns null + +cov = accumulator( 4.0, 5.0 ); +// returns 4.0 + +cov = accumulator(); +// returns 4.0 +``` + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var nancovariance = require( '@stdlib/stats/incr/nancovariance' ); + +var accumulator; +var i; +var x; +var y; +accumulator = nancovariance(); +for ( i = 0; i < 100; i++ ) { + x = ( randu() < 0.2 ) ? NaN : randu() * 100.0; + y = ( randu() < 0.2 ) ? NaN : randu() * 100.0; + accumulator( x, y ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nancovariance/benchmark/benchmark.js new file mode 100644 index 000000000000..5574d4884989 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/benchmark/benchmark.js @@ -0,0 +1,74 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var nancovariance = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var acc; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + acc = nancovariance(); + if ( typeof acc !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof acc !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::accumulator', pkg ), function benchmark( b ) { + var acc; + var x; + var y; + var v; + var i; + + acc = nancovariance(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu() < 0.1 ) ? NaN : randu() * 10; // ~10% NaN + y = ( randu() < 0.1 ) ? NaN : randu() * 10; // ~10% NaN + v = acc( x, y ); + if ( v !== v && i > 1 ) { + b.fail( 'accumulator should not return NaN once enough valid data' ); + } + } + 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/nancovariance/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/repl.txt new file mode 100644 index 000000000000..f97e26539b7a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}() + Returns an accumulator function which incrementally computes a sample + covariance, ignoring `NaN` values. + + If provided input values, the accumulator function returns an updated + sample covariance. If not provided input values, the accumulator function + returns the current sample covariance. + + If an input value is `NaN`, the input pair is ignored and the accumulated + value remains unchanged. + + Until at least two valid data pairs have been provided, the accumulator + function returns `null`. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var v = accumulator() + null + > v = accumulator( 2.0, 1.0 ) + null + > v = accumulator( NaN, 5.0 ) + null + > v = accumulator( 4.0, 5.0 ) + 4.0 + > v = accumulator() + 4.0 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/index.d.ts new file mode 100644 index 000000000000..80885c12debe --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided arguments, returns an updated sample covariance; otherwise, returns the current sample covariance. +* +* ## Notes +* +* - If provided `NaN`, the input is ignored and the accumulated value remains unchanged. +* +* @param x - value +* @param y - value +* @returns sample covariance or null +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a sample covariance, ignoring `NaN` values. +* +* @returns accumulator function +* +* @example +* var acc = nancovariance(); +* +* var cov = acc(); +* // returns null +* +* cov = acc( 2.0, 1.0 ); +* // returns null +* +* cov = acc( NaN, 5.0 ); +* // returns null +* +* cov = acc( 4.0, 5.0 ); +* // returns 4.0 +* +* cov = acc(); +* // returns 4.0 +*/ +declare function nancovariance(): accumulator; + + +// EXPORTS // + +export = nancovariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/test.ts new file mode 100644 index 000000000000..acbecede6cf1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import nancovariance = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + nancovariance(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided any arguments... +{ + nancovariance( 3 ); // $ExpectError + nancovariance( 3, 0.0 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = nancovariance(); + acc(); // $ExpectType number | null + acc( 2.0, 1.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = nancovariance(); + + acc( '5', 2.0 ); // $ExpectError + acc( true, 2.0 ); // $ExpectError + acc( false, 2.0 ); // $ExpectError + acc( null, 2.0 ); // $ExpectError + acc( [], 2.0 ); // $ExpectError + acc( {}, 2.0 ); // $ExpectError + acc( ( x: number ): number => x, 2.0 ); // $ExpectError + + acc( 2.0, '5' ); // $ExpectError + acc( 2.0, true ); // $ExpectError + acc( 2.0, false ); // $ExpectError + acc( 2.0, null ); // $ExpectError + acc( 2.0, [] ); // $ExpectError + acc( 2.0, {} ); // $ExpectError + acc( 2.0, ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nancovariance/examples/index.js new file mode 100644 index 000000000000..47eff782e13d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var nancovariance = require( './../lib' ); + +var acc = nancovariance(); + +var dataX = [ 2.0, 2.0, NaN, 3.0, 5.0 ]; +var dataY = [ 1.0, 5.0, 4.0, NaN, 5.0 ]; + +var i; +var cov; +for ( i = 0; i < dataX.length; i++ ) { + cov = acc( dataX[ i ], dataY[ i ] ); + console.log( 'x=%s, y=%s, cov=%s', dataX[ i ], dataY[ i ], ( cov === null ) ? 'null' : cov.toString() ); +} + +console.log( '\nFinal sample covariance:', acc() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/index.js new file mode 100644 index 000000000000..01852a1896a0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/index.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute a sample covariance incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nancovariance +* +* @example +* var nancovariance = require( '@stdlib/stats/incr/nancovariance' ); +* +* var acc = nancovariance(); +* +* var cov = acc(); +* // returns null +* +* // Covariance is undefined for a single pair: +* cov = acc( 2.0, 1.0 ); +* // returns null +* +* // An input pair containing NaN is ignored: +* cov = acc( NaN, 5.0 ); +* // returns null +* +* cov = acc( 4.0, 5.0 ); +* // returns 4.0 +* +* cov = acc(); +* // returns 4.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/main.js new file mode 100644 index 000000000000..5b5e68fb71dc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/main.js @@ -0,0 +1,85 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var covariance = require( '@stdlib/stats/incr/covariance' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a sample covariance, ignoring `NaN` values. +* +* @returns {Function} accumulator function +* +* @example +* var nancovariance = require( '@stdlib/stats/incr/nancovariance' ); +* +* var acc = nancovariance(); +* +* var cov = acc(); +* // returns null +* +* cov = acc( 2.0, 1.0 ); +* // returns null +* +* cov = acc( NaN, 5.0 ); +* // returns null +* +* cov = acc( 4.0, 5.0 ); +* // returns 4.0 +* +* cov = acc(); +* // returns 4.0 +*/ +function nancovariance() { + var count = 0; + var acc = covariance(); + return accumulator; + + /** + * Updates and returns the sample covariance when provided two numeric arguments. + * If no arguments are provided, returns the current sample covariance. + * + * @private + * @param {number} [x] - new x value + * @param {number} [y] - new y value + * @returns {(number|null)} sample covariance or null + */ + function accumulator( x, y ) { + var val; + if ( arguments.length === 0 ) { + return ( count < 2 ) ? null : acc(); + } + if ( isnan( x ) || isnan( y ) ) { + return ( count < 2 ) ? null : acc(); + } + count += 1; + val = acc( x, y ); + return ( count < 2 ) ? null : val; + } +} + + +// EXPORTS // + +module.exports = nancovariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/package.json b/lib/node_modules/@stdlib/stats/incr/nancovariance/package.json new file mode 100644 index 000000000000..fa2560df8810 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/package.json @@ -0,0 +1,60 @@ +{ + "name": "@stdlib/stats/incr/nancovariance", + "version": "0.0.0", + "description": "Compute a sample covariance 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", + "statistics", + "covariance", + "incremental", + "accumulator", + "nan", + "sample covariance" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/test/test.js b/lib/node_modules/@stdlib/stats/incr/nancovariance/test/test.js new file mode 100644 index 000000000000..e3df6c4ba79a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/test/test.js @@ -0,0 +1,125 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var nancovariance = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof nancovariance, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + var acc = nancovariance(); + t.strictEqual( typeof acc, 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function returns null if no valid pairs have been provided', function test( t ) { + var acc = nancovariance(); + t.equal( acc(), null, 'returns null initially' ); + + // Provide an invalid pair: + acc( NaN, 5.0 ); + t.equal( acc(), null, 'still null after invalid pair' ); + t.end(); +}); + +tape( 'the accumulator function returns null if only one valid pair has been provided', function test( t ) { + var acc = nancovariance(); + + // Add the first valid pair: + acc( 2.0, 1.0 ); + t.equal( acc(), null, 'returns null for single valid pair' ); + t.end(); +}); + +tape( 'the accumulator function computes sample covariance once two valid pairs have been provided', function test( t ) { + var acc = nancovariance(); + var cov; + + // Pair #1: + cov = acc( 2.0, 1.0 ); + t.equal( cov, null, 'still null with only one valid pair' ); + + // Pair #2: + cov = acc( 4.0, 5.0 ); + t.equal( cov, 4.0, 'returns sample covariance after second valid pair' ); + + // Current covariance with no arguments: + t.equal( acc(), 4.0, 'returns current covariance' ); + t.end(); +}); + +tape( 'the accumulator function skips pairs containing NaN', function test( t ) { + var acc = nancovariance(); + var cov; + + // Provide first valid pair: + cov = acc( 2.0, 1.0 ); + t.equal( cov, null, 'null with only one valid pair' ); + + // Invalid pair => skip: + cov = acc( NaN, 10.0 ); + t.equal( cov, null, 'still null, as second pair is invalid' ); + + // Provide second valid pair: + cov = acc( 2.0, 5.0 ); + // For (2,1) and (2,5): same X => covariance=0 + t.equal( cov, 0.0, 'returns 0.0 for these pairs' ); + + // Provide third valid pair: + cov = acc( 5.0, 9.0 ); + t.equal( typeof cov, 'number', 'returns a number after third valid pair' ); + t.end(); +}); + +tape( 'the accumulator function computes correct covariance for multiple valid pairs interspersed with invalid pairs', function test( t ) { + var expected = 14.0/3.0; + var delta; + var data = [ + [ 2.0, 1.0 ], + [ NaN, 4.0 ], + [ 4.0, 5.0 ], + [ 3.0, 7.0 ], + [ NaN, NaN ], + [ 5.0, 11.0 ] + ]; + var acc = nancovariance(); + var cov; + var tol; + var i; + for ( i = 0; i < data.length; i++ ) { + cov = acc( data[ i ][ 0 ], data[ i ][ 1 ] ); + } + delta = abs( cov - expected ); + tol = EPS * abs( expected ); + t.equal( typeof cov, 'number', 'returns a number' ); + t.ok( delta < tol, 'within tolerance. Actual: '+cov+'. Expected: '+expected+'. delta: '+delta+'. tol: '+tol+'.' ); + t.end(); +});