From 0561d700521b13ca511cdd5ab360da0649a14882 Mon Sep 17 00:00:00 2001 From: hans Date: Mon, 17 Mar 2025 21:57:16 -0400 Subject: [PATCH 1/3] feat(stats): add nanmpcorr package --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/stats/incr/nanmpcorr/README.md | 198 +++++ .../incr/nanmpcorr/benchmark/benchmark.js | 121 +++ ...uation_pearson_correlation_coefficient.svg | 48 + ...sample_pearson_correlation_coefficient.svg | 126 +++ .../stats/incr/nanmpcorr/docs/repl.txt | 55 ++ .../incr/nanmpcorr/docs/types/index.d.ts | 99 +++ .../stats/incr/nanmpcorr/docs/types/test.ts | 123 +++ .../stats/incr/nanmpcorr/examples/index.js | 40 + .../@stdlib/stats/incr/nanmpcorr/lib/index.js | 63 ++ .../@stdlib/stats/incr/nanmpcorr/lib/main.js | 112 +++ .../@stdlib/stats/incr/nanmpcorr/package.json | 79 ++ .../@stdlib/stats/incr/nanmpcorr/test/test.js | 834 ++++++++++++++++++ 12 files changed, 1898 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/img/equation_pearson_correlation_coefficient.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/img/equation_sample_pearson_correlation_coefficient.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/README.md b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/README.md new file mode 100644 index 000000000000..e275ab93b4ff --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/README.md @@ -0,0 +1,198 @@ + + +# incrmpcorr + +> Compute a moving [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 `W`, 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})}{\sqrt{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \displaystyle\sqrt{\displaystyle\sum_{i=0}^{n-1} (y_i - \bar{y})^2}} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrmpcorr = require( '@stdlib/stats/incr/mpcorr' ); +``` + +#### incrmpcorr( window\[, mx, my] ) + +Returns an accumulator `function` which incrementally computes a moving [sample Pearson product-moment correlation coefficient][pearson-correlation]. The `window` parameter defines the number of values over which to compute the moving [sample Pearson product-moment correlation coefficient][pearson-correlation]. + +```javascript +var accumulator = incrmpcorr( 3 ); +``` + +If means are already known, provide `mx` and `my` arguments. + +```javascript +var accumulator = incrmpcorr( 3, 5.0, -3.14 ); +``` + +#### accumulator( \[x, y] ) + +If provided input values `x` and `y`, the accumulator function returns an updated [sample Pearson product-moment correlation coefficient][pearson-correlation]. If not provided input values `x` and `y`, the accumulator function returns the current [sample Pearson product-moment correlation coefficient][pearson-correlation]. + +```javascript +var accumulator = incrmpcorr( 3 ); + +var r = accumulator(); +// returns null + +// Fill the window... +r = accumulator( 2.0, 1.0 ); // [(2.0, 1.0)] +// returns 0.0 + +r = accumulator( -5.0, 3.14 ); // [(2.0, 1.0), (-5.0, 3.14)] +// returns ~-1.0 + +r = accumulator( 3.0, -1.0 ); // [(2.0, 1.0), (-5.0, 3.14), (3.0, -1.0)] +// returns ~-0.925 + +// Window begins sliding... +r = accumulator( 5.0, -9.5 ); // [(-5.0, 3.14), (3.0, -1.0), (5.0, -9.5)] +// returns ~-0.863 + +r = accumulator( -5.0, 1.5 ); // [(3.0, -1.0), (5.0, -9.5), (-5.0, 1.5)] +// returns ~-0.803 + +r = accumulator(); +// returns ~-0.803 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **at least** `W-1` future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. +- As `W` (x,y) pairs are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrmpcorr = require( '@stdlib/stats/incr/mpcorr' ); + +var accumulator; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrmpcorr( 5 ); + +// For each simulated datum, update the moving sample correlation coefficient... +for ( i = 0; i < 100; i++ ) { + x = randu() * 100.0; + y = randu() * 100.0; + accumulator( x, y ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/benchmark/benchmark.js new file mode 100644 index 000000000000..7fa187d872aa --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/benchmark/benchmark.js @@ -0,0 +1,121 @@ +/** +* @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 incrnanmpcorr = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmpcorr( (i%5)+1 ); + 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 = incrnanmpcorr( 5 ); + + 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 = incrnanmpcorr( 5, 3.0, -1.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(); +}); + +bench( pkg+'::accumulator,with_nan', function benchmark( b ) { + var acc; + var v; + var i; + acc = incrnanmpcorr( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + if ( randu() < 0.2 ) { + if ( randu() < 0.5 ) { + v = acc( NaN, randu() ); + } else { + v = acc( randu(), NaN ); + } + } else { + v = acc( randu(), randu() ); + } + // Ensure that even with occasional NaN inputs the accumulator does not return NaN: + 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/nanmpcorr/docs/img/equation_pearson_correlation_coefficient.svg b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/img/equation_pearson_correlation_coefficient.svg new file mode 100644 index 000000000000..61e3ef3e3500 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/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/nanmpcorr/docs/img/equation_sample_pearson_correlation_coefficient.svg b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/img/equation_sample_pearson_correlation_coefficient.svg new file mode 100644 index 000000000000..31facfed161b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/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/nanmpcorr/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/repl.txt new file mode 100644 index 000000000000..d16308a944c4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/repl.txt @@ -0,0 +1,55 @@ + +{{alias}}( W[, mx, my] ) + Returns an accumulator function which incrementally computes a moving + sample Pearson product-moment correlation coefficient. + + The `W` parameter defines the number of values over which to compute the + moving sample correlation coefficient. + + If provided values, the accumulator function returns an updated moving + sample correlation coefficient. If not provided values, the accumulator + function returns the current moving sample correlation coefficient. + + As `W` (x,y) pairs are needed to fill the window buffer, the first `W-1` + returned values are calculated from smaller sample sizes. Until the window + is full, each returned value is calculated from all provided values. + + Parameters + ---------- + W: integer + Window size. + + mx: number (optional) + Known mean. + + my: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 3 ); + > var r = accumulator() + null + > r = accumulator( 2.0, 1.0 ) + 0.0 + > r = accumulator( -2.0, NaN ) + 0.0 + > r = accumulator( -5.0, 3.14 ) + ~-1.0 + > r = accumulator( NaN, 2.71 ) + ~-1.0 + > r = accumulator( 3.0, -1.0 ) + ~-0.925 + > r = accumulator( 5.0, -9.5 ) + ~-0.863 + > r = accumulator() + ~-0.863 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/index.d.ts new file mode 100644 index 000000000000..e5f4b00bc4eb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/index.d.ts @@ -0,0 +1,99 @@ +/* +* @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 moving sample correlation coefficient. +* +* ## Notes +* +* - 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 moving sample correlation coefficient +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes an updated moving sample correlation coefficient, ignoring `NaN` values. +* +* ## Notes +* +* - The `W` parameter defines the number of values over which to compute the moving sample correlation coefficient. +* - As `W` (x,y) pairs are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. +* +* @param W - window size +* @param meanx - mean value +* @param meany - mean value +* @throws first argument must be a positive integer +* @returns accumulator function +* +* @example +* var accumulator = incrnanmpcorr( 3, -2.0, 10.0 ); +*/ +declare function incrnanmpcorr( W: number, meanx: number, meany: number ): accumulator; + +/** +* Returns an accumulator function which incrementally computes an updated moving sample correlation coefficient, ignoring `NaN` values. +* +* ## Notes +* +* - The `W` parameter defines the number of values over which to compute the moving sample correlation coefficient. +* - As `W` (x,y) pairs are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. +* +* @param W - window size +* @throws first argument must be a positive integer +* @returns accumulator function +* +* @example +* var accumulator = incrnanmpcorr( 3 ); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r = accumulator( -2.0, NaN ); +* // returns 0.0 +* +* r = accumulator( -5.0, 3.14 ); +* // returns ~-1.0 +* +* r = accumulator( NaN, 2.71 ); +* // returns ~-1.0 +* +* r = accumulator( 3.0, -1.0 ); +* // returns ~-0.925 +* +* r = accumulator( 5.0, -9.5 ); +* // returns ~-0.863 +* +* r = accumulator(); +* // returns ~-0.863 +*/ +declare function incrnanmpcorr( W: number ): accumulator; + + +// EXPORTS // + +export = incrnanmpcorr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/test.ts new file mode 100644 index 000000000000..498a4e744320 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/test.ts @@ -0,0 +1,123 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrnanmpcorr = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmpcorr( 3 ); // $ExpectType accumulator + incrnanmpcorr( 3, 1.5, 2.5 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided non-numeric arguments... +{ + incrnanmpcorr( 2, '5' ); // $ExpectError + incrnanmpcorr( 2, true ); // $ExpectError + incrnanmpcorr( 2, false ); // $ExpectError + incrnanmpcorr( 2, null ); // $ExpectError + incrnanmpcorr( 2, undefined ); // $ExpectError + incrnanmpcorr( 2, [] ); // $ExpectError + incrnanmpcorr( 2, {} ); // $ExpectError + incrnanmpcorr( 2, ( x: number ): number => x ); // $ExpectError + + incrnanmpcorr( '5', 4 ); // $ExpectError + incrnanmpcorr( true, 4 ); // $ExpectError + incrnanmpcorr( false, 4 ); // $ExpectError + incrnanmpcorr( null, 4 ); // $ExpectError + incrnanmpcorr( undefined, 4 ); // $ExpectError + incrnanmpcorr( [], 4 ); // $ExpectError + incrnanmpcorr( {}, 4 ); // $ExpectError + incrnanmpcorr( ( x: number ): number => x, 4 ); // $ExpectError + + incrnanmpcorr( '5', 2.5, 4 ); // $ExpectError + incrnanmpcorr( true, 2.5, 4 ); // $ExpectError + incrnanmpcorr( false, 2.5, 4 ); // $ExpectError + incrnanmpcorr( null, 2.5, 4 ); // $ExpectError + incrnanmpcorr( undefined, 2.5, 4 ); // $ExpectError + incrnanmpcorr( [], 2.5, 4 ); // $ExpectError + incrnanmpcorr( {}, 2.5, 4 ); // $ExpectError + incrnanmpcorr( ( x: number ): number => x, 2.5, 4 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanmpcorr( ); // $ExpectError + incrnanmpcorr( 2, 3 ); // $ExpectError + incrnanmpcorr( 2, 2, 3, 4 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmpcorr( 3 ); + + 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 = incrnanmpcorr( 3, 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 = incrnanmpcorr( 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 +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments (known means)... +{ + const acc = incrnanmpcorr( 3, 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/nanmpcorr/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/examples/index.js new file mode 100644 index 000000000000..241eee70edcd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 incrnanmpcorr = require( './../lib' ); + +var accumulator; +var r; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanmpcorr( 5 ); + +// For each simulated datum, update the moving sample correlation coefficient... +console.log( '\nx\ty\tCorrelation Coefficient\n' ); +for ( i = 0; i < 100; i++ ) { + x = ( randu() < 0.2 ) ? NaN : randu() * 100.0; + y = ( randu() < 0.2 ) ? NaN : randu() * 100.0; + r = accumulator( x, y ); + console.log( '%d\t%d\t%d', x.toFixed( 4 ), y.toFixed( 4 ), r.toFixed( 4 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/index.js new file mode 100644 index 000000000000..8a33e7903371 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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 moving sample Pearson product-moment correlation coefficient incrementally, ignoring `NaN` values.. +* +* @module @stdlib/stats/incr/nanmpcorr +* +* @example +* var incrnanmpcorr = require( '@stdlib/stats/incr/nanmpcorr' ); +* +* var accumulator = incrnanmpcorr( 3 ); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r = accumulator( -2.0, NaN ); +* // returns 0.0 +* +* r = accumulator( -5.0, 3.14 ); +* // returns ~-1.0 +* +* r = accumulator( NaN, 2.71 ); +* // returns ~-1.0 +* +* r = accumulator( 3.0, -1.0 ); +* // returns ~-0.925 +* +* r = accumulator( 5.0, -9.5 ); +* // returns ~-0.863 +* +* r = accumulator(); +* // returns ~-0.863 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/main.js new file mode 100644 index 000000000000..633bdc40739d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/main.js @@ -0,0 +1,112 @@ +/** +* @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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var format = require( '@stdlib/string/format' ); +var incrmpcorr = require( '@stdlib/stats/incr/mpcorr' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a moving sample Pearson product-moment correlation coefficient, ignoring `NaN` values.. +* +* @param {PositiveInteger} W - window size +* @param {number} [meanx] - mean value +* @param {number} [meany] - mean value +* @throws {TypeError} first argument must be a positive integer +* @throws {TypeError} second argument must be a number +* @throws {TypeError} third argument must be a number +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmpcorr( 3 ); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r = accumulator( -2.0, NaN ); +* // returns 0.0 +* +* r = accumulator( -5.0, 3.14 ); +* // returns ~-1.0 +* +* r = accumulator( NaN, 2.71 ); +* // returns ~-1.0 +* +* r = accumulator( 3.0, -1.0 ); +* // returns ~-0.925 +* +* r = accumulator( 5.0, -9.5 ); +* // returns ~-0.863 +* +* r = accumulator(); +* // returns ~-0.863 +* +* @example +* var accumulator = incrnanmpcorr( 3, -2.0, 10.0 ); +*/ +function incrnanmpcorr( W, meanx, meany ) { + var mpcorr; + if ( !isPositiveInteger( W ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a positive integer. Value: `%s`.', W ) ); + } + if ( arguments.length > 1 ) { + if ( !isNumber( meanx ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a number. Value: `%s`.', meanx ) ); + } + if ( !isNumber( meany ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a number. Value: `%s`.', meany ) ); + } + mpcorr = incrmpcorr( W, meanx, meany ); + } else { + mpcorr = incrmpcorr( W ); + } + return accumulator; + /** + * If provided a value, the accumulator function returns an updated sample correlation coefficient. If not provided a value, the accumulator function returns the current sample correlation coefficient. + * + * @private + * @param {number} [x] - input value + * @param {number} [y] - input value + * @returns {(number|null)} sample correlation coefficient or null + */ + function accumulator( x, y ) { + if ( arguments.length === 0 ) { + return mpcorr(); + } + if ( isnan( x ) || isnan( y ) ) { + return mpcorr(); + } + return mpcorr( x, y ); + } +} + + +// EXPORTS // + +module.exports = incrnanmpcorr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/package.json b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/package.json new file mode 100644 index 000000000000..a71181ccd241 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/package.json @@ -0,0 +1,79 @@ +{ + "name": "@stdlib/stats/incr/nanmpcorr", + "version": "0.0.0", + "description": "Compute a moving sample Pearson product-moment correlation coefficient incrementally.", + "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", + "pcorr", + "pearson", + "product-moment", + "bivariate", + "incremental", + "accumulator", + "moving covariance", + "moving correlation", + "sliding window", + "sliding", + "window", + "moving", + "rolling" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js new file mode 100644 index 000000000000..cded67f77321 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js @@ -0,0 +1,834 @@ +/** +* @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 randu = require( '@stdlib/random/base/randu' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var incrnanmpcorr = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Computes sample means using Welford's algorithm. +* +* @private +* @param {Array} out - output array +* @param {ArrayArray} arr - input array +* @returns {Array} output array +*/ +function mean( out, arr ) { + var delta; + var mx; + var my; + var N; + var i; + + mx = 0.0; + my = 0.0; + + N = 0; + for ( i = 0; i < arr.length; i++ ) { + N += 1; + delta = arr[i][0] - mx; + mx += delta / N; + delta = arr[i][1] - my; + my += delta / N; + } + out[ 0 ] = mx; + out[ 1 ] = my; + return out; +} + +/** +* Computes standard deviations. +* +* @private +* @param {Array} out - output array +* @param {ArrayArray} arr - input array +* @param {number} mx - `x` mean +* @param {number} my - `y` mean +* @param {boolean} bool - boolean indicating whether to compute a biased standard deviation +* @returns {Array} output array +*/ +function stdev( out, arr, mx, my, bool ) { + var delta; + var M2x; + var M2y; + var N; + var i; + + M2x = 0.0; + M2y = 0.0; + + N = 0; + for ( i = 0; i < arr.length; i++ ) { + N += 1; + delta = arr[i][0] - mx; + M2x += delta * delta; + delta = arr[i][1] - my; + M2y += delta * delta; + } + if ( bool ) { + out[ 0 ] = sqrt( M2x / N ); + out[ 1 ] = sqrt( M2y / N ); + return out; + } + if ( N < 2 ) { + out[ 0 ] = 0.0; + out[ 1 ] = 0.0; + return out; + } + out[ 0 ] = sqrt( M2x / (N-1) ); + out[ 1 ] = sqrt( M2y / (N-1) ); + return out; +} + +/** +* Computes the covariance using textbook formula. +* +* @private +* @param {ArrayArray} arr - input array +* @param {number} mx - `x` mean +* @param {number} my - `y` mean +* @param {boolean} bool - boolean indicating whether to compute the population covariance +* @returns {number} covariance +*/ +function covariance( arr, mx, my, bool ) { + var N; + var C; + var i; + + N = arr.length; + C = 0.0; + for ( i = 0; i < N; i++ ) { + C += ( arr[i][0]-mx ) * ( arr[i][1]-my ); + } + if ( bool ) { + return C / N; + } + if ( N === 1 ) { + return 0.0; + } + return C / (N-1); +} + +/** +* Computes the sample Pearson product-moment correlation coefficient using textbook formula. +* +* @private +* @param {ArrayArray} arr - input array +* @param {number} mx - `x` mean +* @param {number} my - `y` mean +* @param {boolean} bool - boolean indicating whether to compute the population correlation coefficient +* @returns {number} correlation coefficient +*/ +function pcorr( arr, mx, my, bool ) { + var cov; + var sd; + if ( bool === false && arr.length < 2 ) { + return 0.0; + } + sd = stdev( [ 0.0, 0.0 ], arr, mx, my, bool ); + cov = covariance( arr, mx, my, bool ); + return cov / ( sd[0]*sd[1] ); +} + +/** +* Generates a set of sample datasets. +* +* @private +* @param {PositiveInteger} N - number of datasets +* @param {PositiveInteger} M - dataset length +* @param {PositiveInteger} [seed] - PRNG seed +* @returns {ArrayArray} sample datasets +*/ +function datasets( N, M, seed ) { + var nanProb; + var choice; + var data; + var rand; + var tmp; + var i; + var j; + var x; + var y; + + nanProb = 0.2; + rand = randu.factory({ + 'seed': seed || ( randu()*pow( 2.0, 31 ) )|0 + }); + + data = []; + for ( i = 0; i < N; i++ ) { + tmp = []; + for ( j = 0; j < M; j++ ) { + x = rand() * pow( 10.0, i ); + y = rand() * pow( 10.0, i ); + if ( rand() < nanProb ) { + choice = floor( rand() * 3 ); + if ( choice === 0 ) { + x = NaN; + } else if ( choice === 1 ) { + y = NaN; + } else { + x = NaN; + y = NaN; + } + } + tmp.append( [ x, y ] ); + } + data.append( tmp ); + } + return data; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmpcorr, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a positive integer for the window size', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + null, + void 0, + NaN, + [], + {}, + 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() { + incrnanmpcorr( value ); + }; + } +}); + +tape( 'the function throws an error if not provided a positive integer for the window size (known means)', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + null, + void 0, + NaN, + [], + {}, + 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() { + incrnanmpcorr( value, 3.0, 3.14 ); + }; + } +}); + +tape( 'the function throws an error if not provided a number as the mean 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() { + incrnanmpcorr( 3, value, 3.14 ); + }; + } +}); + +tape( 'the function throws an error if not provided a number as the mean 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() { + incrnanmpcorr( 3, 3.14, value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmpcorr( 3 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known means)', function test( t ) { + t.equal( typeof incrnanmpcorr( 3, 3.0, 3.14 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function computes a moving sample Pearson product-moment correlation coefficient incrementally', function test( t ) { + var expected; + var actual; + var delta; + var means; + var data; + var acc; + var arr; + var tol; + var d; + var N; + var M; + var W; + var i; + var j; + + N = 10; + M = 100; + data = datasets( N, M, randu.seed ); + + // Define the window size: + W = 10; + + // For each dataset, compute the actual and expected correlation coefficients... + for ( i = 0; i < N; i++ ) { + d = data[ i ]; + + acc = incrnanmpcorr( W ); + for ( j = 0; j < M; j++ ) { + actual = acc( d[j][0], d[j][1] ); + if ( j < W ) { + arr = d.slice( 0, j+1 ).filter( function removeNan( pair ) { + return !( isnan( pair[0] ) || isnan( pair[1] ) ); + }); + } else { + arr = d.slice( j-W+1, j+1 ).filter( function removeNan( pair ) { + return !( isnan( pair[0] ) || isnan( pair[1] ) ); + }); + } + if ( arr.length === 0 ) { + expected = 0.0; + } else { + means = mean( [ 0.0, 0.0 ], arr ); + expected = pcorr( arr, means[ 0 ], means[ 1 ], false ); + } + if ( actual === expected ) { + t.equal( actual, expected, 'returns expected value. dataset: '+i+'. window: '+j+'.' ); + } else { + delta = abs( actual - expected ); + tol = 5.0e5 * EPS * abs( expected ); + t.equal( delta < tol, true, 'dataset: '+i+'. window: '+j+'. expected: '+expected+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + } + t.end(); +}); + +tape( 'the accumulator function computes a moving sample Pearson product-moment correlation coefficient incrementally (known means)', function test( t ) { + var expected; + var actual; + var means; + var delta; + var data; + var acc; + var arr; + var tol; + var d; + var N; + var M; + var W; + var i; + var j; + + N = 10; + M = 100; + data = datasets( N, M, randu.seed ); + + // Define the window size: + W = 10; + + // For each dataset, compute the actual and expected correlation coefficients... + for ( i = 0; i < N; i++ ) { + d = data[ i ]; + means = mean( [ 0.0, 0.0 ], d ); + acc = incrnanmpcorr( W, means[ 0 ], means[ 1 ] ); + for ( j = 0; j < M; j++ ) { + actual = acc( d[j][0], d[j][1] ); + if ( j < W ) { + arr = d.slice( 0, j+1 ).filter( function removeNan( pair ) { + return !( isnan( pair[0] ) || isnan( pair[1] ) ); + }); + } else { + arr = d.slice( j-W+1, j+1 ).filter( function removeNan( pair ) { + return !( isnan( pair[0] ) || isnan( pair[1] ) ); + }); + } + if ( arr.length === 0 ) { + expected = 0.0; + } else { + expected = pcorr( arr, means[ 0 ], means[ 1 ], true ); + } + if ( actual === expected ) { + t.equal( actual, expected, 'returns expected value. dataset: '+i+'. window: '+j+'.' ); + } else { + delta = abs( actual - expected ); + tol = 5.0e5 * EPS * abs( expected ); + t.equal( delta < tol, true, 'dataset: '+i+'. window: '+j+'. expected: '+expected+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current sample correlation coefficient (unknown means)', function test( t ) { + var expected; + var actual; + var means; + var delta; + var data; + var tol; + var acc; + var W; + var N; + var d; + var i; + + data = [ + [ 2.0, 1.0 ], + [ -5.0, 3.14 ], + [ NaN, -1.0 ], + [ 5.0, -9.5 ] + ]; + N = data.length; + + // Window size: + W = 3; + + acc = incrnanmpcorr( W ); + for ( i = 0; i < N; i++ ) { + acc( data[ i ][ 0 ], data[ i ][ 1 ] ); + actual = acc(); + if ( i < W-1 ) { + d = data.slice( 0, i+1 ); + } else { + d = data.slice( i-W+1, i+1 ).filter( function removeNan( pair ) { + return !( isnan(pair[0]) || isnan(pair[1]) ); + }); + } + if ( d.length === 0 ) { + expected = 0.0; + } else { + means = mean( [ 0.0, 0.0 ], d ); + expected = pcorr( d, means[ 0 ], means[ 1 ], false ); + } + if ( actual === expected ) { + t.equal( actual, expected, 'returns expected value. window: '+i+'.' ); + } else { + delta = abs( actual - expected ); + tol = 1.0 * EPS * abs( expected ); + t.equal( delta < tol, true, 'window: '+i+'. expected: '+expected+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current sample correlation coefficient (known means)', function test( t ) { + var expected; + var actual; + var means; + var delta; + var data; + var tol; + var acc; + var W; + var N; + var d; + var i; + + data = [ + [ 2.0, 1.0 ], + [ -5.0, 3.14 ], + [ 3.0, NaN ], + [ 5.0, -9.5 ] + ]; + N = data.length; + means = mean( [ 0.0, 0.0 ], data ); + + // Window size: + W = 3; + + acc = incrnanmpcorr( W, means[ 0 ], means[ 1 ] ); + for ( i = 0; i < N; i++ ) { + acc( data[ i ][ 0 ], data[ i ][ 1 ] ); + actual = acc(); + if ( i < W-1 ) { + d = data.slice( 0, i+1 ); + } else { + d = data.slice( i-W+1, i+1 ).filter( function removeNan( pair ) { + return !( isnan(pair[0]) || isnan(pair[1]) ); + }); + } + if ( d.length === 0 ) { + expected = 0.0; + } else { + expected = pcorr( d, means[ 0 ], means[ 1 ], true ); + } + if ( actual === expected ) { + t.equal( actual, expected, 'returns expected value. window: '+i+'.' ); + } else { + delta = abs( actual - expected ); + tol = 1.0 * EPS * abs( expected ); + t.equal( delta < tol, true, 'window: '+i+'. expected: '+expected+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmpcorr( 3 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null` (known means)', function test( t ) { + var acc = incrnanmpcorr( 3, 3.0, 3.14 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'if only one datum has been provided and the means are unknown, the accumulator function returns `0`', function test( t ) { + var acc = incrnanmpcorr( 3 ); + acc( 2.0, 3.14 ); + t.equal( acc(), 0.0, 'returns 0' ); + t.end(); +}); + +tape( 'if only one datum has been provided and the means are known, the accumulator function may not return `0`', function test( t ) { + var acc = incrnanmpcorr( 3, 30.0, -100.0 ); + acc( 2.0, 1.0 ); + t.notEqual( acc(), 0.0, 'does not return 0' ); + t.end(); +}); + +tape( 'if the window size is `1` and the means are unknown, the accumulator function always returns `0`', function test( t ) { + var acc; + var r; + var i; + + acc = incrnanmpcorr( 1 ); + for ( i = 0; i < 100; i++ ) { + r = acc( randu()*100.0, randu()*100.0 ); + t.equal( r, 0.0, 'returns 0' ); + } + t.end(); +}); + +tape( 'if the window size is `1` and the means are known, the accumulator function may not always return `0`', function test( t ) { + var acc; + var r; + var i; + + acc = incrnanmpcorr( 1, 500.0, -500.0 ); // means are outside the range of simulated values so the correlation should never be zero + for ( i = 0; i < 100; i++ ) { + r = acc( randu()*100.0, randu()*100.0 ); + t.notEqual( r, 0.0, 'does not return 0' ); + } + t.end(); +}); + +tape( 'if provided `NaN`, the accumulated value is `NaN` for at least `W` invocations (unknown means)', function test( t ) { + var expected; + var delta; + var data; + var acc; + var tol; + var v; + var i; + + expected = [ + NaN, + NaN, + NaN, + NaN, + NaN, + NaN, + NaN, // 0/0 + 1.0, + NaN, + NaN, + NaN, + NaN, // 0/0 + 1.0, + NaN, + NaN, + NaN, + NaN, + NaN, + NaN, + NaN, + NaN + ]; + + data = [ + [ NaN, 3.14 ], // NaN + [ 3.14, 3.14 ], // NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ NaN, 3.14 ], // 3.14, 3.14, NaN + [ 3.14, 3.14 ], // 3.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, 3.14 ], // 3.14, 3.14, 3.14 + [ -3.14, -3.14 ], // 3.14, 3.14, -3.14 + [ NaN, 3.14 ], // 3.14, -3.14, NaN + [ 3.14, 3.14 ], // -3.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, 3.14 ], // 3.14, 3.14, 3.14 + [ 6.14, 6.14 ], // 3.14, 3.14, 6.14 + [ NaN, 3.14 ], // 3.14, 6.14, NaN + [ 3.14, 3.14 ], // 6.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ NaN, 3.14 ], // 3.14, 3.14, NaN + [ NaN, 3.14 ], // 3.14, NaN, NaN + [ NaN, 3.14 ], // NaN, NaN, NaN + [ NaN, 3.14 ], // NaN, NaN, NaN + [ 3.14, 3.14 ] // NaN, NaN, 3.14 + ]; + + acc = incrnanmpcorr( 3 ); + + for ( i = 0; i < data.length; i++ ) { + v = acc( data[i][0], data[i][1] ); + if ( isnan( expected[ i ] ) ) { + t.equal( isnan( v ), true, 'returns expected value for window '+i ); + t.equal( isnan( acc() ), true, 'returns expected value for window '+i ); + } else if ( v === expected[ i ] ) { + t.equal( v, expected[ i ], 'returns expected value for window: '+i ); + } else { + delta = abs( v - expected[ i ] ); + tol = EPS * abs( expected[ i ] ); + t.equal( delta < tol, true, 'window: '+i+'. expected: '+expected[ i ]+'. actual: '+v+'. tol: '+tol+'. delta: '+delta+'.' ); + t.equal( acc(), v, 'returns expected value for window '+i ); + } + } + + data = [ + [ 3.14, NaN ], // NaN + [ 3.14, 3.14 ], // NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, NaN ], // 3.14, 3.14, NaN + [ 3.14, 3.14 ], // 3.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, 3.14 ], // 3.14, 3.14, 3.14 + [ -3.14, -3.14 ], // 3.14, 3.14, -3.14 + [ 3.14, NaN ], // 3.14, -3.14, NaN + [ 3.14, 3.14 ], // -3.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, 3.14 ], // 3.14, 3.14, 3.14 + [ 6.14, 6.14 ], // 3.14, 3.14, 6.14 + [ 3.14, NaN ], // 3.14, 6.14, NaN + [ 3.14, 3.14 ], // 6.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, NaN ], // 3.14, 3.14, NaN + [ 3.14, NaN ], // 3.14, NaN, NaN + [ 3.14, NaN ], // NaN, NaN, NaN + [ 3.14, NaN ], // NaN, NaN, NaN + [ 3.14, 3.14 ] // NaN, NaN, 3.14 + ]; + + acc = incrnanmpcorr( 3 ); + + for ( i = 0; i < data.length; i++ ) { + v = acc( data[i][0], data[i][1] ); + if ( isnan( expected[ i ] ) ) { + t.equal( isnan( v ), true, 'returns expected value for window '+i ); + t.equal( isnan( acc() ), true, 'returns expected value for window '+i ); + } else if ( v === expected[ i ] ) { + t.equal( v, expected[ i ], 'returns expected value for window: '+i ); + } else { + delta = abs( v - expected[ i ] ); + tol = EPS * abs( expected[ i ] ); + t.equal( delta < tol, true, 'window: '+i+'. expected: '+expected[ i ]+'. actual: '+v+'. tol: '+tol+'. delta: '+delta+'.' ); + t.equal( acc(), v, 'returns expected value for window '+i ); + } + } + t.end(); +}); + +tape( 'if provided `NaN`, the accumulated value is `NaN` for at least `W` invocations (known means)', function test( t ) { + var expected; + var data; + var acc; + var v; + var i; + + expected = [ + NaN, + NaN, + NaN, + NaN, + NaN, + NaN, + NaN, // 0/0 + 1.0, + NaN, + NaN, + NaN, + NaN, // 0/0 + 1.0, + NaN, + NaN, + NaN, + NaN, + NaN, + NaN, + NaN, + NaN + ]; + + data = [ + [ NaN, 3.14 ], // NaN + [ 3.14, 3.14 ], // NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ NaN, 3.14 ], // 3.14, 3.14, NaN + [ 3.14, 3.14 ], // 3.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, 3.14 ], // 3.14, 3.14, 3.14 + [ -3.14, -3.14 ], // 3.14, 3.14, -3.14 + [ NaN, 3.14 ], // 3.14, -3.14, NaN + [ 3.14, 3.14 ], // -3.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, 3.14 ], // 3.14, 3.14, 3.14 + [ 6.14, 6.14 ], // 3.14, 3.14, 6.14 + [ NaN, 3.14 ], // 3.14, 6.14, NaN + [ 3.14, 3.14 ], // 6.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ NaN, 3.14 ], // 3.14, 3.14, NaN + [ NaN, 3.14 ], // 3.14, NaN, NaN + [ NaN, 3.14 ], // NaN, NaN, NaN + [ NaN, 3.14 ], // NaN, NaN, NaN + [ 3.14, 3.14 ] // NaN, NaN, 3.14 + ]; + + acc = incrnanmpcorr( 3, 3.14, 3.14 ); + + for ( i = 0; i < data.length; i++ ) { + v = acc( data[i][0], data[i][1] ); + if ( isnan( expected[ i ] ) ) { + t.equal( isnan( v ), true, 'returns expected value for window '+i ); + t.equal( isnan( acc() ), true, 'returns expected value for window '+i ); + } else { + t.equal( v, expected[ i ], 'returns expected value for window '+i ); + t.equal( acc(), expected[ i ], 'returns expected value for window '+i ); + } + } + + data = [ + [ 3.14, NaN ], // NaN + [ 3.14, 3.14 ], // NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, NaN ], // 3.14, 3.14, NaN + [ 3.14, 3.14 ], // 3.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, 3.14 ], // 3.14, 3.14, 3.14 + [ -3.14, -3.14 ], // 3.14, 3.14, -3.14 + [ 3.14, NaN ], // 3.14, -3.14, NaN + [ 3.14, 3.14 ], // -3.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, 3.14 ], // 3.14, 3.14, 3.14 + [ 6.14, 6.14 ], // 3.14, 3.14, 6.14 + [ 3.14, NaN ], // 3.14, 6.14, NaN + [ 3.14, 3.14 ], // 6.14, NaN, 3.14 + [ 3.14, 3.14 ], // NaN, 3.14, 3.14 + [ 3.14, NaN ], // 3.14, 3.14, NaN + [ 3.14, NaN ], // 3.14, NaN, NaN + [ 3.14, NaN ], // NaN, NaN, NaN + [ 3.14, NaN ], // NaN, NaN, NaN + [ 3.14, 3.14 ] // NaN, NaN, 3.14 + ]; + + acc = incrnanmpcorr( 3, 3.14, 3.14 ); + + for ( i = 0; i < data.length; i++ ) { + v = acc( data[i][0], data[i][1] ); + if ( isnan( expected[ i ] ) ) { + t.equal( isnan( v ), true, 'returns expected value for window '+i ); + t.equal( isnan( acc() ), true, 'returns expected value for window '+i ); + } else { + t.equal( v, expected[ i ], 'returns expected value for window '+i ); + t.equal( acc(), expected[ i ], 'returns expected value for window '+i ); + } + } + t.end(); +}); From 594c5af32af15a98caf8b601d05bf9b2a65715ef Mon Sep 17 00:00:00 2001 From: hans Date: Mon, 17 Mar 2025 22:01:45 -0400 Subject: [PATCH 2/3] fix(stats): syntax error --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js index cded67f77321..536be8f26cef 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js @@ -200,9 +200,9 @@ function datasets( N, M, seed ) { y = NaN; } } - tmp.append( [ x, y ] ); + tmp.push( [ x, y ] ); } - data.append( tmp ); + data.push( tmp ); } return data; } From f91104fa885b7803b3c3a84bcc39a538c29e4cf8 Mon Sep 17 00:00:00 2001 From: hans Date: Mon, 17 Mar 2025 23:02:16 -0400 Subject: [PATCH 3/3] fix(stats): update benchmarks --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../incr/nanmpcorr/benchmark/benchmark.js | 14 +--- .../stats/incr/nanmpcorr/docs/repl.txt | 12 +-- .../incr/nanmpcorr/docs/types/index.d.ts | 16 ++-- .../@stdlib/stats/incr/nanmpcorr/lib/index.js | 14 ++-- .../@stdlib/stats/incr/nanmpcorr/lib/main.js | 18 ++-- .../@stdlib/stats/incr/nanmpcorr/test/test.js | 83 +++++-------------- 6 files changed, 51 insertions(+), 106 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/benchmark/benchmark.js index 7fa187d872aa..205b62736b96 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/benchmark/benchmark.js @@ -92,7 +92,6 @@ bench( pkg+'::accumulator,known_means', function benchmark( b ) { bench( pkg+'::accumulator,with_nan', function benchmark( b ) { var acc; - var v; var i; acc = incrnanmpcorr( 5 ); @@ -100,22 +99,15 @@ bench( pkg+'::accumulator,with_nan', function benchmark( b ) { for ( i = 0; i < b.iterations; i++ ) { if ( randu() < 0.2 ) { if ( randu() < 0.5 ) { - v = acc( NaN, randu() ); + acc( NaN, randu() ); } else { - v = acc( randu(), NaN ); + acc( randu(), NaN ); } } else { - v = acc( randu(), randu() ); - } - // Ensure that even with occasional NaN inputs the accumulator does not return NaN: - if ( v !== v ) { - b.fail( 'should not return NaN' ); + acc( randu(), randu() ); } } 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/nanmpcorr/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/repl.txt index d16308a944c4..d84297609e0e 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/repl.txt @@ -38,17 +38,17 @@ > r = accumulator( 2.0, 1.0 ) 0.0 > r = accumulator( -2.0, NaN ) - 0.0 + NaN > r = accumulator( -5.0, 3.14 ) - ~-1.0 + NaN > r = accumulator( NaN, 2.71 ) - ~-1.0 + NaN > r = accumulator( 3.0, -1.0 ) - ~-0.925 + NaN > r = accumulator( 5.0, -9.5 ) - ~-0.863 + NaN > r = accumulator() - ~-0.863 + NaN See Also -------- diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/index.d.ts index e5f4b00bc4eb..98c6c088f090 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/index.d.ts @@ -34,7 +34,7 @@ type accumulator = ( x?: number, y?: number ) => number | null; /** -* Returns an accumulator function which incrementally computes an updated moving sample correlation coefficient, ignoring `NaN` values. +* Returns an accumulator function which incrementally computes an updated moving sample correlation coefficient. * * ## Notes * @@ -53,7 +53,7 @@ type accumulator = ( x?: number, y?: number ) => number | null; declare function incrnanmpcorr( W: number, meanx: number, meany: number ): accumulator; /** -* Returns an accumulator function which incrementally computes an updated moving sample correlation coefficient, ignoring `NaN` values. +* Returns an accumulator function which incrementally computes an updated moving sample correlation coefficient. * * ## Notes * @@ -74,22 +74,22 @@ declare function incrnanmpcorr( W: number, meanx: number, meany: number ): accum * // returns 0.0 * * r = accumulator( -2.0, NaN ); -* // returns 0.0 +* // returns NaN * * r = accumulator( -5.0, 3.14 ); -* // returns ~-1.0 +* // returns NaN * * r = accumulator( NaN, 2.71 ); -* // returns ~-1.0 +* // returns NaN * * r = accumulator( 3.0, -1.0 ); -* // returns ~-0.925 +* // returns NaN * * r = accumulator( 5.0, -9.5 ); -* // returns ~-0.863 +* // returns NaN * * r = accumulator(); -* // returns ~-0.863 +* // returns NaN */ declare function incrnanmpcorr( W: number ): accumulator; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/index.js index 8a33e7903371..93b67b286690 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Compute a moving sample Pearson product-moment correlation coefficient incrementally, ignoring `NaN` values.. +* Compute a moving sample Pearson product-moment correlation coefficient incrementally. * * @module @stdlib/stats/incr/nanmpcorr * @@ -35,22 +35,22 @@ * // returns 0.0 * * r = accumulator( -2.0, NaN ); -* // returns 0.0 +* // returns NaN * * r = accumulator( -5.0, 3.14 ); -* // returns ~-1.0 +* // returns NaN * * r = accumulator( NaN, 2.71 ); -* // returns ~-1.0 +* // returns NaN * * r = accumulator( 3.0, -1.0 ); -* // returns ~-0.925 +* // returns NaN * * r = accumulator( 5.0, -9.5 ); -* // returns ~-0.863 +* // returns NaN * * r = accumulator(); -* // returns ~-0.863 +* // returns NaN */ // MODULES // diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/main.js index 633bdc40739d..6d6f625c0e8a 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/main.js @@ -22,7 +22,6 @@ var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; -var isnan = require( '@stdlib/math/base/assert/is-nan' ); var format = require( '@stdlib/string/format' ); var incrmpcorr = require( '@stdlib/stats/incr/mpcorr' ); @@ -30,7 +29,7 @@ var incrmpcorr = require( '@stdlib/stats/incr/mpcorr' ); // MAIN // /** -* Returns an accumulator function which incrementally computes a moving sample Pearson product-moment correlation coefficient, ignoring `NaN` values.. +* Returns an accumulator function which incrementally computes a moving sample Pearson product-moment correlation coefficient. * * @param {PositiveInteger} W - window size * @param {number} [meanx] - mean value @@ -50,22 +49,22 @@ var incrmpcorr = require( '@stdlib/stats/incr/mpcorr' ); * // returns 0.0 * * r = accumulator( -2.0, NaN ); -* // returns 0.0 +* // returns NaN * * r = accumulator( -5.0, 3.14 ); -* // returns ~-1.0 +* // returns NaN * * r = accumulator( NaN, 2.71 ); -* // returns ~-1.0 +* // returns NaN * * r = accumulator( 3.0, -1.0 ); -* // returns ~-0.925 +* // returns NaN * * r = accumulator( 5.0, -9.5 ); -* // returns ~-0.863 +* // returns NaN * * r = accumulator(); -* // returns ~-0.863 +* // returns NaN * * @example * var accumulator = incrnanmpcorr( 3, -2.0, 10.0 ); @@ -99,9 +98,6 @@ function incrnanmpcorr( W, meanx, meany ) { if ( arguments.length === 0 ) { return mpcorr(); } - if ( isnan( x ) || isnan( y ) ) { - return mpcorr(); - } return mpcorr( x, y ); } } diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js index 536be8f26cef..7186f9adaa83 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorr/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2018 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +27,6 @@ var pow = require( '@stdlib/math/base/special/pow' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var EPS = require( '@stdlib/constants/float64/eps' ); -var floor = require( '@stdlib/math/base/special/floor' ); var incrnanmpcorr = require( './../lib' ); @@ -168,39 +167,25 @@ function pcorr( arr, mx, my, bool ) { * @returns {ArrayArray} sample datasets */ function datasets( N, M, seed ) { - var nanProb; - var choice; var data; var rand; var tmp; var i; var j; - var x; - var y; - nanProb = 0.2; rand = randu.factory({ 'seed': seed || ( randu()*pow( 2.0, 31 ) )|0 }); + // Generate datasets consisting of (x,y) pairs of varying value ranges... data = []; for ( i = 0; i < N; i++ ) { tmp = []; for ( j = 0; j < M; j++ ) { - x = rand() * pow( 10.0, i ); - y = rand() * pow( 10.0, i ); - if ( rand() < nanProb ) { - choice = floor( rand() * 3 ); - if ( choice === 0 ) { - x = NaN; - } else if ( choice === 1 ) { - y = NaN; - } else { - x = NaN; - y = NaN; - } - } - tmp.push( [ x, y ] ); + tmp.push([ + rand() * pow( 10.0, i ), + rand() * pow( 10.0, i ) + ]); } data.push( tmp ); } @@ -371,20 +356,12 @@ tape( 'the accumulator function computes a moving sample Pearson product-moment for ( j = 0; j < M; j++ ) { actual = acc( d[j][0], d[j][1] ); if ( j < W ) { - arr = d.slice( 0, j+1 ).filter( function removeNan( pair ) { - return !( isnan( pair[0] ) || isnan( pair[1] ) ); - }); - } else { - arr = d.slice( j-W+1, j+1 ).filter( function removeNan( pair ) { - return !( isnan( pair[0] ) || isnan( pair[1] ) ); - }); - } - if ( arr.length === 0 ) { - expected = 0.0; + arr = d.slice( 0, j+1 ); } else { - means = mean( [ 0.0, 0.0 ], arr ); - expected = pcorr( arr, means[ 0 ], means[ 1 ], false ); + arr = d.slice( j-W+1, j+1 ); } + means = mean( [ 0.0, 0.0 ], arr ); + expected = pcorr( arr, means[ 0 ], means[ 1 ], false ); if ( actual === expected ) { t.equal( actual, expected, 'returns expected value. dataset: '+i+'. window: '+j+'.' ); } else { @@ -428,19 +405,11 @@ tape( 'the accumulator function computes a moving sample Pearson product-moment for ( j = 0; j < M; j++ ) { actual = acc( d[j][0], d[j][1] ); if ( j < W ) { - arr = d.slice( 0, j+1 ).filter( function removeNan( pair ) { - return !( isnan( pair[0] ) || isnan( pair[1] ) ); - }); - } else { - arr = d.slice( j-W+1, j+1 ).filter( function removeNan( pair ) { - return !( isnan( pair[0] ) || isnan( pair[1] ) ); - }); - } - if ( arr.length === 0 ) { - expected = 0.0; + arr = d.slice( 0, j+1 ); } else { - expected = pcorr( arr, means[ 0 ], means[ 1 ], true ); + arr = d.slice( j-W+1, j+1 ); } + expected = pcorr( arr, means[ 0 ], means[ 1 ], true ); if ( actual === expected ) { t.equal( actual, expected, 'returns expected value. dataset: '+i+'. window: '+j+'.' ); } else { @@ -469,7 +438,7 @@ tape( 'if not provided an input value, the accumulator function returns the curr data = [ [ 2.0, 1.0 ], [ -5.0, 3.14 ], - [ NaN, -1.0 ], + [ 3.0, -1.0 ], [ 5.0, -9.5 ] ]; N = data.length; @@ -484,16 +453,10 @@ tape( 'if not provided an input value, the accumulator function returns the curr if ( i < W-1 ) { d = data.slice( 0, i+1 ); } else { - d = data.slice( i-W+1, i+1 ).filter( function removeNan( pair ) { - return !( isnan(pair[0]) || isnan(pair[1]) ); - }); - } - if ( d.length === 0 ) { - expected = 0.0; - } else { - means = mean( [ 0.0, 0.0 ], d ); - expected = pcorr( d, means[ 0 ], means[ 1 ], false ); + d = data.slice( i-W+1, i+1 ); } + means = mean( [ 0.0, 0.0 ], d ); + expected = pcorr( d, means[ 0 ], means[ 1 ], false ); if ( actual === expected ) { t.equal( actual, expected, 'returns expected value. window: '+i+'.' ); } else { @@ -521,7 +484,7 @@ tape( 'if not provided an input value, the accumulator function returns the curr data = [ [ 2.0, 1.0 ], [ -5.0, 3.14 ], - [ 3.0, NaN ], + [ 3.0, -1.0 ], [ 5.0, -9.5 ] ]; N = data.length; @@ -537,15 +500,9 @@ tape( 'if not provided an input value, the accumulator function returns the curr if ( i < W-1 ) { d = data.slice( 0, i+1 ); } else { - d = data.slice( i-W+1, i+1 ).filter( function removeNan( pair ) { - return !( isnan(pair[0]) || isnan(pair[1]) ); - }); - } - if ( d.length === 0 ) { - expected = 0.0; - } else { - expected = pcorr( d, means[ 0 ], means[ 1 ], true ); + d = data.slice( i-W+1, i+1 ); } + expected = pcorr( d, means[ 0 ], means[ 1 ], true ); if ( actual === expected ) { t.equal( actual, expected, 'returns expected value. window: '+i+'.' ); } else {