|
| 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 < VMR < 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 --> |
0 commit comments