Skip to content

Commit 717bdbb

Browse files
committed
feat: add nanvmr package
1 parent 13b7262 commit 717bdbb

File tree

13 files changed

+1023
-0
lines changed

13 files changed

+1023
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# incrnanvmr
22+
23+
> Compute a [variance-to-mean ratio][variance-to-mean-ratio] (VMR) incrementally, ignoring `NaN` values.
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/nanvmr/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/nanvmr/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean.">
52+
<br>
53+
</div> -->
54+
55+
<!-- </equation> -->
56+
57+
</section>
58+
59+
<!-- /.intro -->
60+
61+
<section class="usage">
62+
63+
## Usage
64+
65+
```javascript
66+
var incrnanvmr = require( '@stdlib/stats/incr/nanvmr' );
67+
```
68+
69+
#### incrnanvmr( \[mean] )
70+
71+
Returns an accumulator function which incrementally computes a [variance-to-mean ratio][variance-to-mean-ratio], ignoring `NaN` values.
72+
73+
```javascript
74+
var accumulator = incrnanvmr();
75+
```
76+
77+
If the mean is already known, provide a `mean` argument.
78+
79+
```javascript
80+
var accumulator = incrnanvmr( 3.0 );
81+
```
82+
83+
#### accumulator( \[x] )
84+
85+
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.
86+
87+
```javascript
88+
var accumulator = incrnanvmr();
89+
90+
var D = accumulator( 2.0 );
91+
// returns 0.0
92+
93+
D = accumulator( 1.0 ); // => s^2 = ((2-1.5)^2+(1-1.5)^2) / (2-1)
94+
// returns ~0.33
95+
96+
D = accumulator( 3.0 ); // => s^2 = ((2-2)^2+(1-2)^2+(3-2)^2) / (3-1)
97+
// returns 0.5
98+
99+
D = accumulator( NaN )
100+
// returns 0.5
101+
102+
D = accumulator();
103+
// returns 0.5
104+
```
105+
106+
</section>
107+
108+
<!-- /.usage -->
109+
110+
<section class="notes">
111+
112+
## Notes
113+
114+
- 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.
115+
116+
- The following table summarizes how to interpret the [variance-to-mean ratio][variance-to-mean-ratio]:
117+
118+
| VMR | Description | Example Distribution |
119+
| :---------------: | :-------------: | :--------------------------: |
120+
| 0 | not dispersed | constant |
121+
| 0 &lt; VMR &lt; 1 | under-dispersed | binomial |
122+
| 1 | -- | Poisson |
123+
| >1 | over-dispersed | geometric, negative-binomial |
124+
125+
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).
126+
127+
- 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.
128+
129+
- 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**.
130+
131+
</section>
132+
133+
<!-- /.notes -->
134+
135+
<section class="examples">
136+
137+
## Examples
138+
139+
<!-- eslint no-undef: "error" -->
140+
141+
```javascript
142+
var uniform = require( '@stdlib/random/base/uniform' );
143+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
144+
var incrnanvmr = require( '@stdlib/stats/incr/nanvmr' );
145+
146+
// Initialize an accumulator:
147+
var accumulator = incrnanvmr();
148+
149+
// For each simulated datum, update the variance-to-mean ratio...
150+
var v;
151+
var i;
152+
for ( i = 0; i < 100; i++ ) {
153+
accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0, 100.0 ) );
154+
}
155+
console.log( accumulator() );
156+
```
157+
158+
</section>
159+
160+
<!-- /.examples -->
161+
162+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
163+
164+
<section class="related">
165+
166+
</section>
167+
168+
<!-- /.related -->
169+
170+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
171+
172+
<section class="links">
173+
174+
[variance-to-mean-ratio]: https://en.wikipedia.org/wiki/Index_of_dispersion
175+
176+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
177+
178+
[sample-variance]: https://en.wikipedia.org/wiki/Variance
179+
180+
</section>
181+
182+
<!-- /.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) 2025 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)