Skip to content

Commit 0c87c0d

Browse files
feat: add stats/incr/nanmmape
PR-URL: #9296 Closes: #5583 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Nirmal Jyoti Biswas <59522492+nirmaljb@users.noreply.github.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 0f96d30 commit 0c87c0d

11 files changed

Lines changed: 1084 additions & 0 deletions

File tree

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
# incrnanmmape
22+
23+
> Compute a moving [mean absolute percentage error][mean-absolute-percentage-error] incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, 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}{W} \sum_{i=0}^{W-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}{W} \sum_{i=0}^{W-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}{W} \sum_{i=0}^{W-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@f13330c8d3bb837438b30381f6cd70dbccd4837c/lib/node_modules/@stdlib/stats/incr/mmape/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 incrnanmmape = require( '@stdlib/stats/incr/nanmmape' );
54+
```
55+
56+
#### incrnanmmape( window )
57+
58+
Returns an accumulator `function` which incrementally computes a moving [mean absolute percentage error][mean-absolute-percentage-error]. The `window` parameter defines the number of values over which to compute the moving [mean absolute percentage error][mean-absolute-percentage-error], ignoring `NaN` values.
59+
60+
```javascript
61+
var accumulator = incrnanmmape( 3 );
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 = incrnanmmape( 3 );
70+
71+
var m = accumulator();
72+
// returns null
73+
74+
// Fill the window...
75+
m = accumulator( 2.0, 3.0 ); // [(2.0,3.0)]
76+
// returns ~33.33
77+
78+
m = accumulator( NaN, 3.0 ); // [(2.0,3.0)]
79+
// returns ~33.33
80+
81+
m = accumulator( 1.0, 4.0 ); // [(2.0,3.0), (1.0,4.0)]
82+
// returns ~54.17
83+
84+
m = accumulator( 3.0, 9.0 ); // [(2.0,3.0), (1.0,4.0), (3.0,9.0)]
85+
// returns ~58.33
86+
87+
// Window begins sliding...
88+
m = accumulator( 7.0, 3.0 ); // [(1.0,4.0), (3.0,9.0), (7.0,3.0)]
89+
// returns ~91.67
90+
91+
m = accumulator( 7.0, NaN ); // [(1.0,4.0), (3.0,9.0), (7.0,3.0)]
92+
// returns ~91.67
93+
94+
m = accumulator( 5.0, 3.0 ); // [(3.0,9.0), (7.0,3.0), (5.0,3.0)]
95+
// returns ~88.89
96+
97+
m = accumulator();
98+
// returns ~88.89
99+
```
100+
101+
</section>
102+
103+
<!-- /.usage -->
104+
105+
<section class="notes">
106+
107+
## Notes
108+
109+
- 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.
110+
111+
- As `W` (f,a) 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.
112+
113+
- **Warning**: the [mean absolute percentage error][mean-absolute-percentage-error] has several shortcomings:
114+
115+
- The measure is **not** suitable for intermittent demand patterns (i.e., when `a_i` is `0`).
116+
- 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".
117+
- When used to compare the accuracy of forecast models (e.g., predicting demand), the measure is biased toward forecasts which are too low.
118+
119+
</section>
120+
121+
<!-- /.notes -->
122+
123+
<section class="examples">
124+
125+
## Examples
126+
127+
<!-- eslint no-undef: "error" -->
128+
129+
```javascript
130+
var uniform = require( '@stdlib/random/base/uniform' );
131+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
132+
var incrnanmmape = require( '@stdlib/stats/incr/nanmmape' );
133+
134+
var accumulator;
135+
var v1;
136+
var v2;
137+
var i;
138+
139+
// Initialize an accumulator:
140+
accumulator = incrnanmmape( 5 );
141+
142+
// For each simulated datum, update the moving mean absolute percentage error...
143+
for ( i = 0; i < 100; i++ ) {
144+
v1 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 );
145+
v2 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 );
146+
accumulator( v1, v2 );
147+
}
148+
console.log( accumulator() );
149+
```
150+
151+
</section>
152+
153+
<!-- /.examples -->
154+
155+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
156+
157+
<section class="related">
158+
159+
* * *
160+
161+
## See Also
162+
163+
- <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>
164+
- <span class="package-name">[`@stdlib/stats/incr/mmaape`][@stdlib/stats/incr/mmaape]</span><span class="delimiter">: </span><span class="description">compute a moving arctangent mean absolute percentage error (MAAPE) incrementally.</span>
165+
- <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>
166+
- <span class="package-name">[`@stdlib/stats/incr/mmean`][@stdlib/stats/incr/mmean]</span><span class="delimiter">: </span><span class="description">compute a moving arithmetic mean incrementally.</span>
167+
168+
</section>
169+
170+
<!-- /.related -->
171+
172+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
173+
174+
<section class="links">
175+
176+
[mean-absolute-percentage-error]: https://en.wikipedia.org/wiki/Mean_absolute_percentage_error
177+
178+
<!-- <related-links> -->
179+
180+
[@stdlib/stats/incr/mape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mape
181+
182+
[@stdlib/stats/incr/mmaape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmaape
183+
184+
[@stdlib/stats/incr/mmpe]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmpe
185+
186+
[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean
187+
188+
<!-- </related-links> -->
189+
190+
</section>
191+
192+
<!-- /.links -->
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
26+
var pkg = require( './../package.json' ).name;
27+
var incrnanmmape = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var f;
34+
var i;
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
f = incrnanmmape( (i%5)+1 );
38+
if ( typeof f !== 'function' ) {
39+
b.fail( 'should return a function' );
40+
}
41+
}
42+
b.toc();
43+
if ( typeof f !== 'function' ) {
44+
b.fail( 'should return a function' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( format( '%s::accumulator', pkg ), function benchmark( b ) {
51+
var acc;
52+
var v;
53+
var i;
54+
55+
acc = incrnanmmape( 5 );
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
v = acc( randu()+0.5, randu()+0.5 );
60+
if ( v !== v ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
}
64+
b.toc();
65+
if ( v !== v ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});
Lines changed: 87 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)