diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/README.md b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/README.md new file mode 100644 index 000000000000..d128c305f3fe --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/README.md @@ -0,0 +1,195 @@ + + +# incrnanpcorr2 + +> Compute a squared sample [Pearson product-moment correlation coefficient][pearson-correlation] incrementally. + +
+ +The [Pearson product-moment correlation coefficient][pearson-correlation] between random variables `X` and `Y` is defined as + + + +```math +\rho_{X,Y} = \frac{\mathop{\mathrm{cov}}(X,Y)}{\sigma_X \sigma_Y} +``` + + + + + +where the numerator is the [covariance][covariance] and the denominator is the product of the respective standard deviations. + +For a sample of size `n`, the sample [Pearson product-moment correlation coefficient][pearson-correlation] is defined as + + + +```math +r = \frac{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})(y_i - \bar{y})}{\displaystyle\sqrt{\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \sqrt{\sum_{i=0}^{n-1} (y_i - \bar{y})^2}} +``` + + + + + +The squared sample [Pearson product-moment correlation coefficient][pearson-correlation] is thus defined as the square of the sample [Pearson product-moment correlation coefficient][pearson-correlation]. + +
+ + + +
+ +## Usage + +```javascript +var incrnanpcorr2 = require( '@stdlib/stats/incr/nanpcorr2' ); +``` + +#### incrnanpcorr2( \[mx, my] ) + +Returns an accumulator `function` which incrementally computes a squared sample [Pearson product-moment correlation coefficient][pearson-correlation]. + +```javascript +var accumulator = incrnanpcorr2(); +``` + +If the means are already known, provide `mx` and `my` arguments. + +```javascript +var accumulator = incrnanpcorr2( 3.0, -5.5 ); +``` + +#### accumulator( \[x, y] ) + +If provided input value `x` and `y`, the accumulator function returns an updated accumulated value. If not provided input values `x` and `y`, the accumulator function returns the current accumulated value. + +```javascript +var accumulator = incrnanpcorr2(); + +var r2 = accumulator( 2.0, 1.0 ); +// returns 0.0 + +r2 = accumulator( 1.0, 90 ); +// returns 1.0 + +r2 = accumulator( NaN, NaN ); +// returns ~1.0 + +r2 = accumulator( 8.0, NaN ); +// returns ~1.0 + +r2 = accumulator(); +// returns ~1.0 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN`, when used in computations,gets ignored. +- In comparison to the sample [Pearson product-moment correlation coefficient][pearson-correlation], the squared sample [Pearson product-moment correlation coefficient][pearson-correlation] is useful for emphasizing strong correlations. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanpcorr2 = require( '@stdlib/stats/incr/nanpcorr2' ); + +var accumulator; +var x; +var y; +var r2; +var i; + +// Initialize an accumulator: +accumulator = incrnanpcorr2(); + +// For each simulated datum, update the squared sample correlation coefficient... +console.log( '\nx\ty\tr^2\n' ); +for ( i = 0; i < 100; i++ ) { + x = (randu() < 0.9) ? (randu() * 100.0) : NaN; + y = (randu() < 0.9) ? (randu() * 100.0) : NaN; + r2 = accumulator( x, y ); + console.log('%s\t%s\t%s', ( isNaN( x ) ) ? 'NaN' : x.toFixed( 4 ), (isNaN(y)) ? 'NaN' : y.toFixed( 4 ), ( r2 === null ) ? 'null' : r2.toFixed( 4 )); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/benchmark/benchmark.js new file mode 100644 index 000000000000..c64b79de2b20 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/benchmark/benchmark.js @@ -0,0 +1,91 @@ +/** +* @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 incrnanpcorr2 = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanpcorr2(); + 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 = incrnanpcorr2(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu(), 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_means', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanpcorr2( 3.0, -2.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu(), 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/nanpcorr2/docs/img/equation_pearson_correlation_coefficient.svg b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_pearson_correlation_coefficient.svg new file mode 100644 index 000000000000..61e3ef3e3500 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_pearson_correlation_coefficient.svg @@ -0,0 +1,48 @@ + +rho Subscript upper X comma upper Y Baseline equals StartFraction c o v left-parenthesis upper X comma upper Y right-parenthesis Over sigma Subscript upper X Baseline sigma Subscript upper Y Baseline EndFraction + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_sample_pearson_correlation_coefficient.svg b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_sample_pearson_correlation_coefficient.svg new file mode 100644 index 000000000000..31facfed161b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_sample_pearson_correlation_coefficient.svg @@ -0,0 +1,126 @@ + +r equals StartFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis left-parenthesis y Subscript i Baseline minus y overbar right-parenthesis Over StartRoot sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis squared EndRoot StartRoot sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis y Subscript i Baseline minus y overbar right-parenthesis squared EndRoot EndFraction + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/repl.txt new file mode 100644 index 000000000000..3f4f92c136f3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/repl.txt @@ -0,0 +1,41 @@ + +{{alias}}( [mx, my] ) + Returns an accumulator function which incrementally computes the squared + sample Pearson product-moment correlation coefficient ignoring NaN values. + + If provided values, the accumulator function returns an updated accumulated + value. If not provided values, the accumulator function returns the current + accumulated value. + + Parameters + ---------- + mx: number (optional) + Known mean. + + my: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var r2 = accumulator() + null + > r2 = accumulator( 2.0, 1.0 ) + 0.0 + > r2 = accumulator( -5.0, NaN ) + ~0.0 + >r2 = accumulator(NaN,NaN) + ~0.0 + >r2 = accumulator(NaN,-8) + ~0.0 + > r2 = accumulator() + ~0.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/index.d.ts new file mode 100644 index 000000000000..9964141d44dc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/index.d.ts @@ -0,0 +1,75 @@ +/* +* @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 arguments, returns an updated squared sample correlation coefficient. +* +* @param x - value +* @param y - value +* @returns updated squared sample correlation coefficient +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a squared sample Pearson product-moment correlation coefficient, ignoring NaN Values. +* +* @param meanx - mean value +* @param meany - mean value +* @returns accumulator function +* +* @example +* var accumulator = incrnanpcorr2( 2.0, -3.0 ); +*/ +declare function incrnanpcorr2( meanx: number, meany: number ): accumulator; + +/** +* Returns an accumulator function which incrementally computes a squared sample Pearson product-moment correlation coefficient . +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanpcorr2(); +* +* var r2 = accumulator(); +* // returns null +* +* r2 = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r2 = accumulator( -5.0, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, 8.0 ); +* // returns ~0.0 +* +* r2 = accumulator(); +* // returns ~0.0 +*/ +declare function incrnanpcorr2(): accumulator; + + +// EXPORTS // + +export = incrnanpcorr2; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/test.ts new file mode 100644 index 000000000000..2e753ebac0ef --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/test.ts @@ -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. +*/ + +import incrnanpcorr2 = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanpcorr2(); // $ExpectType accumulator + incrnanpcorr2( 2, 4 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided non-numeric arguments... +{ + incrnanpcorr2( 2, '5' ); // $ExpectError + incrnanpcorr2( 2, true ); // $ExpectError + incrnanpcorr2( 2, false ); // $ExpectError + incrnanpcorr2( 2, null ); // $ExpectError + incrnanpcorr2( 2, undefined ); // $ExpectError + incrnanpcorr2( 2, [] ); // $ExpectError + incrnanpcorr2( 2, {} ); // $ExpectError + incrnanpcorr2( 2, ( x: number ): number => x ); // $ExpectError + + incrnanpcorr2( '5', 4 ); // $ExpectError + incrnanpcorr2( true, 4 ); // $ExpectError + incrnanpcorr2( false, 4 ); // $ExpectError + incrnanpcorr2( null, 4 ); // $ExpectError + incrnanpcorr2( undefined, 4 ); // $ExpectError + incrnanpcorr2( [], 4 ); // $ExpectError + incrnanpcorr2( {}, 4 ); // $ExpectError + incrnanpcorr2( ( x: number ): number => x, 4 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanpcorr2( 1 ); // $ExpectError + incrnanpcorr2( 2, 2, 3 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanpcorr2(); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The function returns an accumulator function which returns an accumulated result (known means)... +{ + const acc = incrnanpcorr2( 2, -3 ); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanpcorr2(); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments (known means)... +{ + const acc = incrnanpcorr2( 2, -3 ); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/examples/index.js new file mode 100644 index 000000000000..f511df59f7d4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/examples/index.js @@ -0,0 +1,41 @@ +/** +* @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 incrnanpcorr2 = require( './../lib' ); + +var accumulator; +var r2; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanpcorr2(); + +// For each simulated datum, update the squared sample Pearson correlation coefficient... +console.log( '\nx\ty\tr^2\n' ); +for ( i = 0; i < 100; i++ ) { + x = ( randu() < 0.9 ) ? ( randu() * 100.0 ) : NaN; + y = ( randu() < 0.9 ) ? ( randu() * 100.0 ) : NaN; + r2 = accumulator( x, y ); + console.log( '%d\t%d\t%d', ( isNaN( x ) ) ? 'NaN' : x.toFixed( 4 ), ( isNaN( y ) ) ? 'NaN' : y.toFixed( 4 ), ( r2 === null ) ? 'null' : r2.toFixed( 4 ) ); +} +console.log( '\nFinal r^2: %d\n', accumulator()); diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/index.js new file mode 100644 index 000000000000..cc5ecb531bd9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/index.js @@ -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. +*/ + +'use strict'; + +/** +* Compute a squared sample Pearson product-moment correlation coefficient incrementally. +* +* @module @stdlib/stats/incr/nanpcorr2 +* +* @example +* var incrnanpcorr2 = require( '@stdlib/stats/incr/nanpcorr2' ); +* +* var accumulator = incrnanpcorr2(); +* +* var r2 = accumulator(); +* // returns null +* +* r2 = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r2 = accumulator( -5.0, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, 8.0 ); +* // returns ~0.0 +* +* r2 = accumulator(); +* // returns ~0.0 +*/ + +// MODULES // + +var incrnanpcorr2 = require( './main.js' ); + + +// EXPORTS // + +module.exports = incrnanpcorr2; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/main.js new file mode 100644 index 000000000000..f8288147cead --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/main.js @@ -0,0 +1,93 @@ +/** +* @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 incrpcorr2 = require( '@stdlib/stats/incr/pcorr2' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a squared sample Pearson product-moment correlation coefficient, ignoring NaN Values. +* +* @param {number} [meanx] - mean value +* @param {number} [meany] - mean value +* @throws {TypeError} first argument must be a number +* @throws {TypeError} second argument must be a number +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanpcorr2(); +* +* var r2 = accumulator(); +* // returns null +* +* r2 = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r2 = accumulator( -5.0, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, 8.0 ); +* // returns ~0.0 +* +* r2 = accumulator(); +* // returns ~0.0 +* +* @example +* var accumulator = incrnanpcorr2( 2.0, -3.0 ); +*/ +function incrnanpcorr2( meanx, meany ) { + var pcorr2; + if ( arguments.length === 0 ) { + pcorr2 = incrpcorr2(); + } + else { + pcorr2 = incrpcorr2(meanx, meany); + } + return accumulator; + /** + * If provided input values, the accumulator function returns an updated accumulated value. If not provided input values, the accumulator function returns the current accumulate value. + * + * @private + * @param {number} [x] - new value + * @param {number} [y] - new value + * @returns {(number|null)} squared sample correlation coefficient or null + */ + function accumulator( x, y ) { + if ( arguments.length === 0 ) { + return pcorr2(); + } + if ( isnan( x ) || isnan( y ) ) { + return pcorr2(); + } + return pcorr2( x, y ); + } +} + + +// EXPORTS // + +module.exports = incrnanpcorr2; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/package.json b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/package.json new file mode 100644 index 000000000000..95c6ccf1a894 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/stats/incr/nanpcorr2", + "version": "0.0.0", + "description": "Compute a squared sample Pearson product-moment correlation coefficient, 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", + "covariance", + "sample covariance", + "variance", + "unbiased", + "var", + "correlation", + "corr", + "pearson", + "product-moment", + "r-squared", + "r2", + "value", + "bivariate", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/test/test.js new file mode 100644 index 000000000000..05ad5a5181e0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/test/test.js @@ -0,0 +1,316 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var incrnanpcorr2 = require( '@stdlib/stats/incr/nanpcorr2/lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanpcorr2, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a non-numeric value for the first argument', 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() { + incrnanpcorr2( value, 5.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a non-numeric value for the second argument', 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() { + incrnanpcorr2( 3.0, value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanpcorr2(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known means)', function test( t ) { + t.equal( typeof incrnanpcorr2( 3.0, -5.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a square sample correlation coefficient', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + y = [ 1.5, -0.6, 3.14, 4.0, -2.0, 10.0 ]; + + // Test against Julia: ((sum((x[1:n]-mean(x[1:n])).*(y[1:n]-mean(y[1:n]))[:])/(n-1))/(std(x[1:n])*std(y[1:n]))) + expected = [ + 0.0, + -1.0 * -1.0, + -0.8992664495010921 * -0.8992664495010921, + 0.23547201823172273 * 0.23547201823172273, + 0.06765492498103522 * 0.06765492498103522, + 0.4944446711225878 * 0.4944446711225878 + ]; + + acc = incrnanpcorr2(); + + for ( i = 0; i < x.length; i++ ) { + actual = acc( x[ i ], y[ i ] ); + if ( actual === expected[ i ] ) { + t.strictEqual( actual, expected[ i ], 'returns expected result. x: '+x[i]+'. y: '+y[i]+'.' ); + } else { + delta = abs( expected[ i ] - actual ); + tol = 7.0 * EPS * abs( expected[ i ] ); + t.equal( delta <= tol, true, 'x: '+x[i]+'. y: '+y[i]+'. expected: '+expected[i]+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + t.end(); +}); + +tape( 'the accumulator function incrementally computes a squared sample correlation coefficient (known means)', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + y = [ 1.5, -0.6, 3.14, 4.0, -2.0, 10.0 ]; + + // Test against Julia: (sum((x[1:n]-3.0).*(y[1:n]-2.6733333333333333)[:])/(n-1))/(stdm(x[1:n],3.0)*stdm(y[1:n],2.6733333333333333)) + expected = [ + 1.0, + 0.337429241307504 * 0.337429241307504, + 0.14242449698664145 * 0.14242449698664145, + 0.31297710227544034 * 0.31297710227544034, + 0.19590456246309015 * 0.19590456246309015, + 0.4944446711225878 * 0.4944446711225878 + ]; + + acc = incrnanpcorr2( 3.0, 2.6733333333333333 ); + + for ( i = 0; i < x.length; i++ ) { + actual = acc( x[ i ], y[ i ] ); + if ( actual === expected[ i ] ) { + t.strictEqual( actual, expected[ i ], 'returns expected result. x: '+x[i]+'. y: '+y[i]+'.' ); + } else { + delta = abs( expected[ i ] - actual ); + tol = 2.0 * EPS * abs( expected[ i ] ); + t.equal( delta <= tol, true, 'x: '+x[i]+'. y: '+y[i]+'. expected: '+expected[i]+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current squared sample correlation coefficient', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 1.0 ]; + y = [ 3.14, -1.0, 2.4 ]; + + acc = incrnanpcorr2(); + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + t.equal( acc(), 0.7699852380946451 * 0.7699852380946451, 'returns expected result' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current squared sample correlation coefficient (known means)', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 1.0 ]; + y = [ 3.14, -1.0, 2.4 ]; + + acc = incrnanpcorr2( 2.0, 1.5133333333333334 ); + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + t.equal( acc(), 0.7699852380946453 * 0.7699852380946453, 'returns expected result' ); + t.end(); +}); + +tape( 'the squared sample correlation coefficient is `null` until at least 1 datum has been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorr2(); + + v = acc(); + t.equal( v, null, 'returns null' ); + + v = acc( 2.0, 10.0 ); + t.notEqual( v, null, 'does not return null' ); + + v = acc(); + t.notEqual( v, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the squared sample correlation coefficient is `null` until at least 1 datum has been provided (known means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorr2( 3.0, -5.0 ); + + v = acc(); + t.equal( v, null, 'returns null' ); + + v = acc( 2.0, 10.0 ); + t.notEqual( v, null, 'does not return null' ); + + v = acc(); + t.notEqual( v, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the squared sample correlation coefficient is `0` until at least 2 datums have been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorr2(); + + v = acc( 2.0, 10.0 ); + t.equal( v, 0.0, 'returns 0' ); + + v = acc(); + t.equal( v, 0.0, 'returns 0' ); + + v = acc( 3.0, -3.14 ); + t.notEqual( v, 0.0, 'does not return 0' ); + + v = acc(); + t.notEqual( v, 0.0, 'does not return 0' ); + + t.end(); +}); + +tape( 'the accumulated result ignores NaN values', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + // Data with NaN values: + x = [ 2.0, NaN, 3.0, 4.0, NaN, 5.0 ]; + y = [ 1.5, 2.0, NaN, 4.0, 5.0, NaN ]; + + // Only pairs where neither x nor y is NaN should be considered + + // Effective data: [ [2.0, 1.5], [4.0, 4.0] ] + expected = 1.0; // Perfect correlation with only these two points + + acc = incrnanpcorr2(); + + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + actual = acc(); + if ( actual === expected ) { + t.strictEqual( actual, expected, 'returns expected result' ); + } else { + delta = abs( expected - actual ); + tol = 7.0 * EPS * abs( expected ); + t.equal( delta <= tol, true, 'expected: '+expected+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + t.end(); +}); + +tape( 'the accumulated result ignores NaN values (known means)', function test( t ) { + var acc; + + // Create accumulator with known means + acc = incrnanpcorr2( 3.0, 2.0 ); + + acc( 2.0, 1.0 ); + acc( NaN, 3.0 ); + acc( 4.0, NaN ); + acc( 4.0, 3.0 ); + + // NaN values should be ignored in the calculation + t.false( isnan(acc()), 'does not return NaN' ); + t.end(); +});