diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/README.md b/lib/node_modules/@stdlib/stats/incr/nanvmr/README.md new file mode 100644 index 000000000000..a9792ddbfd4b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/README.md @@ -0,0 +1,182 @@ + + +# incrnanvmr + +> Compute a [variance-to-mean ratio][variance-to-mean-ratio] (VMR) incrementally, ignoring `NaN` values. + +
+ +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 +``` + + + + + +and the [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanvmr = require( '@stdlib/stats/incr/nanvmr' ); +``` + +#### incrnanvmr( \[mean] ) + +Returns an accumulator function which incrementally computes a [variance-to-mean ratio][variance-to-mean-ratio], ignoring `NaN` values. + +```javascript +var accumulator = incrnanvmr(); +``` + +If the mean is already known, provide a `mean` argument. + +```javascript +var accumulator = incrnanvmr( 3.0 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated accumulated value. If not provided an input value `x`, the accumulator function returns the current accumulated value. + +```javascript +var accumulator = incrnanvmr(); + +var D = accumulator( 2.0 ); +// returns 0.0 + +D = accumulator( 1.0 ); // => s^2 = ((2-1.5)^2+(1-1.5)^2) / (2-1) +// returns ~0.33 + +D = accumulator( 3.0 ); // => s^2 = ((2-2)^2+(1-2)^2+(3-2)^2) / (3-1) +// returns 0.5 + +D = accumulator( NaN ); +// returns 0.5 + +D = accumulator(); +// returns 0.5 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. + +- The following table summarizes how to interpret the [variance-to-mean ratio][variance-to-mean-ratio]: + + | VMR | Description | Example Distribution | + | :---------------: | :-------------: | :--------------------------: | + | 0 | not dispersed | constant | + | 0 < VMR < 1 | under-dispersed | binomial | + | 1 | -- | Poisson | + | >1 | over-dispersed | geometric, negative-binomial | + + Accordingly, one can use the [variance-to-mean ratio][variance-to-mean-ratio] to assess whether observed data can be modeled as a Poisson process. When observed data is "under-dispersed", observed data may be more regular than as would be the case for a Poisson process. When observed data is "over-dispersed", observed data may contain clusters (i.e., clumped, concentrated data). + +- The [variance-to-mean ratio][variance-to-mean-ratio] is typically computed on nonnegative values. The measure may lack meaning for data which can assume both positive and negative values. + +- The [variance-to-mean ratio][variance-to-mean-ratio] is also known as the **index of dispersion**, **dispersion index**, **coefficient of dispersion**, and **relative variance**. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var incrnanvmr = require( '@stdlib/stats/incr/nanvmr' ); + +// Initialize an accumulator: +var accumulator = incrnanvmr(); + +// For each simulated datum, update the variance-to-mean ratio... +var v; +var i; +for ( i = 0; i < 100; i++ ) { + accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0, 100.0 ) ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanvmr/benchmark/benchmark.js new file mode 100644 index 000000000000..2611a82ee40f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/benchmark/benchmark.js @@ -0,0 +1,91 @@ +/** +* @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 incrnanvmr = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanvmr(); + 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 = incrnanvmr(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( 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_mean', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanvmr( 3.14 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( 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/nanvmr/docs/img/equation_arithmetic_mean.svg b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/img/equation_arithmetic_mean.svg new file mode 100644 index 000000000000..aea7a5f6687a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/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/nanvmr/docs/img/equation_unbiased_sample_variance.svg b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/img/equation_unbiased_sample_variance.svg new file mode 100644 index 000000000000..1ae1283e7fb1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/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/nanvmr/docs/img/equation_variance_to_mean_ratio.svg b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/img/equation_variance_to_mean_ratio.svg new file mode 100644 index 000000000000..1b6c30bd309a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/img/equation_variance_to_mean_ratio.svg @@ -0,0 +1,28 @@ + +upper D equals StartFraction s squared Over x overbar EndFraction + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt new file mode 100644 index 000000000000..b1127cd35c7a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( [mean] ) + Returns an accumulator function which incrementally computes a variance-to- + mean ratio (VMR), ignoring `NaN` values. + + If provided a value, the accumulator function returns an updated accumulated + value. If not provided a value, the accumulator function returns the current + accumulated value. + + Parameters + ---------- + mean: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var D = accumulator() + null + > D = accumulator( 2.0 ) + 0.0 + > D = accumulator( 1.0 ) + ~0.33 + > D = accumulator( NaN ) + ~0.33 + > D = accumulator() + ~0.33 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/index.d.ts new file mode 100644 index 000000000000..b5506547f0db --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/index.d.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. +*/ + +// TypeScript Version: 4.1 + +/** +* If provided a value, the accumulator function returns an updated accumulated value. If not provided a value, the accumulator function returns the current accumulated value. +* +* @param x - value +* @returns accumulated value or null +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a variance-to-mean ratio (VMR), ignoring `NaN` values. +* +* @param mean - mean value +* @returns accumulator function +* +* @example +* var accumulator = incrnanvmr(); +* +* var D = accumulator(); +* // returns null +* +* D = accumulator( 2.0 ); +* // returns 0.0 +* +* D = accumulator( 1.0 ); +* // returns ~0.33 +* +* D = accumulator( NaN ); +* // returns ~0.33 +* +* D = accumulator(); +* // returns ~0.33 +* +* @example +* var accumulator = incrnanvmr( 3.14 ); +*/ +declare function incrnanvmr( mean?: number ): accumulator; + + +// EXPORTS // + +export = incrnanvmr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/test.ts new file mode 100644 index 000000000000..1146fab7058d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/test.ts @@ -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. +*/ + +import incrnanvmr = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanvmr(); // $ExpectType accumulator + incrnanvmr( 0.0 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided invalid arguments... +{ + incrnanvmr( '5' ); // $ExpectError + incrnanvmr( true ); // $ExpectError + incrnanvmr( false ); // $ExpectError + incrnanvmr( null ); // $ExpectError + incrnanvmr( [] ); // $ExpectError + incrnanvmr( {} ); // $ExpectError + incrnanvmr( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanvmr(); + + acc(); // $ExpectType number | null + acc( 3.14 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanvmr(); + + 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/nanvmr/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js new file mode 100644 index 000000000000..0b9b1f41e40c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js @@ -0,0 +1,38 @@ +/** +* @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 uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var incrnanvmr = require( './../lib' ); + +// Initialize an accumulator: +var accumulator = incrnanvmr(); + +// For each simulated datum, update the variance-to-mean ratio... +console.log( '\nValue\tVMR\n' ); +var D; +var v; +var i; +for ( i = 0; i < 100; i++ ) { + v = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0, 100.0 ); + D = accumulator( v ); + console.log( '%d\t%d', v.toFixed( 4 ), ( D === null ) ? NaN : D.toFixed( 4 ) ); +} +console.log( '\nFinal VMR: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/index.js new file mode 100644 index 000000000000..0bd3708b0d8a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute a variance-to-mean ratio (VMR) incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanvmr +* +* @example +* var incrnanvmr = require( '@stdlib/stats/incr/nanvmr' ); +* +* var accumulator = incrnanvmr(); +* +* var D = accumulator(); +* // returns null +* +* D = accumulator( 2.0 ); +* // returns 0.0 +* +* D = accumulator( 1.0 ); +* // returns ~0.33 +* +* D = accumulator( NaN ); +* // returns ~0.33 +* +* D = accumulator(); +* // returns ~0.33 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/main.js new file mode 100644 index 000000000000..bd13ca12db8d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/main.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrvmr = require( '@stdlib/stats/incr/vmr' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a variance-to-mean ratio (VMR), ignoring `NaN` values. +* +* @param {number} [mean] - mean value +* @throws {TypeError} must provide a number primitive +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanvmr(); +* +* var D = accumulator(); +* // returns null +* +* D = accumulator( 2.0 ); +* // returns 0.0 +* +* D = accumulator( 1.0 ); +* // returns ~0.33 +* +* D = accumulator( NaN ); +* // returns ~0.33 +* +* D = accumulator(); +* // returns ~0.33 +* +* @example +* var accumulator = incrnanvmr( 3.14 ); +*/ +function incrnanvmr( mean ) { + var vmr = ( arguments.length === 0 ) ? incrvmr() : incrvmr( mean ); + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated accumulated value. If not provided a value, the accumulator function returns the current accumulated value. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} accumulated value or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return vmr(); + } + return vmr( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanvmr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/package.json b/lib/node_modules/@stdlib/stats/incr/nanvmr/package.json new file mode 100644 index 000000000000..7134442da432 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/stats/incr/nanvmr", + "version": "0.0.0", + "description": "Compute a variance-to-mean ratio (VMR) incrementally, 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", + "variance", + "sample variance", + "unbiased", + "var", + "dispersion", + "index of dispersion", + "fano factor", + "dispersion index", + "relative variance", + "vmr", + "mean", + "ratio", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js new file mode 100644 index 000000000000..b60a4b2e3b59 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js @@ -0,0 +1,216 @@ +/** +* @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 incrnanvmr = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanvmr, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanvmr(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known mean)', function test( t ) { + t.equal( typeof incrnanvmr( 3.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a non-numeric 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() { + incrnanvmr( value ); + }; + } +}); + +tape( 'the accumulator function incrementally computes a variance-to-mean ratio', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 2.0, NaN, 4.0, 3.0, NaN, 4.0 ]; + + // Test against Julia: + expected = [ + 0.0/(2.0/1.0), + 0.5/(5.0/2.0), + 0.33333333333333337/(7.0/3.0), + 0.33333333333333337/(7.0/3.0), + 0.9166666666666666/(11.0/4.0), + 0.7/(14.0/5.0), + 0.7/(14.0/5.0), + 0.8/(18.0/6.0) + ]; + + acc = incrnanvmr(); + + actual = []; + for ( i = 0; i < data.length; i++ ) { + actual.push( acc( data[ i ] ) ); + } + t.deepEqual( actual, expected, 'returns expected results' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a variance-to-mean ratio (known mean)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 2.0, NaN, 4.0, 3.0, NaN, 4.0 ]; + + // Test against Julia: + expected = [ + 1.0/3.0, + 0.5/3.0, + 0.6666666666666666/3.0, + 0.6666666666666666/3.0, + 0.75/3.0, + 0.6/3.0, + 0.6/3.0, + 0.6666666666666666/3.0 + ]; + + acc = incrnanvmr( 3.0 ); + + actual = []; + for ( i = 0; i < data.length; i++ ) { + actual.push( acc( data[ i ] ) ); + } + t.deepEqual( actual, expected, 'returns expected results' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current variance-to-mean ratio', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 1.0 ]; + acc = incrnanvmr(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 1.0/2.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current variance-to-mean ratio (known mean)', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 1.0 ]; + acc = incrnanvmr( 2.0 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 0.6666666666666666/2.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulated value is `null` until at least 1 datum has been provided (unknown mean)', function test( t ) { + var acc; + var D; + + acc = incrnanvmr(); + + D = acc(); + t.equal( D, null, 'returns null' ); + + D = acc( 3.0 ); + t.notEqual( D, null, 'does not return null' ); + + D = acc(); + t.notEqual( D, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the accumulated value is `null` until at least 1 datum has been provided (known mean)', function test( t ) { + var acc; + var D; + + acc = incrnanvmr( 3.0 ); + + D = acc(); + t.equal( D, null, 'returns null' ); + + D = acc( 3.0 ); + t.notEqual( D, null, 'does not return null' ); + + D = acc(); + t.notEqual( D, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the accumulated value is `0` until at least 2 datums have been provided (unknown mean)', function test( t ) { + var acc; + var D; + + acc = incrnanvmr(); + + D = acc( 2.0 ); + t.equal( D, 0.0, 'returns 0' ); + + D = acc(); + t.equal( D, 0.0, 'returns 0' ); + + D = acc( 3.0 ); + t.notEqual( D, 0.0, 'does not return 0' ); + + D = acc(); + t.notEqual( D, 0.0, 'does not return 0' ); + + t.end(); +});