|
| 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 | +# incrnanmpe |
| 22 | + |
| 23 | +> Compute the [mean percentage error][mean-percentage-error] (MPE) incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [mean percentage error][mean-percentage-error] is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:mean_percentage_error" align="center" raw="\operatorname{MPE} = \frac{100}{n} \sum_{i=0}^{n-1} \frac{a_i - f_i}{a_i}" alt="Equation for the mean percentage error."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\mathop{\mathrm{MPE}} = \frac{100}{n} \sum_{i=0}^{n-1} \frac{a_i - f_i}{a_i} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\operatorname{MPE} = \frac{100}{n} \sum_{i=0}^{n-1} \frac{a_i - f_i}{a_i}" data-equation="eq:mean_percentage_error"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@2acedf866c9a4f1353af22f95780535612c5ee06/lib/node_modules/@stdlib/stats/incr/nanmpe/docs/img/equation_mean_percentage_error.svg" alt="Equation for the mean 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 incrnanmpe = require( '@stdlib/stats/incr/nanmpe' ); |
| 54 | +``` |
| 55 | + |
| 56 | +#### incrnanmpe() |
| 57 | + |
| 58 | +Returns an accumulator `function` which incrementally computes the [mean percentage error][mean-percentage-error], ignoring `NaN` values. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var accumulator = incrnanmpe(); |
| 62 | +``` |
| 63 | + |
| 64 | +#### accumulator( \[f, a] ) |
| 65 | + |
| 66 | +If provided input values `f` and `a`, the accumulator function returns an updated [mean percentage error][mean-percentage-error]. If not provided input values `f` and `a`, the accumulator function returns the current [mean percentage error][mean-percentage-error]. |
| 67 | + |
| 68 | +```javascript |
| 69 | +var accumulator = incrnanmpe(); |
| 70 | + |
| 71 | +var m = accumulator( 2.0, 3.0 ); |
| 72 | +// returns ~33.33 |
| 73 | + |
| 74 | +m = accumulator( 2.0, NaN ); |
| 75 | +// returns ~33.33 |
| 76 | + |
| 77 | +m = accumulator( 1.0, 4.0 ); |
| 78 | +// returns ~54.17 |
| 79 | + |
| 80 | +m = accumulator( 3.0, 5.0 ); |
| 81 | +// returns ~49.44 |
| 82 | + |
| 83 | +m = accumulator(); |
| 84 | +// returns ~49.44 |
| 85 | +``` |
| 86 | + |
| 87 | +</section> |
| 88 | + |
| 89 | +<!-- /.usage --> |
| 90 | + |
| 91 | +<section class="notes"> |
| 92 | + |
| 93 | +## Notes |
| 94 | + |
| 95 | +- 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. |
| 96 | +- Be careful when interpreting the [mean percentage error][mean-percentage-error] as errors can cancel. This stated, that errors can cancel makes the [mean percentage error][mean-percentage-error] suitable for measuring the bias in forecasts. |
| 97 | +- **Warning**: the [mean percentage error][mean-percentage-error] is **not** suitable for intermittent demand patterns (i.e., when `a_i` is `0`). Interpretation is most straightforward when actual and forecast values are positive valued (e.g., number of widgets sold). |
| 98 | + |
| 99 | +</section> |
| 100 | + |
| 101 | +<!-- /.notes --> |
| 102 | + |
| 103 | +<section class="examples"> |
| 104 | + |
| 105 | +## Examples |
| 106 | + |
| 107 | +<!-- eslint no-undef: "error" --> |
| 108 | + |
| 109 | +```javascript |
| 110 | +var randu = require( '@stdlib/random/base/randu' ); |
| 111 | +var incrnanmpe = require( '@stdlib/stats/incr/nanmpe' ); |
| 112 | + |
| 113 | +var accumulator; |
| 114 | +var v1; |
| 115 | +var v2; |
| 116 | +var i; |
| 117 | + |
| 118 | +// Initialize an accumulator: |
| 119 | +accumulator = incrnanmpe(); |
| 120 | + |
| 121 | +// For each simulated datum, update the mean percentage error... |
| 122 | +for ( i = 0; i < 100; i++ ) { |
| 123 | + if ( randu() < 0.2 ) { |
| 124 | + v1 = NaN; |
| 125 | + v2 = NaN; |
| 126 | + } else { |
| 127 | + v1 = ( randu()*100.0 ) + 50.0; |
| 128 | + v2 = ( randu()*100.0 ) + 50.0; |
| 129 | + } |
| 130 | + accumulator( v1, v2 ); |
| 131 | +} |
| 132 | +console.log( accumulator() ); |
| 133 | +``` |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.examples --> |
| 138 | + |
| 139 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 140 | + |
| 141 | +<section class="related"> |
| 142 | + |
| 143 | +* * * |
| 144 | + |
| 145 | +## See Also |
| 146 | + |
| 147 | +- <span class="package-name">[`@stdlib/stats/incr/mape`][@stdlib/stats/incr/mape]</span><span class="delimiter">: </span><span class="description">compute the mean absolute percentage error (MAPE) incrementally.</span> |
| 148 | +- <span class="package-name">[`@stdlib/stats/incr/me`][@stdlib/stats/incr/me]</span><span class="delimiter">: </span><span class="description">compute the mean error (ME) incrementally.</span> |
| 149 | +- <span class="package-name">[`@stdlib/stats/incr/mmpe`][@stdlib/stats/incr/mmpe]</span><span class="delimiter">: </span><span class="description">compute a moving mean percentage error (MPE) incrementally.</span> |
| 150 | + |
| 151 | +</section> |
| 152 | + |
| 153 | +<!-- /.related --> |
| 154 | + |
| 155 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 156 | + |
| 157 | +<section class="links"> |
| 158 | + |
| 159 | +[mean-percentage-error]: https://en.wikipedia.org/wiki/Mean_percentage_error |
| 160 | + |
| 161 | +<!-- <related-links> --> |
| 162 | + |
| 163 | +[@stdlib/stats/incr/mape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mape |
| 164 | + |
| 165 | +[@stdlib/stats/incr/me]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/me |
| 166 | + |
| 167 | +[@stdlib/stats/incr/mmpe]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmpe |
| 168 | + |
| 169 | +<!-- </related-links> --> |
| 170 | + |
| 171 | +</section> |
| 172 | + |
| 173 | +<!-- /.links --> |
0 commit comments