From d64593a9b10a6e636e387223ba1fa60582d331fe Mon Sep 17 00:00:00 2001 From: Girish Garg Date: Sat, 15 Mar 2025 19:04:58 +0530 Subject: [PATCH] build: Add incremental absolute Pearson correlation function ignoring NaN --- .../@stdlib/stats/incr/nanapcorr/README.md | 197 ++++++++++ .../incr/nanapcorr/benchmark/benchmark.js | 91 +++++ ...uation_pearson_correlation_coefficient.svg | 48 +++ ...sample_pearson_correlation_coefficient.svg | 126 ++++++ .../stats/incr/nanapcorr/docs/repl.txt | 43 +++ .../incr/nanapcorr/docs/types/index.d.ts | 80 ++++ .../stats/incr/nanapcorr/docs/types/test.ts | 113 ++++++ .../stats/incr/nanapcorr/examples/index.js | 44 +++ .../@stdlib/stats/incr/nanapcorr/lib/index.js | 57 +++ .../@stdlib/stats/incr/nanapcorr/lib/main.js | 107 ++++++ .../@stdlib/stats/incr/nanapcorr/package.json | 74 ++++ .../@stdlib/stats/incr/nanapcorr/test/test.js | 359 ++++++++++++++++++ 12 files changed, 1339 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/img/equation_pearson_correlation_coefficient.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/img/equation_sample_pearson_correlation_coefficient.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanapcorr/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanapcorr/README.md b/lib/node_modules/@stdlib/stats/incr/nanapcorr/README.md new file mode 100644 index 000000000000..8f395e6913ab --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/README.md @@ -0,0 +1,197 @@ + + +# incrnanapcorr + +> Compute a sample absolute [Pearson product-moment correlation coefficient][pearson-correlation] incrementally. + +
+ +The [Pearson product-moment correlation coefficient][pearson-correlation] between random variables `X` and `Y` is defined as + + + +```math +\rho_{X,Y} = \frac{\mathop{\mathrm{cov}}(X,Y)}{\sigma_X \sigma_Y} +``` + + + + + +where the numerator is the [covariance][covariance] and the denominator is the product of the respective standard deviations. + +For a sample of size `n`, the sample [Pearson product-moment correlation coefficient][pearson-correlation] is defined as + + + +```math +r = \frac{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})(y_i - \bar{y})}{\displaystyle\sqrt{\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \sqrt{\sum_{i=0}^{n-1} (y_i - \bar{y})^2}} +``` + + + + + +The sample **absolute** [Pearson product-moment correlation coefficient][pearson-correlation] is thus defined as the absolute value of the sample [Pearson product-moment correlation coefficient][pearson-correlation]. + +
+ + + +
+ +## Usage + +```javascript +var incrnanapcorr = require( '@stdlib/stats/incr/nanapcorr' ); +``` + +#### incrnanapcorr( \[mx, my] ) + +Returns an accumulator `function` which incrementally computes a sample absolute [Pearson product-moment correlation coefficient][pearson-correlation]. + +```javascript +var accumulator = incrnanapcorr(); +``` + +If the means are already known, provide `mx` and `my` arguments. + +```javascript +var accumulator = incrnanapcorr( 3.0, -5.5 ); +``` + +#### accumulator( \[x, y] ) + +If provided input value `x` and `y`, the accumulator function returns an updated accumulated value. If not provided input values `x` and `y`, the accumulator function returns the current accumulated value. + +```javascript +var accumulator = incrnanapcorr(); + +var v = accumulator( 2.0, 1.0 ); +// returns 0.0 + +var v = accumulator( NaN, 1.0 ); //ignore NaN +// returns 0.0 + +v = accumulator( 1.0, -5.0 ); +// returns 1.0 + +v = accumulator( 3.0, 3.14 ); +// returns ~0.965 + +v = accumulator( 3.0, NaN ); //ignore NaN +// returns ~0.965 + +v = accumulator(); +// returns ~0.965 +``` + +
+ + + +
+ +## Notes + +- Input values are type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, then it will be Ignored but you are advised to type check and handle accordingly **before** passing the value to the accumulator function. +- In comparison to the sample [Pearson product-moment correlation coefficient][pearson-correlation], the sample absolute [Pearson product-moment correlation coefficient][pearson-correlation] is useful when only concerned with the strength of the correlation and not the direction. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanapcorr = require( '@stdlib/stats/incr/nanapcorr' ); + +var accumulator; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanapcorr(); + +// For each simulated datum, update the sample absolute correlation coefficient... +for ( i = 0; i < 100; i++ ) { + + x = (randu() < 0.2) ? NaN : randu() * 100.0; // 20% of the time, generate a NaN + y = (randu() < 0.1) ? NaN : randu() * 100.0; // 10% of the time, generate a NaN + + ar = accumulator( x, y ); + console.log( '%d\t%d\t%d', isnan(x) ? 'NaN' : x.toFixed( 4 ), isnan(y) ? 'NaN' : y.toFixed( 4 ), ar.toFixed( 4 ) ); +} +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanapcorr/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanapcorr/benchmark/benchmark.js new file mode 100644 index 000000000000..361b2d241adc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/benchmark/benchmark.js @@ -0,0 +1,91 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 incrnanapcorr = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanapcorr(); + 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 = incrnanapcorr(); + + 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 = incrnanapcorr( 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/nanapcorr/docs/img/equation_pearson_correlation_coefficient.svg b/lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/img/equation_pearson_correlation_coefficient.svg new file mode 100644 index 000000000000..61e3ef3e3500 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/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/nanapcorr/docs/img/equation_sample_pearson_correlation_coefficient.svg b/lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/img/equation_sample_pearson_correlation_coefficient.svg new file mode 100644 index 000000000000..31facfed161b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/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/nanapcorr/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/repl.txt new file mode 100644 index 000000000000..62cb2e192978 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/repl.txt @@ -0,0 +1,43 @@ + +{{alias}}( [mx, my] ) + Returns an accumulator function which incrementally computes the absolute + value of the sample Pearson product-moment correlation coefficient. + + If provided values, the accumulator function returns an updated accumulated + value. If not provided values, the accumulator function returns the current + accumulated value. + + If provided `NaN` or a value which, when used in computations, results in + `NaN`, it will be ignored by accumulator function. + + Parameters + ---------- + mx: number (optional) + Known mean. + + my: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var ar = accumulator() + null + > ar = accumulator( 2.0, 1.0 ) + 0.0 + > ar = accumulator( NaN, 3.14 ); #ignores NaN + 0.0 + > ar = accumulator( -5.0, 3.14 ); + ~1.0 + > ar = accumulator( 4.0, NaN ); #ignores NaN + ~1.0 + > ar = accumulator() + ~1.0 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/types/index.d.ts new file mode 100644 index 000000000000..0d8762efc9fb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/types/index.d.ts @@ -0,0 +1,80 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided arguments, returns an updated sample absolute Pearson product-moment correlation coefficient; otherwise, returns the current sample absolute Pearson product-moment correlation coefficient. +* +* ## Notes +* +* - If provided `NaN` or a value which, when used in computations, it will be ignored by accumulator function. +* +* @param x - value +* @param y - value +* @returns sample absolute Pearson product-moment correlation coefficient +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a sample absolute Pearson product-moment correlation coefficient. +* +* @param meanx - mean value +* @param meany - mean value +* @returns accumulator function +* +* @example +* var accumulator = incrnanapcorr( 2.0, -3.0 ); +*/ +declare function incrnanapcorr( meanx: number, meany: number ): accumulator; + +/** +* Returns an accumulator function which incrementally computes a sample absolute Pearson product-moment correlation coefficient. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanapcorr(); +* +* var ar = accumulator(); +* // returns null +* +* ar = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* ar = accumulator( NaN, 3.14 ); // ignores NaN +* // returns 0.0 +* +* ar = accumulator( -5.0, 3.14 ); +* // returns ~1.0 +* +* ar = accumulator( 4.0, NaN ); // ignores NaN +* // returns ~1.0 +* +* ar = accumulator(); +* // returns ~1.0 +* +*/ +declare function incrnanapcorr(): accumulator; + + +// EXPORTS // + +export = incrnanapcorr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/types/test.ts new file mode 100644 index 000000000000..6c900420c9c5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/docs/types/test.ts @@ -0,0 +1,113 @@ +/* +* @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 incrnanapcorr = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanapcorr(); // $ExpectType accumulator + incrnanapcorr( 2, 4 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided non-numeric arguments... +{ + incrnanapcorr( 2, '5' ); // $ExpectError + incrnanapcorr( 2, true ); // $ExpectError + incrnanapcorr( 2, false ); // $ExpectError + incrnanapcorr( 2, null ); // $ExpectError + incrnanapcorr( 2, undefined ); // $ExpectError + incrnanapcorr( 2, [] ); // $ExpectError + incrnanapcorr( 2, {} ); // $ExpectError + incrnanapcorr( 2, ( x: number ): number => x ); // $ExpectError + + incrnanapcorr( '5', 4 ); // $ExpectError + incrnanapcorr( true, 4 ); // $ExpectError + incrnanapcorr( false, 4 ); // $ExpectError + incrnanapcorr( null, 4 ); // $ExpectError + incrnanapcorr( undefined, 4 ); // $ExpectError + incrnanapcorr( [], 4 ); // $ExpectError + incrnanapcorr( {}, 4 ); // $ExpectError + incrnanapcorr( ( x: number ): number => x, 4 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanapcorr( 1 ); // $ExpectError + incrnanapcorr( 2, 2, 3 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanapcorr(); + + 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 = incrnanapcorr( 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 = incrnanapcorr(); + + 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 = incrnanapcorr( 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/nanapcorr/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanapcorr/examples/index.js new file mode 100644 index 000000000000..5f69254a08cd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/examples/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 incrnanapcorr = require( './../lib' ); +const isnan = require('@stdlib/math/base/assert/is-nan'); + +var accumulator; +var ar; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanapcorr(); + +// For each simulated datum, update the sample absolute Pearson correlation coefficient... +console.log( '\nx\ty\t|r|\n' ); +for ( i = 0; i < 100; i++ ) { + + x = (randu() < 0.2) ? NaN : randu() * 100.0; // 20% of the time, generate a NaN + y = (randu() < 0.1) ? NaN : randu() * 100.0; // 10% of the time, generate a NaN + + ar = accumulator( x, y ); + console.log( '%d\t%d\t%d', isnan(x) ? 'NaN' : x.toFixed( 4 ), isnan(y) ? 'NaN' : y.toFixed( 4 ), ar.toFixed( 4 ) ); +} +console.log( '\nFinal |r|: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/index.js new file mode 100644 index 000000000000..3f0d20e421bd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 absolute Pearson product-moment correlation coefficient incrementally ignoring NaN. +* +* @module @stdlib/stats/incr/nanapcorr +* +* @example +* var incrapcorr = require( '@stdlib/stats/incr/nanapcorr' ); +* +* var accumulator = incrnanapcorr(); +* +* var ar = accumulator(); +* // returns null +* +* ar = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* ar = accumulator( NaN, 3.14 ); +* // ignores NaN, returns previous correlation +* +* ar = accumulator( -5.0, 3.14 ); +* // returns ~1.0 +* +* ar = accumulator( 4.0, NaN ); +* // ignores NaN, returns previous correlation +* +* ar = accumulator(); +* // returns ~1.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js new file mode 100644 index 000000000000..ada21eab5893 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js @@ -0,0 +1,107 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 incrpcorr = require('@stdlib/stats/incr/pcorr'); +var abs = require('@stdlib/math/base/special/abs'); +var format = require('@stdlib/string/format'); + +// MAIN // + +/** + * Returns an accumulator function which incrementally computes a sample absolute Pearson product-moment correlation coefficient, + * while **ignoring NaN values**. + * + * @param {number} [meanx] - mean value + * @param {number} [meany] - mean value + * @throws {TypeError} first argument must be a number + * @throws {TypeError} second argument must be a number + * @returns {Function} accumulator function + * + * @example + * var accumulator = incrnanapcorr(); + * + * var ar = accumulator(); + * // returns null + * + * ar = accumulator( 2.0, 1.0 ); + * // returns 0.0 + * + * ar = accumulator( NaN, 3.14 ); + * // ignores NaN, returns previous correlation + * + * ar = accumulator( -5.0, 3.14 ); + * // returns ~1.0 + * + * ar = accumulator( 4.0, NaN ); + * // ignores NaN, returns previous correlation + * + * ar = accumulator(); + * // returns ~1.0 + * + * @example + * var accumulator = incrnanapcorr( 2.0, -3.0 ); + */ +function incrnanapcorr(meanx, meany) { + var acc; + var N; + if (arguments.length) { + if (!isNumber(meanx)) { + throw new TypeError(format('invalid argument. First argument must be a number. Value: `%s`.', meanx)); + } + if (!isNumber(meany)) { + throw new TypeError(format('invalid argument. Second argument must be a number. Value: `%s`.', meany)); + } + acc = incrpcorr(meanx, meany); + } else { + acc = incrpcorr(); + } + N = 0; + return accumulator; + + /** + * If provided input values, the accumulator function returns an updated sample correlation coefficient. + * If not provided input values, the accumulator function returns the current sample correlation coefficient. + * + * @private + * @param {number} [x] - new value + * @param {number} [y] - new value + * @returns {(number|null)} sample absolute correlation coefficient or null + */ + function accumulator(x, y) { + if (arguments.length === 0) { + if (N === 0) { + return null; + } + return abs(acc()); + } + // Ignore NaN values + if (x !== x || y !== y) { + return abs(acc()); + } + N += 1; + return abs(acc(x, y)); + } +} + +// EXPORTS // +module.exports = incrnanapcorr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanapcorr/package.json b/lib/node_modules/@stdlib/stats/incr/nanapcorr/package.json new file mode 100644 index 000000000000..fb608881be12 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/stats/incr/nanapcorr", + "version": "0.0.0", + "description": "Compute a sample absolute Pearson product-moment correlation coefficient ignoring NaN.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "covariance", + "sample covariance", + "variance", + "unbiased", + "var", + "correlation", + "corr", + "pearson", + "product-moment", + "abs", + "absolute", + "value", + "bivariate", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanapcorr/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanapcorr/test/test.js new file mode 100644 index 000000000000..c5f8bfd9e496 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanapcorr/test/test.js @@ -0,0 +1,359 @@ +/** +* @license Apache-2.0 +* +* 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var incrnanapcorr = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanapcorr, '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() { + incrnanapcorr( 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() { + incrnanapcorr( 3.0, value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanapcorr(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known means)', function test( t ) { + t.equal( typeof incrnanapcorr( 3.0, -5.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a sample absolute correlation coefficient', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + y = [ 1.5, -0.6, 3.14, 4.0, -2.0, 10.0 ]; + + // Test against Julia: abs((sum((x[1:n]-mean(x[1:n])).*(y[1:n]-mean(y[1:n]))[:])/(n-1))/(std(x[1:n])*std(y[1:n]))) + expected = [ + 0.0, + 1.0, + 0.8992664495010921, + 0.23547201823172273, + 0.06765492498103522, + 0.4944446711225878 + ]; + + acc = incrnanapcorr(); + + 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 absolute correlation coefficient (known means)', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + y = [ 1.5, -0.6, 3.14, 4.0, -2.0, 10.0 ]; + + // Test against Julia: (abs(sum((x[1:n]-3.0).*(y[1:n]-2.6733333333333333)[:])/(n-1))/(stdm(x[1:n],3.0)*stdm(y[1:n],2.6733333333333333))) + expected = [ + 1.0, + 0.337429241307504, + 0.14242449698664145, + 0.31297710227544034, + 0.19590456246309015, + 0.4944446711225878 + ]; + + acc = incrnanapcorr( 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( 'the accumulator function incrementally computes a sample absolute correlation coefficient ignoring NaN', 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, 4.0, 3.0, NaN, 4.0 ]; + y = [ 1.5, -0.6, 3.14, 3.14, 4.0, NaN, -2.0, NaN, 10.0 ]; + + // Test against Julia: abs((sum((x[1:n]-mean(x[1:n])).*(y[1:n]-mean(y[1:n]))[:])/(n-1))/(std(x[1:n])*std(y[1:n]))) + expected = [ + 0.0, + 1.0, + 1.0, + 0.8992664495010921, + 0.23547201823172273, + 0.23547201823172273, + 0.06765492498103522, + 0.06765492498103522, + 0.4944446711225878 + ]; + + acc = incrnanapcorr(); + + 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 absolute correlation coefficient ignoring NaN(known means)', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, NaN, 3.0, 2.0, 4.0, 4.0, 3.0, NaN, 4.0 ]; + y = [ 1.5, 1.5, -0.6, 3.14, NaN, 4.0, -2.0, NaN, 10.0 ]; + + // Test against Julia: (abs(sum((x[1:n]-3.0).*(y[1:n]-2.6733333333333333)[:])/(n-1))/(stdm(x[1:n],3.0)*stdm(y[1:n],2.6733333333333333))) + expected = [ + 1.0, + 1.0, + 0.337429241307504, + 0.14242449698664145, + 0.14242449698664145, + 0.31297710227544034, + 0.19590456246309015, + 0.19590456246309015, + 0.4944446711225878 + ]; + + acc = incrnanapcorr( 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 absolute correlation coefficient', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, NaN, 1.0, NaN ]; + y = [ 3.14, -1.0, 2.4, 2.4, NaN ]; + + acc = incrnanapcorr(); + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + t.equal( acc(), 0.7699852380946451, 'returns expected result' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current sample absolute correlation coefficient (known means)', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, NaN, 1.0, NaN ]; + y = [ 3.14, -1.0, 2.4, 2.4, NaN ]; + + acc = incrnanapcorr( 2.0, 1.5133333333333334 ); + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + t.equal( acc(), 0.7699852380946453, 'returns expected result' ); + t.end(); +}); + +tape( 'the sample absolute correlation coefficient is `null` until at least 1 datum has been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanapcorr(); + + v = acc(); + t.equal( v, null, 'returns null' ); + + v = acc( 2.0, 10.0 ); + t.notEqual( v, null, 'does not return null' ); + + v = acc(); + t.notEqual( v, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the sample absolute correlation coefficient is `null` until at least 1 datum has been provided (known means)', function test( t ) { + var acc; + var v; + + acc = incrnanapcorr( 3.0, -5.0 ); + + v = acc(); + t.equal( v, null, 'returns null' ); + + v = acc( 2.0, 10.0 ); + t.notEqual( v, null, 'does not return null' ); + + v = acc(); + t.notEqual( v, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the sample absolute correlation coefficient is `0` until at least 2 datums have been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanapcorr(); + + v = acc( 2.0, 10.0 ); + t.equal( v, 0.0, 'returns 0' ); + + v = acc(); + t.equal( v, 0.0, 'returns 0' ); + + v = acc( 3.0, -3.14 ); + t.notEqual( v, 0.0, 'does not return 0' ); + + v = acc(); + t.notEqual( v, 0.0, 'does not return 0' ); + + t.end(); +}); + +tape( 'if provided NaN values, the accumulator function should handle them correctly', function test( t ) { + var acc; + + acc = incrnanapcorr(); + acc( NaN, 2.0 ); + acc( 3.0, NaN ); + acc( NaN, NaN ); + + t.ok( !isnan( acc() ), 'does not return NaN' ); + t.end(); +});