diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/README.md b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/README.md new file mode 100644 index 000000000000..957ad380fb88 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/README.md @@ -0,0 +1,175 @@ + + +# incrnanmpcorrdist + +> Compute a moving [sample Pearson product-moment correlation distance][pearson-correlation] incrementally, ignoring `NaN` values. + +
+ +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 incrnanmpcorrdist = require( '@stdlib/stats/incr/nanmpcorrdist' ); +``` + +#### incrnanmpcorrdist( window\[, mx, my] ) + +Returns an accumulator `function` which incrementally computes a moving [sample Pearson product-moment correlation distance][pearson-correlation] while ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving [sample Pearson product-moment correlation distance][pearson-correlation]. + +```javascript +var accumulator = incrnanmpcorrdist( 3 ); +``` + +If means are already known, provide `mx` and `my` arguments. + +```javascript +var accumulator = incrnanmpcorrdist( 3, 5.0, -3.14 ); +``` + +#### accumulator( \[x, y] ) + +If provided input values `x` and `y`, the accumulator function returns an updated [sample Pearson product-moment correlation distance][pearson-correlation]. If not provided input values `x` and `y`, the accumulator function returns the current [sample Pearson product-moment correlation distance][pearson-correlation]. + +```javascript +var accumulator = incrnanmpcorrdist( 3 ); + +var r = accumulator(); +// returns null + +// Fill the window... +r = accumulator( 2.0, 1.0 ); // [(2.0, 1.0)] +// returns 1.0 + +r = accumulator( NaN, 3.0 ); // [(2.0, 1.0), (NaN, 3.0)] +// returns 1.0 + +r = accumulator( 4.0, NaN ); // [(2.0, 1.0), (NaN, 3.0), (4.0, NaN)] +// returns 1.0 + +// Window begins sliding... +r = accumulator( NaN, NaN ); // [(NaN, 3.0), (4.0, NaN), (NaN, NaN)] +// returns 1.0 + +r = accumulator( 5.0, 2.0 ); // [(4.0, NaN), (NaN, NaN), 5.0, 2.0] +// returns 0.0 + +r = accumulator(); +// returns 0.0 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, it will be ignored, and the accumulated value is the last one. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. +- As `W` (x,y) pairs are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. +- Due to limitations inherent in representing numeric values using floating-point format (i.e., the inability to represent numeric values with infinite precision), the [sample correlation distance][pearson-correlation] between perfectly correlated random variables may **not** be `0` or `2`. In fact, the [sample correlation distance][pearson-correlation] is **not** guaranteed to be strictly on the interval `[0,2]`. Any computed distance should, however, be within floating-point roundoff error. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanmpcorrdist = require( '@stdlib/stats/incr/nanmpcorrdist' ); + +var accumulator; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanmpcorrdist( 5 ); +var d; + +// For each simulated datum, update the moving sample correlation distance... +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + x = NaN; + } + else { + x = randu() * 100.0; + } + if ( randu() < 0.2 ) { + y = NaN; + } + else { + y = randu() * 100.0; + } + d = accumulator( x, y ); + console.log( '%d\t%d\t%d', x.toFixed( 4 ), y.toFixed( 4 ), ( d === null ) ? NaN : d.toFixed( 4 ) ); +} +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/benchmark/benchmark.js new file mode 100644 index 000000000000..a21dc347570a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/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 incrnanmpcorrdist = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmpcorrdist( ( i%5 ) +1 ); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanmpcorrdist( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu(), randu() ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator,known_means', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanmpcorrdist( 5, 3.0, -1.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu(), randu() ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/docs/img/equation_pearson_distance.svg b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/docs/img/equation_pearson_distance.svg new file mode 100644 index 000000000000..6df633f17b81 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/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/nanmpcorrdist/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/docs/repl.txt new file mode 100644 index 000000000000..e9488748c4ce --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/docs/repl.txt @@ -0,0 +1,64 @@ + +{{alias}}( W[, mx, my] ) + Returns an accumulator function which incrementally computes a moving + sample Pearson product-moment correlation distance, ignoring + `NaN` values. + + The correlation distance is defined as one minus the Pearson product-moment + correlation coefficient and, thus, resides on the interval [0,2]. + + However, due to limitations inherent in representing numeric values using + floating-point format (i.e., the inability to represent numeric values with + infinite precision), the correlation distance between perfectly correlated + random variables may *not* be `0` or `2`. In fact, the correlation distance + is *not* guaranteed to be strictly on the interval [0,2]. Any computed + distance should, however, be within floating-point roundoff error. + + The `W` parameter defines the number of values over which to compute the + moving sample correlation distance. + + If provided values, the accumulator function returns an updated moving + sample correlation distance. If not provided values, the accumulator + function returns the current moving sample correlation distance. + + As `W` (x,y) pairs are needed to fill the window buffer, the first `W-1` + returned values are calculated from smaller sample sizes. Until the window + is full, each returned value is calculated from all provided values. + + Parameters + ---------- + W: integer + Window size. + + mx: number (optional) + Known mean. + + my: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 3 ); + > var d = accumulator() + null + > d = accumulator( 2.0, 1.0 ) + 1.0 + > d = accumulator( NaN, 3.0 ) + 1.0 + > d = accumulator( 4.0, NaN ) + 1.0 + > d = accumulator( NaN, NaN ) + 1.0 + > d = accumulator( 5.0, 2.0 ) + 0.0 + > d = accumulator() + 0.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/docs/types/index.d.ts new file mode 100644 index 000000000000..d33bf2c245be --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/docs/types/index.d.ts @@ -0,0 +1,97 @@ +/* +* @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 + +/// + +/** +* With ignoring `NaN` values, if provided input values, the accumulator function returns an updated moving sample correlation distance ignoring . If not provided input values, the accumulator function returns the current moving 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 moving sample correlation distance +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a moving sample Pearson product-moment correlation distance ignoring ignoring `NaN` values. +* +* ## Notes +* +* - The `W` parameter defines the number of values over which to compute the moving sample correlation distance. +* - As `W` (x,y) pairs are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. +* +* @param W - window size +* @param meanx - mean value +* @param meany - mean value +* @throws first argument must be a positive integer +* @returns accumulator function +* +* @example +* var accumulator = incrnanmpcorrdist( 3, -2.0, 10.0 ); +*/ +declare function incrnanmpcorrdist( W: number, meanx: number, meany: number ): accumulator; + +/** +* Returns an accumulator function which incrementally computes a moving sample Pearson product-moment correlation distance ignoring `NaN` values. +* +* ## Notes +* +* - The `W` parameter defines the number of values over which to compute the moving sample correlation distance. +* - As `W` (x,y) pairs are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. +* +* @param W - window size +* @throws first argument must be a positive integer +* @returns accumulator function +* +* @example +* var accumulator = incrnanmpcorrdist( 3 ); +* +* var d = accumulator(); +* // returns null +* +* d = accumulator( 2.0, 1.0 ); +* // returns 1.0 +* +* d = accumulator( NaN, 3.0 ); +* // returns 1.0 +* +* d = accumulator( 4.0, NaN ); +* // returns 1.0 +* +* d = accumulator( NaN, NaN ); +* // returns 1.0 +* +* d = accumulator( 5.0, 2.0 ); +* // returns 0.0 +* +* d = accumulator(); +* // returns 0.0 +*/ +declare function incrnanmpcorrdist( W: number ): accumulator; + + +// EXPORTS // + +export = incrnanmpcorrdist; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/docs/types/test.ts new file mode 100644 index 000000000000..2f45627eae36 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/docs/types/test.ts @@ -0,0 +1,123 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrnanmpcorrdist = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmpcorrdist( 3 ); // $ExpectType accumulator + incrnanmpcorrdist( 3, 2.5, 4.5 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided non-numeric arguments... +{ + incrnanmpcorrdist( 2, '5' ); // $ExpectError + incrnanmpcorrdist( 2, true ); // $ExpectError + incrnanmpcorrdist( 2, false ); // $ExpectError + incrnanmpcorrdist( 2, null ); // $ExpectError + incrnanmpcorrdist( 2, undefined ); // $ExpectError + incrnanmpcorrdist( 2, [] ); // $ExpectError + incrnanmpcorrdist( 2, {} ); // $ExpectError + incrnanmpcorrdist( 2, ( x: number ): number => x ); // $ExpectError + + incrnanmpcorrdist( '5', 4 ); // $ExpectError + incrnanmpcorrdist( true, 4 ); // $ExpectError + incrnanmpcorrdist( false, 4 ); // $ExpectError + incrnanmpcorrdist( null, 4 ); // $ExpectError + incrnanmpcorrdist( undefined, 4 ); // $ExpectError + incrnanmpcorrdist( [], 4 ); // $ExpectError + incrnanmpcorrdist( {}, 4 ); // $ExpectError + incrnanmpcorrdist( ( x: number ): number => x, 4 ); // $ExpectError + + incrnanmpcorrdist( '5', 2.5, 3.5 ); // $ExpectError + incrnanmpcorrdist( true, 2.5, 3.5 ); // $ExpectError + incrnanmpcorrdist( false, 2.5, 3.5 ); // $ExpectError + incrnanmpcorrdist( null, 2.5, 3.5 ); // $ExpectError + incrnanmpcorrdist( undefined, 2.5, 3.5 ); // $ExpectError + incrnanmpcorrdist( [], 2.5, 3.5 ); // $ExpectError + incrnanmpcorrdist( {}, 2.5, 3.5 ); // $ExpectError + incrnanmpcorrdist( ( x: number ): number => x, 2.5, 3.5 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanmpcorrdist(); // $ExpectError + incrnanmpcorrdist( 2, 2 ); // $ExpectError + incrnanmpcorrdist( 2, 2, 3, 4 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmpcorrdist( 3 ); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The function returns an accumulator function which returns an accumulated result (known means)... +{ + const acc = incrnanmpcorrdist( 3, 2, -3 ); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanmpcorrdist( 3 ); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments (known means)... +{ + const acc = incrnanmpcorrdist( 3, 2, -3 ); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/examples/index.js new file mode 100644 index 000000000000..1e837fc367df --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/examples/index.js @@ -0,0 +1,50 @@ +/** +* @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 incrnanmpcorrdist = require( './../lib' ); + +var accumulator; +var d; +var x; +var y; +var i; + +// Initialize an accumulator: +accumulator = incrnanmpcorrdist( 5 ); + +// For each simulated datum, update the moving sample correlation distance... +console.log( '\nx\ty\tCorrelation Distance\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + x = NaN; + } + else { + x = randu() * 100.0; + } + if ( randu() < 0.2 ) { + y = NaN; + } + else { + y = randu() * 100.0; + } + d = accumulator( x, y ); + console.log( '%d\t%d\t%d', x.toFixed( 4 ), y.toFixed( 4 ), ( d === null ) ? NaN : d.toFixed( 4 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/lib/index.js new file mode 100644 index 000000000000..85da6ba3b046 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/lib/index.js @@ -0,0 +1,60 @@ +/** +* @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 moving sample Pearson product-moment correlation distance incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanmpcorrdist +* +* @example +* var incrnanmpcorrdist = require( '@stdlib/stats/incr/nanmpcorrdist' ); +* +* var accumulator = incrnanmpcorrdist( 3 ); +* +* var d = accumulator(); +* // returns null +* +* d = accumulator( 2.0, 1.0 ); +* // returns 1.0 +* +* d = accumulator( NaN, 3.0 ); +* // returns 1.0 +* +* d = accumulator( 4.0, NaN ); +* // returns 1.0 +* +* d = accumulator( NaN, NaN ); +* // returns 1.0 +* +* d = accumulator( 5.0, 2.0 ); +* // returns 0.0 +* +* d = accumulator(); +* // returns 0.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/lib/main.js new file mode 100644 index 000000000000..b6b15612aa90 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/lib/main.js @@ -0,0 +1,99 @@ +/** +* @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 incrmpcorrdist = require( '@stdlib/stats/incr/mpcorrdist' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a moving sample Pearson product-moment correlation distance, ignoring `NaN` values. +* +* @param {PositiveInteger} W - Sliding window size for the moving calculation. Must be a positive integer. +* @param {number} [x] - Initial mean value of the first dataset. Defaults to `NaN`. +* @param {number} [y] - Initial mean value of the second dataset. Defaults to `NaN`. +* @returns {Function} Accumulator function which computes the Pearson correlation distance. +* +* @example +* var accumulator = incrnanmpcorrdist( 3 ); +* +* var d = accumulator(); +* // returns null +* +* d = accumulator( 2.0, 1.0 ); +* // returns 1.0 +* +* d = accumulator( NaN, 3.0 ); +* // returns 1.0 +* +* d = accumulator( 4.0, NaN ); +* // returns 1.0 +* +* d = accumulator( NaN, NaN ); +* // returns 1.0 +* +* d = accumulator( 5.0, 2.0 ); +* // returns 0.0 +* +* d = accumulator(); +* // returns 0.0 +* +* @example +* var accumulator = incrnanmpcorrdist( 3, -2.0, 10.0 ); +*/ +function incrnanmpcorrdist( W, x, y ) { + var d; + if ( arguments.length > 1 ) { + if ( isnan( x ) || isnan( y ) ) { + d = incrmpcorrdist( W ); + } + else { + d = incrmpcorrdist( W, x, y ); + } + } + else { + d = incrmpcorrdist( W ); + } + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated moving sample Pearson product-moment correlation distance. If not provided a value, the accumulator function returns the current moving sample Pearson product-moment correlation distance. + * + * @private + * @param {number} [x] - new value + * @param {number} [y] - new value + * @returns {(number|null)} moving sample Pearson product-moment correlation distance or null + */ + function accumulator( x, y ) { + if ( arguments.length === 0 || isnan( x ) || isnan( y ) ) { + return d(); + } + + return d( x, y ); + } +} + + +// EXPORTS // + +module.exports = incrnanmpcorrdist; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/package.json b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/package.json new file mode 100644 index 000000000000..cc4d4bc12d76 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/stats/incr/nanmpcorrdist", + "version": "0.0.0", + "description": "Compute a moving sample Pearson product-moment correlation distance incrementally while 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", + "summation", + "sum", + "total", + "incremental", + "accumulator" + ] + } diff --git a/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/test/test.js new file mode 100644 index 000000000000..488f0cf0df9b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmpcorrdist/test/test.js @@ -0,0 +1,671 @@ +/** +* @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 randu = require( '@stdlib/random/base/randu' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var min = require( '@stdlib/math/base/special/min' ); +var incrnanmpcorrdist = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Computes sample means using Welford's algorithm. +* +* @private +* @param {Array} out - output array +* @param {ArrayArray} arr - input array +* @returns {Array} output array +*/ +function mean( out, arr ) { + var delta; + var mx; + var my; + var N; + var i; + + mx = 0.0; + my = 0.0; + + N = 0; + for ( i = 0; i < arr.length; i++ ) { + N += 1; + delta = arr[ i ][ 0 ] - mx; + mx += delta / N; + delta = arr[ i ][ 1 ] - my; + my += delta / N; + } + out[ 0 ] = mx; + out[ 1 ] = my; + return out; +} + +/** +* Computes standard deviations. +* +* @private +* @param {Array} out - output array +* @param {ArrayArray} arr - input array +* @param {number} mx - `x` mean +* @param {number} my - `y` mean +* @param {boolean} bool - boolean indicating whether to compute a biased standard deviation +* @returns {Array} output array +*/ +function stdev( out, arr, mx, my, bool ) { + var delta; + var M2x; + var M2y; + var N; + var i; + + M2x = 0.0; + M2y = 0.0; + + N = 0; + for ( i = 0; i < arr.length; i++ ) { + N += 1; + delta = arr[ i ][ 0 ] - mx; + M2x += delta * delta; + delta = arr[ i ][ 1 ] - my; + M2y += delta * delta; + } + if ( bool ) { + out[ 0 ] = sqrt( M2x / N ); + out[ 1 ] = sqrt( M2y / N ); + return out; + } + if ( N < 2 ) { + out[ 0 ] = 0.0; + out[ 1 ] = 0.0; + return out; + } + out[ 0 ] = sqrt( M2x / ( N - 1 ) ); + out[ 1 ] = sqrt( M2y / ( N - 1 ) ); + return out; +} + +/** +* Computes the covariance using textbook formula. +* +* @private +* @param {ArrayArray} arr - input array +* @param {number} mx - `x` mean +* @param {number} my - `y` mean +* @param {boolean} bool - boolean indicating whether to compute the population covariance +* @returns {number} covariance +*/ +function covariance( arr, mx, my, bool ) { + var N; + var C; + var i; + + N = arr.length; + C = 0.0; + for ( i = 0; i < N; i++ ) { + C += ( arr[ i ][ 0 ] - mx ) * ( arr[ i ][ 1 ] - my ); + } + if ( bool ) { + return C / N; + } + if ( N === 1 ) { + return 0.0; + } + return C / ( N - 1 ); +} + +/** +* Computes the sample Pearson product-moment correlation distance using textbook formula. +* +* @private +* @param {ArrayArray} arr - input array +* @param {number} mx - `x` mean +* @param {number} my - `y` mean +* @param {boolean} bool - boolean indicating whether to compute the population correlation distance +* @returns {number} correlation distance +*/ +function pcorrdist( arr, mx, my, bool ) { + var cov; + var sd; + var d; + if ( bool === false && arr.length < 2 ) { + return 1.0; + } + sd = stdev( [ 0.0, 0.0 ], arr, mx, my, bool ); + cov = covariance( arr, mx, my, bool ); + d = 1.0 - ( cov / ( sd[ 0 ] * sd[ 1 ] ) ); + if ( d < 0.0 ) { + return 0.0; + } + if ( d > 2.0 ) { + return 2.0; + } + return d; +} + +/** +* Generates a set of sample datasets. +* +* @private +* @param {PositiveInteger} N - number of datasets +* @param {PositiveInteger} M - dataset length +* @param {PositiveInteger} [seed] - PRNG seed +* @returns {ArrayArray} sample datasets +*/ +function nandatasets( N, M, seed ) { + var data; + var rand; + var tmp; + var i; + var j; + + rand = randu.factory({ + 'seed': seed || ( randu() * pow( 2.0, 31 ) )|0 + }); + + // Generate datasets consisting of (x,y) pairs of varying value ranges, where some of the pairs may contain `(NaN, y)`, `(x, NaN)`, or `(NaN, NaN)`... + data = []; + for ( i = 0; i < N; i++ ) { + tmp = []; + for ( j = 0; j < M; j++ ) { + tmp.push([ + ( rand() < 0.2 ) ? NaN : rand() * pow( 10.0, i ), + ( rand() < 0.2 ) ? NaN : rand() * pow( 10.0, i ) + ]); + } + data.push( tmp ); + } + return data; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmpcorrdist, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a positive integer for the window size', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + null, + void 0, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanmpcorrdist( value ); + }; + } +}); + +tape( 'the function throws an error if not provided a positive integer for the window size (known means)', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + null, + void 0, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanmpcorrdist( value, 3.0, 3.14 ); + }; + } +}); + +tape( 'the function throws an error if not provided a number as the mean value', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanmpcorrdist( 3, value, 3.14 ); + }; + } +}); + +tape( 'the function throws an error if not provided a number as the mean value', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanmpcorrdist( 3, 3.14, value ); + }; + } +}); + +tape('the function properly ignores NaN values in x and y inputs', function test( t ) { + var result; + var acc; + + acc = incrnanmpcorrdist( 3, 3.14, 0.0 ); + result = acc(); + + t.ok(!isnan( result ), 'result should not be NaN'); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmpcorrdist( 3 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known means)', function test( t ) { + t.equal( typeof incrnanmpcorrdist( 3, 3.0, 3.14 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function computes a moving sample Pearson product-moment correlation distance incrementally', function test( t ) { + var countNonNanPairs; + var expected; + var actual; + var delta; + var means; + var data; + var acc; + var arr; + var tol; + var d; + var N; + var M; + var W; + var i; + var j; + var k; + + N = 10; + M = 100; + data = nandatasets( N, M, randu.seed ); + + t.pass( 'seed: ' + randu.seed ); + + // Define the window size: + W = 10; + + // For each dataset, compute the actual and expected correlation distances... + for ( i = 0; i < N; i++ ) { + d = data[ i ]; + + acc = incrnanmpcorrdist( W ); + countNonNanPairs = 0; + for ( j = 0; j < M; j++ ) { + actual = acc( d[ j ][ 0 ], d[ j ][ 1 ] ); + countNonNanPairs += ( !isnan( d[ j ][ 0 ] ) && !isnan( d[ j ][ 1 ] ) ) ? 1 : 0; + + arr = []; + k = j; + while ( arr.length < min( countNonNanPairs, W ) && k >= 0 ) { + if ( !isnan( d[ k ][ 0 ] ) && !isnan( d[ k ][ 1 ] ) ) { + arr.push( d[ k ] ); + } + k -= 1; + } + + if ( arr.length > 0 ) { + means = mean( [ 0.0, 0.0 ], arr ); + } else { + means = null; + } + + if ( arr.length > 0 ) { + expected = pcorrdist( arr, means[ 0 ], means[ 1 ], false ); + } else { + expected = null; + } + + if ( actual === expected ) { + t.equal( actual, expected, 'returns expected value. dataset: ' + i + '. window: ' + j + '.' ); + } else { + if ( actual < 0.0 ) { + actual = 0.0; // NOTE: this addresses occasional negative values due to accumulated floating-point error. Based on observation, typically `|actual| ≅ |expected|`, but `actual < 0` and `expected > 0`, suggesting that a sign got "flipped" along the way due to, e.g., operations which theoretically should compute to the same value, but do not due to floating-point error. + } + delta = abs( actual - expected ); + if ( expected === 0.0 || actual === 0.0 ) { + tol = 10.0 * EPS; + } else { + tol = 1.0e6 * EPS * abs( expected ); + } + t.equal( delta <= tol, true, 'dataset: ' + i + '. window: ' + j + '. expected: ' + expected + '. actual: ' + actual + '. tol: ' + tol + '. delta: ' + delta + '.' ); + } + } + } + t.end(); +}); + +tape( 'the accumulator function computes a moving sample Pearson product-moment correlation distance incrementally (known means)', function test( t ) { + var countNonNanPairs; + var filteredArr; + var expected; + var actual; + var means; + var delta; + var data; + var acc; + var arr; + var tol; + var d; + var N; + var M; + var W; + var i; + var j; + var k; + + N = 10; + M = 100; + data = nandatasets( N, M, randu.seed ); + + t.pass( 'seed: ' + randu.seed ); + + // Define the window size: + W = 10; + + // For each dataset, compute the actual and expected correlation distances... + function isNotNaN( pair ) { + return !isnan( pair[ 0 ] ) && !isnan( pair[ 1 ] ); + } + for ( i = 0; i < N; i++ ) { + d = data[ i ]; + filteredArr = d.filter( isNotNaN ); + if ( filteredArr.length > 0 ) { + means = mean( [ 0.0, 0.0 ], filteredArr ); + } else { + means = null; + } + acc = incrnanmpcorrdist( W, means[ 0 ], means[ 1 ] ); + countNonNanPairs = 0; + for ( j = 0; j < M; j++ ) { + actual = acc( d[ j ][ 0 ], d[ j ][ 1 ] ); + countNonNanPairs += ( !isnan( d[ j ][ 0 ] ) && !isnan( d[ j ][ 1 ] ) ) ? 1 : 0; + + arr = []; + k = j; + while ( arr.length < min( countNonNanPairs, W ) && k >= 0 ) { + if ( !isnan( d[ k ][ 0 ] ) && !isnan( d[ k ][ 1 ] ) ) { + arr.push( d[ k ] ); + } + k -= 1; + } + + if ( means !== null && arr.length > 0 ) { + expected = pcorrdist( arr, means[ 0 ], means[ 1 ], true ); + } else { + expected = null; + } + + if ( actual === expected ) { + t.equal( actual, expected, 'returns expected value. dataset: ' + i + '. window: ' + j + '.' ); + } else { + delta = abs( actual - expected ); + tol = 1.0e6 * EPS * abs( expected ); + t.equal( delta <= tol, true, 'dataset: ' + i + '. window: ' + j + '. expected: ' + expected + '. actual: ' + actual + '. tol: ' + tol + '. delta: ' + delta + '.' ); + } + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current sample correlation distance (unknown means)', function test( t ) { + var expected; + var actual; + var means; + var delta; + var data; + var tol; + var acc; + var W; + var N; + var d; + var i; + + data = [ + [ 2.0, 1.0 ], + [ -5.0, NaN ], + [ 3.0, -1.0 ], + [ NaN, NaN ] + ]; + N = data.length; + + // Window size: + W = 4; + + acc = incrnanmpcorrdist( W ); + for ( i = 0; i < N; i++ ) { + acc( data[ i ][ 0 ], data[ i ][ 1 ] ); + actual = acc(); + if ( i < W - 1 ) { + d = data.slice( 0, i + 1 ); + } else { + d = data.slice( i - W + 1, i + 1 ); + } + d = d.filter( function isNotNaN( pair ) { + return !isnan( pair[ 0 ] ) && !isnan( pair[ 1 ] ); + }); + + if ( d.length > 0 ) { + means = mean( [ 0.0, 0.0 ], d ); + } else { + means = null; + } + + if ( means !== null ) { + expected = pcorrdist( d, means[ 0 ], means[ 1 ], false ); + } + + if ( actual === expected ) { + t.equal( actual, expected, 'returns expected value. window: ' + i + '.' ); + } else { + delta = abs( actual - expected ); + tol = 1.0 * EPS * abs( expected ); + t.equal( delta < tol, true, 'window: ' + i + '. expected: ' + expected + '. actual: ' + actual + '. tol: ' + tol + '. delta: ' + delta + '.' ); + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current sample correlation distance (known means)', function test( t ) { + var filteredArr; + var expected; + var actual; + var means; + var delta; + var data; + var tol; + var acc; + var W; + var N; + var d; + var i; + + data = [ + [ 2.0, 1.0 ], + [ -5.0, NaN ], + [ 3.0, -1.0 ], + [ NaN, NaN ] + ]; + + N = data.length; + function isNotNaN( pair ) { + return !isnan( pair[ 0 ] ) && !isnan( pair[ 1 ] ); + } + filteredArr = data.filter( isNotNaN ); + if ( filteredArr.length > 0 ) { + means = mean( [ 0.0, 0.0 ], filteredArr ); + } else { + means = null; + } + + // Window size: + W = 4; + + acc = incrnanmpcorrdist( W, means[ 0 ], means[ 1 ] ); + for ( i = 0; i < N; i++ ) { + acc( data[ i ][ 0 ], data[ i ][ 1 ] ); + actual = acc(); + if ( i < W - 1 ) { + d = data.slice( 0, i + 1 ); + } else { + d = data.slice( i - W + 1, i + 1 ); + } + + d = d.filter( function isNotNaN( pair ) { + return !isnan( pair[ 0 ] ) && !isnan( pair[ 1 ] ); + }); + if ( means === null ) { + expected = null; + } else { + expected = pcorrdist( d, means[ 0 ], means[ 1 ], true ); + } + + if ( actual === expected ) { + t.equal( actual, expected, 'returns expected value. window: ' + i + '.' ); + } else { + delta = abs( actual - expected ); + tol = 1.0 * EPS * abs( expected ); + t.equal( delta < tol, true, 'window: ' + i + '. expected: ' + expected + '. actual: ' + actual + '. tol: ' + tol + '. delta: ' + delta + '.' ); + } + } + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmpcorrdist( 3 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null` (known means)', function test( t ) { + var acc = incrnanmpcorrdist( 3, 3.0, 3.14 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'if only one datum has been provided and the means are unknown, the accumulator function returns `1`', function test( t ) { + var acc = incrnanmpcorrdist( 3 ); + acc( 2.0, 3.14 ); + t.equal( acc(), 1.0, 'returns 1' ); + t.end(); +}); + +tape( 'if only one datum has been provided and the means are known, the accumulator function may not return `1`', function test( t ) { + var acc = incrnanmpcorrdist( 3, 30.0, -100.0 ); + acc( 2.0, 1.0 ); + t.notEqual( acc(), 1.0, 'does not return 1' ); + t.end(); +}); + +tape( 'if the window size is `1` and the means are unknown, the accumulator function always returns `1`', function test( t ) { + var acc; + var r; + var i; + + acc = incrnanmpcorrdist( 1 ); + for ( i = 0; i < 100; i++ ) { + r = acc( randu() * 100.0, randu() * 100.0 ); + t.equal( r, 1.0, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if the window size is `1` and the means are known, the accumulator function may not always return `1`', function test( t ) { + var acc; + var r; + var i; + + acc = incrnanmpcorrdist( 1, 500.0, -500.0 ); // means are outside the range of simulated values so the correlation should never be zero + for ( i = 0; i < 100; i++ ) { + r = acc( randu() * 100.0, randu() * 100.0 ); + t.notEqual( r, 1.0, 'does not return 1' ); + } + t.end(); +});