|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 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 | +# incrnanmme |
| 22 | + |
| 23 | +> Compute a moving [mean error][mean-absolute-error] (ME) incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +For a window of size `W`, the [mean error][mean-absolute-error] is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:mean_error" align="center" raw="\mathop{\mathrm{ME}} = \frac{1}{W} \sum_{i=0}^{W-1} (y_i - x_i)" alt="Equation for the mean error."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\mathop{\mathrm{ME}} = \frac{1}{W} \sum_{i=0}^{W-1} (y_i - x_i) |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\operatorname{ME} = \frac{1}{W} \sum_{i=0}^{W-1} (y_i - x_i)" data-equation="eq:mean_error"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@634ac3689760e2f57fd51085f387d8dc5bb3b927/lib/node_modules/@stdlib/stats/incr/nanmme/docs/img/equation_mean_error.svg" alt="Equation for the mean error."> |
| 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 incrnanmme = require( '@stdlib/stats/incr/nanmme' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### incrnanmme( window ) |
| 55 | + |
| 56 | +Returns an accumulator `function` which incrementally computes a moving [mean error][mean-absolute-error], ignoring any `(x, y)` pairs containing `NaN`. The `window` parameter defines the number of values over which to compute the moving [mean error][mean-absolute-error]. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var accumulator = incrnanmme( 3 ); |
| 60 | +``` |
| 61 | + |
| 62 | +#### accumulator( \[x, y] ) |
| 63 | + |
| 64 | +If provided input values `x` and `y`, the accumulator function returns an updated [mean error][mean-absolute-error]. If not provided input values `x` and `y`, the accumulator function returns the current [mean error][mean-absolute-error]. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var accumulator = incrnanmme( 3 ); |
| 68 | + |
| 69 | +var m = accumulator(); |
| 70 | +// returns null |
| 71 | + |
| 72 | +// Fill the window... |
| 73 | +m = accumulator( 2.0, 3.0 ); // [(2.0, 3.0)] |
| 74 | +// returns 1.0 |
| 75 | + |
| 76 | +m = accumulator( -5.0, 2.0 ); // [(2.0, 3.0), (-5.0, 2.0)] |
| 77 | +// returns 4.0 |
| 78 | + |
| 79 | +m = accumulator( 3.0, NaN ); // NaN pair ignored: [(2.0, 3.0), (-5.0, 2.0)] |
| 80 | +// returns 4.0 |
| 81 | + |
| 82 | +// Window begins sliding... |
| 83 | +m = accumulator( -7.0, 3.0 ); // [(2.0, 3.0), (-5.0, 2.0), (-7.0, 3.0)] |
| 84 | +// returns 6.0 |
| 85 | + |
| 86 | +m = accumulator( -5.0, -3.0 ); // [(-5.0, 2.0), (-7.0, 3.0), (-5.0, -3.0)] |
| 87 | +// returns ~6.33 |
| 88 | + |
| 89 | +m = accumulator(); |
| 90 | +// returns ~6.33 |
| 91 | +``` |
| 92 | + |
| 93 | +</section> |
| 94 | + |
| 95 | +<!-- /.usage --> |
| 96 | + |
| 97 | +<section class="notes"> |
| 98 | + |
| 99 | +## Notes |
| 100 | + |
| 101 | +- Input values are **not** type checked. Any `(x, y)` pair containing `NaN` is ignored and does **not** update the window. |
| 102 | +- As `W` (x,y) pairs 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. |
| 103 | +- Be careful when interpreting the [mean error][mean-absolute-error] as errors can cancel. This stated, that errors can cancel makes the [mean error][mean-absolute-error] suitable for measuring the bias in forecasts. |
| 104 | +- **Warning**: the [mean error][mean-absolute-error] is scale-dependent and, thus, the measure should **not** be used to make comparisons between datasets having different scales. |
| 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 incrnanmme = require( '@stdlib/stats/incr/nanmme' ); |
| 119 | + |
| 120 | +var accumulator; |
| 121 | +var v1; |
| 122 | +var v2; |
| 123 | +var i; |
| 124 | + |
| 125 | +// Initialize an accumulator: |
| 126 | +accumulator = incrnanmme( 5 ); |
| 127 | + |
| 128 | +// For each simulated datum, update the moving mean error... |
| 129 | +for ( i = 0; i < 100; i++ ) { |
| 130 | + v1 = ( randu()*100.0 ) - 50.0; |
| 131 | + v2 = ( randu()*100.0 ) - 50.0; |
| 132 | + accumulator( v1, v2 ); |
| 133 | +} |
| 134 | +console.log( accumulator() ); |
| 135 | +``` |
| 136 | + |
| 137 | +</section> |
| 138 | + |
| 139 | +<!-- /.examples --> |
| 140 | + |
| 141 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 142 | + |
| 143 | +<section class="links"> |
| 144 | + |
| 145 | +[mean-absolute-error]: https://en.wikipedia.org/wiki/Mean_absolute_error |
| 146 | + |
| 147 | +<!-- <related-links> --> |
| 148 | + |
| 149 | +<!-- </related-links> --> |
| 150 | + |
| 151 | +</section> |
| 152 | + |
| 153 | +<!-- /.links --> |
| 154 | + |
0 commit comments