diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/README.md b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/README.md new file mode 100644 index 000000000000..0dfa18f16dc1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/README.md @@ -0,0 +1,183 @@ + + +# incrnanpcorrdist + +> Compute a [sample Pearson product-moment correlation distance][pearson-correlation] incrementally, ignoring `NaN` value. + +
+ +The [sample Pearson product-moment correlation distance][pearson-correlation] is defined as + + + +```math +d_{x,y} = 1 - r_{x,y} = 1 - \frac{\mathop{\mathrm{cov_n(x,y)}}}{\sigma_x \sigma_y} +``` + + + + + +where `r` is the [sample Pearson product-moment correlation coefficient][pearson-correlation], `cov(x,y)` is the sample covariance, and `σ` corresponds to the sample standard deviation. As `r` resides on the interval `[-1,1]`, `d` resides on the interval `[0,2]`. + +
+ + + +
+ +## Usage + +```javascript +var incrnanpcorrdist = require( '@stdlib/stats/incr/nanpcorrdist' ); +``` + +#### incrnanpcorrdist( \[mx, my] ) + +Returns an accumulator `function` which incrementally computes a [sample Pearson product-moment correlation distance][pearson-correlation], ignoring `NaN` value. + +```javascript +var accumulator = incrnanpcorrdist(); +``` + +If the means are already known, provide `mx` and `my` arguments. + +```javascript +var accumulator = incrnanpcorrdist( 3.0, -5.5 ); +``` + +#### accumulator( \[x, y] ) + +If provided input value `x` and `y`, the accumulator function returns an updated [sample correlation coefficient][pearson-correlation]. If not provided input values `x` and `y`, the accumulator function returns the current [sample correlation coefficient][pearson-correlation]. + +```javascript +var accumulator = incrnanpcorrdist(); + +var d = accumulator( 2.0, 1.0 ); +// returns 1.0 + +d = accumulator( NaN, 1.0 ); +// returns 1.0 + +d = accumulator( 1.0, -5.0 ); +// returns 0.0 + +d = accumulator( 1.0, NaN ); +// returns 0.0 + +d = accumulator( 3.0, 3.14 ); +// returns ~0.035 + +d = accumulator(); +// returns ~0.035 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulator function skips updating its state and continues to return the most recent valid accumulated value. If non-numeric inputs are possible, 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 incrnanpcorrdist = require( '@stdlib/stats/incr/nanpcorrdist' ); + +var accumulator; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanpcorrdist(); + +// For each simulated datum, update the sample correlation distance... +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + x = NaN; + } else { + x = randu() * 100.0; + } + if ( randu() < 0.2 ) { + y = NaN; + } else { + y = randu() * 100.0; + } + accumulator( x, y ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/benchmark/benchmark.js new file mode 100644 index 000000000000..a3c41e8eacdf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/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 incrnanpcorrdist = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanpcorrdist(); + 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 = incrnanpcorrdist(); + + 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 = incrnanpcorrdist( 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/nanpcorrdist/docs/img/equation_pearson_distance.svg b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/img/equation_pearson_distance.svg new file mode 100644 index 000000000000..6df633f17b81 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/img/equation_pearson_distance.svg @@ -0,0 +1,70 @@ + +d Subscript x comma y Baseline equals 1 minus r Subscript x comma y Baseline equals 1 minus StartFraction normal c normal o normal v Subscript normal n Baseline left-parenthesis normal x comma normal y right-parenthesis Over sigma Subscript x Baseline sigma Subscript y Baseline EndFraction + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/repl.txt new file mode 100644 index 000000000000..f605e1b8a16b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/repl.txt @@ -0,0 +1,47 @@ + +{{alias}}( [mx, my] ) + Returns an accumulator function which incrementally computes a sample + Pearson product-moment correlation distance, ignoring `NaN` value. + + The correlation distance is defined as one minus the Pearson product-moment + correlation coefficient and, thus, resides on the interval [0,2]. + + If provided values, the accumulator function returns an updated sample + correlation distance. If not provided values, the accumulator function + returns the current sample correlation distance. + + If provided `NaN` or a value which, when used in computations, results in + `NaN`, the accumulated value is `NaN` for all future invocations. + + Parameters + ---------- + mx: number (optional) + Known mean. + + my: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var d = accumulator() + null + > d = accumulator( 2.0, 1.0 ) + ~1.0 + > d = accumulator( NaN, 1.0 ) + ~1.0 + > d = accumulator( -5.0, 3.14 ) + ~2.0 + > d = accumulator( -5.0, NaN ) + ~2.0 + > d = accumulator() + ~2.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/types/index.d.ts new file mode 100644 index 000000000000..44dfcc89d6f3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/types/index.d.ts @@ -0,0 +1,80 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided input values, the accumulator function returns an updated sample correlation distance. If not provided input values, the accumulator function returns the current sample correlation distance. +* +* ## Notes +* +* - The correlation distance is defined as one minus the Pearson product-moment correlation coefficient and, thus, resides on the interval [0,2]. +* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations. +* +* @param x - value +* @param y - value +* @returns updated sample correlation distance +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a sample Pearson product-moment correlation distance, ignoring `NaN` value. +* +* @param meanx - mean value +* @param meany - mean value +* @returns accumulator function +* +* @example +* var accumulator = incrnanpcorrdist( 2.0, -3.0 ); +*/ +declare function incrnanpcorrdist( meanx: number, meany: number ): accumulator; + +/** +* Returns an accumulator function which incrementally computes a sample Pearson product-moment correlation distance. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanpcorrdist(); +* +* var d = accumulator(); +* // returns null +* +* d = accumulator( 2.0, 1.0 ); +* // returns 1.0 +* +* d = accumulator( NaN, 1.0 ); +* // returns 1.0 +* +* d = accumulator( -5.0, 3.14 ); +* // returns ~2.0 +* +* d = accumulator( -5.0, NaN ); +* // returns ~2.0 +* +* d = accumulator(); +* // returns ~2.0 +*/ +declare function incrnanpcorrdist(): accumulator; + + +// EXPORTS // + +export = incrnanpcorrdist; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/types/test.ts new file mode 100644 index 000000000000..72712fcc3cb5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/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 incrnanpcorrdist = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanpcorrdist(); // $ExpectType accumulator + incrnanpcorrdist( 2, 4 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided non-numeric arguments... +{ + incrnanpcorrdist( 2, '5' ); // $ExpectError + incrnanpcorrdist( 2, true ); // $ExpectError + incrnanpcorrdist( 2, false ); // $ExpectError + incrnanpcorrdist( 2, null ); // $ExpectError + incrnanpcorrdist( 2, undefined ); // $ExpectError + incrnanpcorrdist( 2, [] ); // $ExpectError + incrnanpcorrdist( 2, {} ); // $ExpectError + incrnanpcorrdist( 2, ( x: number ): number => x ); // $ExpectError + + incrnanpcorrdist( '5', 4 ); // $ExpectError + incrnanpcorrdist( true, 4 ); // $ExpectError + incrnanpcorrdist( false, 4 ); // $ExpectError + incrnanpcorrdist( null, 4 ); // $ExpectError + incrnanpcorrdist( undefined, 4 ); // $ExpectError + incrnanpcorrdist( [], 4 ); // $ExpectError + incrnanpcorrdist( {}, 4 ); // $ExpectError + incrnanpcorrdist( ( x: number ): number => x, 4 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanpcorrdist( 1 ); // $ExpectError + incrnanpcorrdist( 2, 2, 3 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanpcorrdist(); + + 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 = incrnanpcorrdist( 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 = incrnanpcorrdist(); + + 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 = incrnanpcorrdist( 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/nanpcorrdist/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/examples/index.js new file mode 100644 index 000000000000..a664ef6cc6a0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/examples/index.js @@ -0,0 +1,49 @@ +/** +* @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 incrnanpcorrdist = require( './../lib' ); + +var accumulator; +var d; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanpcorrdist(); + +// For each simulated datum, update the sample Pearson correlation distance... +console.log( '\nx\ty\tCorrelation Distance\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + x = NaN; + } else { + x = randu() * 100.0; + } + if ( randu() < 0.2 ) { + y = NaN; + } else { + y = randu() * 100.0; + } + d = accumulator( x, y ); + console.log( '%d\t%d\t%d', x.toFixed( 4 ), y.toFixed( 4 ), d.toFixed( 4 ) ); +} +console.log( '\nFinal 1-r: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/index.js new file mode 100644 index 000000000000..787eff16f5ce --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/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 sample Pearson product-moment correlation distance incrementally. +* +* @module @stdlib/stats/incr/nanpcorrdist +* +* @example +* var incrnanpcorrdist = require( '@stdlib/stats/incr/nanpcorrdist' ); +* +* var accumulator = incrnanpcorrdist(); +* +* var d = accumulator(); +* // returns null +* +* d = accumulator( 2.0, 1.0 ); +* // returns 1.0 +* +* d = accumulator( NaN, 1.0 ); +* // returns 1.0 +* +* d = accumulator( -5.0, 3.14 ); +* // returns ~2.0 +* +* d = accumulator( -5.0, NaN ); +* // returns ~2.0 +* +* d = accumulator(); +* // returns ~2.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/main.js new file mode 100644 index 000000000000..1aa5f43b13af --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/main.js @@ -0,0 +1,101 @@ +/** +* @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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var incrpcorrdist = require( '@stdlib/stats/incr/pcorrdist' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a sample Pearson product-moment correlation distance, ignoring `NaN` value. +* +* ## Method +* +* - The sample Pearson product-moment correlation distance is defined as +* +* ```tex +* d = 1 - r = 1 - \frac{\operatorname{cov}_n(x,y)}{\sigma_x \sigma_y} +* ``` +* +* - The implementation thus computes the sample Pearson product-moment correlation coefficient \\(r\\) and subtracts the coefficient from 1. +* +* @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 = incrnanpcorrdist(); +* +* var d = accumulator(); +* // returns null +* +* d = accumulator( 2.0, 1.0 ); +* // returns 1.0 +* +* d = accumulator( NaN, 1.0 ); +* // returns 1.0 +* +* d = accumulator( -5.0, 3.14 ); +* // returns ~2.0 +* +* d = accumulator( -5.0, NaN ); +* // returns ~2.0 +* +* d = accumulator(); +* // returns ~2.0 +* +* @example +* var accumulator = incrnanpcorrdist( 2.0, -3.0 ); +*/ +function incrnanpcorrdist( meanx, meany ) { + var pcorrdist; + if ( arguments.length > 0 ) { + pcorrdist = incrpcorrdist( meanx, meany ); + } else { + pcorrdist = incrpcorrdist(); + } + return accumulator; + + /** + * If provided input values, the accumulator function returns an updated sample correlation distance. If not provided input values, the accumulator function returns the current sample correlation distance. + * + * @private + * @param {number} [x] - new value + * @param {number} [y] - new value + * @returns {(number|null)} sample correlation distance or null + */ + function accumulator( x, y ) { + if ( arguments.length === 0 || isnan( x ) || !isNumber( x ) || isnan( y ) || !isNumber( y ) ) { + return pcorrdist(); + } + return pcorrdist( x, y ); + } +} + + +// EXPORTS // + +module.exports = incrnanpcorrdist; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/package.json b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/package.json new file mode 100644 index 000000000000..00095e0e995d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/stats/incr/nanpcorrdist", + "version": "0.0.0", + "description": "Compute a sample Pearson product-moment correlation distance.", + "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", + "correlation", + "corr", + "pcorr", + "ppmcc", + "pcc", + "pearson", + "product-moment", + "bivariate", + "distance", + "dist", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/test/test.js new file mode 100644 index 000000000000..da600bfe6936 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/test/test.js @@ -0,0 +1,324 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var incrnanpcorrdist = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanpcorrdist, '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() { + incrnanpcorrdist( 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() { + incrnanpcorrdist( 3.0, value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanpcorrdist(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known means)', function test( t ) { + t.equal( typeof incrnanpcorrdist( 3.0, -5.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a sample correlation distance', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, NaN, 2.0, 4.0, 3.0, 4.0 ]; + y = [ 1.5, -0.6, 3.14, 3.14, 4.0, -2.0, 10.0 ]; + + // Test against Julia: 1.0-(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 = [ + 1.0, + 2.0, + 2.0, + 1.8992664495010921, + 0.7645279817682773, + 0.9323450750189648, + 0.5055553288774122 + ]; + + acc = incrnanpcorrdist(); + + 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 = 3.7 * 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 sample correlation distance (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, NaN, 2.0, 4.0, 3.0, 4.0 ]; + y = [ 1.5, -0.6, 3.12, 3.14, 4.0, -2.0, 10.0 ]; + + // Test against Julia: 1.0-(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 = [ + 0.0, + 0.662570758692496, + 0.662570758692496, + 0.8575755030133585, + 0.6870228977245596, + 0.8040954375369098, + 0.5055553288774122 + ]; + + acc = incrnanpcorrdist( 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 = 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 sample correlation distance', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, NaN, 3.0, 1.0 ]; + y = [ 3.14, 2.14, -1.0, 2.4]; + + acc = incrnanpcorrdist(); + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + t.equal( acc(), 1.7699852380946451, 'returns expected result' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current sample correlation distance (known means)', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, NaN, 3.0, 1.0 ]; + y = [ 3.14, 2.2, -1.0, 2.4 ]; + + acc = incrnanpcorrdist( 2.0, 1.5133333333333334 ); + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + t.equal( acc(), 1.7699852380946453, 'returns expected result' ); + t.end(); +}); + +tape( 'the sample correlation distance is `null` until at least 1 datum has been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorrdist(); + + v = acc(); + t.equal( v, null, 'returns null' ); + + v = acc( 3.0, -3.14 ); + t.notEqual( v, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the sample correlation distance is `null` until at least 1 datum has been provided (known means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorrdist( 3.0, -5.0 ); + + v = acc(); + t.equal( v, null, 'returns null' ); + + v = acc( 3.0, -3.14 ); + t.notEqual( v, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the sample correlation distance is `1` until at least 2 datums have been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorrdist(); + + v = acc( 2.0, 10.0 ); + t.equal( v, 1.0, 'returns 1' ); + + v = acc( 3.0, -3.14 ); + t.notEqual( v, 1.0, 'does not return 1' ); + + t.end(); +}); + +tape( 'if provided a `NaN`, the accumulator function skips updating its state (unknown means)', function test( t ) { + var data; + var prev; + var acc; + var v; + + data = [ + [ 2.0, 1.0 ], + [ 1.0, 2.0 ], + [ NaN, 1.0 ], + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ], + [ 7.0, 0.0 ] + ]; + acc = incrnanpcorrdist(); + + v = acc( data[ 0 ][ 0 ], data[ 0 ][ 1 ] ); + v = acc( data[ 1 ][ 0 ], data[ 1 ][ 1 ] ); + prev = acc(); + + v = acc( data[ 2 ][ 0 ], data[ 2 ][ 1 ] ); + t.equal( v, prev, 'skips update when first parameter is NaN' ); + t.equal( acc(), prev, 'state remains unchanged' ); + + v = acc( data[ 3 ][ 0 ], data[ 3 ][ 1 ] ); + prev = v; + + v = acc( 4.0, NaN ); + t.equal( v, prev, 'skips update when second parameter is NaN' ); + t.equal( acc(), prev, 'state remains unchanged' ); + + t.end(); +}); + +tape( 'if provided a `NaN`, the accumulator function skips updating its state (known means)', function test( t ) { + var data; + var prev; + var acc; + var v; + + data = [ + [ 2.0, 1.0 ], + [ 1.0, 2.0 ], + [ NaN, 1.0 ], + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ], + [ 7.0, 0.0 ] + ]; + acc = incrnanpcorrdist( 3.14, 1.0 ); + + v = acc( data[ 0 ][ 0 ], data[ 0 ][ 1 ] ); + v = acc( data[ 1 ][ 0 ], data[ 1 ][ 1 ] ); + prev = acc(); + + v = acc( data[ 2 ][ 0 ], data[ 2 ][ 1 ] ); + t.equal( v, prev, 'skips update when first parameter is NaN' ); + t.equal( acc(), prev, 'state remains unchanged' ); + + v = acc( data[ 3 ][ 0 ], data[ 3 ][ 1 ] ); + prev = v; + + v = acc( 4.0, NaN ); + t.equal( v, prev, 'skips update when second parameter is NaN' ); + t.equal( acc(), prev, 'state remains unchanged' ); + + t.end(); +});