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..d9be6a4479ed --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/README.md @@ -0,0 +1,227 @@ + + +# incrnanmvmr + +> 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 incrnanmvmr = require( '@stdlib/stats/incr/nanmvmr' ); +``` + +#### 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 = incrnanmvmr( 3 ); +``` + +If the mean is already known, provide a `mean` argument. + +```javascript +var accumulator = incrnanmvmr( 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 = incrnanmvmr( 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 + +- 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 incrnanmvmr = require( '@stdlib/stats/incr/nanmvmr' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +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; + 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..dc52a35a0c1d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/benchmark/benchmark.js @@ -0,0 +1,91 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var 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..eaee383b3bc1 --- /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) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided a value, the accumulator function returns an updated accumulated value. If not provided a value, the accumulator function returns the current accumulated value. +* +* ## 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..29577d71b496 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/docs/types/test.ts @@ -0,0 +1,77 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import 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..dce324a7dde2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var 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) && F === null ) ? '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..9f2efa7f6f99 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/index.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute a 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..70d229c05a5f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/lib/main.js @@ -0,0 +1,170 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var 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..2b1225f78deb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/stats/incr/nanmvmr", + "version": "0.0.0", + "description": "Compute a moving 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", + "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..7de2e459f9bc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmvmr/test/test.js @@ -0,0 +1,534 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var randu = require( '@stdlib/random/base/randu' ); +var abs = require( '@stdlib/math/base/special/abs' ); +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 = []; + for ( i = 0; i < N; i++ ) { + actual.push( 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 = []; + for ( i = 0; i < N; i++ ) { + actual.push( 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(); +});