README.md (3519B)
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 # increwmean 22 23 > Compute an [exponentially weighted mean][moving-average] incrementally. 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 <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"> 32 <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."> 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 increwmean = require( '@stdlib/stats/incr/ewmean' ); 48 ``` 49 50 #### increwmean( alpha ) 51 52 Returns an accumulator `function` which incrementally computes an [exponentially weighted mean][moving-average], where `alpha` is a smoothing factor between `0` and `1`. 53 54 ```javascript 55 var accumulator = increwmean( 0.5 ); 56 ``` 57 58 #### accumulator( \[x] ) 59 60 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. 61 62 ```javascript 63 var accumulator = increwmean( 0.5 ); 64 65 var v = accumulator(); 66 // returns null 67 68 v = accumulator( 2.0 ); 69 // returns 2.0 70 71 v = accumulator( 1.0 ); 72 // returns 1.5 73 74 v = accumulator( 3.0 ); 75 // returns 2.25 76 77 v = accumulator(); 78 // returns 2.25 79 ``` 80 81 </section> 82 83 <!-- /.usage --> 84 85 <section class="notes"> 86 87 ## Notes 88 89 - 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. 90 91 </section> 92 93 <!-- /.notes --> 94 95 <section class="examples"> 96 97 ## Examples 98 99 <!-- eslint no-undef: "error" --> 100 101 ```javascript 102 var randu = require( '@stdlib/random/base/randu' ); 103 var increwmean = require( '@stdlib/stats/incr/ewmean' ); 104 105 var accumulator; 106 var v; 107 var i; 108 109 // Initialize an accumulator: 110 accumulator = increwmean( 0.5 ); 111 112 // For each simulated datum, update the exponentially weighted mean... 113 for ( i = 0; i < 100; i++ ) { 114 v = randu() * 100.0; 115 accumulator( v ); 116 } 117 console.log( accumulator() ); 118 ``` 119 120 </section> 121 122 <!-- /.examples --> 123 124 <section class="links"> 125 126 [moving-average]: https://en.wikipedia.org/wiki/Moving_average 127 128 </section> 129 130 <!-- /.links -->