|
| 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 | +# incrnanmmae |
| 22 | + |
| 23 | +> Compute a moving [mean absolute error][mean-absolute-error] (MAE) incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +For a window of size `W`, the [mean absolute error][mean-absolute-error] is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:mean_absolute_error" align="center" raw="\operatorname{MAE} = \frac{1}{W} \sum_{i=0}^{W-1} |y_i - x_i|" alt="Equation for the mean absolute error."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\mathop{\mathrm{MAE}} = \frac{1}{W} \sum_{i=0}^{W-1} |y_i - x_i| |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\operatorname{MAE} = \frac{1}{W} \sum_{i=0}^{W-1} |y_i - x_i|" data-equation="eq:mean_absolute_error"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@2fd94e331f96b2984303ca92fad16757cfc5fdcb/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/img/equation_mean_absolute_error.svg" alt="Equation for the mean absolute 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 incrnanmmae = require( '@stdlib/stats/incr/nanmmae' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### incrnanmmae( window ) |
| 55 | + |
| 56 | +Returns an accumulator function which incrementally computes a moving [mean absolute error][mean-absolute-error]. The `window` parameter defines the number of values over which to compute the moving [mean absolute error][mean-absolute-error], ignoring `NaN` values. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var accumulator = incrnanmmae( 3 ); |
| 60 | +``` |
| 61 | + |
| 62 | +#### accumulator( \[x, y] ) |
| 63 | + |
| 64 | +If provided input values `x` and `y`, the accumulator function returns an updated [mean absolute error][mean-absolute-error]. If not provided input values `x` and `y`, the accumulator function returns the current [mean absolute error][mean-absolute-error]. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var accumulator = incrnanmmae( 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( NaN, 2.0 ); |
| 77 | +// returns 1.0 |
| 78 | + |
| 79 | +m = accumulator( -5.0, NaN ); |
| 80 | +// returns 1.0 |
| 81 | + |
| 82 | +m = accumulator( -1.0, 4.0 ); // [(2.0,3.0), (-1.0,4.0)] |
| 83 | +// returns 3.0 |
| 84 | + |
| 85 | +m = accumulator( 3.0, 9.0 ); // [(2.0,3.0), (-1.0,4.0), (3.0,9.0)] |
| 86 | +// returns 4.0 |
| 87 | + |
| 88 | +// Window begins sliding... |
| 89 | +m = accumulator( -7.0, 3.0 ); // [(-1.0,4.0), (3.0,9.0), (-7.0,3.0)] |
| 90 | +// returns 7.0 |
| 91 | + |
| 92 | +m = accumulator( -5.0, -3.0 ); // [(3.0,9.0), (-7.0,3.0), (-5.0,-3.0)] |
| 93 | +// returns 6.0 |
| 94 | + |
| 95 | +m = accumulator(); |
| 96 | +// returns 6.0 |
| 97 | +``` |
| 98 | + |
| 99 | +</section> |
| 100 | + |
| 101 | +<!-- /.usage --> |
| 102 | + |
| 103 | +<section class="notes"> |
| 104 | + |
| 105 | +## Notes |
| 106 | + |
| 107 | +- 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. |
| 108 | +- 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. |
| 109 | +- **Warning**: the [mean absolute error][mean-absolute-error] is scale-dependent and, thus, the measure should **not** be used to make comparisons between datasets having different scales. |
| 110 | + |
| 111 | +</section> |
| 112 | + |
| 113 | +<!-- /.notes --> |
| 114 | + |
| 115 | +<section class="examples"> |
| 116 | + |
| 117 | +## Examples |
| 118 | + |
| 119 | +<!-- eslint no-undef: "error" --> |
| 120 | + |
| 121 | +```javascript |
| 122 | +var randu = require( '@stdlib/random/base/randu' ); |
| 123 | +var incrnanmmae = require( '@stdlib/stats/incr/nanmmae' ); |
| 124 | + |
| 125 | +// Initialize an accumulator: |
| 126 | +var accumulator = incrnanmmae( 5 ); |
| 127 | + |
| 128 | +// For each simulated datum, update the moving mean absolute error... |
| 129 | +var v1; |
| 130 | +var v2; |
| 131 | +var i; |
| 132 | +for ( i = 0; i < 100; i++ ) { |
| 133 | + v1 = ( randu()*100.0 ) - 50.0; |
| 134 | + v2 = ( randu()*100.0 ) - 50.0; |
| 135 | + accumulator( v1, v2 ); |
| 136 | +} |
| 137 | +console.log( accumulator() ); |
| 138 | +``` |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.examples --> |
| 143 | + |
| 144 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 145 | + |
| 146 | +<section class="related"> |
| 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 | +[mean-absolute-error]: https://en.wikipedia.org/wiki/Mean_absolute_error |
| 157 | + |
| 158 | +</section> |
| 159 | + |
| 160 | +<!-- /.links --> |
0 commit comments