README.md (4242B)
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 # incrmstdev 22 23 > Compute a moving [corrected sample standard deviation][standard-deviation] incrementally. 24 25 <section class="intro"> 26 27 For a window of size `W`, the [corrected sample standard deviation][standard-deviation] is defined as 28 29 <!-- <equation class="equation" label="eq:corrected_sample_standard_deviation" align="center" raw="s = \sqrt{\frac{1}{W-1} \sum_{i=0}^{W-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}{W-1} \sum_{i=0}^{W-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/mstdev/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 incrmstdev = require( '@stdlib/stats/incr/mstdev' ); 48 ``` 49 50 #### incrmstdev( window\[, mean] ) 51 52 Returns an accumulator `function` which incrementally computes a moving [corrected sample standard deviation][standard-deviation]. The `window` parameter defines the number of values over which to compute the moving [corrected sample standard deviation][standard-deviation]. 53 54 ```javascript 55 var accumulator = incrmstdev( 3 ); 56 ``` 57 58 If the mean is already known, provide a `mean` argument. 59 60 ```javascript 61 var accumulator = incrmstdev( 3, 5.0 ); 62 ``` 63 64 #### accumulator( \[x] ) 65 66 If provided an input value `x`, the accumulator function returns an updated [corrected sample standard deviation][standard-deviation]. If not provided an input value `x`, the accumulator function returns the current [corrected sample standard deviation][standard-deviation]. 67 68 ```javascript 69 var accumulator = incrmstdev( 3 ); 70 71 var s = accumulator(); 72 // returns null 73 74 // Fill the window... 75 s = accumulator( 2.0 ); // [2.0] 76 // returns 0.0 77 78 s = accumulator( 1.0 ); // [2.0, 1.0] 79 // returns ~0.7071 80 81 s = accumulator( 3.0 ); // [2.0, 1.0, 3.0] 82 // returns 1.0 83 84 // Window begins sliding... 85 s = accumulator( -7.0 ); // [1.0, 3.0, -7.0] 86 // returns ~5.29 87 88 s = accumulator( -5.0 ); // [3.0, -7.0, -5.0] 89 // returns ~5.29 90 91 s = accumulator(); 92 // returns ~5.29 93 ``` 94 95 </section> 96 97 <!-- /.usage --> 98 99 <section class="notes"> 100 101 ## Notes 102 103 - 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. 104 - 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. 105 106 </section> 107 108 <!-- /.notes --> 109 110 <section class="examples"> 111 112 ## Examples 113 114 <!-- eslint no-undef: "error" --> 115 116 ```javascript 117 var randu = require( '@stdlib/random/base/randu' ); 118 var incrmstdev = require( '@stdlib/stats/incr/mstdev' ); 119 120 var accumulator; 121 var v; 122 var i; 123 124 // Initialize an accumulator: 125 accumulator = incrmstdev( 5 ); 126 127 // For each simulated datum, update the moving corrected sample standard deviation... 128 for ( i = 0; i < 100; i++ ) { 129 v = randu() * 100.0; 130 accumulator( v ); 131 } 132 console.log( accumulator() ); 133 ``` 134 135 </section> 136 137 <!-- /.examples --> 138 139 <section class="links"> 140 141 [standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation 142 143 </section> 144 145 <!-- /.links -->