diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/README.md b/lib/node_modules/@stdlib/stats/incr/nancovariance/README.md new file mode 100644 index 000000000000..b50811877bdf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/README.md @@ -0,0 +1,121 @@ + + +# incrnancovariance + +> Compute an incremental sample covariance while ignoring `NaN` values. + +
+ +## Usage + +```javascript +var nancovariance = require( '@stdlib/stats/incr/nancovariance' ); +``` + +#### incrnancovariance + +Returns an accumulator function which incrementally computes a sample covariance, ignoring any input pairs that contain NaN values. The accumulator returns null until at least two valid data pairs have been provided. + +```javascript +var accumulator = nancovariance(); +``` + +#### accumulator(`[x, y]`) + +If provided a pair of numeric input values `[x, y]`, the accumulator updates and returns the sample covariance. If not provided any input values, the accumulator returns the current sample covariance. + +```javascript +var accumulator = nancovariance(); + +var cov = accumulator(); +// returns null + +cov = accumulator( 2.0, 1.0 ); +// returns null + +cov = accumulator( NaN, 1.0 ); +// returns null + +cov = accumulator( 4.0, 5.0 ); +// returns 4.0 + +cov = accumulator(); +// returns 4.0 +``` + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var nancovariance = require( '@stdlib/stats/incr/nancovariance' ); + +var accumulator; +var i; +var x; +var y; +accumulator = nancovariance(); +for ( i = 0; i < 100; i++ ) { + x = ( randu() < 0.2 ) ? NaN : randu() * 100.0; + y = ( randu() < 0.2 ) ? NaN : randu() * 100.0; + accumulator( x, y ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nancovariance/benchmark/benchmark.js new file mode 100644 index 000000000000..7f43432ae8a6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/benchmark/benchmark.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 nancovariance = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var acc; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + acc = nancovariance(); + if ( typeof acc !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof acc !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator', function benchmark( b ) { + var acc; + var x; + var y; + var v; + var i; + + acc = nancovariance(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu() < 0.1 ) ? NaN : randu()*10; // ~10% NaN + y = ( randu() < 0.1 ) ? NaN : randu()*10; // ~10% NaN + v = acc( x, y ); + if ( v !== v && i > 1 ) { + b.fail( 'accumulator should not return NaN once enough valid data' ); + } + } + 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/nancovariance/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/repl.txt new file mode 100644 index 000000000000..d696647ca7ae --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/repl.txt @@ -0,0 +1,28 @@ +{{alias}}() + Returns an accumulator function which incrementally updates a count, + ignoring `NaN` values. + + If provided a value, the accumulator function returns an updated count. If + not provided a value, the accumulator function returns the current count. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var v = accumulator() + null + > v = accumulator( 2.0 ) + null + > v = accumulator( NaN ) + null + > v = accumulator( -5.0 ) + NaN + > v = accumulator() + NaN + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/index.d.ts new file mode 100644 index 000000000000..7a8e9967ae69 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/index.d.ts @@ -0,0 +1,68 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 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 a pair of values, updates and returns the sample covariance; + * otherwise, returns the current sample covariance. + * + * @param x - first numeric value + * @param y - second numeric value + * @returns sample covariance or null + */ +interface Accumulator { + ( x: number, y: number ): number | null; + (): number | null; +} + + +/** +* Returns an accumulator function which incrementally computes a count, ignoring `NaN` values. +* +* @returns accumulator function +* +* @example +* var nancovariance = require( '@stdlib/stats/incr/nancovariance' ); +* +* var acc = nancovariance(); +* +* var cov = acc(); +* // returns null +* +* cov = acc( 2.0, 1.0 ); +* // returns null +* +* cov = acc( NaN, 5.0 ); +* // returns null +* +* cov = acc( 4.0, 5.0 ); +* // returns 4.0 +* +* cov = acc(); +* // returns 4.0 +*/ + +declare function nancovariance( ...args: [] ): Accumulator; + + +// EXPORTS // + +export = nancovariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/test.ts new file mode 100644 index 000000000000..0bf914168755 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/docs/types/test.ts @@ -0,0 +1,50 @@ +/* +* @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 nancovariance = require( '@stdlib/stats/incr/nancovariance' ); + +// TESTS // + +// The function returns an accumulator function... +{ + nancovariance(); // $ExpectType Accumulator +} + +// The compiler throws an error if the function is provided any arguments... +{ + nancovariance( 3 ); // $ExpectError + nancovariance( 3, 0.0 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = nancovariance(); + acc(); // $ExpectType number | null + acc( 2.0, 1.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = nancovariance(); + acc( '5', 2.0 ); // $ExpectError + acc( true, 2.0 ); // $ExpectError + acc( 2.0 ); // $ExpectError + acc( null, 2.0 ); // $ExpectError + acc( [], {} ); // $ExpectError + acc( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nancovariance/examples/index.js new file mode 100644 index 000000000000..3307796fcc3d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 nancovariance = require( './../lib' ); + +var acc = nancovariance(); + +var dataX = [ 2.0, 2.0, NaN, 3.0, 5.0 ]; +var dataY = [ 1.0, 5.0, 4.0, NaN, 5.0 ]; + +var i; +var cov; +for ( i = 0; i < dataX.length; i++ ) { + cov = acc( dataX[ i ], dataY[ i ] ); + console.log( 'x=%s, y=%s, cov=%s', dataX[ i ], dataY[ i ], ( cov === null ) ? 'null' : cov.toString() ); +} + +console.log( '\nFinal sample covariance:', acc() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/index.js new file mode 100644 index 000000000000..da2a461fcc5e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 an incremental sample covariance while ignoring NaN values. +* +* @module @stdlib/stats/incr/nancovariance +* +* @example +* var nancovariance = require( '@stdlib/stats/incr/nancovariance' ); +* +* var acc = nancovariance(); +* +* var cov = acc(); +* // returns null +* +* cov = acc( 2.0, 1.0 ); +* // returns null (covariance is undefined for a single pair) +* +* cov = acc( NaN , 5.0 ); +* // returns null (An input with a NaN is ignored) +* +* cov = acc( 4.0, 5.0 ); +* // returns 4.0 +* +* cov = acc(); +* // returns 4.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/main.js new file mode 100644 index 000000000000..4ea0de981100 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/lib/main.js @@ -0,0 +1,85 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 covariance = require( '@stdlib/stats/incr/covariance' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a sample covariance. +* +* @returns {Function} accumulator function +* +* @example +* var nancovariance = require( '@stdlib/stats/incr/nancovariance' ); +* +* var acc = nancovariance(); +* +* var cov = acc(); +* // returns null +* +* cov = acc( 2.0, 1.0 ); +* // returns null +* +* cov = acc( NaN, 5.0 ); +* // returns null +* +* cov = acc( 4.0, 5.0 ); +* // returns 4.0 +* +* cov = acc(); +* // returns 4.0 +*/ +function nancovariance() { + var count = 0; + var acc = covariance(); + return accumulator; + + /** + * Updates and returns the sample covariance when provided two numeric arguments. + * If no arguments are provided, returns the current sample covariance. + * + * @private + * @param {number} [x] - new x value + * @param {number} [y] - new y value + * @returns {(number|null)} sample covariance or null + */ + function accumulator( x, y ) { + var val; + if ( arguments.length === 0 ) { + return ( count < 2 ) ? null : acc(); + } + if ( isnan( x ) || isnan( y ) ) { + return ( count < 2 ) ? null : acc(); + } + count += 1; + val = acc( x, y ); + return ( count < 2 ) ? null : val; + } +} + + +// EXPORTS // + +module.exports = nancovariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/package.json b/lib/node_modules/@stdlib/stats/incr/nancovariance/package.json new file mode 100644 index 000000000000..b80e2ab2744e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/package.json @@ -0,0 +1,60 @@ +{ + "name": "@stdlib/stats/incr/nancovariance", + "version": "0.0.0", + "description": "Compute an incremental sample covariance 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", + "statistics", + "covariance", + "incremental", + "accumulator", + "nan", + "sample covariance" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nancovariance/test/test.js b/lib/node_modules/@stdlib/stats/incr/nancovariance/test/test.js new file mode 100644 index 000000000000..43e9fc9afae9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nancovariance/test/test.js @@ -0,0 +1,127 @@ +/** +* @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 EPS = require( '@stdlib/constants/float64/eps' ); +var nancovariance = require( './../lib' ); + + +// MAIN // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof nancovariance, 'function', 'main export is a function' ); + t.end(); +}); + +// Additional tests... + +tape( 'the function returns an accumulator function', function test( t ) { + var acc = nancovariance(); + t.strictEqual( typeof acc, 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function returns null if no valid pairs have been provided', function test( t ) { + var acc = nancovariance(); + t.equal( acc(), null, 'returns null initially' ); + + // Provide an invalid pair: + acc( NaN, 5.0 ); + t.equal( acc(), null, 'still null after invalid pair' ); + t.end(); +}); + +tape( 'the accumulator function returns null if only one valid pair has been provided', function test( t ) { + var acc = nancovariance(); + + // Add the first valid pair: + acc( 2.0, 1.0 ); + t.equal( acc(), null, 'returns null for single valid pair' ); + t.end(); +}); + +tape( 'the accumulator function computes sample covariance once two valid pairs have been provided', function test( t ) { + var acc = nancovariance(); + var cov; + + // Pair #1: + cov = acc( 2.0, 1.0 ); + t.equal( cov, null, 'still null with only one valid pair' ); + + // Pair #2: + cov = acc( 4.0, 5.0 ); + t.equal( cov, 4.0, 'returns sample covariance after second valid pair' ); + + // Current covariance with no arguments: + t.equal( acc(), 4.0, 'returns current covariance' ); + t.end(); +}); + +tape( 'the accumulator function skips pairs containing NaN', function test( t ) { + var acc = nancovariance(); + var cov; + + // Provide first valid pair: + cov = acc( 2.0, 1.0 ); + t.equal( cov, null, 'null with only one valid pair' ); + + // Invalid pair => skip: + cov = acc( NaN, 10.0 ); + t.equal( cov, null, 'still null, as second pair is invalid' ); + + // Provide second valid pair: + cov = acc( 2.0, 5.0 ); + // For (2,1) and (2,5): same X => covariance=0 + t.equal( cov, 0.0, 'returns 0.0 for these pairs' ); + + // Provide third valid pair: + cov = acc( 5.0, 9.0 ); + t.equal( typeof cov, 'number', 'returns a number after third valid pair' ); + t.end(); +}); + +tape( 'the accumulator function computes correct covariance for multiple valid pairs interspersed with invalid pairs', function test( t ) { + var expected = 14.0/3.0; + var delta; + var data = [ + [ 2.0, 1.0 ], + [ NaN, 4.0 ], + [ 4.0, 5.0 ], + [ 3.0, 7.0 ], + [ NaN, NaN ], + [ 5.0, 11.0 ] + ]; + var acc = nancovariance(); + var cov; + var tol; + var i; + for ( i = 0; i < data.length; i++ ) { + cov = acc( data[i][0], data[i][1] ); + } + delta = abs( cov - expected ); + tol = EPS * abs( expected ); + t.equal( typeof cov, 'number', 'returns a number' ); + t.ok( delta < tol, 'within tolerance. Actual: '+cov+'. Expected: '+expected+'. delta: '+delta+'. tol: '+tol+'.' ); + t.end(); +});