From a3fa94b561f12643a9d9a3d7684a23ba67befc31 Mon Sep 17 00:00:00 2001 From: Ayushi Jain Date: Sat, 17 Jan 2026 13:57:39 +0000 Subject: [PATCH 1/4] feat: add NaN-safe vmr accumulator --- .../@stdlib/stats/incr/nanvmr/README.md | 221 +++++++++++++++++ .../stats/incr/nanvmr/benchmark/benchmark.js | 91 +++++++ .../docs/img/equation_arithmetic_mean.svg | 43 ++++ .../img/equation_unbiased_sample_variance.svg | 61 +++++ .../img/equation_variance_to_mean_ratio.svg | 28 +++ .../@stdlib/stats/incr/nanvmr/docs/repl.txt | 37 +++ .../stats/incr/nanvmr/docs/types/index.d.ts | 63 +++++ .../stats/incr/nanvmr/docs/types/test.ts | 60 +++++ .../stats/incr/nanvmr/examples/index.js | 47 ++++ .../@stdlib/stats/incr/nanvmr/lib/index.js | 51 ++++ .../@stdlib/stats/incr/nanvmr/lib/main.js | 82 +++++++ .../@stdlib/stats/incr/nanvmr/package.json | 73 ++++++ .../@stdlib/stats/incr/nanvmr/test/test.js | 227 ++++++++++++++++++ 13 files changed, 1084 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/docs/img/equation_arithmetic_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/docs/img/equation_unbiased_sample_variance.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/docs/img/equation_variance_to_mean_ratio.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js 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..f61dd57842bd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/README.md @@ -0,0 +1,221 @@ + + +# incrvmr + +> Compute a [variance-to-mean ratio][variance-to-mean-ratio] (VMR) incrementally. + +
+ +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 +``` + + + + + +The [variance-to-mean ratio][variance-to-mean-ratio] (VMR) is thus defined as + + + +```math +D = \frac{s^2}{\bar{x}} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanvmr = require( '@stdlib/stats/incr/nanvmr' ); +``` + +#### incrvmr( \[mean] ) + +Returns an accumulator `function` which incrementally computes a [variance-to-mean ratio][variance-to-mean-ratio]. + +```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( NaN ); +// 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(); +// 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 randu = require( '@stdlib/random/base/randu' ); +var incrnanvmr = require( '@stdlib/stats/incr/nanvmr' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanvmr(); + +// For each simulated datum, update the variance-to-mean ratio... +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { // 20% chance of NaN + v = NaN; + } else { + v = randu() * 100.0; + } + accumulator( v ); // ignores NaN values +} +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..37c55eb8247d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/benchmark/benchmark.js @@ -0,0 +1,91 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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..b3eb33b691b0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt @@ -0,0 +1,37 @@ + +{{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( NaN ) + 0.0 + > D = accumulator( 1.0 ) + ~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..3f8ab0925620 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/index.d.ts @@ -0,0 +1,63 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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( NaN ); +* // returns 0.0 +* +* D = accumulator( 1.0 ); +* // 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..3a28e20de77b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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..ca1dde2540bb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 incrnanvmr = require( './../lib' ); + +var accumulator; +var D; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanvmr(); + +// For each simulated datum, update the variance-to-mean ratio... +console.log( '\nValue\tVMR\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 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..1b08df884619 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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(); +* // 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..ec3dc0d73cfb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/main.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 = incrvmr(); +* +* var D = accumulator(); +* // returns null +* +* D = accumulator( 2.0 ); +* // returns 0.0 +* +* D = accumulator( 1.0 ); +* // returns ~0.33 +* +* D = accumulator(); +* // returns ~0.33 +* +* @example +* var accumulator = incrvmr( 3.14 ); +*/ +function incrnanvmr( mean ) { + var acc; + + if ( arguments.length ) { + acc = incrvmr( mean ); + } else { + acc = incrvmr(); + } + 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 acc(); + } + return acc( 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..91fe5369dd89 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var zeros = require( '@stdlib/array/base/zeros' ); +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.strictEqual( typeof incrnanvmr(), 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known mean)', function test( t ) { + t.strictEqual( typeof incrnanvmr( 3.0 ), 'function', 'returns expected value' ); + 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, NaN, 2.0, 4.0, NaN, 3.0, 4.0 ]; + + acc = incrnanvmr(); + expected = []; + actual = zeros( data.length ); + + for ( i = 0; i < data.length; i++ ) { + actual[i] = acc( data[i] ); + + // compute expected ignoring NaNs: + var valid = data.slice(0, i+1).filter( function(d){ return !isnan(d); }); + if ( valid.length === 0 ) { + expected[i] = null; + } else if ( valid.length === 1 ) { + expected[i] = 0.0 / ( valid[0] ); // single datum: VMR=0 + } else { + var mean = valid.reduce((a,b)=>a+b,0)/valid.length; + var M2 = valid.reduce( (s,x) => s + (x-mean)*(x-mean), 0); + expected[i] = ( M2 / (valid.length-1) ) / mean; + } + } + 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, NaN, 3.0, 2.0, NaN, 4.0, 3.0 ]; + + acc = incrnanvmr( 3.0 ); + expected = []; + actual = zeros( data.length ); + + for ( i = 0; i < data.length; i++ ) { + actual[i] = acc( data[i] ); + + // compute expected ignoring NaNs: + var valid = data.slice(0,i+1).filter( function(d){ return !isnan(d); }); + if ( valid.length === 0 ) { + expected[i] = null; + } else { + var M2 = valid.reduce( (s,x) => s + (x-3.0)*(x-3.0), 0 ); + expected[i] = ( M2 / valid.length ) / 3.0; + } + } + t.deepEqual( actual, expected, 'returns expected value' ); + 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, NaN, 3.0, 1.0 ]; + acc = incrnanvmr(); + for ( i = 0; i < data.length; i++ ) { + acc( data[i] ); + } + + var expectedValid = data.filter(d => !isnan(d)); + var mean = expectedValid.reduce((a,b)=>a+b,0)/expectedValid.length; + var M2 = expectedValid.reduce((s,x)=>s+(x-mean)*(x-mean),0); + var expected = ( M2/(expectedValid.length-1) ) / mean; + + t.strictEqual( acc(), expected, '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.strictEqual( D, null, 'returns expected value' ); + + 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.strictEqual( D, null, 'returns expected value' ); + + 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.strictEqual( D, 0.0, 'returns expected value' ); + + D = acc(); + t.strictEqual( D, 0.0, 'returns expected value' ); + + 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(); +}); + +tape( 'the accumulator function ignores NaNs for all future invocations', function test( t ) { + var data = [ 2.0, NaN, 1.0, 3.0 ]; + var acc = incrnanvmr(); + var results = []; + var i; + + for ( i = 0; i < data.length; i++ ) { + results.push( acc( data[ i ] ) ); + } + + t.deepEqual( + results, + [ 0.0, 0.0, 0.3333333333333333, 0.5 ], + 'NaNs are ignored and do not break accumulator' +); + t.end(); +}); From 03818b1a9ed84f58efa01e69b3c0ed4056cdaa9c Mon Sep 17 00:00:00 2001 From: Ayushi Jain Date: Sun, 18 Jan 2026 05:26:14 +0000 Subject: [PATCH 2/4] fix: resolve ESlint issue in examples --- .../@stdlib/stats/incr/nanvmr/examples/index.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js index ca1dde2540bb..84c81ec8e5ab 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js @@ -38,10 +38,6 @@ for ( i = 0; i < 100; i++ ) { v = randu() * 100.0; } D = accumulator( v ); - console.log( - '%d\t%d', - v.toFixed( 4 ), - ( D === null ) ? NaN : D.toFixed( 4 ) - ); + console.log('%d\t%d', v.toFixed( 4 ), ( D === null ) ? NaN : D.toFixed( 4 )); } console.log( '\nFinal VMR: %d\n', accumulator() ); From a28abee9f7f5de7d73fb4e0f43a57b9cc9c6ad5d Mon Sep 17 00:00:00 2001 From: Ayushi Jain Date: Sun, 18 Jan 2026 05:53:17 +0000 Subject: [PATCH 3/4] fix: nanvmr test to ignore NaN --- .../@stdlib/stats/incr/nanvmr/test/test.js | 104 +++++++++++++----- 1 file changed, 79 insertions(+), 25 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js index 91fe5369dd89..001a73e3b483 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js @@ -73,9 +73,15 @@ tape( 'the function throws an error if provided a non-numeric value', function t tape( 'the accumulator function incrementally computes a variance-to-mean ratio', function test( t ) { var expected; var actual; + var valid; var data; + var diff; + var mean; + var sum; var acc; + var M2; var i; + var j; data = [ 2.0, 3.0, NaN, 2.0, 4.0, NaN, 3.0, 4.0 ]; @@ -84,20 +90,35 @@ tape( 'the accumulator function incrementally computes a variance-to-mean ratio' actual = zeros( data.length ); for ( i = 0; i < data.length; i++ ) { - actual[i] = acc( data[i] ); + actual[ i ] = acc( data[ i ] ); + + valid = []; + for ( j = 0; j <= i; j++ ) { + if ( !isnan( data[ j ] ) ) { + valid.push( data[ j ] ); + } + } - // compute expected ignoring NaNs: - var valid = data.slice(0, i+1).filter( function(d){ return !isnan(d); }); if ( valid.length === 0 ) { - expected[i] = null; + expected[ i ] = null; } else if ( valid.length === 1 ) { - expected[i] = 0.0 / ( valid[0] ); // single datum: VMR=0 + expected[ i ] = 0.0 / valid[ 0 ]; } else { - var mean = valid.reduce((a,b)=>a+b,0)/valid.length; - var M2 = valid.reduce( (s,x) => s + (x-mean)*(x-mean), 0); - expected[i] = ( M2 / (valid.length-1) ) / mean; + sum = 0.0; + for ( j = 0; j < valid.length; j++ ) { + sum += valid[ j ]; } + mean = sum / valid.length; + + M2 = 0.0; + for ( j = 0; j < valid.length; j++ ) { + diff = valid[ j ] - mean; + M2 += diff * diff; + } + expected[ i ] = ( M2 / ( valid.length - 1 ) ) / mean; + } } + t.deepEqual( actual, expected, 'returns expected results' ); t.end(); }); @@ -105,9 +126,13 @@ tape( 'the accumulator function incrementally computes a variance-to-mean ratio' tape( 'the accumulator function incrementally computes a variance-to-mean ratio (known mean)', function test( t ) { var expected; var actual; + var valid; var data; + var diff; var acc; + var M2; var i; + var j; data = [ 2.0, NaN, 3.0, 2.0, NaN, 4.0, 3.0 ]; @@ -116,15 +141,24 @@ tape( 'the accumulator function incrementally computes a variance-to-mean ratio actual = zeros( data.length ); for ( i = 0; i < data.length; i++ ) { - actual[i] = acc( data[i] ); + actual[ i ] = acc( data[ i ] ); + + valid = []; + for ( j = 0; j <= i; j++ ) { + if ( !isnan( data[ j ] ) ) { + valid.push( data[ j ] ); + } + } - // compute expected ignoring NaNs: - var valid = data.slice(0,i+1).filter( function(d){ return !isnan(d); }); if ( valid.length === 0 ) { - expected[i] = null; + expected[ i ] = null; } else { - var M2 = valid.reduce( (s,x) => s + (x-3.0)*(x-3.0), 0 ); - expected[i] = ( M2 / valid.length ) / 3.0; + M2 = 0.0; + for ( j = 0; j < valid.length; j++ ) { + diff = valid[ j ] - 3.0; + M2 += ( diff * diff ); + } + expected[ i ] = ( M2 / valid.length ) / 3.0; } } t.deepEqual( actual, expected, 'returns expected value' ); @@ -132,20 +166,44 @@ tape( 'the accumulator function incrementally computes a variance-to-mean ratio }); tape( 'if not provided an input value, the accumulator function returns the current variance-to-mean ratio', function test( t ) { + var expected; + var valid; var data; + var mean; + var diff; var acc; + var sum; + var M2; var i; + var j; data = [ 2.0, NaN, 3.0, 1.0 ]; acc = incrnanvmr(); + + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + + valid = []; for ( i = 0; i < data.length; i++ ) { - acc( data[i] ); + if ( !isnan( data[ i ] ) ) { + valid.push( data[ i ] ); + } } - var expectedValid = data.filter(d => !isnan(d)); - var mean = expectedValid.reduce((a,b)=>a+b,0)/expectedValid.length; - var M2 = expectedValid.reduce((s,x)=>s+(x-mean)*(x-mean),0); - var expected = ( M2/(expectedValid.length-1) ) / mean; + sum = 0.0; + for ( j = 0; j < valid.length; j++ ) { + sum += valid[ j ]; + } + mean = sum / valid.length; + + M2 = 0.0; + for ( j = 0; j < valid.length; j++ ) { + diff = valid[ j ] - mean; + M2 += ( diff * diff ); + } + + expected = ( M2 / ( valid.length - 1 ) ) / mean; t.strictEqual( acc(), expected, 'returns expected value' ); t.end(); @@ -209,19 +267,15 @@ tape( 'the accumulated value is `0` until at least 2 datums have been provided ( }); tape( 'the accumulator function ignores NaNs for all future invocations', function test( t ) { + var results = []; var data = [ 2.0, NaN, 1.0, 3.0 ]; var acc = incrnanvmr(); - var results = []; var i; for ( i = 0; i < data.length; i++ ) { results.push( acc( data[ i ] ) ); } - t.deepEqual( - results, - [ 0.0, 0.0, 0.3333333333333333, 0.5 ], - 'NaNs are ignored and do not break accumulator' -); + t.deepEqual(results, [ 0.0, 0.0, 0.3333333333333333, 0.5 ], 'NaNs are ignored and do not break accumulator'); t.end(); }); From 28f7610b7ce8931eead907b19bd0f347542d2a86 Mon Sep 17 00:00:00 2001 From: Ayushi Jain Date: Tue, 20 Jan 2026 05:34:23 +0000 Subject: [PATCH 4/4] docs: fix copy-paste errors and align incrnanvmr naming --- lib/node_modules/@stdlib/stats/incr/nanvmr/README.md | 4 ++-- lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt | 2 +- lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js | 2 +- lib/node_modules/@stdlib/stats/incr/nanvmr/lib/main.js | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/README.md b/lib/node_modules/@stdlib/stats/incr/nanvmr/README.md index f61dd57842bd..58683b88e90a 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanvmr/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/README.md @@ -18,7 +18,7 @@ limitations under the License. --> -# incrvmr +# incrnanvmr > Compute a [variance-to-mean ratio][variance-to-mean-ratio] (VMR) incrementally. @@ -81,7 +81,7 @@ D = \frac{s^2}{\bar{x}} var incrnanvmr = require( '@stdlib/stats/incr/nanvmr' ); ``` -#### incrvmr( \[mean] ) +#### incrnanvmr( \[mean] ) Returns an accumulator `function` which incrementally computes a [variance-to-mean ratio][variance-to-mean-ratio]. diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt index b3eb33b691b0..87b9ea6c8304 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt @@ -25,7 +25,7 @@ null > D = accumulator( 2.0 ) 0.0 - > D =accumulator( NaN ) + > D = accumulator( NaN ) 0.0 > D = accumulator( 1.0 ) ~0.33 diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js index 84c81ec8e5ab..619586c9f9a0 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js @@ -38,6 +38,6 @@ for ( i = 0; i < 100; i++ ) { v = randu() * 100.0; } D = accumulator( v ); - console.log('%d\t%d', v.toFixed( 4 ), ( D === null ) ? NaN : D.toFixed( 4 )); + 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/main.js b/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/main.js index ec3dc0d73cfb..111c39183f51 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/main.js @@ -34,7 +34,7 @@ var incrvmr = require( '@stdlib/stats/incr/vmr' ); * @returns {Function} accumulator function * * @example -* var accumulator = incrvmr(); +* var accumulator = incrnanvmr(); * * var D = accumulator(); * // returns null @@ -49,7 +49,7 @@ var incrvmr = require( '@stdlib/stats/incr/vmr' ); * // returns ~0.33 * * @example -* var accumulator = incrvmr( 3.14 ); +* var accumulator = incrnanvmr( 3.14 ); */ function incrnanvmr( mean ) { var acc;