|
| 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 | +# incrnanmape |
| 22 | + |
| 23 | +> Compute the [mean absolute percentage error][mean-absolute-percentage-error] (MAPE) incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [mean absolute percentage error][mean-absolute-percentage-error] is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:mean_absolute_percentage_error" align="center" raw="\operatorname{MAPE} = \frac{100}{n} \sum_{i=0}^{n-1} \biggl| \frac{a_i - f_i}{a_i} \biggr|" alt="Equation for the mean absolute percentage error."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\mathop{\mathrm{MAPE}} = \frac{100}{n} \sum_{i=0}^{n-1} \biggl| \frac{a_i - f_i}{a_i} \biggr| |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\operatorname{MAPE} = \frac{100}{n} \sum_{i=0}^{n-1} \biggl| \frac{a_i - f_i}{a_i} \biggr|" data-equation="eq:mean_absolute_percentage_error"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@d4867162fd6445b10f93ca01f3f764bc646662d8/lib/node_modules/@stdlib/stats/incr/nanmape/docs/img/equation_mean_absolute_percentage_error.svg" alt="Equation for the mean absolute percentage error."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +where `f_i` is the forecast value and `a_i` is the actual value. |
| 43 | + |
| 44 | +</section> |
| 45 | + |
| 46 | +<!-- /.intro --> |
| 47 | + |
| 48 | +<section class="usage"> |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +```javascript |
| 53 | +var incrnanmape = require( '@stdlib/stats/incr/nanmape' ); |
| 54 | +``` |
| 55 | + |
| 56 | +#### incrnanmape() |
| 57 | + |
| 58 | +Returns an accumulator `function` which incrementally computes the [mean absolute percentage error][mean-absolute-percentage-error]. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var accumulator = incrnanmape(); |
| 62 | +``` |
| 63 | + |
| 64 | +#### accumulator( \[f, a] ) |
| 65 | + |
| 66 | +If provided input values `f` and `a`, the accumulator function returns an updated [mean absolute percentage error][mean-absolute-percentage-error]. If not provided input values `f` and `a`, the accumulator function returns the current [mean absolute percentage error][mean-absolute-percentage-error]. |
| 67 | + |
| 68 | +```javascript |
| 69 | +var accumulator = incrnanmape(); |
| 70 | + |
| 71 | +var m = accumulator( 2.0, 3.0 ); |
| 72 | +// returns ~33.33 |
| 73 | + |
| 74 | +m = accumulator( 1.0, 4.0 ); |
| 75 | +// returns ~54.17 |
| 76 | + |
| 77 | +m = accumulator( NaN, 4.0 ); |
| 78 | +// returns ~54.17 |
| 79 | + |
| 80 | +m = accumulator( 3.0, 5.0 ); |
| 81 | +// returns ~49.44 |
| 82 | + |
| 83 | +m = accumulator( 3.0, NaN ); |
| 84 | +// returns ~49.44 |
| 85 | + |
| 86 | +m = accumulator( NaN, NaN ); |
| 87 | +// returns ~49.44 |
| 88 | + |
| 89 | +m = accumulator(); |
| 90 | +// returns ~49.44 |
| 91 | +``` |
| 92 | + |
| 93 | +</section> |
| 94 | + |
| 95 | +<!-- /.usage --> |
| 96 | + |
| 97 | +<section class="notes"> |
| 98 | + |
| 99 | +## Notes |
| 100 | + |
| 101 | +- 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. |
| 102 | + |
| 103 | +- **Warning**: the [mean absolute percentage error][mean-absolute-percentage-error] has several shortcomings: |
| 104 | + |
| 105 | + - The measure is **not** suitable for intermittent demand patterns (i.e., when `a_i` is `0`). |
| 106 | + - The [mean absolute percentage error][mean-absolute-percentage-error] is not symmetrical, as the measure cannot exceed 100% for forecasts which are too "low" and has no limit for forecasts which are too "high". |
| 107 | + - When used to compare the accuracy of forecast models (e.g., predicting demand), the measure is biased toward forecasts which are too low. |
| 108 | + |
| 109 | +</section> |
| 110 | + |
| 111 | +<!-- /.notes --> |
| 112 | + |
| 113 | +<section class="examples"> |
| 114 | + |
| 115 | +## Examples |
| 116 | + |
| 117 | +<!-- eslint no-undef: "error" --> |
| 118 | + |
| 119 | +```javascript |
| 120 | +var randu = require( '@stdlib/random/base/randu' ); |
| 121 | +var incrnanmape = require( '@stdlib/stats/incr/nanmape' ); |
| 122 | + |
| 123 | +var accumulator; |
| 124 | +var v1; |
| 125 | +var v2; |
| 126 | +var i; |
| 127 | + |
| 128 | +// Initialize an accumulator: |
| 129 | +accumulator = incrnanmape(); |
| 130 | + |
| 131 | +// For each simulated datum, update the mean absolute percentage error... |
| 132 | +console.log( '\nValue\tValue\tMAPE\n' ); |
| 133 | +for ( i = 0; i < 100; i++ ) { |
| 134 | + v1 = ( randu() < 0.2 ) ? NaN : randu() * 100.0; |
| 135 | + v2 = ( randu() < 0.2 ) ? NaN : randu() * 100.0; |
| 136 | + accumulator( v1, v2 ); |
| 137 | +} |
| 138 | +console.log( '\nFinal MAPE: %d\n', accumulator() ); |
| 139 | +``` |
| 140 | + |
| 141 | +</section> |
| 142 | + |
| 143 | +<!-- /.examples --> |
| 144 | + |
| 145 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 146 | + |
| 147 | +<section class="related"> |
| 148 | + |
| 149 | +* * * |
| 150 | + |
| 151 | +## See Also |
| 152 | + |
| 153 | +- <span class="package-name">[`@stdlib/stats/incr/maape`][@stdlib/stats/incr/maape]</span><span class="delimiter">: </span><span class="description">compute the mean arctangent absolute percentage error (MAAPE) incrementally.</span> |
| 154 | +- <span class="package-name">[`@stdlib/stats/incr/mae`][@stdlib/stats/incr/mae]</span><span class="delimiter">: </span><span class="description">compute the mean absolute error (MAE) incrementally.</span> |
| 155 | +- <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> |
| 156 | +- <span class="package-name">[`@stdlib/stats/incr/mmape`][@stdlib/stats/incr/mmape]</span><span class="delimiter">: </span><span class="description">compute a moving mean absolute percentage error (MAPE) incrementally.</span> |
| 157 | + |
| 158 | +</section> |
| 159 | + |
| 160 | +<!-- /.related --> |
| 161 | + |
| 162 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 163 | + |
| 164 | +<section class="links"> |
| 165 | + |
| 166 | +[mean-absolute-percentage-error]: https://en.wikipedia.org/wiki/Mean_absolute_percentage_error |
| 167 | + |
| 168 | +<!-- <related-links> --> |
| 169 | + |
| 170 | +[@stdlib/stats/incr/maape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/maape |
| 171 | + |
| 172 | +[@stdlib/stats/incr/mae]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mae |
| 173 | + |
| 174 | +[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean |
| 175 | + |
| 176 | +[@stdlib/stats/incr/mmape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmape |
| 177 | + |
| 178 | +<!-- </related-links> --> |
| 179 | + |
| 180 | +</section> |
| 181 | + |
| 182 | +<!-- /.links --> |
0 commit comments