From c4b6b5625584f8062082f4b05fc295316102e292 Mon Sep 17 00:00:00 2001 From: hrshya Date: Sun, 9 Mar 2025 23:32:38 +0530 Subject: [PATCH 1/2] [RFC]: add stats/incr/nanvmr --- .../@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 | 35 +++ .../stats/incr/nanvmr/docs/types/index.d.ts | 63 +++++ .../stats/incr/nanvmr/docs/types/test.ts | 60 +++++ .../stats/incr/nanvmr/examples/index.js | 43 +++ .../@stdlib/stats/incr/nanvmr/lib/index.js | 53 ++++ .../@stdlib/stats/incr/nanvmr/lib/main.js | 77 ++++++ .../@stdlib/stats/incr/nanvmr/package.json | 73 +++++ .../@stdlib/stats/incr/nanvmr/test/test.js | 251 ++++++++++++++++++ 13 files changed, 1099 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..07c0aa6754f9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/README.md @@ -0,0 +1,221 @@ + + +# incrnanvmr + +> 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' ); +``` + +#### 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( NaN ); +// 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 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + \ No newline at end of file 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..f1b40963738e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/benchmark/benchmark.js @@ -0,0 +1,91 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var 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(); +}); \ No newline at end of file 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..829e45faf266 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/repl.txt @@ -0,0 +1,35 @@ + +{{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 + -------- \ No newline at end of file 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..ec79a9c851c2 --- /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) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* 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; \ No newline at end of file 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..4bd8a1aca240 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @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 incrnanvmr from './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 +} \ No newline at end of file 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..f14727cf57b2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/examples/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var 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() ); \ No newline at end of file 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..b7cefc752ca4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/index.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute a variance-to-mean ratio (VMR) incrementally, with special handling for 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 NaN +* +* D = accumulator(); +* // returns NaN +*/ + +// 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..319a108d2c23 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/lib/main.js @@ -0,0 +1,77 @@ +/** +* @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 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). +* +* @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() { + var vmr = incrvmr(); + return accumulator; + + /** + * If provided a value, the accumulator function updates and returns the VMR. + * If not provided a value, it returns the current VMR. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} VMR or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return vmr(); + } + return vmr( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanvmr; \ No newline at end of file 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..9ee03cb3e4d1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/stats/incr/vmr", + "version": "0.0.0", + "description": "Compute a variance-to-mean ratio (VMR) incrementally.", + "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" + ] + } \ No newline at end of file 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..46de084197b2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js @@ -0,0 +1,251 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +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, 4.0, 3.0, 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.9166666666666666/(11.0/4.0), + 0.7/(14.0/5.0), + 0.8/(18.0/6.0) + ]; + + acc = incrnanvmr(); + + actual = new Array( data.length ); + for ( i = 0; i < data.length; i++ ) { + actual[ i ] = 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, 4.0, 3.0, 4.0 ]; + + // Test against Julia: + expected = [ + 1.0/3.0, + 0.5/3.0, + 0.6666666666666666/3.0, + 0.75/3.0, + 0.6/3.0, + 0.6666666666666666/3.0 + ]; + + acc = incrnanvmr( 3.0 ); + + actual = new Array( data.length ); + for ( i = 0; i < data.length; i++ ) { + actual[ i ] = 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(); +}); + +tape( 'if provided a `NaN`, the accumulator function ignores it and continues with the next value (unknown mean)', function test( t ) { + var data; + var acc; + var v; + var i; + + data = [ NaN, 2.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]; + acc = incrvmr(); + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + if (!isnan( data[i] )) { + t.notEqual( isnan( v ), true, 'returns valid value' ); + } + } + t.end(); +}); + + +tape( 'if provided a `NaN`, the accumulator function ignores it and continues with the next value (known mean)', function test( t ) { + var data; + var acc; + var v; + var i; + + data = [ NaN, 2.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]; + acc = incrnanvmr( 3.14 ); + + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + if (!isnan( data[i] )) { + t.notEqual( isnan( v ), true, 'returns valid value after NaN' ); + } + } + + t.notEqual( isnan( acc() ), true, 'returns valid final value after ignoring NaN' ); + t.end(); +}); From 35ec6fa3dc40f08977f3757c0e4f4e7c14fbfe54 Mon Sep 17 00:00:00 2001 From: hrshya Date: Mon, 10 Mar 2025 01:56:04 +0530 Subject: [PATCH 2/2] [RFC]: add stats/incr/nanvmr --- lib/node_modules/@stdlib/stats/incr/nanvmr/package.json | 2 +- lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanvmr/package.json b/lib/node_modules/@stdlib/stats/incr/nanvmr/package.json index 9ee03cb3e4d1..3b73632120f3 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanvmr/package.json +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/package.json @@ -1,5 +1,5 @@ { - "name": "@stdlib/stats/incr/vmr", + "name": "@stdlib/stats/incr/nanvmr", "version": "0.0.0", "description": "Compute a variance-to-mean ratio (VMR) incrementally.", "license": "Apache-2.0", 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 46de084197b2..c7a20a95f12d 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanvmr/test/test.js @@ -219,7 +219,7 @@ tape( 'if provided a `NaN`, the accumulator function ignores it and continues wi var i; data = [ NaN, 2.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]; - acc = incrvmr(); + acc = incrnanvmr(); for ( i = 0; i < data.length; i++ ) { v = acc( data[ i ] ); if (!isnan( data[i] )) {