From 3382cd3c12fa27b98290383c0a87ab6a9d2ceeec Mon Sep 17 00:00:00 2001 From: Hemang Chodhary Date: Sat, 22 Mar 2025 18:20:29 +0530 Subject: [PATCH 1/7] Added nanmvmr --- .../@stdlib/stats/incr/nanmvmr/README.md | 229 ++++++++ .../stats/incr/nanmvmr/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/nanmvmr/docs/repl.txt | 53 ++ .../stats/incr/nanmvmr/docs/types/index.d.ts | 79 +++ .../stats/incr/nanmvmr/docs/types/test.ts | 77 +++ .../stats/incr/nanmvmr/examples/index.js | 38 ++ .../@stdlib/stats/incr/nanmvmr/lib/index.js | 63 +++ .../@stdlib/stats/incr/nanmvmr/lib/main.js | 168 ++++++ .../@stdlib/stats/incr/nanmvmr/package.json | 78 +++ .../@stdlib/stats/incr/nanmvmr/test/test.js | 535 ++++++++++++++++++ 13 files changed, 1543 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/img/equation_arithmetic_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/img/equation_unbiased_sample_variance.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/img/equation_variance_to_mean_ratio.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md b/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md new file mode 100644 index 000000000000..e136e75bb12b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md @@ -0,0 +1,229 @@ + + +# incrmvmr + +> Compute a moving [variance-to-mean ratio][variance-to-mean-ratio] (VMR) incrementally. + +
+ +For a window of size `W`, the [unbiased sample variance][sample-variance] is defined as + + + +```math +s^2 = \frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2 +``` + + + + + +and the [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} x_i +``` + + + + + +The [variance-to-mean ratio][variance-to-mean-ratio] (VMR) is thus defined as + + + +```math +F = \frac{s^2}{\bar{x}} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrmvmr = require( '@stdlib/stats/incr/mvmr' ); +``` + +#### incrmvmr( window\[, mean] ) + +Returns an accumulator `function` which incrementally computes a moving [variance-to-mean ratio][variance-to-mean-ratio]. The `window` parameter defines the number of values over which to compute the moving [variance-to-mean ratio][variance-to-mean-ratio]. + +```javascript +var accumulator = incrmvmr( 3 ); +``` + +If the mean is already known, provide a `mean` argument. + +```javascript +var accumulator = incrmvmr( 3, 5.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 = incrmvmr( 3 ); + +var F = accumulator(); +// returns null + +// Fill the window... +F = accumulator( 2.0 ); // [2.0] +// returns 0.0 + +F = accumulator( 1.0 ); // [2.0, 1.0] +// returns ~0.33 + +F = accumulator( 3.0 ); // [2.0, 1.0, 3.0] +// returns 0.5 + +// Window begins sliding... +F = accumulator( 7.0 ); // [1.0, 3.0, 7.0] +// returns ~2.55 + +F = accumulator( 5.0 ); // [3.0, 7.0, 5.0] +// returns ~0.80 + +F = accumulator(); +// returns ~0.80 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **at least** `W-1` future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. + +- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. + +- 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**, **relative variance**, and the [**Fano factor**][fano-factor]. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrmvmr = require( '@stdlib/stats/incr/mvmr' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrmvmr( 5 ); + +// For each simulated datum, update the moving variance-to-mean ratio... +for ( i = 0; i < 100; i++ ) { + v = randu() * 100.0; + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/benchmark/benchmark.js new file mode 100644 index 000000000000..1dd42be9132b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/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 incrnanmvmr = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmvmr( (i%5)+1 ); + 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 = incrnanmvmr( 5 ); + + 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 = incrnanmvmr( 5, 0.5 ); + + 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/nanmvmr/docs/img/equation_arithmetic_mean.svg b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/img/equation_arithmetic_mean.svg new file mode 100644 index 000000000000..1a89a2bfb996 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/img/equation_arithmetic_mean.svg @@ -0,0 +1,43 @@ + +x overbar equals StartFraction 1 Over upper W EndFraction sigma-summation Underscript i equals 0 Overscript upper W minus 1 Endscripts x Subscript i + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/img/equation_unbiased_sample_variance.svg b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/img/equation_unbiased_sample_variance.svg new file mode 100644 index 000000000000..f4722f986a70 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/img/equation_unbiased_sample_variance.svg @@ -0,0 +1,61 @@ + +s squared equals StartFraction 1 Over upper W minus 1 EndFraction sigma-summation Underscript i equals 0 Overscript upper W 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/nanmvmr/docs/img/equation_variance_to_mean_ratio.svg b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/img/equation_variance_to_mean_ratio.svg new file mode 100644 index 000000000000..d7318aa42b21 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/img/equation_variance_to_mean_ratio.svg @@ -0,0 +1,28 @@ + +upper F equals StartFraction s squared Over x overbar EndFraction + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/repl.txt new file mode 100644 index 000000000000..388d1fb8cb26 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/repl.txt @@ -0,0 +1,53 @@ +{{alias}}( W[, mean] ) + Returns an accumulator function which incrementally computes a moving + variance-to-mean ratio (VMR) while ignoring NaN values. + + The `W` parameter defines the number of values over which to compute the + moving variance-to-mean ratio. + + If provided a value, the accumulator function returns an updated moving + variance-to-mean ratio. If not provided a value, the accumulator function + returns the current moving variance-to-mean ratio. + + As `W` values are needed to fill the window buffer, the first `W-1` returned + values are calculated from smaller sample sizes. Until the window is full, + each returned value is calculated from all provided values. + + NaN input values are ignored. + + Parameters + ---------- + W: integer + Window size. + + mean: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 3 ); + > var F = accumulator() + null + > F = accumulator( 2.0 ) + 0.0 + > F = accumulator( NaN ) + 0.0 + > F = accumulator( 1.0 ) + ~0.33 + > F = accumulator( 3.0 ) + 0.5 + > F = accumulator( NaN ) + 0.5 + > F = accumulator( 7.0 ) + ~2.55 + > F = accumulator() + ~2.55 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/index.d.ts new file mode 100644 index 000000000000..85a577008363 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/index.d.ts @@ -0,0 +1,79 @@ +/* +* @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. +* +* ## Notes +* +* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations. +* - NaN values are ignored. +* +* @param x - value +* @returns accumulated value or null +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a moving variance-to-mean ratio (VMR) while ignoring NaN values. +* +* @param W - window size +* @param mean - mean value +* @throws first argument must be a positive integer +* @returns accumulator function +* +* @example +* var accumulator = incrnanmvmr( 3 ); +* +* var F = accumulator(); +* // returns null +* +* F = accumulator( 2.0 ); +* // returns 0.0 +* +* F = accumulator( NaN ); +* // returns 0.0 +* +* F = accumulator( 1.0 ); +* // returns ~0.33 +* +* F = accumulator( 3.0 ); +* // returns 0.5 +* +* F = accumulator( NaN ); +* // returns 0.5 +* +* F = accumulator( 7.0 ); +* // returns ~2.55 +* +* F = accumulator(); +* // returns ~2.55 +* +* @example +* var accumulator = incrnanmvmr( 3, 2.0 ); +*/ +declare function incrnanmvmr( W: number, mean?: number ): accumulator; + + +// EXPORTS // + +export = incrnanmvmr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/test.ts new file mode 100644 index 000000000000..3e51dc840d73 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/test.ts @@ -0,0 +1,77 @@ +/* +* @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 incrnanmvmr = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmvmr( 3 ); // $ExpectType accumulator + incrnanmvmr( 3, 0.0 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided a first argument that is not a number... +{ + incrnanmvmr( '5' ); // $ExpectError + incrnanmvmr( true ); // $ExpectError + incrnanmvmr( false ); // $ExpectError + incrnanmvmr( null ); // $ExpectError + incrnanmvmr( [] ); // $ExpectError + incrnanmvmr( {} ); // $ExpectError + incrnanmvmr( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument that is not a number... +{ + incrnanmvmr( 2, '5' ); // $ExpectError + incrnanmvmr( 2, true ); // $ExpectError + incrnanmvmr( 2, false ); // $ExpectError + incrnanmvmr( 2, null ); // $ExpectError + incrnanmvmr( 2, [] ); // $ExpectError + incrnanmvmr( 2, {} ); // $ExpectError + incrnanmvmr( 2, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanmvmr(); // $ExpectError + incrnanmvmr( 2, 3, 4 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmvmr( 3 ); + + 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 = incrnanmvmr( 3 ); + + 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/nanmvmr/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js new file mode 100644 index 000000000000..a7390545702b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js @@ -0,0 +1,38 @@ +/** +* @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 incrnanmvmr = require( './../lib' ); + +var accumulator; +var F; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmvmr( 5 ); + +// For each simulated datum, update the moving variance-to-mean ratio... +console.log( '\nValue\tVMR\n' ); +for ( i = 0; i < 100; i++ ) { + v = randu() < 0.2 ? NaN : randu() * 100.0; + F = accumulator( v ); + console.log( '%s\t%s', Number.isNaN(v) ? 'NaN' : v.toFixed( 4 ), Number.isNaN(F) ? 'NaN' : F.toFixed( 4 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/index.js new file mode 100644 index 000000000000..5726b8cae4fc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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 moving variance-to-mean ratio (VMR) incrementally, ignoring NaN values. +* +* @module @stdlib/stats/incr/nanmvmr +* +* @example +* var incrnanmvmr = require( '@stdlib/stats/incr/nanmvmr' ); +* +* var accumulator = incrnanmvmr( 3 ); +* +* var F = accumulator(); +* // returns null +* +* F = accumulator( 2.0 ); +* // returns 0.0 +* +* F = accumulator( NaN ); +* // returns 0.0 +* +* F = accumulator( 1.0 ); +* // returns ~0.33 +* +* F = accumulator( 3.0 ); +* // returns 0.5 +* +* F = accumulator( NaN ); +* // returns 0.5 +* +* F = accumulator( 7.0 ); +* // returns ~2.55 +* +* F = accumulator(); +* // returns ~2.55 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js new file mode 100644 index 000000000000..018717636f81 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js @@ -0,0 +1,168 @@ +/** +* @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 incrmvmr = require( '@stdlib/stats/incr/mvmr' ); +var isNaN = require( '@stdlib/math/base/assert/is-nan' ); + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a moving variance-to-mean ratio (VMR) while ignoring NaN values. +* +* ## Method +* +* - Let \\(W\\) be a window of \\(N\\) elements over which we want to compute a variance-to-mean ratio (VMR). +* +* - The difference between the unbiased sample variance in a window \\(W_i\\) and the unbiased sample variance in a window \\(W_{i+1})\\) is given by +* +* ```tex +* \Delta s^2 = s_{i+1}^2 - s_{i}^2 +* ``` +* +* - If we multiply both sides by \\(N-1\\), +* +* ```tex +* (N-1)(\Delta s^2) = (N-1)s_{i+1}^2 - (N-1)s_{i}^2 +* ``` +* +* - If we substitute the definition of the unbiased sample variance having the form +* +* ```tex +* \begin{align*} +* s^2 &= \frac{1}{N-1} \biggl( \sum_{i=1}^{N} (x_i - \bar{x})^2 \biggr) \\ +* &= \frac{1}{N-1} \biggl( \sum_{i=1}^{N} (x_i^2 - 2\bar{x}x_i + \bar{x}^2) \biggr) \\ +* &= \frac{1}{N-1} \biggl( \sum_{i=1}^{N} x_i^2 - 2\bar{x} \sum_{i=1}^{N} x_i + \sum_{i=1}^{N} \bar{x}^2) \biggr) \\ +* &= \frac{1}{N-1} \biggl( \sum_{i=1}^{N} x_i^2 - \frac{2N\bar{x}\sum_{i=1}^{N} x_i}{N} + N\bar{x}^2 \biggr) \\ +* &= \frac{1}{N-1} \biggl( \sum_{i=1}^{N} x_i^2 - 2N\bar{x}^2 + N\bar{x}^2 \biggr) \\ +* &= \frac{1}{N-1} \biggl( \sum_{i=1}^{N} x_i^2 - N\bar{x}^2 \biggr) +* \end{align*} +* ``` +* +* we return +* +* ```tex +* (N-1)(\Delta s^2) = \biggl(\sum_{k=1}^N x_k^2 - N\bar{x}_{i+1}^2 \biggr) - \biggl(\sum_{k=0}^{N-1} x_k^2 - N\bar{x}_{i}^2 \biggr) +* ``` +* +* - This can be further simplified by recognizing that subtracting the sums reduces to \\(x_N^2 - x_0^2\\); in which case, +* +* ```tex +* \begin{align*} +* (N-1)(\Delta s^2) &= x_N^2 - x_0^2 - N\bar{x}_{i+1}^2 + N\bar{x}_{i}^2 \\ +* &= x_N^2 - x_0^2 - N(\bar{x}_{i+1}^2 - \bar{x}_{i}^2) \\ +* &= x_N^2 - x_0^2 - N(\bar{x}_{i+1} - \bar{x}_{i})(\bar{x}_{i+1} + \bar{x}_{i}) +* \end{align*} +* ``` +* +* - Recognizing that the difference of means can be expressed +* +* ```tex +* \bar{x}_{i+1} - \bar{x}_i = \frac{1}{N} \biggl( \sum_{k=1}^N x_k - \sum_{k=0}^{N-1} x_k \biggr) = \frac{x_N - x_0}{N} +* ``` +* +* and substituting into the equation above +* +* ```tex +* (N-1)(\Delta s^2) = x_N^2 - x_0^2 - (x_N - x_0)(\bar{x}_{i+1} + \bar{x}_{i}) +* ``` +* +* - Rearranging terms gives us the update equation for the unbiased sample variance +* +* ```tex +* \begin{align*} +* (N-1)(\Delta s^2) &= (x_N - x_0)(x_N + x_0) - (x_N - x_0)(\bar{x}_{i+1} + \bar{x}_{i}) +* &= (x_N - x_0)(x_N + x_0 - \bar{x}_{i+1} - \bar{x}_{i}) \\ +* &= (x_N - x_0)(x_N - \bar{x}_{i+1} + x_0 - \bar{x}_{i}) +* \end{align*} +* ``` +* +* @param {PositiveInteger} W - window size +* @param {number} [mean] - mean value +* @throws {TypeError} first argument must be a positive integer +* @throws {TypeError} second argument must be a number +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmvmr( 3 ); +* +* var F = accumulator(); +* // returns null +* +* F = accumulator( 2.0 ); +* // returns 0.0 +* +* F = accumulator( NaN ); +* // returns 0.0 +* +* F = accumulator( 1.0 ); +* // returns ~0.33 +* +* F = accumulator( 3.0 ); +* // returns 0.5 +* +* F = accumulator( NaN ); +* // returns 0.5 +* +* F = accumulator( 7.0 ); +* // returns ~2.55 +* +* F = accumulator(); +* // returns ~2.55 +* +* @example +* var accumulator = incrnanmvmr( 3, 2.0 ); +*/ +function incrnanmvmr( W, mean ) { + var acc; + + // Initialize the wrapped accumulator: + if ( arguments.length > 1 ) { + acc = incrmvmr( W, mean ); + } else { + acc = incrmvmr( W ); + } + + 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. + * NaN input values are ignored. + * + * @private + * @param {number} [x] - input value + * @returns {(number|null)} accumulated value or null + */ + function accumulator( x ) { + if ( arguments.length === 0 ) { + return acc(); + } + if ( isNaN( x ) === false ) { + return acc( x ); + } + + return acc(); + } +} + +// EXPORTS // + +module.exports = incrnanmvmr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json b/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json new file mode 100644 index 000000000000..3b04483cfad7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/stats/incr/mvmr", + "version": "0.0.0", + "description": "Compute a moving 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", + "sample variance", + "unbiased", + "dispersion", + "index of dispersion", + "fano factor", + "fano", + "dispersion index", + "relative variance", + "vmr", + "mean", + "ratio", + "incremental", + "accumulator", + "sliding window", + "sliding", + "window", + "moving" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js new file mode 100644 index 000000000000..84f26e7480ed --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js @@ -0,0 +1,535 @@ +/** +* @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 randu = require( '@stdlib/random/base/randu' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var incrnanmvmr = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmvmr, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a positive integer for the window size', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + 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() { + incrnanmvmr( value ); + }; + } +}); + +tape( 'the function throws an error if not provided a positive integer for the window size (known mean)', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + 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() { + incrnanmvmr( value, 3.0 ); + }; + } +}); + +tape( 'the function throws an error if not provided a number as the mean 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() { + incrnanmvmr( 3, value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmvmr( 3 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known mean)', function test( t ) { + t.equal( typeof incrnanmvmr( 3, 3.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function computes a moving variance-to-mean ratio incrementally', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, 3.0, 4.0, -1.0, 3.0, 1.0 ]; + N = data.length; + + expected = [ + 0.0/2.0, + 0.5/2.5, + 1.0/3.0, + 7.0/2.0, + 7.0/2.0, + 4.0/1.0 + ]; + + acc = incrnanmvmr( 3 ); + + actual = new Array( N ); + for ( i = 0; i < N; i++ ) { + actual[ i ] = acc( data[ i ] ); + } + t.deepEqual( actual, expected, 'returns expected results' ); + t.end(); +}); + +tape( 'the accumulator function computes a moving variance-to-mean ratio incrementally (known mean)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, 3.0, 4.0, -1.0, 3.0, 1.0 ]; + N = data.length; + + acc = incrnanmvmr( 3, 2.0 ); + + actual = new Array( N ); + for ( i = 0; i < N; i++ ) { + actual[ i ] = acc( data[ i ] ); + } + expected = [ + 0.0/2.0, + 0.5/2.0, + 1.6666666666666667/2.0, + 4.666666666666667/2.0, + 4.666666666666667/2.0, + 3.6666666666666665/2.0 + ]; + t.deepEqual( actual, expected, 'returns expected results' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current accumulated value', function test( t ) { + var expected; + var actual; + var delta; + var data; + var tol; + var acc; + var i; + + data = [ 2.0, 3.0, 10.0 ]; + acc = incrnanmvmr( 3 ); + for ( i = 0; i < data.length-1; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 0.5/2.5, 'returns expected value' ); + + acc( data[ data.length-1 ] ); + + expected = 19.0/5.0; + actual = acc(); + delta = abs( actual - expected ); + tol = EPS * expected; + + t.equal( delta < tol, true, 'expected: '+expected+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current accumulated value (known mean)', function test( t ) { + var expected; + var actual; + var delta; + var data; + var tol; + var acc; + var i; + + data = [ 2.0, 3.0, 10.0 ]; + acc = incrnanmvmr( 3, 5.0 ); + for ( i = 0; i < data.length-1; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 6.5/5.0, 'returns expected value' ); + + acc( data[ data.length-1 ] ); + + expected = 12.666666666666666/5.0; + actual = acc(); + delta = abs( actual - expected ); + tol = EPS * expected; + + t.equal( delta < tol, true, 'expected: '+expected+'. actual: '+actual+'. tol: '+tol+'. delta: '+delta+'.' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmvmr( 3 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null` (known mean)', function test( t ) { + var acc = incrnanmvmr( 3, 3.0 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'if only one datum has been provided and the mean is unknown, the accumulator function returns `0`', function test( t ) { + var acc = incrnanmvmr( 3 ); + acc( 2.0 ); + t.equal( acc(), 0.0, 'returns 0' ); + t.end(); +}); + +tape( 'if only one datum has been provided and the mean is known, the accumulator function may not return `0`', function test( t ) { + var acc = incrnanmvmr( 3, 30 ); + acc( 2.0 ); + t.notEqual( acc(), 0.0, 'does not return 0' ); + t.end(); +}); + +tape( 'if the window size is `1` and the mean is unknown, the accumulator function always returns `0`', function test( t ) { + var acc; + var F; + var i; + + acc = incrnanmvmr( 1 ); + for ( i = 0; i < 100; i++ ) { + F = acc( randu() * 100.0 ); + t.equal( F, 0.0, 'returns 0' ); + } + t.end(); +}); + +tape( 'if the window size is `1` and the mean is known, the accumulator function may not always return `0`', function test( t ) { + var acc; + var F; + var i; + + acc = incrnanmvmr( 1, 500.0 ); // mean is outside the range of simulated values so the variance should never be zero + for ( i = 0; i < 100; i++ ) { + F = acc( randu() * 100.0 ); + t.notEqual( F, 0.0, 'does not return 0' ); + } + t.end(); +}); + +tape( 'if provided `NaN`, the accumulated value is not affected (unknown mean)', function test( t ) { + var expected; + var data; + var acc; + var v; + var i; + + acc = incrnanmvmr( 3 ); + + data = [ + NaN, // NaN (ignored) + 3.14, // 3.14 + 3.14, // 3.14, 3.14 + NaN, // 3.14, 3.14 (NaN ignored) + 3.14, // 3.14, 3.14, 3.14 + 3.14, // 3.14, 3.14, 3.14 + 3.14, // 3.14, 3.14, 3.14 + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + 3.14, // 3.14, 3.14, 3.14 + 3.14, // 3.14, 3.14, 3.14 + 3.14, // 3.14, 3.14, 3.14 + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + 3.14, // 3.14, 3.14, 3.14 + 3.14, // 3.14, 3.14, 3.14 + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + 3.14 // 3.14, 3.14, 3.14 + ]; + expected = [ + null, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ]; + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + t.equal( v, expected[ i ], 'returns expected value for window '+i ); + t.equal( acc(), expected[ i ], 'returns expected value for window '+i ); + } + t.end(); +}); + +tape( 'if provided `NaN`, the accumulated value is not affected (known mean)', function test( t ) { + var expected; + var data; + var acc; + var v; + var i; + + acc = incrnanmvmr( 3, 3.14 ); + + data = [ + NaN, // NaN (ignored) + 3.14, // 3.14 + 3.14, // 3.14, 3.14 + NaN, // 3.14, 3.14 (NaN ignored) + 3.14, // 3.14, 3.14, 3.14 + 3.14, // 3.14, 3.14, 3.14 + 3.14, // 3.14, 3.14, 3.14 + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + 3.14, // 3.14, 3.14, 3.14 + 3.14, // 3.14, 3.14, 3.14 + 3.14, // 3.14, 3.14, 3.14 + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + 3.14, // 3.14, 3.14, 3.14 + 3.14, // 3.14, 3.14, 3.14 + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + NaN, // 3.14, 3.14, 3.14 (NaN ignored) + 3.14 // 3.14, 3.14, 3.14 + ]; + expected = [ + null, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ]; + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + t.equal( v, expected[ i ], 'returns expected value for window '+i ); + t.equal( acc(), expected[ i ], 'returns expected value for window '+i ); + } + t.end(); +}); + +tape( 'if provided `NaN`, the accumulated value is not affected (unknown mean, W=1)', function test( t ) { + var expected; + var data; + var acc; + var v; + var i; + + acc = incrnanmvmr( 1 ); + + data = [ + NaN, // NaN (ignored) + 3.14, // 3.14 + 3.14, // 3.14 + NaN, // 3.14 (NaN ignored) + 3.14, // 3.14 + 3.14, // 3.14 + 3.14, // 3.14 + NaN, // 3.14 (NaN ignored) + 3.14, // 3.14 + 3.14, // 3.14 + 3.14, // 3.14 + NaN, // 3.14 (NaN ignored) + 3.14, // 3.14 + 3.14, // 3.14 + NaN, // 3.14 (NaN ignored) + NaN, // 3.14 (NaN ignored) + NaN, // 3.14 (NaN ignored) + NaN, // 3.14 (NaN ignored) + 3.14 // 3.14 + ]; + expected = [ + null, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ]; + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + t.equal( v, expected[ i ], 'returns expected value for window '+i ); + t.equal( acc(), expected[ i ], 'returns expected value for window '+i ); + } + t.end(); +}); + +tape( 'if provided `NaN`, the accumulated value is not affected (known mean, W=1)', function test( t ) { + var expected; + var data; + var acc; + var v; + var i; + + acc = incrnanmvmr( 1, 3.14 ); + + data = [ + NaN, // NaN (ignored) + 3.14, // 3.14 + 3.14, // 3.14 + NaN, // 3.14 (NaN ignored) + 3.14, // 3.14 + 3.14, // 3.14 + 3.14, // 3.14 + NaN, // 3.14 (NaN ignored) + 3.14, // 3.14 + 3.14, // 3.14 + 3.14, // 3.14 + NaN, // 3.14 (NaN ignored) + 3.14, // 3.14 + 3.14, // 3.14 + NaN, // 3.14 (NaN ignored) + NaN, // 3.14 (NaN ignored) + NaN, // 3.14 (NaN ignored) + NaN, // 3.14 (NaN ignored) + 3.14 // 3.14 + ]; + expected = [ + null, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ]; + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + t.equal( v, expected[ i ], 'returns expected value for window '+i ); + t.equal( acc(), expected[ i ], 'returns expected value for window '+i ); + } + t.end(); +}); From d73b2daf60468231f5ed9456397fbd7a59d12dce Mon Sep 17 00:00:00 2001 From: Hemang Chodhary Date: Sat, 22 Mar 2025 18:22:31 +0530 Subject: [PATCH 2/7] Changed Packagejson --- lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json b/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json index 3b04483cfad7..45437903a7cd 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json @@ -1,5 +1,5 @@ { - "name": "@stdlib/stats/incr/mvmr", + "name": "@stdlib/stats/incr/nanmvmr", "version": "0.0.0", "description": "Compute a moving variance-to-mean ratio (VMR) incrementally.", "license": "Apache-2.0", From 2f5cdabbd977b0dea1afa3aa0464e85e45b714da Mon Sep 17 00:00:00 2001 From: Hemang Chodhary Date: Sat, 22 Mar 2025 22:50:53 +0530 Subject: [PATCH 3/7] Updated Readme.md --- .../@stdlib/stats/incr/nanmvmr/README.md | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md b/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md index e136e75bb12b..3f42bd57b253 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md @@ -18,7 +18,7 @@ limitations under the License. --> -# incrmvmr +# incrnanmvmr > Compute a moving [variance-to-mean ratio][variance-to-mean-ratio] (VMR) incrementally. @@ -33,7 +33,7 @@ s^2 = \frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2 ``` @@ -48,7 +48,7 @@ and the [arithmetic mean][arithmetic-mean] is defined as ``` @@ -63,7 +63,7 @@ F = \frac{s^2}{\bar{x}} ``` @@ -78,21 +78,21 @@ F = \frac{s^2}{\bar{x}} ## Usage ```javascript -var incrmvmr = require( '@stdlib/stats/incr/mvmr' ); +var incrnanmvmr = require( '@stdlib/stats/incr/nanmvmr' ); ``` -#### incrmvmr( window\[, mean] ) +#### incrnanmvmr( window\[, mean] ) Returns an accumulator `function` which incrementally computes a moving [variance-to-mean ratio][variance-to-mean-ratio]. The `window` parameter defines the number of values over which to compute the moving [variance-to-mean ratio][variance-to-mean-ratio]. ```javascript -var accumulator = incrmvmr( 3 ); +var accumulator = incrnanmvmr( 3 ); ``` If the mean is already known, provide a `mean` argument. ```javascript -var accumulator = incrmvmr( 3, 5.0 ); +var accumulator = incrnanmvmr( 3, 5.0 ); ``` #### accumulator( \[x] ) @@ -100,7 +100,7 @@ var accumulator = incrmvmr( 3, 5.0 ); 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 = incrmvmr( 3 ); +var accumulator = incrnanmvmr( 3 ); var F = accumulator(); // returns null @@ -134,8 +134,6 @@ F = accumulator(); ## Notes -- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **at least** `W-1` future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. - - As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. - The following table summarizes how to interpret the [variance-to-mean ratio][variance-to-mean-ratio]: @@ -165,18 +163,18 @@ F = accumulator(); ```javascript var randu = require( '@stdlib/random/base/randu' ); -var incrmvmr = require( '@stdlib/stats/incr/mvmr' ); +var incrnanmvmr = require( '@stdlib/stats/incr/nanmvmr' ); var accumulator; var v; var i; // Initialize an accumulator: -accumulator = incrmvmr( 5 ); +accumulator = incrnanmvmr( 5 ); // For each simulated datum, update the moving variance-to-mean ratio... for ( i = 0; i < 100; i++ ) { - v = randu() * 100.0; + v = randu()> 0.2 ? NaN : randu() * 100.0; accumulator( v ); } console.log( accumulator() ); From 313295d7640f3e578ba0d115d0ca5f34c5d69660 Mon Sep 17 00:00:00 2001 From: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Date: Wed, 16 Apr 2025 00:14:06 +0000 Subject: [PATCH 4/7] chore: update copyright years --- lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md | 2 +- .../@stdlib/stats/incr/nanmvmr/benchmark/benchmark.js | 2 +- .../@stdlib/stats/incr/nanmvmr/docs/types/index.d.ts | 2 +- lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/test.ts | 2 +- lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js | 2 +- lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/index.js | 2 +- lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js | 2 +- lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md b/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md index 3f42bd57b253..9101aa29d357 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2018 The Stdlib Authors. +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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/benchmark/benchmark.js index 1dd42be9132b..dc52a35a0c1d 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/index.d.ts index 85a577008363..eaee383b3bc1 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/index.d.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2019 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/test.ts index 3e51dc840d73..29577d71b496 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2019 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js index a7390545702b..ee588d8fbde2 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/index.js index 5726b8cae4fc..9f2efa7f6f99 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js index 018717636f81..4bcfaaaba73a 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js index 84f26e7480ed..552c09042223 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. From d108f52fcecad010efc6d77f2ca0fb5457c7bd79 Mon Sep 17 00:00:00 2001 From: Hemang Chodhary Date: Sat, 26 Apr 2025 01:16:13 +0530 Subject: [PATCH 5/7] [Fixed Lint Issues] --- .../@stdlib/stats/incr/nanmvmr/README.md | 2 +- .../@stdlib/stats/incr/nanmvmr/examples/index.js | 4 ++-- .../@stdlib/stats/incr/nanmvmr/lib/main.js | 16 +++++++++------- .../@stdlib/stats/incr/nanmvmr/test/test.js | 9 ++++----- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md b/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md index 9101aa29d357..d9be6a4479ed 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md @@ -174,7 +174,7 @@ accumulator = incrnanmvmr( 5 ); // For each simulated datum, update the moving variance-to-mean ratio... for ( i = 0; i < 100; i++ ) { - v = randu()> 0.2 ? NaN : randu() * 100.0; + v = ( randu()> 0.2 ) ? NaN : randu() * 100.0; accumulator( v ); } console.log( accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js index ee588d8fbde2..94266381974d 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js @@ -32,7 +32,7 @@ accumulator = incrnanmvmr( 5 ); // For each simulated datum, update the moving variance-to-mean ratio... console.log( '\nValue\tVMR\n' ); for ( i = 0; i < 100; i++ ) { - v = randu() < 0.2 ? NaN : randu() * 100.0; + v = ( randu() < 0.2 ) ? NaN : randu() * 100.0; F = accumulator( v ); - console.log( '%s\t%s', Number.isNaN(v) ? 'NaN' : v.toFixed( 4 ), Number.isNaN(F) ? 'NaN' : F.toFixed( 4 ) ); + console.log( '%s\t%s', ( Number.isNaN(v) ) ? 'NaN' : v.toFixed( 4 ), ( Number.isNaN(F) ) ? 'NaN' : F.toFixed( 4 ) ); } diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js index 4bcfaaaba73a..70d229c05a5f 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js @@ -21,7 +21,8 @@ // MODULES // var incrmvmr = require( '@stdlib/stats/incr/mvmr' ); -var isNaN = require( '@stdlib/math/base/assert/is-nan' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + // MAIN // @@ -89,7 +90,7 @@ var isNaN = require( '@stdlib/math/base/assert/is-nan' ); * * ```tex * \begin{align*} -* (N-1)(\Delta s^2) &= (x_N - x_0)(x_N + x_0) - (x_N - x_0)(\bar{x}_{i+1} + \bar{x}_{i}) +* (N-1)(\Delta s^2) &= (x_N - x_0)(x_N + x_0) - (x_N - x_0)(\bar{x}_{i+1} + \bar{x}_{i}) \\ * &= (x_N - x_0)(x_N + x_0 - \bar{x}_{i+1} - \bar{x}_{i}) \\ * &= (x_N - x_0)(x_N - \bar{x}_{i+1} + x_0 - \bar{x}_{i}) * \end{align*} @@ -133,16 +134,16 @@ var isNaN = require( '@stdlib/math/base/assert/is-nan' ); */ function incrnanmvmr( W, mean ) { var acc; - + // Initialize the wrapped accumulator: if ( arguments.length > 1 ) { acc = incrmvmr( W, mean ); } else { acc = incrmvmr( W ); } - + 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. * NaN input values are ignored. @@ -155,14 +156,15 @@ function incrnanmvmr( W, mean ) { if ( arguments.length === 0 ) { return acc(); } - if ( isNaN( x ) === false ) { + if ( isnan( x ) === false ) { return acc( x ); } - + return acc(); } } + // EXPORTS // module.exports = incrnanmvmr; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js index 552c09042223..7de2e459f9bc 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js @@ -23,7 +23,6 @@ var tape = require( 'tape' ); var randu = require( '@stdlib/random/base/randu' ); var abs = require( '@stdlib/math/base/special/abs' ); -var isnan = require( '@stdlib/math/base/assert/is-nan' ); var EPS = require( '@stdlib/constants/float64/eps' ); var incrnanmvmr = require( './../lib' ); @@ -155,9 +154,9 @@ tape( 'the accumulator function computes a moving variance-to-mean ratio increme acc = incrnanmvmr( 3 ); - actual = new Array( N ); + actual = []; for ( i = 0; i < N; i++ ) { - actual[ i ] = acc( data[ i ] ); + actual.push( acc( data[ i ] ) ); } t.deepEqual( actual, expected, 'returns expected results' ); t.end(); @@ -176,9 +175,9 @@ tape( 'the accumulator function computes a moving variance-to-mean ratio increme acc = incrnanmvmr( 3, 2.0 ); - actual = new Array( N ); + actual = []; for ( i = 0; i < N; i++ ) { - actual[ i ] = acc( data[ i ] ); + actual.push( acc( data[ i ] ) ); } expected = [ 0.0/2.0, From 377362367c6cd878d9a5fd1848b0c841f54ec96c Mon Sep 17 00:00:00 2001 From: Hemang Chodhary Date: Sat, 26 Apr 2025 02:20:05 +0530 Subject: [PATCH 6/7] [Fixed Lint Issues] --- lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js index 94266381974d..dce324a7dde2 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js @@ -34,5 +34,5 @@ console.log( '\nValue\tVMR\n' ); for ( i = 0; i < 100; i++ ) { v = ( randu() < 0.2 ) ? NaN : randu() * 100.0; F = accumulator( v ); - console.log( '%s\t%s', ( Number.isNaN(v) ) ? 'NaN' : v.toFixed( 4 ), ( Number.isNaN(F) ) ? 'NaN' : F.toFixed( 4 ) ); + console.log( '%s\t%s', ( Number.isNaN(v) ) ? 'NaN' : v.toFixed( 4 ), ( Number.isNaN(F) && F === null ) ? 'NaN' : F.toFixed( 4 ) ); } From d0b886760423915d0ae7f12440cade8f42e67ef5 Mon Sep 17 00:00:00 2001 From: Hemang Chodhary Date: Tue, 29 Apr 2025 18:07:08 +0530 Subject: [PATCH 7/7] [Fixed Issues] --- lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json b/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json index 45437903a7cd..2b1225f78deb 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/stats/incr/nanmvmr", "version": "0.0.0", - "description": "Compute a moving variance-to-mean ratio (VMR) incrementally.", + "description": "Compute a moving variance-to-mean ratio (VMR) incrementally, ignoring NaN values.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors",