Skip to content

Commit e5f33c6

Browse files
committed
feat: add stats/incr/nanmape
1 parent 87abb74 commit e5f33c6

File tree

11 files changed

+878
-0
lines changed

11 files changed

+878
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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], ignoring `NaN` values.
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( 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+
97+
- **Warning**: the [mean absolute percentage error][mean-absolute-percentage-error] has several shortcomings:
98+
99+
- The measure is **not** suitable for intermittent demand patterns (i.e., when `a_i` is `0`).
100+
- 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".
101+
- When used to compare the accuracy of forecast models (e.g., predicting demand), the measure is biased toward forecasts which are too low.
102+
103+
</section>
104+
105+
<!-- /.notes -->
106+
107+
<section class="examples">
108+
109+
## Examples
110+
111+
<!-- eslint no-undef: "error" -->
112+
113+
```javascript
114+
var randu = require( '@stdlib/random/base/randu' );
115+
var incrnanmape = require( '@stdlib/stats/incr/nanmape' );
116+
117+
var accumulator;
118+
var v1;
119+
var v2;
120+
var i;
121+
122+
// Initialize an accumulator:
123+
accumulator = incrnanmape();
124+
125+
// For each simulated datum, update the mean absolute percentage error...
126+
for ( i = 0; i < 100; i++ ) {
127+
if ( randu() < 0.2 ) {
128+
v1 = NaN;
129+
v2 = NaN;
130+
} else {
131+
v1 = ( randu()*100.0 ) + 50.0;
132+
v2 = ( randu()*100.0 ) + 50.0;
133+
}
134+
accumulator( v1, v2 );
135+
}
136+
console.log( accumulator() );
137+
```
138+
139+
</section>
140+
141+
<!-- /.examples -->
142+
143+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
* * *
148+
149+
## See Also
150+
151+
- <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>
152+
- <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>
153+
- <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>
154+
- <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>
155+
156+
</section>
157+
158+
<!-- /.related -->
159+
160+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="links">
163+
164+
[mean-absolute-percentage-error]: https://en.wikipedia.org/wiki/Mean_absolute_percentage_error
165+
166+
<!-- <related-links> -->
167+
168+
[@stdlib/stats/incr/maape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/maape
169+
170+
[@stdlib/stats/incr/mae]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mae
171+
172+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
173+
174+
[@stdlib/stats/incr/mmape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmape
175+
176+
<!-- </related-links> -->
177+
178+
</section>
179+
180+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrnanmape = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnanmape();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanmape();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu()+0.5, randu()+0.5 );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 87 additions & 0 deletions
Loading
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes the mean
4+
absolute percentage error (MAPE), ignoring `NaN` values.
5+
6+
If provided input values, the accumulator function returns an updated mean
7+
absolute percentage error. If not provided input values, the accumulator
8+
function returns the current mean absolute percentage error.
9+
10+
Returns
11+
-------
12+
acc: Function
13+
Accumulator function.
14+
15+
Examples
16+
--------
17+
> var accumulator = {{alias}}();
18+
> var m = accumulator()
19+
null
20+
> m = accumulator( 2.0, 3.0 )
21+
~33.33
22+
> m = accumulator( 2.0, NaN )
23+
~33.33
24+
> m = accumulator( 5.0, 2.0 )
25+
~91.67
26+
> m = accumulator()
27+
~91.67
28+
29+
See Also
30+
--------

0 commit comments

Comments
 (0)