README.md (6134B)
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 # 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 <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"> 32 <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."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 and the [arithmetic mean][arithmetic-mean] is defined as 39 40 <!-- <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."> --> 41 42 <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"> 43 <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."> 44 <br> 45 </div> 46 47 <!-- </equation> --> 48 49 The [variance-to-mean ratio][variance-to-mean-ratio] (VMR) is thus defined as 50 51 <!-- <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)."> --> 52 53 <div class="equation" align="center" data-raw-text="D = \frac{s^2}{\bar{x}}" data-equation="eq:variance_to_mean_ratio"> 54 <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)."> 55 <br> 56 </div> 57 58 <!-- </equation> --> 59 60 </section> 61 62 <!-- /.intro --> 63 64 <section class="usage"> 65 66 ## Usage 67 68 ```javascript 69 var incrvmr = require( '@stdlib/stats/incr/vmr' ); 70 ``` 71 72 #### incrvmr( \[mean] ) 73 74 Returns an accumulator `function` which incrementally computes a [variance-to-mean ratio][variance-to-mean-ratio]. 75 76 ```javascript 77 var accumulator = incrvmr(); 78 ``` 79 80 If the mean is already known, provide a `mean` argument. 81 82 ```javascript 83 var accumulator = incrvmr( 3.0 ); 84 ``` 85 86 #### accumulator( \[x] ) 87 88 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. 89 90 ```javascript 91 var accumulator = incrvmr(); 92 93 var D = accumulator( 2.0 ); 94 // returns 0.0 95 96 D = accumulator( 1.0 ); // => s^2 = ((2-1.5)^2+(1-1.5)^2) / (2-1) 97 // returns ~0.33 98 99 D = accumulator( 3.0 ); // => s^2 = ((2-2)^2+(1-2)^2+(3-2)^2) / (3-1) 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 provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **all** 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. 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 randu = require( '@stdlib/random/base/randu' ); 143 var incrvmr = require( '@stdlib/stats/incr/vmr' ); 144 145 var accumulator; 146 var v; 147 var i; 148 149 // Initialize an accumulator: 150 accumulator = incrvmr(); 151 152 // For each simulated datum, update the variance-to-mean ratio... 153 for ( i = 0; i < 100; i++ ) { 154 v = randu() * 100.0; 155 accumulator( v ); 156 } 157 console.log( accumulator() ); 158 ``` 159 160 </section> 161 162 <!-- /.examples --> 163 164 <section class="links"> 165 166 [variance-to-mean-ratio]: https://en.wikipedia.org/wiki/Index_of_dispersion 167 168 [arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean 169 170 [sample-variance]: https://en.wikipedia.org/wiki/Variance 171 172 </section> 173 174 <!-- /.links -->