README.md (3488B)
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 # incrvariance 22 23 > Compute an [unbiased sample variance][sample-variance] 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@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/variance/docs/img/equation_unbiased_sample_variance.svg" alt="Equation for the unbiased sample variance."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 </section> 39 40 <!-- /.intro --> 41 42 <section class="usage"> 43 44 ## Usage 45 46 ```javascript 47 var incrvariance = require( '@stdlib/stats/incr/variance' ); 48 ``` 49 50 #### incrvariance( \[mean] ) 51 52 Returns an accumulator `function` which incrementally computes an [unbiased sample variance][sample-variance]. 53 54 ```javascript 55 var accumulator = incrvariance(); 56 ``` 57 58 If the mean is already known, provide a `mean` argument. 59 60 ```javascript 61 var accumulator = incrvariance( 3.0 ); 62 ``` 63 64 #### accumulator( \[x] ) 65 66 If provided an input value `x`, the accumulator function returns an updated [unbiased sample variance][sample-variance]. If not provided an input value `x`, the accumulator function returns the current [unbiased sample variance][sample-variance]. 67 68 ```javascript 69 var accumulator = incrvariance(); 70 71 var s2 = accumulator( 2.0 ); 72 // returns 0.0 73 74 s2 = accumulator( 1.0 ); // => ((2-1.5)^2+(1-1.5)^2) / (2-1) 75 // returns 0.5 76 77 s2 = accumulator( 3.0 ); // => ((2-2)^2+(1-2)^2+(3-2)^2) / (3-1) 78 // returns 1.0 79 80 s2 = accumulator(); 81 // returns 1.0 82 ``` 83 84 </section> 85 86 <!-- /.usage --> 87 88 <section class="notes"> 89 90 ## Notes 91 92 - 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. 93 94 </section> 95 96 <!-- /.notes --> 97 98 <section class="examples"> 99 100 ## Examples 101 102 <!-- eslint no-undef: "error" --> 103 104 ```javascript 105 var randu = require( '@stdlib/random/base/randu' ); 106 var incrvariance = require( '@stdlib/stats/incr/variance' ); 107 108 var accumulator; 109 var v; 110 var i; 111 112 // Initialize an accumulator: 113 accumulator = incrvariance(); 114 115 // For each simulated datum, update the unbiased sample variance... 116 for ( i = 0; i < 100; i++ ) { 117 v = randu() * 100.0; 118 accumulator( v ); 119 } 120 console.log( accumulator() ); 121 ``` 122 123 </section> 124 125 <!-- /.examples --> 126 127 <section class="links"> 128 129 [sample-variance]: https://en.wikipedia.org/wiki/Variance 130 131 </section> 132 133 <!-- /.links -->