README.md (3578B)
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 # incrstdev 22 23 > Compute a [corrected sample standard deviation][sample-stdev] incrementally. 24 25 <section class="intro"> 26 27 The [corrected sample standard deviation][sample-stdev] is defined as 28 29 <!-- <equation class="equation" label="eq:corrected_sample_standard_deviation" align="center" raw="s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2}" alt="Equation for the corrected sample standard deviation."> --> 30 31 <div class="equation" align="center" data-raw-text="s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2}" data-equation="eq:corrected_sample_standard_deviation"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/stdev/docs/img/equation_corrected_sample_standard_deviation.svg" alt="Equation for the corrected sample standard deviation."> 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 incrstdev = require( '@stdlib/stats/incr/stdev' ); 48 ``` 49 50 #### incrstdev( \[mean] ) 51 52 Returns an accumulator `function` which incrementally computes a [corrected sample standard deviation][sample-stdev]. 53 54 ```javascript 55 var accumulator = incrstdev(); 56 ``` 57 58 If the mean is already known, provide a `mean` argument. 59 60 ```javascript 61 var accumulator = incrstdev( 3.0 ); 62 ``` 63 64 #### accumulator( \[x] ) 65 66 If provided an input value `x`, the accumulator function returns an updated [corrected sample standard deviation][sample-stdev]. If not provided an input value `x`, the accumulator function returns the current [corrected sample standard deviation][sample-stdev]. 67 68 ```javascript 69 var accumulator = incrstdev(); 70 71 var s = accumulator( 2.0 ); 72 // returns 0.0 73 74 s = accumulator( 1.0 ); // => sqrt(((2-1.5)^2+(1-1.5)^2) / (2-1)) 75 // returns ~0.7071 76 77 s = accumulator( 3.0 ); // => sqrt(((2-2)^2+(1-2)^2+(3-2)^2) / (3-1)) 78 // returns 1.0 79 80 s = 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 incrstdev = require( '@stdlib/stats/incr/stdev' ); 107 108 var accumulator; 109 var v; 110 var i; 111 112 // Initialize an accumulator: 113 accumulator = incrstdev(); 114 115 // For each simulated datum, update the sample standard deviation... 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-stdev]: https://en.wikipedia.org/wiki/Standard_deviation 130 131 </section> 132 133 <!-- /.links -->