README.md (3965B)
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 # increwstdev 22 23 > Compute an [exponentially weighted standard deviation][moving-average] incrementally. 24 25 <section class="intro"> 26 27 An [exponentially weighted variance][moving-average] can be defined recursively as 28 29 <!-- <equation class="equation" label="eq:exponentially_weighted_variance" align="center" raw="S_n = \begin{cases} 0 & \textrm{if}\ n = 0 \\ (1 - \alpha) (S_{n-1} + \alpha(x_n - \mu_{n-1})^2) & \textrm{if}\ n > 0 \end{cases}" alt="Recursive definition for computing an exponentially weighted variance."> --> 30 31 <div class="equation" align="center" data-raw-text="S_n = \begin{cases} 0 & \textrm{if}\ n = 0 \\ (1 - \alpha) (S_{n-1} + \alpha(x_n - \mu_{n-1})^2) & \textrm{if}\ n > 0 \end{cases}" data-equation="eq:exponentially_weighted_variance"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@b6bfc5be3086b5ddfeed2311afee7c9201fbdcbb/lib/node_modules/@stdlib/stats/incr/ewstdev/docs/img/equation_exponentially_weighted_variance.svg" alt="Recursive definition for computing an exponentially weighted variance."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 where `μ` is the [exponentially weighted mean][@stdlib/stats/incr/ewmean]. The [exponentially weighted standard deviation][moving-average] is the square root of the [exponentially weighted variance][moving-average]. 39 40 </section> 41 42 <!-- /.intro --> 43 44 <section class="usage"> 45 46 ## Usage 47 48 ```javascript 49 var increwstdev = require( '@stdlib/stats/incr/ewstdev' ); 50 ``` 51 52 #### increwstdev( alpha ) 53 54 Returns an accumulator `function` which incrementally computes an [exponentially weighted standard deviation][moving-average], where `alpha` is a smoothing factor between `0` and `1`. 55 56 ```javascript 57 var accumulator = increwstdev( 0.5 ); 58 ``` 59 60 #### accumulator( \[x] ) 61 62 If provided an input value `x`, the accumulator function returns an updated standard deviation. If not provided an input value `x`, the accumulator function returns the current standard deviation. 63 64 ```javascript 65 var accumulator = increwstdev( 0.5 ); 66 67 var s = accumulator(); 68 // returns null 69 70 s = accumulator( 2.0 ); 71 // returns 0.0 72 73 s = accumulator( 1.0 ); 74 // returns 0.5 75 76 s = accumulator( 3.0 ); 77 // returns ~0.83 78 79 s = accumulator(); 80 // returns ~0.83 81 ``` 82 83 </section> 84 85 <!-- /.usage --> 86 87 <section class="notes"> 88 89 ## Notes 90 91 - 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. 92 93 </section> 94 95 <!-- /.notes --> 96 97 <section class="examples"> 98 99 ## Examples 100 101 <!-- eslint no-undef: "error" --> 102 103 ```javascript 104 var randu = require( '@stdlib/random/base/randu' ); 105 var increwstdev = require( '@stdlib/stats/incr/ewstdev' ); 106 107 var accumulator; 108 var v; 109 var i; 110 111 // Initialize an accumulator: 112 accumulator = increwstdev( 0.5 ); 113 114 // For each simulated datum, update the exponentially weighted standard deviation... 115 for ( i = 0; i < 100; i++ ) { 116 v = randu() * 100.0; 117 accumulator( v ); 118 } 119 console.log( accumulator() ); 120 ``` 121 122 </section> 123 124 <!-- /.examples --> 125 126 <section class="links"> 127 128 [moving-average]: https://en.wikipedia.org/wiki/Moving_average 129 130 [@stdlib/stats/incr/ewmean]: https://www.npmjs.com/package/@stdlib/stats/tree/main/incr/ewmean 131 132 </section> 133 134 <!-- /.links -->