|
| 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 | +# incrnanewstdev |
| 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 | +```math |
| 32 | +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} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <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"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@b6bfc5be3086b5ddfeed2311afee7c9201fbdcbb/lib/node_modules/@stdlib/stats/incr/nanewstdev/docs/img/equation_exponentially_weighted_variance.svg" alt="Recursive definition for computing an exponentially weighted variance."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +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]. |
| 43 | + |
| 44 | +</section> |
| 45 | + |
| 46 | +<!-- /.intro --> |
| 47 | + |
| 48 | +<section class="usage"> |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +```javascript |
| 53 | +var incrnanewstdev = require( '@stdlib/stats/incr/nanewstdev' ); |
| 54 | +``` |
| 55 | + |
| 56 | +#### incrnanewstdev( alpha ) |
| 57 | + |
| 58 | +Returns an accumulator `function` which incrementally computes an [exponentially weighted standard deviation][moving-average], where `alpha` is a smoothing factor between `0` and `1`. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var accumulator = incrnanewstdev( 0.5 ); |
| 62 | +``` |
| 63 | + |
| 64 | +#### accumulator( \[x] ) |
| 65 | + |
| 66 | +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. |
| 67 | + |
| 68 | +```javascript |
| 69 | +var accumulator = incrnanewstdev( 0.5 ); |
| 70 | + |
| 71 | +var s = accumulator(); |
| 72 | +// returns null |
| 73 | + |
| 74 | +s = accumulator( 2.0 ); |
| 75 | +// returns 0.0 |
| 76 | + |
| 77 | +s = accumulator( 1.0 ); |
| 78 | +// returns 0.5 |
| 79 | + |
| 80 | +s = accumulator( NaN ); |
| 81 | +// returns 0.5 |
| 82 | + |
| 83 | +s = accumulator( 3.0 ); |
| 84 | +// returns ~0.83 |
| 85 | + |
| 86 | +s = accumulator(); |
| 87 | +// returns ~0.83 |
| 88 | +``` |
| 89 | + |
| 90 | +</section> |
| 91 | + |
| 92 | +<!-- /.usage --> |
| 93 | + |
| 94 | +<section class="notes"> |
| 95 | + |
| 96 | +## Notes |
| 97 | + |
| 98 | +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, it will be **ignored** but you are advised to type check and handle accordingly **before** passing the value to the accumulator function. |
| 99 | + |
| 100 | +</section> |
| 101 | + |
| 102 | +<!-- /.notes --> |
| 103 | + |
| 104 | +<section class="examples"> |
| 105 | + |
| 106 | +## Examples |
| 107 | + |
| 108 | +<!-- eslint no-undef: "error" --> |
| 109 | + |
| 110 | +```javascript |
| 111 | +var randu = require( '@stdlib/random/base/randu' ); |
| 112 | +var incrnanewstdev = require( './../lib' ); |
| 113 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 114 | + |
| 115 | +var accumulator; |
| 116 | +var s; |
| 117 | +var v; |
| 118 | +var i; |
| 119 | + |
| 120 | +// Initialize an accumulator: |
| 121 | +accumulator = incrnanewstdev( 0.5 ); |
| 122 | + |
| 123 | +// For each simulated datum, update the exponentially weighted standard deviation... |
| 124 | +for ( i = 0; i < 100; i++ ) { |
| 125 | + v = (randu() < 0.1) ? NaN : randu() * 100.0; |
| 126 | + s = accumulator( v ); |
| 127 | +} |
| 128 | +console.log( accumulator() ); |
| 129 | +``` |
| 130 | + |
| 131 | +</section> |
| 132 | + |
| 133 | +<!-- /.examples --> |
| 134 | + |
| 135 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 136 | + |
| 137 | +<section class="related"> |
| 138 | + |
| 139 | +* * * |
| 140 | + |
| 141 | +## See Also |
| 142 | + |
| 143 | +- <span class="package-name">[`@stdlib/stats/incr/ewvariance`][@stdlib/stats/incr/ewvariance]</span><span class="delimiter">: </span><span class="description">compute an exponentially weighted variance incrementally.</span> |
| 144 | +- <span class="package-name">[`@stdlib/stats/incr/mstdev`][@stdlib/stats/incr/mstdev]</span><span class="delimiter">: </span><span class="description">compute a moving corrected sample standard deviation incrementally.</span> |
| 145 | +- <span class="package-name">[`@stdlib/stats/incr/stdev`][@stdlib/stats/incr/stdev]</span><span class="delimiter">: </span><span class="description">compute a corrected sample standard deviation incrementally.</span> |
| 146 | + |
| 147 | +</section> |
| 148 | + |
| 149 | +<!-- /.related --> |
| 150 | + |
| 151 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 152 | + |
| 153 | +<section class="links"> |
| 154 | + |
| 155 | +[moving-average]: https://en.wikipedia.org/wiki/Moving_average |
| 156 | + |
| 157 | +[@stdlib/stats/incr/ewmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/ewmean |
| 158 | + |
| 159 | +<!-- <related-links> --> |
| 160 | + |
| 161 | +[@stdlib/stats/incr/ewvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/ewvariance |
| 162 | + |
| 163 | +[@stdlib/stats/incr/mstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mstdev |
| 164 | + |
| 165 | +[@stdlib/stats/incr/stdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/stdev |
| 166 | + |
| 167 | +<!-- </related-links> --> |
| 168 | + |
| 169 | +</section> |
| 170 | + |
| 171 | +<!-- /.links --> |
0 commit comments