diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr/README.md b/lib/node_modules/@stdlib/stats/incr/nanpcorr/README.md new file mode 100644 index 000000000000..373b24481ad8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/README.md @@ -0,0 +1,195 @@ + + +# incrnanpcorr + +> Compute a [sample Pearson product-moment correlation coefficient][pearson-correlation] incrementally, ignoring `NaN` values. + +
+ +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}} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanpcorr = require( '@stdlib/stats/incr/nanpcorr' ); +``` + +#### incrnanpcorr( \[mx, my] ) + +Returns an accumulator function which incrementally computes a [sample Pearson product-moment correlation coefficient][pearson-correlation], ignoring `NaN` values. + +```javascript +var accumulator = incrnanpcorr(); +``` + +If the means are already known, provide `mx` and `my` arguments. + +```javascript +var accumulator = incrnanpcorr( 3.0, -5.5 ); +``` + +#### accumulator( \[x, y] ) + +If provided input value `x` and `y`, the accumulator function returns an updated [sample correlation coefficient][pearson-correlation]. If not provided input values `x` and `y`, the accumulator function returns the current [sample correlation coefficient][pearson-correlation]. + +```javascript +var accumulator = incrnanpcorr(); + +var v = accumulator( 2.0, 1.0 ); +// returns 0.0 + +v = accumulator( NaN, 1.0 ); +// returns 0.0 + +v = accumulator( 1.0, -5.0 ); +// returns 1.0 + +v = accumulator( 1.0, NaN ); +// returns 1.0 + +v = accumulator( 3.0, 3.14 ); +// returns ~0.965 + +v = accumulator(); +// returns ~0.965 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrnanpcorr = require( '@stdlib/stats/incr/nanpcorr' ); + +var accumulator; +var r; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanpcorr(); + +// For each simulated datum, update the sample Pearson correlation coefficient... +console.log( '\nx\ty\tCorrelation Coefficient\n' ); +for ( i = 0; i < 100; i++ ) { + x = ( randu() < 0.2 ) ? NaN : randu()*100.0; // ~20% NaN values assigned to `x` + y = ( randu() < 0.2 ) ? NaN : randu()*100.0; // ~20% NaN values assigned to `y` + r = accumulator( x, y ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr/benchmark/benchmark.js new file mode 100644 index 000000000000..bac1ce63d9b9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/benchmark/benchmark.js @@ -0,0 +1,90 @@ +/** +* @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 pkg = require( './../package.json' ).name; +var incrnanpcorr = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanpcorr(); + 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 = incrnanpcorr(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( i, i ); + 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 = incrnanpcorr( 3.0, -2.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( i, i ); + 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/nanpcorr/docs/img/equation_pearson_correlation_coefficient.svg b/lib/node_modules/@stdlib/stats/incr/nanpcorr/docs/img/equation_pearson_correlation_coefficient.svg new file mode 100644 index 000000000000..61e3ef3e3500 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/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/nanpcorr/docs/img/equation_sample_pearson_correlation_coefficient.svg b/lib/node_modules/@stdlib/stats/incr/nanpcorr/docs/img/equation_sample_pearson_correlation_coefficient.svg new file mode 100644 index 000000000000..31facfed161b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/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/nanpcorr/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanpcorr/docs/repl.txt new file mode 100644 index 000000000000..8b28f55b3e85 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/docs/repl.txt @@ -0,0 +1,41 @@ + +{{alias}}( [mx, my] ) + Returns an accumulator function which incrementally computes a sample + Pearson product-moment correlation coefficient, ignoring `NaN` values. + + If provided values, the accumulator function returns an updated sample + correlation coefficient. If not provided values, the accumulator function + returns the current sample correlation coefficient. + + Parameters + ---------- + mx: number (optional) + Known mean. + + my: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var r = accumulator() + null + > r = accumulator( 2.0, 1.0 ) + 0.0 + > r = accumulator( NaN, 1.0 ) + 0.0 + > r = accumulator( -5.0, 3.14 ) + ~-1.0 + > r = accumulator( -5.0, NaN ) + ~-1.0 + > r = accumulator() + ~-1.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanpcorr/docs/types/index.d.ts new file mode 100644 index 000000000000..29c2c6838028 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/docs/types/index.d.ts @@ -0,0 +1,69 @@ +/* +* @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 sample correlation coefficient. +* +* @param x - value +* @param y - value +* @returns updated sample correlation coefficient +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes an updated sample correlation coefficient, ignoring `NaN` values. +* +* @param meanx - mean value +* @param meany - mean value +* @returns accumulator function +* +* @example +* var accumulator = incrpcorr( 2.0, -3.0 ); +*/ +declare function incrnanpcorr( meanx: number, meany: number ): accumulator; + +/** +* Returns an accumulator function which incrementally computes an updated sample correlation coefficient, ignoring `NaN` values. +* +* @returns accumulator function +* +* @example +* var accumulator = incrpcorr(); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r = accumulator( -5.0, 3.14 ); +* // returns ~-1.0 +* +* r = accumulator(); +* // returns ~-1.0 +*/ +declare function incrnanpcorr(): accumulator; + + +// EXPORTS // + +export = incrnanpcorr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanpcorr/docs/types/test.ts new file mode 100644 index 000000000000..520670dd4dc1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/docs/types/test.ts @@ -0,0 +1,113 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrnanpcorr = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanpcorr(); // $ExpectType accumulator + incrnanpcorr( 2, 4 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided non-numeric arguments... +{ + incrnanpcorr( 2, '5' ); // $ExpectError + incrnanpcorr( 2, true ); // $ExpectError + incrnanpcorr( 2, false ); // $ExpectError + incrnanpcorr( 2, null ); // $ExpectError + incrnanpcorr( 2, undefined ); // $ExpectError + incrnanpcorr( 2, [] ); // $ExpectError + incrnanpcorr( 2, {} ); // $ExpectError + incrnanpcorr( 2, ( x: number ): number => x ); // $ExpectError + + incrnanpcorr( '5', 4 ); // $ExpectError + incrnanpcorr( true, 4 ); // $ExpectError + incrnanpcorr( false, 4 ); // $ExpectError + incrnanpcorr( null, 4 ); // $ExpectError + incrnanpcorr( undefined, 4 ); // $ExpectError + incrnanpcorr( [], 4 ); // $ExpectError + incrnanpcorr( {}, 4 ); // $ExpectError + incrnanpcorr( ( x: number ): number => x, 4 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanpcorr( 1 ); // $ExpectError + incrnanpcorr( 2, 2, 3 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanpcorr(); + + 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 = incrnanpcorr( 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 = incrnanpcorr(); + + 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 = incrnanpcorr( 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/nanpcorr/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr/examples/index.js new file mode 100644 index 000000000000..b2f76c277525 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/examples/index.js @@ -0,0 +1,39 @@ +/** +* @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 incrnanpcorr = require( './../lib' ); + +// Initialize an accumulator: +var accumulator = incrnanpcorr(); + +// For each simulated datum, update the sample Pearson correlation coefficient... +console.log( '\nx\ty\tCorrelation Coefficient\n' ); +var r; +var x; +var y; +var i; +for ( i = 0; i < 100; i++ ) { + x = ( randu() < 0.2 ) ? NaN : randu() * 100.0; // ~20% NaN values assigned to `x` + y = ( randu() < 0.2 ) ? NaN : randu() * 100.0; // ~20% NaN values assigned to `y` + r = accumulator( x, y ); + console.log( '%d\t%d\t%d', x.toFixed( 4 ), y.toFixed( 4 ), ( r === null ) ? NaN : r.toFixed( 4 ) ); +} +console.log( '\nFinal r: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr/lib/index.js new file mode 100644 index 000000000000..86cbf5a9573c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute a sample Pearson product-moment correlation coefficient incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanpcorr +* +* @example +* var incrpcorr = require( '@stdlib/stats/incr/nanpcorr' ); +* +* var accumulator = incrnanpcorr(); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r = accumulator( -5.0, NaN ); +* // returns 0.0 +* +* r = accumulator( -5.0, 3.14 ); +* // returns ~-1.0 +* +* r = accumulator( NaN, 3.14 ); +* // returns ~-1.0 +* +* r = accumulator(); +* // returns ~-1.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr/lib/main.js new file mode 100644 index 000000000000..437857a844b4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/lib/main.js @@ -0,0 +1,205 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrpcorr = require( '@stdlib/stats/incr/pcorr' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a sample Pearson product-moment correlation coefficient, ignoring `NaN` values. +* +* ## Method +* +* - We begin by defining the co-moment \\(C_n\\) +* +* ```tex +* C_n = \sum_{i=1}^{n} ( x_i - \bar{x}_n ) ( y_i - \bar{y}_n ) +* ``` +* +* where \\(\bar{x}_n\\) and \\(\bar{y}_n\\) are the sample means for \\(x\\) and \\(y\\), respectively. +* +* - Based on Welford's method, we know the update formulas for the sample means are given by +* +* ```tex +* \bar{x}_n = \bar{x}_{n-1} + \frac{x_n - \bar{x}_{n-1}}{n} +* ``` +* +* and +* +* ```tex +* \bar{y}_n = \bar{y}_{n-1} + \frac{y_n - \bar{y}_{n-1}}{n} +* ``` +* +* - Substituting into the equation for \\(C_n\\) and rearranging terms +* +* ```tex +* C_n = C_{n-1} + (x_n - \bar{x}_n) (y_n - \bar{y}_{n-1}) +* ``` +* +* where the apparent asymmetry arises from +* +* ```tex +* x_n - \bar{x}_n = \frac{n-1}{n} (x_n - \bar{x}_{n-1}) +* ``` +* +* and, hence, the update term can be equivalently expressed +* +* ```tex +* \frac{n-1}{n} (x_n - \bar{x}_{n-1}) (y_n - \bar{y}_{n-1}) +* ``` +* +* - The covariance can be defined +* +* ```tex +* \begin{align*} +* \operatorname{cov}_n(x,y) &= \frac{C_n}{n} \\ +* &= \frac{C_{n-1} + (x_n - \bar{x}_n) (y_n - \bar{y}_{n-1})}{n} \\ +* &= \frac{(n-1)\operatorname{cov}_{n-1}(x,y) + (x_n - \bar{x}_n) (y_n - \bar{y}_{n-1})}{n} +* \end{align*} +* ``` +* +* - Applying Bessel's correction, we arrive at an update formula for calculating an unbiased sample covariance +* +* ```tex +* \begin{align*} +* \operatorname{cov}_n(x,y) &= \frac{n}{n-1}\cdot\frac{(n-1)\operatorname{cov}_{n-1}(x,y) + (x_n - \bar{x}_n) (y_n - \bar{y}_{n-1})}{n} \\ +* &= \operatorname{cov}_{n-1}(x,y) + \frac{(x_n - \bar{x}_n) (y_n - \bar{y}_{n-1})}{n-1} \\ +* &= \frac{C_{n-1} + (x_n - \bar{x}_n) (y_n - \bar{y}_{n-1})}{n-1} +* &= \frac{C_{n-1} + (x_n - \bar{x}_{n-1}) (y_n - \bar{y}_n)}{n-1} +* \end{align*} +* ``` +* +* - To calculate the corrected sample standard deviation, we can use Welford's method, which can be derived as follows. We can express the variance as +* +* ```tex +* \begin{align*} +* S_n &= n \sigma_n^2 \\ +* &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\ +* &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2 +* \end{align*} +* ``` +* +* Accordingly, +* +* ```tex +* \begin{align*} +* S_n - S_{n-1} &= \sum_{i=1}^{n} x_i^2 - n\mu_n^2 - \sum_{i=1}^{n-1} x_i^2 + (n-1)\mu_{n-1}^2 \\ +* &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\ +* &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\ +* &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\ +* &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\ +* &= x_n^2 - \mu_{n-1}^2 + \mu_{n-1}^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ +* &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ +* &= (x_n - \mu_{n-1})(x_n - \mu_n) \\ +* &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n) +* \end{align*} +* ``` +* +* where we use the identity +* +* ```tex +* x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1}) +* ``` +* +* - To compute the corrected sample standard deviation, we apply Bessel's correction and take the square root. +* +* - The sample Pearson product-moment correlation coefficient can thus be calculated as +* +* ```tex +* r = \frac{\operatorname{cov}_n(x,y)}{\sigma_x \sigma_y} +* ``` +* +* where \\(\sigma_x\\) and \\(\sigma_y\\) are the corrected sample standard deviations for \\(x\\) and \\(y\\), respectively. +* +* @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 = incrnanpcorr(); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r = accumulator( -5.0, NaN ); +* // returns 0.0 +* +* r = accumulator( -5.0, 3.14 ); +* // returns ~-1.0 +* +* r = accumulator( NaN, 3.14 ); +* // returns ~-1.0 +* +* r = accumulator(); +* // returns ~-1.0 +* +* @example +* var accumulator = incrpcorr( 2.0, -3.0 ); +*/ +function incrnanpcorr( meanx, meany ) { + var pcorr; + var N; + + if ( arguments.length ) { + pcorr = incrpcorr( meanx, meany ); + } else { + pcorr = incrpcorr(); + } + + N = 0; + + return accumulator; + + /** + * Accumulator function that updates the sample correlation coefficient, ignoring `NaN` values. + * + * @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 pcorr(); + } + if ( isnan( x ) || isnan( y ) ) { + return pcorr(); + } + N += 1; + return pcorr( x, y ); + } +} + + +// EXPORTS // + +module.exports = incrnanpcorr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr/package.json b/lib/node_modules/@stdlib/stats/incr/nanpcorr/package.json new file mode 100644 index 000000000000..7c68ccadef36 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/stats/incr/nanpcorr", + "version": "0.0.0", + "description": "Compute a sample Pearson product-moment correlation coefficient, ignoring `NaN` values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "covariance", + "sample covariance", + "variance", + "unbiased", + "var", + "correlation", + "corr", + "pearson", + "product-moment", + "bivariate", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr/test/test.js new file mode 100644 index 000000000000..262e20c0ca4c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr/test/test.js @@ -0,0 +1,270 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var incrnanpcorr = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanpcorr, '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() { + incrnanpcorr( 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() { + incrnanpcorr( 3.0, value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanpcorr(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known means)', function test( t ) { + t.equal( typeof incrnanpcorr( 3.0, -5.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a sample correlation coefficient', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, NaN, 3.0, 2.0, 2.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: (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, + 0.0, + -1.0, + -0.8992664495010921, + -0.8992664495010921, + 0.23547201823172273, + 0.06765492498103522, + 0.06765492498103522, + 0.4944446711225878 + ]; + + acc = incrnanpcorr(); + + for ( i = 0; i < x.length; i++ ) { + actual = acc( x[ i ], y[ i ] ); + if ( actual === expected[ i ] ) { + t.strictEqual( actual, expected[ i ], 'returns expected result. x: '+x[i]+'. y: '+y[i]+'.' ); + } else { + delta = abs( expected[ i ] - actual ); + tol = 3.7 * EPS * abs( expected[ i ] ); + t.equal( delta <= tol, true, 'x: '+x[i]+'. y: '+y[i]+'. expected: '+expected[i]+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + t.end(); +}); + +tape( 'the accumulator function incrementally computes a sample correlation 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, NaN, 3.0, 2.0, 2.0, 4.0, 3.0, NaN, 4.0 ]; + y = [ 1.5, -0.6, -0.6, 3.14, NaN, 4.0, -2.0, NaN, 10.0 ]; + + // Test against Julia: (sum((x[1:n]-3.0).*(y[1:n]-2.6733333333333333)[:])/(n-1))/(stdm(x[1:n],3.0)*stdm(y[1:n],2.6733333333333333)) + expected = [ + 1.0, + 1.0, + 0.337429241307504, + 0.14242449698664145, + 0.14242449698664145, + 0.31297710227544034, + 0.19590456246309015, + 0.19590456246309015, + 0.4944446711225878 + ]; + + acc = incrnanpcorr( 3.0, 2.6733333333333333 ); + + for ( i = 0; i < x.length; i++ ) { + actual = acc( x[ i ], y[ i ] ); + if ( actual === expected[ i ] ) { + t.strictEqual( actual, expected[ i ], 'returns expected result. x: '+x[i]+'. y: '+y[i]+'.' ); + } else { + delta = abs( expected[ i ] - actual ); + tol = EPS * abs( expected[ i ] ); + t.equal( delta <= tol, true, 'x: '+x[i]+'. y: '+y[i]+'. expected: '+expected[i]+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current sample correlation coefficient', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, NaN, 3.0, 3.0, 1.0 ]; + y = [ 3.14, -1.0, -1.0, NaN, 2.4 ]; + + acc = incrnanpcorr(); + 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 correlation coefficient (known means)', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, NaN, 3.0, 3.0, 1.0 ]; + y = [ 3.14, -1.0, -1.0, NaN, 2.4 ]; + + acc = incrnanpcorr( 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 correlation coefficient is `null` until at least 1 datum has been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorr(); + + 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 correlation coefficient is `null` until at least 1 datum has been provided (known means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorr( 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 correlation coefficient is `0` until at least 2 datums have been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorr(); + + 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(); +});