diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/README.md b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/README.md new file mode 100644 index 000000000000..34b9991b48f0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/README.md @@ -0,0 +1,197 @@ + + +# incrnanpcorr2 + +> Compute a squared sample [Pearson product-moment correlation coefficient][pearson-correlation] incrementally. + +
+ +The [Pearson product-moment correlation coefficient][pearson-correlation] between random variables `X` and `Y` is defined as + + + +```math +\rho_{X,Y} = \frac{\mathop{\mathrm{cov}}(X,Y)}{\sigma_X \sigma_Y} +``` + + + + + +where the numerator is the [covariance][covariance] and the denominator is the product of the respective standard deviations. + +For a sample of size `n`, the sample [Pearson product-moment correlation coefficient][pearson-correlation] is defined as + + + +```math +r = \frac{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})(y_i - \bar{y})}{\displaystyle\sqrt{\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \sqrt{\sum_{i=0}^{n-1} (y_i - \bar{y})^2}} +``` + + + + + +The squared sample [Pearson product-moment correlation coefficient][pearson-correlation] is thus defined as the square of the sample [Pearson product-moment correlation coefficient][pearson-correlation]. + +
+ + + +
+ +## Usage + +```javascript +var incrnanpcorr2 = require( '@stdlib/stats/incr/nanpcorr2' ); +``` + +#### incrnanpcorr2( \[mx, my] ) + +Returns an accumulator `function` which incrementally computes a squared sample [Pearson product-moment correlation coefficient][pearson-correlation]. + +```javascript +var accumulator = incrnanpcorr2(); +``` + +If the means are already known, provide `mx` and `my` arguments. + +```javascript +var accumulator = incrnanpcorr2( 3.0, -5.5 ); +``` + +#### accumulator( \[x, y] ) + +If provided input value `x` and `y`, the accumulator function returns an updated accumulated value. If not provided input values `x` and `y`, the accumulator function returns the current accumulated value. + +```javascript +var accumulator = incrnanpcorr2(); + +var r2 = accumulator( 2.0, 1.0 ); +// returns 0.0 + +r2 = accumulator( 1.0, NaN ); +// returns 0.0 + +r2 = accumulator( NaN, NaN ); +// returns ~0.0 + +r2 = accumulator( 8.0, NaN ); +// returns ~0.0 + +r2 = accumulator(); +// returns ~0.0 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN`, when used in computations,gets ignored. +- In comparison to the sample [Pearson product-moment correlation coefficient][pearson-correlation], the squared sample [Pearson product-moment correlation coefficient][pearson-correlation] is useful for emphasizing strong correlations. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanpcorr2 = require( '@stdlib/stats/incr/nanpcorr2' ); + +var accumulator; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanpcorr2(); + +// For each simulated datum, update the squared sample correlation coefficient... +console.log( '\nx\ty\tr^2\n' ); +for ( i = 0; i < 100; i++ ) { + x = randu() < 0.9 ? randu() * 100.0:NaN; + y = randu() < 0.9 ? randu() * 100.0:NaN; + r2 = accumulator( x, y ); + console.log( '%d\t%d\t%d', + isNaN(x) ? 'NaN' : x.toFixed( 4 ), + isNaN(y) ? 'NaN': y.toFixed( 4 ), + r2 === null ? 'null' : r2.toFixed( 4 ) ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/benchmark/benchmark.js new file mode 100644 index 000000000000..18cadbe94d14 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/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 incrnanpcorr2 = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanpcorr2(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanpcorr2(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu(), randu() ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator,known_means', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanpcorr2( 3.0, -2.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu(), randu() ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_pearson_correlation_coefficient.svg b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_pearson_correlation_coefficient.svg new file mode 100644 index 000000000000..61e3ef3e3500 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_pearson_correlation_coefficient.svg @@ -0,0 +1,48 @@ + +rho Subscript upper X comma upper Y Baseline equals StartFraction c o v left-parenthesis upper X comma upper Y right-parenthesis Over sigma Subscript upper X Baseline sigma Subscript upper Y Baseline EndFraction + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_sample_pearson_correlation_coefficient.svg b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_sample_pearson_correlation_coefficient.svg new file mode 100644 index 000000000000..31facfed161b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/img/equation_sample_pearson_correlation_coefficient.svg @@ -0,0 +1,126 @@ + +r equals StartFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis left-parenthesis y Subscript i Baseline minus y overbar right-parenthesis Over StartRoot sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis squared EndRoot StartRoot sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis y Subscript i Baseline minus y overbar right-parenthesis squared EndRoot EndFraction + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/repl.txt new file mode 100644 index 000000000000..86e3f74b0ebe --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/repl.txt @@ -0,0 +1,44 @@ + +{{alias}}( [mx, my] ) + Returns an accumulator function which incrementally computes the squared + 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 + gets ignored. + Parameters + ---------- + mx: number (optional) + Known mean. + + my: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var r2 = accumulator() + null + > r2 = accumulator( 2.0, 1.0 ) + 0.0 + > r2 = accumulator( -5.0, NaN ) + ~0.0 + >r2 = accumulator(NaN,NaN) + ~0.0 + >r2 = accumulator(NaN,-8) + ~0.0 + > r2 = accumulator() + ~0.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/index.d.ts new file mode 100644 index 000000000000..d59b021b09d8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/index.d.ts @@ -0,0 +1,79 @@ +/* +* @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 squared sample correlation coefficient. +* +* ## Notes +* +* - If provided `NaN` it ignores itand return the previous accumulated value. +* +* @param x - value +* @param y - value +* @returns updated squared sample correlation coefficient +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a squared sample Pearson product-moment correlation coefficient,ignoring NaN Values. +* +* @param meanx - mean value +* @param meany - mean value +* @returns accumulator function +* +* @example +* var accumulator = incrnanpcorr2( 2.0, -3.0 ); +*/ +declare function incrnanpcorr2( meanx: number, meany: number ): accumulator; + +/** +* Returns an accumulator function which incrementally computes a squared sample Pearson product-moment correlation coefficient . +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanpcorr2(); +* + var r2 = accumulator(); +* // returns null +* +* r2 = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r2 = accumulator( -5.0, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, 8.0 ); +* // returns ~0.0 +* +* r2 = accumulator(); +* // returns ~0.0 +*/ +declare function incrnanpcorr2(): accumulator; + + +// EXPORTS // + +export = incrnanpcorr2; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/docs/types/test.ts new file mode 100644 index 000000000000..c550fef45fc9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/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 incrnanpcorr2 = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanpcorr2(); // $ExpectType accumulator + incrnanpcorr2( 2, 4 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided non-numeric arguments... +{ + incrnanpcorr2( 2, '5' ); // $ExpectError + incrnanpcorr2( 2, true ); // $ExpectError + incrnanpcorr2( 2, false ); // $ExpectError + incrnanpcorr2( 2, null ); // $ExpectError + incrnanpcorr2( 2, undefined ); // $ExpectError + incrnanpcorr2( 2, [] ); // $ExpectError + incrnanpcorr2( 2, {} ); // $ExpectError + incrnanpcorr2( 2, ( x: number ): number => x ); // $ExpectError + + incrnanpcorr2( '5', 4 ); // $ExpectError + incrnanpcorr2( true, 4 ); // $ExpectError + incrnanpcorr2( false, 4 ); // $ExpectError + incrnanpcorr2( null, 4 ); // $ExpectError + incrnanpcorr2( undefined, 4 ); // $ExpectError + incrnanpcorr2( [], 4 ); // $ExpectError + incrnanpcorr2( {}, 4 ); // $ExpectError + incrnanpcorr2( ( x: number ): number => x, 4 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanpcorr2( 1 ); // $ExpectError + incrnanpcorr2( 2, 2, 3 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanpcorr2(); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The function returns an accumulator function which returns an accumulated result (known means)... +{ + const acc = incrnanpcorr2( 2, -3 ); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanpcorr2(); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments (known means)... +{ + const acc = incrnanpcorr2( 2, -3 ); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/examples/index.js new file mode 100644 index 000000000000..907a55fdb0c8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/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 incrnanpcorr2 = require( './../lib' ); + +var accumulator; +var r2; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanpcorr2(); + +// For each simulated datum, update the squared sample Pearson correlation coefficient... +console.log( '\nx\ty\tr^2\n' ); +for ( i = 0; i < 100; i++ ) { + x = randu() < 0.9 ? randu() * 100.0:NaN; + y = randu() < 0.9 ? randu() * 100.0:NaN; + r2 = accumulator( x, y ); + console.log( '%d\t%d\t%d', + isNaN(x) ? 'NaN' : x.toFixed( 4 ), + isNaN(y) ? 'NaN': y.toFixed( 4 ), + r2 === null ? 'null' : r2.toFixed( 4 ) ); +} +console.log( '\nFinal r^2: %d\n', accumulator()); diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/index.js new file mode 100644 index 000000000000..139fa4502129 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/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 squared sample Pearson product-moment correlation coefficient incrementally. +* +* @module @stdlib/stats/incr/nanpcorr2 +* +* @example +* var incrnanpcorr2 = require( '@stdlib/stats/incr/nanpcorr2' ); +* +* var accumulator = incrnanpcorr2(); +* +* var r2 = accumulator(); +* // returns null +* +* r2 = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r2 = accumulator( -5.0, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, 8.0 ); +* // returns ~0.0 +* +* r2 = accumulator(); +* // returns ~0.0 +*/ + +// MODULES // + +var incrnanpcorr2 = require( '@stdlib/stats/incr/nanpcorr2/lib/main.js' ); + + +// EXPORTS // + +module.exports = incrnanpcorr2; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/main.js new file mode 100644 index 000000000000..00882b95aecb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/lib/main.js @@ -0,0 +1,92 @@ +/** +* @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 isNaN = require( '@stdlib/math/base/assert/is-nan' ); +var incrpcorr2 = require( '@stdlib/stats/incr/pcorr2' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a squared sample Pearson product-moment correlation coefficient, ignoring NaN Values. +* +* @param {number} [meanx] - mean value +* @param {number} [meany] - mean value +* @throws {TypeError} first argument must be a number +* @throws {TypeError} second argument must be a number +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanpcorr2(); +* +* var r2 = accumulator(); +* // returns null +* +* r2 = accumulator( 2.0, 1.0 ); +* // returns 0.0 +* +* r2 = accumulator( -5.0, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, NaN ); +* // returns ~0.0 +* +* r2 = accumulator( NaN, 8.0 ); +* // returns ~0.0 +* +* r2 = accumulator(); +* // returns ~0.0 +* +* @example +* var accumulator = incrnanpcorr2( 2.0, -3.0 ); +*/ +function incrnanpcorr2( meanx, meany ) { + var pcorr2; + if(arguments.length === 0) { + pcorr2 = incrpcorr2(); + } + else{ + pcorr2 = incrpcorr2(meanx, meany); + } + return accumulator; + /** + * If provided input values, the accumulator function returns an updated accumulated value. If not provided input values, the accumulator function returns the current accumulate value. + * + * @private + * @param {number} [x] - new value + * @param {number} [y] - new value + * @returns {(number|null)} squared sample correlation coefficient or null + */ + function accumulator( x, y ) { + if (arguments.length === 0 ) { + return pcorr2(); + } + if(isNaN(x) || isNaN(y)){ + return pcorr2(); + } + return pcorr2( x, y ); + } +} + + +// EXPORTS // + +module.exports = incrnanpcorr2; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/package.json b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/package.json new file mode 100644 index 000000000000..1d9b8b3991a6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/stats/incr/pcorr2", + "version": "0.0.0", + "description": "Compute a squared sample Pearson product-moment correlation coefficient.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "covariance", + "sample covariance", + "variance", + "unbiased", + "var", + "correlation", + "corr", + "pearson", + "product-moment", + "r-squared", + "r2", + "value", + "bivariate", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorr2/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/test/test.js new file mode 100644 index 000000000000..b86561309859 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorr2/test/test.js @@ -0,0 +1,321 @@ +/** +* @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 incrnanpcorr2 = require( '@stdlib/stats/incr/nanpcorr2/lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanpcorr2, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a non-numeric value for the first argument', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanpcorr2( value, 5.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a non-numeric value for the second argument', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanpcorr2( 3.0, value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanpcorr2(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known means)', function test( t ) { + t.equal( typeof incrnanpcorr2( 3.0, -5.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a square sample correlation coefficient', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + y = [ 1.5, -0.6, 3.14, 4.0, -2.0, 10.0 ]; + + // Test against Julia: ((sum((x[1:n]-mean(x[1:n])).*(y[1:n]-mean(y[1:n]))[:])/(n-1))/(std(x[1:n])*std(y[1:n]))) + expected = [ + 0.0, + -1.0 * -1.0, + -0.8992664495010921 * -0.8992664495010921, + 0.23547201823172273 * 0.23547201823172273, + 0.06765492498103522 * 0.06765492498103522, + 0.4944446711225878 * 0.4944446711225878 + ]; + + acc = incrnanpcorr2(); + + for ( i = 0; i < x.length; i++ ) { + actual = acc( x[ i ], y[ i ] ); + if ( actual === expected[ i ] ) { + t.strictEqual( actual, expected[ i ], 'returns expected result. x: '+x[i]+'. y: '+y[i]+'.' ); + } else { + delta = abs( expected[ i ] - actual ); + tol = 7.0 * EPS * abs( expected[ i ] ); + t.equal( delta <= tol, true, 'x: '+x[i]+'. y: '+y[i]+'. expected: '+expected[i]+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + t.end(); +}); + +tape( 'the accumulator function incrementally computes a squared sample correlation coefficient (known means)', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + y = [ 1.5, -0.6, 3.14, 4.0, -2.0, 10.0 ]; + + // Test against Julia: (sum((x[1:n]-3.0).*(y[1:n]-2.6733333333333333)[:])/(n-1))/(stdm(x[1:n],3.0)*stdm(y[1:n],2.6733333333333333)) + expected = [ + 1.0, + 0.337429241307504 * 0.337429241307504, + 0.14242449698664145 * 0.14242449698664145, + 0.31297710227544034 * 0.31297710227544034, + 0.19590456246309015 * 0.19590456246309015, + 0.4944446711225878 * 0.4944446711225878 + ]; + + acc = incrnanpcorr2( 3.0, 2.6733333333333333 ); + + for ( i = 0; i < x.length; i++ ) { + actual = acc( x[ i ], y[ i ] ); + if ( actual === expected[ i ] ) { + t.strictEqual( actual, expected[ i ], 'returns expected result. x: '+x[i]+'. y: '+y[i]+'.' ); + } else { + delta = abs( expected[ i ] - actual ); + tol = 2.0 * EPS * abs( expected[ i ] ); + t.equal( delta <= tol, true, 'x: '+x[i]+'. y: '+y[i]+'. expected: '+expected[i]+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current squared sample correlation coefficient', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 1.0 ]; + y = [ 3.14, -1.0, 2.4 ]; + + acc = incrnanpcorr2(); + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + t.equal( acc(), 0.7699852380946451 * 0.7699852380946451, 'returns expected result' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current squared sample correlation coefficient (known means)', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 1.0 ]; + y = [ 3.14, -1.0, 2.4 ]; + + acc = incrnanpcorr2( 2.0, 1.5133333333333334 ); + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + t.equal( acc(), 0.7699852380946453 * 0.7699852380946453, 'returns expected result' ); + t.end(); +}); + +tape( 'the squared sample correlation coefficient is `null` until at least 1 datum has been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorr2(); + + v = acc(); + t.equal( v, null, 'returns null' ); + + v = acc( 2.0, 10.0 ); + t.notEqual( v, null, 'does not return null' ); + + v = acc(); + t.notEqual( v, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the squared sample correlation coefficient is `null` until at least 1 datum has been provided (known means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorr2( 3.0, -5.0 ); + + v = acc(); + t.equal( v, null, 'returns null' ); + + v = acc( 2.0, 10.0 ); + t.notEqual( v, null, 'does not return null' ); + + v = acc(); + t.notEqual( v, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the squared sample correlation coefficient is `0` until at least 2 datums have been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorr2(); + + v = acc( 2.0, 10.0 ); + t.equal( v, 0.0, 'returns 0' ); + + v = acc(); + t.equal( v, 0.0, 'returns 0' ); + + v = acc( 3.0, -3.14 ); + t.notEqual( v, 0.0, 'does not return 0' ); + + v = acc(); + t.notEqual( v, 0.0, 'does not return 0' ); + + t.end(); +}); + +tape( 'the accumulated result ignores NaN values', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + // Data with NaN values: + x = [ 2.0, NaN, 3.0, 4.0, NaN, 5.0 ]; + y = [ 1.5, 2.0, NaN, 4.0, 5.0, NaN ]; + + // Only pairs where neither x nor y is NaN should be considered + // Effective data: [ [2.0, 1.5], [4.0, 4.0] ] + expected = 1.0; // Perfect correlation with only these two points + + acc = incrnanpcorr2(); + + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + + actual = acc(); + + if ( actual === expected ) { + t.strictEqual( actual, expected, 'returns expected result' ); + } else { + delta = abs( expected - actual ); + tol = 7.0 * EPS * abs( expected ); + t.equal( delta <= tol, true, 'expected: '+expected+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + + t.end(); +}); + +tape( 'the accumulated result ignores NaN values (known means)', function test( t ) { + var acc; + + // Create accumulator with known means + acc = incrnanpcorr2( 3.0, 2.0 ); + + // Add data with some NaN values + acc( 2.0, 1.0 ); + acc( NaN, 3.0 ); // Should ignore + acc( 4.0, NaN ); // Should ignore + acc( 4.0, 3.0 ); + + // NaN values should be ignored in the calculation + t.false( isnan(acc()), 'does not return NaN' ); + + t.end(); +}); + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/README.md b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/README.md new file mode 100644 index 000000000000..42d55b90a0b2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/README.md @@ -0,0 +1,174 @@ + + +# incrnanpcorrdist + +> Compute a [sample Pearson product-moment correlation distance][pearson-correlation] incrementally. + +
+ +The [sample Pearson product-moment correlation distance][pearson-correlation] is defined as + + + +```math +d_{x,y} = 1 - r_{x,y} = 1 - \frac{\mathop{\mathrm{cov_n(x,y)}}}{\sigma_x \sigma_y} +``` + + + + + +where `r` is the [sample Pearson product-moment correlation coefficient][pearson-correlation], `cov(x,y)` is the sample covariance, and `σ` corresponds to the sample standard deviation. As `r` resides on the interval `[-1,1]`, `d` resides on the interval `[0,2]`. + +
+ + + +
+ +## Usage + +```javascript +var incrnanpcorrdist = require( '@stdlib/stats/incr/nanpcorrdist' ); +``` + +#### incrnanpcorrdist( \[mx, my] ) + +Returns an accumulator `function` which incrementally computes a [sample Pearson product-moment correlation distance][pearson-correlation]. + +```javascript +var accumulator = incrnanpcorrdist(); +``` + +If the means are already known, provide `mx` and `my` arguments. + +```javascript +var accumulator = incrnanpcorrdist( 3.0, -5.5 ); +``` + +#### accumulator( \[x, y] ) + +If provided input value `x` and `y`, the accumulator function returns an updated [sample correlation coefficient][pearson-correlation]. If not provided input values `x` and `y`, the accumulator function returns the current [sample correlation coefficient][pearson-correlation]. + +```javascript +var accumulator = incrnanpcorrdist(); + +var d = accumulator( 2.0, 1.0 ); +// returns 1.0 + +d = accumulator( 1.0, -5.0 ); +// returns 0.0 + +d = accumulator( 3.0, 3.14 ); +// returns ~0.035 + +d = accumulator(); +// returns ~0.035 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN` as a value the accumulator ignores it and + returns previously accumulated values. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanpcorrdist = require( '@stdlib/stats/incr/nanpcorrdist' ); + +var accumulator; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanpcorrdist(); +console.log( '\nx\ty\tCorrelation Distance\n' ); +// For each simulated datum, update the sample correlation distance... +for ( i = 0; i < 100; i++ ) { + x = randu() < 0.9 ? randu() * 100.0: NaN; + y = randu() < 0.9 ? randu() * 100.0: NaN; + d = accumulator( x, y ); + console.log( '%d\t%d\t%d', + isNaN(x) ? 'NaN' : x.toFixed( 4 ), + isNaN(y) ? 'NaN': y.toFixed( 4 ), + d === null ? 'null' : d.toFixed( 4 ) ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/benchmark/benchmark.js new file mode 100644 index 000000000000..6b457c9d0c72 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/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 incrnanpcorrdist = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanpcorrdist(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanpcorrdist(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu(), randu() ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator,known_means', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanpcorrdist( 3.0, -2.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu(), randu() ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/img/equation_pearson_distance.svg b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/img/equation_pearson_distance.svg new file mode 100644 index 000000000000..6df633f17b81 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/img/equation_pearson_distance.svg @@ -0,0 +1,70 @@ + +d Subscript x comma y Baseline equals 1 minus r Subscript x comma y Baseline equals 1 minus StartFraction normal c normal o normal v Subscript normal n Baseline left-parenthesis normal x comma normal y right-parenthesis Over sigma Subscript x Baseline sigma Subscript y Baseline EndFraction + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/repl.txt new file mode 100644 index 000000000000..d34dd66145bd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/repl.txt @@ -0,0 +1,47 @@ + +{{alias}}( [mx, my] ) + Returns an accumulator function which incrementally computes a sample + Pearson product-moment correlation distance. + + The correlation distance is defined as one minus the Pearson product-moment + correlation coefficient and, thus, resides on the interval [0,2]. + + If provided values, the accumulator function returns an updated sample + correlation distance. If not provided values, the accumulator function + returns the current sample correlation distance. + + If provided `NaN` as a value the accumulator ignores it and + returns previously accumulated values. + + Parameters + ---------- + mx: number (optional) + Known mean. + + my: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var d = accumulator() + null + > d = accumulator( 2.0, 1.0 ) + 1.0 + > d = accumulator( -5.0, NaN ) + 1.0 + > d = accumulator( NaN, NaN ) + 1.0 + > d = accumulator( NaN, 9.0 ) + 1.0 + > d = accumulator() + 1.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/types/index.d.ts new file mode 100644 index 000000000000..1e508339dfa1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/types/index.d.ts @@ -0,0 +1,77 @@ +/* +* @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 input values, the accumulator function returns an updated sample correlation distance. If not provided input values, the accumulator function returns the current sample correlation distance. +* +* ## Notes +* +* - The correlation distance is defined as one minus the Pearson product-moment correlation coefficient and, thus, resides on the interval [0,2]. +* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations. +* +* @param x - value +* @param y - value +* @returns updated sample correlation distance +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a sample Pearson product-moment correlation distance. +* +* @param meanx - mean value +* @param meany - mean value +* @returns accumulator function +* +* @example +* var accumulator = incrnanpcorrdist( 2.0, -3.0 ); +*/ +declare function incrnanpcorrdist( meanx: number, meany: number ): accumulator; + +/** +* Returns an accumulator function which incrementally computes a sample Pearson product-moment correlation distance. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanpcorrdist(); +* +* var d = accumulator(); +* // returns null +* +* d = accumulator( 2.0, 1.0 ); +* // returns 1.0 +* +* d = accumulator( -5.0, NaN ); +* // returns 1.0 +* +* d = accumulator( NaN, NaN ); +* // returns 1.0 +* +* d = accumulator(); +* // returns 1.0 +*/ +declare function incrnanpcorrdist(): accumulator; + + +// EXPORTS // + +export = incrnanpcorrdist; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/docs/types/test.ts new file mode 100644 index 000000000000..15e95cdf4ea8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/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 incrnanpcorrdist = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanpcorrdist(); // $ExpectType accumulator + incrnanpcorrdist( 2, 4 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided non-numeric arguments... +{ + incrnanpcorrdist( 2, '5' ); // $ExpectError + incrnanpcorrdist( 2, true ); // $ExpectError + incrnanpcorrdist( 2, false ); // $ExpectError + incrnanpcorrdist( 2, null ); // $ExpectError + incrnanpcorrdist( 2, undefined ); // $ExpectError + incrnanpcorrdist( 2, [] ); // $ExpectError + incrnanpcorrdist( 2, {} ); // $ExpectError + incrnanpcorrdist( 2, ( x: number ): number => x ); // $ExpectError + + incrnanpcorrdist( '5', 4 ); // $ExpectError + incrnanpcorrdist( true, 4 ); // $ExpectError + incrnanpcorrdist( false, 4 ); // $ExpectError + incrnanpcorrdist( null, 4 ); // $ExpectError + incrnanpcorrdist( undefined, 4 ); // $ExpectError + incrnanpcorrdist( [], 4 ); // $ExpectError + incrnanpcorrdist( {}, 4 ); // $ExpectError + incrnanpcorrdist( ( x: number ): number => x, 4 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanpcorrdist( 1 ); // $ExpectError + incrnanpcorrdist( 2, 2, 3 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanpcorrdist(); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The function returns an accumulator function which returns an accumulated result (known means)... +{ + const acc = incrnanpcorrdist( 2, -3 ); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanpcorrdist(); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments (known means)... +{ + const acc = incrnanpcorrdist( 2, -3 ); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/examples/index.js new file mode 100644 index 000000000000..ac4d33a2cd3e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/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 incrpcorrdist = require( './../lib' ); + +var accumulator; +var d; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrpcorrdist(); + +// For each simulated datum, update the sample Pearson correlation distance... +console.log( '\nx\ty\tCorrelation Distance\n' ); +for ( i = 0; i < 100; i++ ) { + x = randu() < 0.9 ? randu() * 100.0: NaN; + y = randu() < 0.9 ? randu() * 100.0: NaN; + d = accumulator( x, y ); + console.log( '%d\t%d\t%d', + isNaN(x) ? 'NaN' : x.toFixed( 4 ), + isNaN(y) ? 'NaN': y.toFixed( 4 ), + d === null ? 'null' : d.toFixed( 4 ) ); +} +console.log( '\nFinal 1-r: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/index.js new file mode 100644 index 000000000000..7e9d7675e4c2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/index.js @@ -0,0 +1,55 @@ +/** +* @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 Pearson product-moment correlation distance incrementally. +* +* @module @stdlib/stats/incr/nanpcorrdist +* +* @example +* var incrnanpcorrdist = require( '@stdlib/stats/incr/nanpcorrdist' ); +* +* var accumulator = incrnanpcorrdist(); +* +* var d = accumulator(); +* // returns null +* +* d = accumulator( 2.0, 1.0 ); +* // returns 1.0 +* +* d = accumulator( -5.0, NaN ); +* // returns 1.0 +* +* d = accumulator( NaN, NaN ); +* // returns 1.0 +* +* d = accumulator(); +* // returns 1.0 +* +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/main.js new file mode 100644 index 000000000000..2b69ee3b5c72 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/lib/main.js @@ -0,0 +1,101 @@ +/** +* @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 incrpcorrdist = require( '@stdlib/stats/incr/pcorrdist' ); +var isNaN = require( '@stdlib/assert/is-nan' ); + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a sample Pearson product-moment correlation distance,, ignoring NaN values. +* +* ## Method +* +* - The sample Pearson product-moment correlation distance is defined as +* +* ```tex +* d = 1 - r = 1 - \frac{\operatorname{cov}_n(x,y)}{\sigma_x \sigma_y} +* ``` +* +* - The implementation thus computes the sample Pearson product-moment correlation coefficient \\(r\\) and subtracts the coefficient from 1, 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 = incrnanpcorrdist(); +* +* var d = accumulator(); +* // returns null +* +* d = accumulator( 2.0, 1.0 ); +* // returns 1.0 +* +* d = accumulator( -5.0, NaN ); +* // returns 1.0 +* +* d = accumulator( NaN, NaN ); +* // returns 1.0 +* +* d = accumulator(); +* // returns 1.0 +* +* @example +* var accumulator = incrnanpcorrdist( 2.0, -3.0 ); +*/ +function incrnanpcorrdist( meanx, meany ) { + var pcorrdist; + if(arguments.length === 0) { + pcorrdist = incrpcorrdist(); + } + else{ + pcorrdist = incrpcorrdist(meanx, meany); + } + return accumulator; + + /** + * If provided input values, the accumulator function returns an updated sample correlation distance. If not provided input values, the accumulator function returns the current sample correlation distance + * ,ignoring NaN values. + * + * @private + * @param {number} [x] - new value + * @param {number} [y] - new value + * @returns {(number|null)} sample correlation distance or null + */ + function accumulator( x, y ) { + if (arguments.length === 0 ) { + return pcorrdist(); + } + if(isNaN(x) || isNaN(y)){ + return pcorrdist(); + } + return pcorrdist( x, y ); + } +} + + +// EXPORTS // + +module.exports = incrnanpcorrdist; diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/package.json b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/package.json new file mode 100644 index 000000000000..ed15ea80bdd2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/stats/incr/pcorrdist", + "version": "0.0.0", + "description": "Compute a sample Pearson product-moment correlation distance.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "correlation", + "corr", + "pcorr", + "ppmcc", + "pcc", + "pearson", + "product-moment", + "bivariate", + "distance", + "dist", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/test/test.js new file mode 100644 index 000000000000..0de7d72a98de --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanpcorrdist/test/test.js @@ -0,0 +1,310 @@ +/** +* @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 incrnanpcorrdist = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanpcorrdist, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a non-numeric value for the first argument', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanpcorrdist( value, 5.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a non-numeric value for the second argument', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanpcorrdist( 3.0, value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanpcorrdist(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known means)', function test( t ) { + t.equal( typeof incrnanpcorrdist( 3.0, -5.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a sample correlation distance', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + y = [ 1.5, -0.6, 3.14, 4.0, -2.0, 10.0 ]; + + // Test against Julia: 1.0-(sum((x[1:n]-mean(x[1:n])).*(y[1:n]-mean(y[1:n]))[:])/(n-1))/(std(x[1:n])*std(y[1:n])) + expected = [ + 1.0, + 2.0, + 1.8992664495010921, + 0.7645279817682773, + 0.9323450750189648, + 0.5055553288774122 + ]; + + acc = incrnanpcorrdist(); + + for ( i = 0; i < x.length; i++ ) { + actual = acc( x[ i ], y[ i ] ); + if ( actual === expected[ i ] ) { + t.strictEqual( actual, expected[ i ], 'returns expected result. x: '+x[i]+'. y: '+y[i]+'.' ); + } else { + delta = abs( expected[ i ] - actual ); + tol = 3.7 * EPS * abs( expected[ i ] ); + t.equal( delta <= tol, true, 'x: '+x[i]+'. y: '+y[i]+'. expected: '+expected[i]+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + t.end(); +}); + +tape( 'the accumulator function incrementally computes a sample correlation distance (known means)', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + y = [ 1.5, -0.6, 3.14, 4.0, -2.0, 10.0 ]; + + // Test against Julia: 1.0-(sum((x[1:n]-3.0).*(y[1:n]-2.6733333333333333)[:])/(n-1))/(stdm(x[1:n],3.0)*stdm(y[1:n],2.6733333333333333)) + expected = [ + 0.0, + 0.662570758692496, + 0.8575755030133585, + 0.6870228977245596, + 0.8040954375369098, + 0.5055553288774122 + ]; + + acc = incrnanpcorrdist( 3.0, 2.6733333333333333 ); + + for ( i = 0; i < x.length; i++ ) { + actual = acc( x[ i ], y[ i ] ); + if ( actual === expected[ i ] ) { + t.strictEqual( actual, expected[ i ], 'returns expected result. x: '+x[i]+'. y: '+y[i]+'.' ); + } else { + delta = abs( expected[ i ] - actual ); + tol = EPS * abs( expected[ i ] ); + t.equal( delta <= tol, true, 'x: '+x[i]+'. y: '+y[i]+'. expected: '+expected[i]+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current sample correlation distance', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 1.0 ]; + y = [ 3.14, -1.0, 2.4 ]; + + acc = incrnanpcorrdist(); + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + t.equal( acc(), 1.7699852380946451, 'returns expected result' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current sample correlation distance (known means)', function test( t ) { + var acc; + var x; + var y; + var i; + + x = [ 2.0, 3.0, 1.0 ]; + y = [ 3.14, -1.0, 2.4 ]; + + acc = incrnanpcorrdist( 2.0, 1.5133333333333334 ); + for ( i = 0; i < x.length; i++ ) { + acc( x[ i ], y[ i ] ); + } + t.equal( acc(), 1.7699852380946453, 'returns expected result' ); + t.end(); +}); + +tape( 'the sample correlation distance is `null` until at least 1 datum has been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorrdist(); + + v = acc(); + t.equal( v, null, 'returns null' ); + + v = acc( 3.0, -3.14 ); + t.notEqual( v, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the sample correlation distance is `null` until at least 1 datum has been provided (known means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorrdist( 3.0, -5.0 ); + + v = acc(); + t.equal( v, null, 'returns null' ); + + v = acc( 3.0, -3.14 ); + t.notEqual( v, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the sample correlation distance is `1` until at least 2 datums have been provided (unknown means)', function test( t ) { + var acc; + var v; + + acc = incrnanpcorrdist(); + + v = acc( 2.0, 10.0 ); + t.equal( v, 1.0, 'returns 1' ); + + v = acc( 3.0, -3.14 ); + t.notEqual( v, 1.0, 'does not return 1' ); + + t.end(); +}); + +tape( 'the accumulator function ignores NaN values (known means)', function test( t ) { + var expected; + var actual; + var delta; + var tol; + var acc; + var x; + var y; + var i; + + // Data with NaN values + x = [ 2.0, NaN, 3.0, 4.0, NaN ]; + y = [ 1.5, -0.6, NaN, 4.0, 5.0 ]; + + // Known means based on the effective data pairs + var meanX = 3.0; + var meanY = 2.75; + + expected = [ + 0.0, // After first valid pair with provided means + 0.0 // After second valid pair + ]; + + acc = incrnanpcorrdist( meanX, meanY ); + + var validPairs = 0; + + for ( i = 0; i < x.length; i++ ) { + actual = acc( x[ i ], y[ i ] ); + + if ( !isnan(x[i]) && !isnan(y[i]) ) { + validPairs++; + + if ( validPairs <= expected.length ) { + if ( actual === expected[ validPairs-1 ] ) { + t.strictEqual( actual, expected[ validPairs-1 ], 'returns expected result. x: '+x[i]+'. y: '+y[i]+'.' ); + } else { + delta = abs( expected[ validPairs-1 ] - actual ); + tol = EPS * abs( expected[ validPairs-1 ] ); + t.equal( delta <= tol, true, 'x: '+x[i]+'. y: '+y[i]+'. expected: '+expected[validPairs-1]+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + } + } + } + + actual = acc(); + if ( actual === expected[expected.length-1] ) { + t.strictEqual( actual, expected[expected.length-1], 'returns expected result' ); + } else { + delta = abs( expected[expected.length-1] - actual ); + tol = EPS * abs( expected[expected.length-1] ); + t.equal( delta <= tol, true, 'expected: '+expected[expected.length-1]+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + } + + t.end(); +}); \ No newline at end of file