Skip to content

Commit 190af4f

Browse files
feat(nanvmr):add NaN-safe VMR accumulator
1 parent 363aead commit 190af4f

13 files changed

Lines changed: 1084 additions & 0 deletions

File tree

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# incrvmr
22+
23+
> Compute a [variance-to-mean ratio][variance-to-mean-ratio] (VMR) incrementally.
24+
25+
<section class="intro">
26+
27+
The [unbiased sample variance][sample-variance] is defined as
28+
29+
<!-- <equation class="equation" label="eq:unbiased_sample_variance" align="center" raw="s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2" alt="Equation for the unbiased sample variance."> -->
30+
31+
```math
32+
s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2" data-equation="eq:unbiased_sample_variance">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@7fe559e94716008fb414ec7c6b3d0e3e1194f2ba/lib/node_modules/@stdlib/stats/incr/vmr/docs/img/equation_unbiased_sample_variance.svg" alt="Equation for the unbiased sample variance.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
and the [arithmetic mean][arithmetic-mean] is defined as
43+
44+
<!-- <equation class="equation" label="eq:arithmetic_mean" align="center" raw="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the arithmetic mean."> -->
45+
46+
```math
47+
\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i
48+
```
49+
50+
<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:arithmetic_mean">
51+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@86f8c49b0e95ee794f0b098b8d17444c0cbeea0a/lib/node_modules/@stdlib/stats/incr/vmr/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean.">
52+
<br>
53+
</div> -->
54+
55+
<!-- </equation> -->
56+
57+
The [variance-to-mean ratio][variance-to-mean-ratio] (VMR) is thus defined as
58+
59+
<!-- <equation class="equation" label="eq:variance_to_mean_ratio" align="center" raw="D = \frac{s^2}{\bar{x}}" alt="Equation for the variance-to-mean ratio (VMR)."> -->
60+
61+
```math
62+
D = \frac{s^2}{\bar{x}}
63+
```
64+
65+
<!-- <div class="equation" align="center" data-raw-text="D = \frac{s^2}{\bar{x}}" data-equation="eq:variance_to_mean_ratio">
66+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@86f8c49b0e95ee794f0b098b8d17444c0cbeea0a/lib/node_modules/@stdlib/stats/incr/vmr/docs/img/equation_variance_to_mean_ratio.svg" alt="Equation for the variance-to-mean ratio (VMR).">
67+
<br>
68+
</div> -->
69+
70+
<!-- </equation> -->
71+
72+
</section>
73+
74+
<!-- /.intro -->
75+
76+
<section class="usage">
77+
78+
## Usage
79+
80+
```javascript
81+
var incrnanvmr = require( '@stdlib/stats/incr/nanvmr' );
82+
```
83+
84+
#### incrvmr( \[mean] )
85+
86+
Returns an accumulator `function` which incrementally computes a [variance-to-mean ratio][variance-to-mean-ratio].
87+
88+
```javascript
89+
var accumulator = incrnanvmr();
90+
```
91+
92+
If the mean is already known, provide a `mean` argument.
93+
94+
```javascript
95+
var accumulator = incrnanvmr( 3.0 );
96+
```
97+
98+
#### accumulator( \[x] )
99+
100+
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.
101+
102+
```javascript
103+
var accumulator = incrnanvmr();
104+
105+
var D = accumulator( 2.0 );
106+
// returns 0.0
107+
108+
D = accumulator( NaN );
109+
// returns 0.0
110+
111+
D = accumulator( 1.0 ); // => s^2 = ((2-1.5)^2+(1-1.5)^2) / (2-1)
112+
// returns ~0.33
113+
114+
D = accumulator( 3.0 ); // => s^2 = ((2-2)^2+(1-2)^2+(3-2)^2) / (3-1)
115+
// returns 0.5
116+
117+
D = accumulator();
118+
// returns 0.5
119+
```
120+
121+
</section>
122+
123+
<!-- /.usage -->
124+
125+
<section class="notes">
126+
127+
## Notes
128+
129+
- 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.
130+
131+
- The following table summarizes how to interpret the [variance-to-mean ratio][variance-to-mean-ratio]:
132+
133+
| VMR | Description | Example Distribution |
134+
| :---------------: | :-------------: | :--------------------------: |
135+
| 0 | not dispersed | constant |
136+
| 0 &lt; VMR &lt; 1 | under-dispersed | binomial |
137+
| 1 | -- | Poisson |
138+
| >1 | over-dispersed | geometric, negative-binomial |
139+
140+
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).
141+
142+
- 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.
143+
144+
- 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**.
145+
146+
</section>
147+
148+
<!-- /.notes -->
149+
150+
<section class="examples">
151+
152+
## Examples
153+
154+
<!-- eslint no-undef: "error" -->
155+
156+
```javascript
157+
var randu = require( '@stdlib/random/base/randu' );
158+
var incrnanvmr = require( '@stdlib/stats/incr/nanvmr' );
159+
160+
var accumulator;
161+
var v;
162+
var i;
163+
164+
// Initialize an accumulator:
165+
accumulator = incrnanvmr();
166+
167+
// For each simulated datum, update the variance-to-mean ratio...
168+
for ( i = 0; i < 100; i++ ) {
169+
if ( randu() < 0.2 ) { // 20% chance of NaN
170+
v = NaN;
171+
} else {
172+
v = randu() * 100.0;
173+
}
174+
accumulator( v ); // ignores NaN values
175+
}
176+
console.log( accumulator() );
177+
```
178+
179+
</section>
180+
181+
<!-- /.examples -->
182+
183+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
184+
185+
<section class="related">
186+
187+
* * *
188+
189+
## See Also
190+
191+
- <span class="package-name">[`@stdlib/stats/incr/mean`][@stdlib/stats/incr/mean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally.</span>
192+
- <span class="package-name">[`@stdlib/stats/incr/mvmr`][@stdlib/stats/incr/mvmr]</span><span class="delimiter">: </span><span class="description">compute a moving variance-to-mean ratio (VMR) incrementally.</span>
193+
- <span class="package-name">[`@stdlib/stats/incr/variance`][@stdlib/stats/incr/variance]</span><span class="delimiter">: </span><span class="description">compute an unbiased sample variance incrementally.</span>
194+
195+
</section>
196+
197+
<!-- /.related -->
198+
199+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
200+
201+
<section class="links">
202+
203+
[variance-to-mean-ratio]: https://en.wikipedia.org/wiki/Index_of_dispersion
204+
205+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
206+
207+
[sample-variance]: https://en.wikipedia.org/wiki/Variance
208+
209+
<!-- <related-links> -->
210+
211+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
212+
213+
[@stdlib/stats/incr/mvmr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mvmr
214+
215+
[@stdlib/stats/incr/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/variance
216+
217+
<!-- </related-links> -->
218+
219+
</section>
220+
221+
<!-- /.links -->
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrnanvmr = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnanvmr();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanvmr();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
70+
71+
bench( pkg+'::accumulator,known_mean', function benchmark( b ) {
72+
var acc;
73+
var v;
74+
var i;
75+
76+
acc = incrnanvmr( 3.14 );
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
v = acc( randu() );
81+
if ( v !== v ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
}
85+
b.toc();
86+
if ( v !== v ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
});
Lines changed: 43 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)