|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# incrnanewmean |
| 22 | + |
| 23 | +> Compute an [exponentially weighted mean][moving-average] incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +An [exponentially weighted mean][moving-average] can be defined recursively as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:exponentially_weighted_mean" align="center" raw="\mu_t = \begin{cases} x_0 & \textrm{if}\ t = 0 \\ \alpha x_t + (1-\alpha) \mu_{t-1} & \textrm{if}\ t > 0 \end{cases}" alt="Recursive definition for computing an exponentially weighted mean."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\mu_t = \begin{cases} x_0 & \textrm{if}\ t = 0 \\ \alpha x_t + (1-\alpha) \mu_{t-1} & \textrm{if}\ t > 0 \end{cases} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\mu_t = \begin{cases} x_0 & \textrm{if}\ t = 0 \\ \alpha x_t + (1-\alpha) \mu_{t-1} & \textrm{if}\ t > 0 \end{cases}" data-equation="eq:exponentially_weighted_mean"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@1445ad5c454bc3c1a86bde2be87d6cec87781174/lib/node_modules/@stdlib/stats/incr/ewmean/docs/img/equation_exponentially_weighted_mean.svg" alt="Recursive definition for computing an exponentially weighted mean."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +</section> |
| 43 | + |
| 44 | +<!-- /.intro --> |
| 45 | + |
| 46 | +<section class="usage"> |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +```javascript |
| 51 | +var incrnanewmean = require( '@stdlib/stats/incr/nanewmean' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### incrnanewmean( alpha ) |
| 55 | + |
| 56 | +Returns an accumulator `function` which incrementally computes an [exponentially weighted mean][moving-average], where `alpha` is a smoothing factor between `0` and `1`, ignoring `NaN` values. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var accumulator = incrnanewmean( 0.5 ); |
| 60 | +``` |
| 61 | + |
| 62 | +#### accumulator( \[x] ) |
| 63 | + |
| 64 | +If provided an input value `x`, the accumulator function returns an updated mean. If not provided an input value `x`, the accumulator function returns the current mean. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var accumulator = incrnanewmean( 0.5 ); |
| 68 | + |
| 69 | +var v = accumulator(); |
| 70 | +// returns null |
| 71 | + |
| 72 | +v = accumulator( 2.0 ); |
| 73 | +// returns 2.0 |
| 74 | + |
| 75 | +v = accumulator( 1.0 ); |
| 76 | +// returns 1.5 |
| 77 | + |
| 78 | +v = accumulator( NaN ); |
| 79 | +// returns 1.5 |
| 80 | + |
| 81 | +v = accumulator( 3.0 ); |
| 82 | +// returns 2.25 |
| 83 | + |
| 84 | +v = accumulator(); |
| 85 | +// returns 2.25 |
| 86 | +``` |
| 87 | + |
| 88 | +</section> |
| 89 | + |
| 90 | +<!-- /.usage --> |
| 91 | + |
| 92 | +<section class="notes"> |
| 93 | + |
| 94 | +## Notes |
| 95 | + |
| 96 | +- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. |
| 97 | + |
| 98 | +</section> |
| 99 | + |
| 100 | +<!-- /.notes --> |
| 101 | + |
| 102 | +<section class="examples"> |
| 103 | + |
| 104 | +## Examples |
| 105 | + |
| 106 | +<!-- eslint no-undef: "error" --> |
| 107 | + |
| 108 | +```javascript |
| 109 | +var randu = require( '@stdlib/random/base/randu' ); |
| 110 | +var incrnanewmean = require( '@stdlib/stats/incr/nanewmean' ); |
| 111 | + |
| 112 | +var accumulator; |
| 113 | +var v; |
| 114 | +var i; |
| 115 | + |
| 116 | +// Initialize an accumulator: |
| 117 | +accumulator = incrnanewmean( 0.5 ); |
| 118 | + |
| 119 | +// For each simulated datum, update the exponentially weighted mean... |
| 120 | +for ( i = 0; i < 100; i++ ) { |
| 121 | + if ( randu() < 0.2 ) { |
| 122 | + v = NaN; |
| 123 | + } else { |
| 124 | + v = randu() * 100.0; |
| 125 | + } |
| 126 | + 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/mean`][@stdlib/stats/incr/mean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally.</span> |
| 145 | +- <span class="package-name">[`@stdlib/stats/incr/mmean`][@stdlib/stats/incr/mmean]</span><span class="delimiter">: </span><span class="description">compute a moving arithmetic mean incrementally.</span> |
| 146 | +- <span class="package-name">[`@stdlib/stats/incr/wmean`][@stdlib/stats/incr/wmean]</span><span class="delimiter">: </span><span class="description">compute a weighted arithmetic mean incrementally.</span> |
| 147 | + |
| 148 | +</section> |
| 149 | + |
| 150 | +<!-- /.related --> |
| 151 | + |
| 152 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 153 | + |
| 154 | +<section class="links"> |
| 155 | + |
| 156 | +[moving-average]: https://en.wikipedia.org/wiki/Moving_average |
| 157 | + |
| 158 | +<!-- <related-links> --> |
| 159 | + |
| 160 | +[@stdlib/stats/incr/ewvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/ewvariance |
| 161 | + |
| 162 | +[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean |
| 163 | + |
| 164 | +[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean |
| 165 | + |
| 166 | +[@stdlib/stats/incr/wmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/wmean |
| 167 | + |
| 168 | +<!-- </related-links> --> |
| 169 | + |
| 170 | +</section> |
| 171 | + |
| 172 | +<!-- /.links --> |
0 commit comments