diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/README.md b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/README.md new file mode 100644 index 000000000000..89a6fa89833d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/README.md @@ -0,0 +1,231 @@ + + +# incrnanmeanvar + +> Compute an [arithmetic mean][arithmetic-mean] and an [unbiased sample variance][sample-variance] incrementally, ignoring `NaN` value. + +
+ +The [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + + + +and the [unbiased sample variance][sample-variance] is defined as + + + +```math +s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2 +``` + + + + + +
+ +## Usage + +```javascript +var incrnanmeanvar = require( '@stdlib/stats/incr/nanmeanvar' ); +``` + +#### incrnanmeanvar( \[out] ) + +Returns an accumulator `function` which incrementally computes an [arithmetic mean][arithmetic-mean] and [unbiased sample variance][sample-variance], ignoring `NaN` value. + +```javascript +var accumulator = incrnanmeanvar(); +``` + +By default, the returned accumulator `function` returns the accumulated values as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var accumulator = incrnanmeanvar( new Float64Array( 2 ) ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns updated accumulated values. If not provided an input value `x`, the accumulator function returns the current accumulated values. + +```javascript +var accumulator = incrnanmeanvar(); + +var mv = accumulator(); +// returns null + +mv = accumulator( 2.0 ); +// returns [ 2.0, 0.0 ] + +mv = accumulator( 1.0 ); +// returns [ 1.5, 0.5 ] + +mv = accumulator( 3.0 ); +// returns [ 2.0, 1.0 ] + +mv = accumulator( NaN ); +// returns [ 2.0, 1.0 ] + +mv = accumulator( -7.0 ); +// returns [ -0.25, ~20.92 ] + +mv = accumulator( -5.0 ); +// returns [ -1.2, 20.2 ] + +mv = accumulator(); +// returns [ -1.2, 20.2 ] +``` + +
+ + + +
+ +## Notes + +- Input values are type checked; if a NaN value is provided, the accumulator skips the update and continues to return the previous state. If non-numeric inputs are possible, you are advised to type check and handle them accordingly **before** passing the value to the accumulator function. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var incrnanmeanvar = require( '@stdlib/stats/incr/nanmeanvar' ); + +var offset; +var acc; +var buf; +var out; +var mv; +var N; +var v; +var i; +var j; + +// Define the number of accumulators: +N = 5; + +// Create an array buffer for storing accumulator output: +buf = new ArrayBuffer( N*2*8 ); // 8 bytes per element + +// Initialize accumulators: +acc = []; +for ( i = 0; i < N; i++ ) { + // Compute the byte offset: + offset = i * 2 * 8; // stride=2, bytes_per_element=8 + + // Create a new view for storing accumulated values: + out = new Float64Array( buf, offset, 2 ); + + // Initialize an accumulator which will write results to the view: + acc.push( incrnanmeanvar( out ) ); +} + +// Simulate data and update the sample means and variances... +for ( i = 0; i < 100; i++ ) { + for ( j = 0; j < N; j++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0 * (j+1); + } + acc[ j ]( v ); + } +} + +// Print the final results: +console.log( 'Mean\tVariance' ); +for ( i = 0; i < N; i++ ) { + mv = acc[ i ](); + console.log( '%d\t%d', mv[ 0 ].toFixed( 3 ), mv[ 1 ].toFixed( 3 ) ); +} +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/benchmark/benchmark.js new file mode 100644 index 000000000000..e3209e9f31d4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var incrnanmeanvar = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmeanvar(); + 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 = incrnanmeanvar(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu() ); + if ( v.length !== 2 ) { + b.fail( 'should contain two elements' ); + } + } + b.toc(); + if ( v[ 0 ] !== v[ 0 ] || v[ 1 ] !== v[ 1 ] ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/img/equation_arithmetic_mean.svg b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/img/equation_arithmetic_mean.svg new file mode 100644 index 000000000000..aea7a5f6687a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/img/equation_arithmetic_mean.svg @@ -0,0 +1,43 @@ + +x overbar equals StartFraction 1 Over n EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts x Subscript i + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/img/equation_unbiased_sample_variance.svg b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/img/equation_unbiased_sample_variance.svg new file mode 100644 index 000000000000..1ae1283e7fb1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/img/equation_unbiased_sample_variance.svg @@ -0,0 +1,61 @@ + +s squared equals StartFraction 1 Over n minus 1 EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis squared + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/repl.txt new file mode 100644 index 000000000000..3b276741410b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( [out] ) + Returns an accumulator function which incrementally computes an arithmetic + mean and unbiased sample variance, ignoring `NaN` value. + + If provided a value, the accumulator function returns updated accumulated + values. If not provided a value, the accumulator function returns the + current accumulated values. + + If provided `NaN`, the update is skipped and the accumulator continues to + return the previous accumulated values. + + + Parameters + ---------- + out: Array|TypedArray (optional) + Output array. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var mv = accumulator() + null + > mv = accumulator( 2.0 ); + > mv = accumulator( -5.0 ); + > mv = accumulator( 3.0 ); + > mv = accumulator( NaN ); + > mv = accumulator( 5.0 ); + > mv = accumulator() + [ 1.25, ~18.92 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/types/index.d.ts new file mode 100644 index 000000000000..23594f6bcdac --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/types/index.d.ts @@ -0,0 +1,72 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ArrayLike } from '@stdlib/types/array'; + +/** +* If provided a value, the accumulator function returns updated results. If not provided a value, the accumulator function returns the current results. +* +* ## Notes +* +* - If provided `NaN`, the arithmetic mean and unbiased sample variance values are equal to `NaN` for all future invocations. +* +* @param x - input value +* @returns output array or null +*/ +type accumulator = ( x?: number ) => ArrayLike | null; + +/** +* Returns an accumulator function which incrementally computes an arithmetic mean and unbiased sample variance, ignoring `NaN` value. +* +* @param out - output array +* @returns accumulator function +* +* @example +* var accumulator = incrnanmeanvar(); +* +* var mv = accumulator(); +* // returns null +* +* mv = accumulator( 2.0 ); +* // returns [ 2.0, 0.0 ] +* +* mv = accumulator( -5.0 ); +* // returns [ -1.5, 24.5 ] +* +* mv = accumulator( 3.0 ); +* // returns [ 0.0, 19.0 ] +* +* mv = accumulator( NaN ); +* // returns [ 0.0, 19.0 ] +* +* mv = accumulator( 5.0 ); +* // returns [ 1.25, ~18.92 ] +* +* mv = accumulator(); +* // returns [ 1.25, ~18.92 ] +*/ +declare function incrnanmeanvar( out?: ArrayLike ): accumulator; + + +// EXPORTS // + +export = incrnanmeanvar; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/types/test.ts new file mode 100644 index 000000000000..37ec2881cb7f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrnanmeanvar = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmeanvar(); // $ExpectType accumulator + const out = [ 0.0, 0.0 ]; + incrnanmeanvar( out ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided an argument that is not an array-like object of numbers... +{ + incrnanmeanvar( '5' ); // $ExpectError + incrnanmeanvar( 5 ); // $ExpectError + incrnanmeanvar( true ); // $ExpectError + incrnanmeanvar( false ); // $ExpectError + incrnanmeanvar( null ); // $ExpectError + incrnanmeanvar( {} ); // $ExpectError + incrnanmeanvar( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmeanvar(); + + acc(); // $ExpectType ArrayLike | null + acc( 3.14 ); // $ExpectType ArrayLike | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanmeanvar(); + + acc( '5' ); // $ExpectError + acc( true ); // $ExpectError + acc( false ); // $ExpectError + acc( null ); // $ExpectError + acc( [] ); // $ExpectError + acc( {} ); // $ExpectError + acc( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/examples/index.js new file mode 100644 index 000000000000..8420b7ec11c2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/examples/index.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var incrnanmeanvar = require( './../lib' ); + +var offset; +var acc; +var buf; +var out; +var mv; +var N; +var v; +var i; +var j; + +// Define the number of accumulators: +N = 5; + +// Create an array buffer for storing accumulator output: +buf = new ArrayBuffer( N*2*8 ); // 8 bytes per element + +// Initialize accumulators: +acc = []; +for ( i = 0; i < N; i++ ) { + // Compute the byte offset: + offset = i * 2 * 8; // stride=2, bytes_per_element=8 + + // Create a new view for storing accumulated values: + out = new Float64Array( buf, offset, 2 ); + + // Initialize an accumulator which will write results to the view: + acc.push( incrnanmeanvar( out ) ); +} + +// Simulate data and update the sample means and variances... +for ( i = 0; i < 100; i++ ) { + for ( j = 0; j < N; j++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0 * (j+1); + } + acc[ j ]( v ); + } +} + +// Print the final results: +console.log( 'Mean\tVariance' ); +for ( i = 0; i < N; i++ ) { + mv = acc[ i ](); + console.log( '%d\t%d', mv[ 0 ].toFixed( 3 ), mv[ 1 ].toFixed( 3 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/lib/index.js new file mode 100644 index 000000000000..49e0f7f4d96d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/lib/index.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute an arithmetic mean and unbiased sample variance incrementally. +* +* @module @stdlib/stats/incr/nanmeanvar +* +* @example +* var incrnanmeanvar = require( '@stdlib/stats/incr/nanmeanvar' ); +* +* var accumulator = incrnanmeanvar(); +* +* var mv = accumulator(); +* // returns null +* +* mv = accumulator( 2.0 ); +* // returns [ 2.0, 0.0 ] +* +* mv = accumulator( -5.0 ); +* // returns [ -1.5, 24.5 ] +* +* mv = accumulator( 3.0 ); +* // returns [ 0.0, 19.0 ] +* +* mv = accumulator( NaN ); +* // returns [ 0.0, 19.0 ] +* +* mv = accumulator( 5.0 ); +* // returns [ 1.25, ~18.92 ] +* +* mv = accumulator(); +* // returns [ 1.25, ~18.92 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/lib/main.js new file mode 100644 index 000000000000..eea3417d971e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/lib/main.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var incrmeanvar = require( '@stdlib/stats/incr/meanvar' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes an arithmetic mean and unbiased sample variance, ignoring `NaN` value. +* +* ## References +* +* - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022). +* - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961). +* +* @param {Collection} [out] - output array +* @throws {TypeError} output argument must be array-like +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmeanvar(); +* +* var mv = accumulator(); +* // returns null +* +* mv = accumulator( 2.0 ); +* // returns [ 2.0, 0.0 ] +* +* mv = accumulator( -5.0 ); +* // returns [ -1.5, 24.5 ] +* +* mv = accumulator( 3.0 ); +* // returns [ 0.0, 19.0 ] +* +* mv = accumulator( NaN ); +* // returns [ 0.0, 19.0 ] +* +* mv = accumulator( 5.0 ); +* // returns [ 1.25, ~18.92 ] +* +* mv = accumulator(); +* // returns [ 1.25, ~18.92 ] +*/ +function incrnanmeanvar( out ) { + var meanvar; + if ( arguments.length > 0 ) { + meanvar = incrmeanvar( out ); + } else { + meanvar = incrmeanvar(); + } + return accumulator; + + /** + * If provided a value, the accumulator function returns updated results. If not provided a value, the accumulator function returns the current results. + * + * @private + * @param {number} [x] - input value + * @returns {(ArrayLikeObject|null)} output array or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) || !isNumber( x ) ) { + return meanvar(); + } + return meanvar( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanmeanvar; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/package.json b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/package.json new file mode 100644 index 000000000000..6c9df94e61e7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/stats/incr/nanmeanvar", + "version": "0.0.0", + "description": "Compute an arithmetic mean and unbiased sample variance incrementally, ignoring `NaN` value.", + "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", + "average", + "avg", + "mean", + "arithmetic mean", + "variance", + "sample variance", + "unbiased", + "var", + "dispersion", + "standard deviation", + "stdev", + "central tendency", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanvar/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/test/test.js new file mode 100644 index 000000000000..aeb4f0065d3f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanvar/test/test.js @@ -0,0 +1,203 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var incrnanmeanvar = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmeanvar, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided an array-like object for an output argument', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + true, + false, + 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() { + incrnanmeanvar( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmeanvar(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (output)', function test( t ) { + t.equal( typeof incrnanmeanvar( [ 0.0, 0.0 ] ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes an arithmetic mean and unbiased sample variance', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, NaN, 3.0, 2.0, NaN, 4.0, 3.0, 4.0 ]; + N = data.length; + + // Test against Julia: + expected = [ + [ 2.0, 0.0 ], + [ 2.0, 0.0 ], + [ 2.5, 0.5 ], + [ 7.0/3.0, 0.33333333333333337 ], + [ 7.0/3.0, 0.33333333333333337 ], + [ 11.0/4.0, 0.9166666666666666 ], + [ 14.0/5.0, 0.7 ], + [ 18.0/6.0, 0.8 ] + ]; + + acc = incrnanmeanvar(); + + for ( i = 0; i < N; i++ ) { + actual = acc( data[ i ] ); + t.deepEqual( actual, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the accumulator function incrementally computes an arithmetic mean and unbiased sample variance (output)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var out; + var N; + var i; + + data = [ 2.0, NaN, 3.0, 2.0, NaN, 4.0, 3.0, 4.0 ]; + N = data.length; + + // Test against Julia: + expected = [ + [ 2.0, 0.0 ], + [ 2.0, 0.0 ], + [ 2.5, 0.5 ], + [ 7.0/3.0, 0.33333333333333337 ], + [ 7.0/3.0, 0.33333333333333337 ], + [ 11.0/4.0, 0.9166666666666666 ], + [ 14.0/5.0, 0.7 ], + [ 18.0/6.0, 0.8 ] + ]; + out = [ 0.0, 0.0 ]; + acc = incrnanmeanvar( out ); + + for ( i = 0; i < N; i++ ) { + actual = acc( data[ i ] ); + t.equal( actual, out, 'returns output array' ); + t.deepEqual( actual, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current mean and unbiased sample variance', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, NaN, 1.0 ]; + acc = incrnanmeanvar(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.deepEqual( acc(), [ 2.0, 1.0 ], 'returns expected value' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmeanvar(); + t.equal( acc(), null, 'returns null' ); + t.equal( acc(), null, 'returns null' ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'the sample variance is `0` until at least 2 datums have been provided', function test( t ) { + var acc; + var mv; + + acc = incrnanmeanvar(); + + mv = acc(); + t.equal( mv, null, 'returns null' ); + + mv = acc( 3.0 ); + t.equal( mv[ 1 ], 0.0, 'returns expected value' ); + + mv = acc(); + t.equal( mv[ 1 ], 0.0, 'returns expected value' ); + + mv = acc( 5.0 ); + t.notEqual( mv[ 1 ], 0.0, 'does not return 0' ); + + mv = acc(); + t.notEqual( mv[ 1 ], 0.0, 'does not return 0' ); + + t.end(); +}); + +tape( 'if the first value is NaN, the accumulator returns its initial state', function test( t ) { + var state; + var acc; + + acc = incrnanmeanvar(); + + // First update is NaN, so no valid update occurs: + state = acc( NaN ); + t.equal( state, null, 'returns null for initial state when first input is NaN' ); + + // Subsequent call without an update should still return null: + state = acc(); + t.equal( state, null, 'remains at initial state (null) when no valid update is provided' ); + + // Now provide a valid update to see the state change: + state = acc( 2.0 ); + t.deepEqual( state, [ 2.0, 0.0 ], 'updates state correctly after a valid input following an initial NaN' ); + t.end(); +});