|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2018 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 | +# incrmvmr |
| 22 | + |
| 23 | +> Compute a moving [variance-to-mean ratio][variance-to-mean-ratio] (VMR) incrementally. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +For a window of size `W`, 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}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2" alt="Equation for the unbiased sample variance."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +s^2 = \frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2 |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="s^2 = \frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2" data-equation="eq:unbiased_sample_variance"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@b331e5634fe726ff0e16e87814ac3f85d8164d31/lib/node_modules/@stdlib/stats/incr/mvmr/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}{W} \sum_{i=0}^{W-1} x_i" alt="Equation for the arithmetic mean."> --> |
| 45 | + |
| 46 | +```math |
| 47 | +\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} x_i |
| 48 | +``` |
| 49 | + |
| 50 | +<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} x_i" data-equation="eq:arithmetic_mean"> |
| 51 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@164b8f1010c4535340eed9ad0b2af32c4a19863c/lib/node_modules/@stdlib/stats/incr/mvmr/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="F = \frac{s^2}{\bar{x}}" alt="Equation for the variance-to-mean ratio (VMR)."> --> |
| 60 | + |
| 61 | +```math |
| 62 | +F = \frac{s^2}{\bar{x}} |
| 63 | +``` |
| 64 | + |
| 65 | +<!-- <div class="equation" align="center" data-raw-text="F = \frac{s^2}{\bar{x}}" data-equation="eq:variance_to_mean_ratio"> |
| 66 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@b331e5634fe726ff0e16e87814ac3f85d8164d31/lib/node_modules/@stdlib/stats/incr/mvmr/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 incrmvmr = require( '@stdlib/stats/incr/mvmr' ); |
| 82 | +``` |
| 83 | + |
| 84 | +#### incrmvmr( window\[, mean] ) |
| 85 | + |
| 86 | +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]. |
| 87 | + |
| 88 | +```javascript |
| 89 | +var accumulator = incrmvmr( 3 ); |
| 90 | +``` |
| 91 | + |
| 92 | +If the mean is already known, provide a `mean` argument. |
| 93 | + |
| 94 | +```javascript |
| 95 | +var accumulator = incrmvmr( 3, 5.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 = incrmvmr( 3 ); |
| 104 | + |
| 105 | +var F = accumulator(); |
| 106 | +// returns null |
| 107 | + |
| 108 | +// Fill the window... |
| 109 | +F = accumulator( 2.0 ); // [2.0] |
| 110 | +// returns 0.0 |
| 111 | + |
| 112 | +F = accumulator( 1.0 ); // [2.0, 1.0] |
| 113 | +// returns ~0.33 |
| 114 | + |
| 115 | +F = accumulator( 3.0 ); // [2.0, 1.0, 3.0] |
| 116 | +// returns 0.5 |
| 117 | + |
| 118 | +// Window begins sliding... |
| 119 | +F = accumulator( 7.0 ); // [1.0, 3.0, 7.0] |
| 120 | +// returns ~2.55 |
| 121 | + |
| 122 | +F = accumulator( 5.0 ); // [3.0, 7.0, 5.0] |
| 123 | +// returns ~0.80 |
| 124 | + |
| 125 | +F = accumulator(); |
| 126 | +// returns ~0.80 |
| 127 | +``` |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.usage --> |
| 132 | + |
| 133 | +<section class="notes"> |
| 134 | + |
| 135 | +## Notes |
| 136 | + |
| 137 | +- 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. |
| 138 | + |
| 139 | +- 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. |
| 140 | + |
| 141 | +- The following table summarizes how to interpret the [variance-to-mean ratio][variance-to-mean-ratio]: |
| 142 | + |
| 143 | + | VMR | Description | Example Distribution | |
| 144 | + | :---------------: | :-------------: | :--------------------------: | |
| 145 | + | 0 | not dispersed | constant | |
| 146 | + | 0 < VMR < 1 | under-dispersed | binomial | |
| 147 | + | 1 | -- | Poisson | |
| 148 | + | >1 | over-dispersed | geometric, negative-binomial | |
| 149 | + |
| 150 | + 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). |
| 151 | + |
| 152 | +- 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. |
| 153 | + |
| 154 | +- 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]. |
| 155 | + |
| 156 | +</section> |
| 157 | + |
| 158 | +<!-- /.notes --> |
| 159 | + |
| 160 | +<section class="examples"> |
| 161 | + |
| 162 | +## Examples |
| 163 | + |
| 164 | +<!-- eslint no-undef: "error" --> |
| 165 | + |
| 166 | +```javascript |
| 167 | +var randu = require( '@stdlib/random/base/randu' ); |
| 168 | +var incrmvmr = require( '@stdlib/stats/incr/mvmr' ); |
| 169 | + |
| 170 | +var accumulator; |
| 171 | +var v; |
| 172 | +var i; |
| 173 | + |
| 174 | +// Initialize an accumulator: |
| 175 | +accumulator = incrmvmr( 5 ); |
| 176 | + |
| 177 | +// For each simulated datum, update the moving variance-to-mean ratio... |
| 178 | +for ( i = 0; i < 100; i++ ) { |
| 179 | + v = randu() * 100.0; |
| 180 | + accumulator( v ); |
| 181 | +} |
| 182 | +console.log( accumulator() ); |
| 183 | +``` |
| 184 | + |
| 185 | +</section> |
| 186 | + |
| 187 | +<!-- /.examples --> |
| 188 | + |
| 189 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 190 | + |
| 191 | +<section class="related"> |
| 192 | + |
| 193 | +* * * |
| 194 | + |
| 195 | +## See Also |
| 196 | + |
| 197 | +- <span class="package-name">[`@stdlib/stats/incr/mmean`][@stdlib/stats/incr/mmean]</span><span class="delimiter">: </span><span class="description">compute a moving arithmetic mean incrementally.</span> |
| 198 | +- <span class="package-name">[`@stdlib/stats/incr/mvariance`][@stdlib/stats/incr/mvariance]</span><span class="delimiter">: </span><span class="description">compute a moving unbiased sample variance incrementally.</span> |
| 199 | +- <span class="package-name">[`@stdlib/stats/incr/vmr`][@stdlib/stats/incr/vmr]</span><span class="delimiter">: </span><span class="description">compute a variance-to-mean ratio (VMR) incrementally.</span> |
| 200 | + |
| 201 | +</section> |
| 202 | + |
| 203 | +<!-- /.related --> |
| 204 | + |
| 205 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 206 | + |
| 207 | +<section class="links"> |
| 208 | + |
| 209 | +[variance-to-mean-ratio]: https://en.wikipedia.org/wiki/Index_of_dispersion |
| 210 | + |
| 211 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 212 | + |
| 213 | +[sample-variance]: https://en.wikipedia.org/wiki/Variance |
| 214 | + |
| 215 | +[fano-factor]: https://en.wikipedia.org/wiki/Fano_factor |
| 216 | + |
| 217 | +<!-- <related-links> --> |
| 218 | + |
| 219 | +[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean |
| 220 | + |
| 221 | +[@stdlib/stats/incr/mvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mvariance |
| 222 | + |
| 223 | +[@stdlib/stats/incr/vmr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/vmr |
| 224 | + |
| 225 | +<!-- </related-links> --> |
| 226 | + |
| 227 | +</section> |
| 228 | + |
| 229 | +<!-- /.links --> |
0 commit comments