Skip to content

Commit 6543d74

Browse files
committed
feat: add stats/incr/nanmape
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 97deb1c commit 6543d74

File tree

11 files changed

+923
-0
lines changed

11 files changed

+923
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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 -->
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 pkg = require( './../package.json' ).name;
25+
var incrnanmape = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg, function benchmark( b ) {
31+
var f;
32+
var i;
33+
b.tic();
34+
for ( i = 0; i < b.iterations; i++ ) {
35+
f = incrnanmape();
36+
if ( typeof f !== 'function' ) {
37+
b.fail( 'should return a function' );
38+
}
39+
}
40+
b.toc();
41+
if ( typeof f !== 'function' ) {
42+
b.fail( 'should return a function' );
43+
}
44+
b.pass( 'benchmark finished' );
45+
b.end();
46+
});
47+
48+
bench( pkg+'::accumulator', function benchmark( b ) {
49+
var acc;
50+
var v;
51+
var i;
52+
53+
acc = incrnanmape();
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = acc( i+0.5, i+0.5 );
58+
if ( v !== v ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( v !== v ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});
Lines changed: 87 additions & 0 deletions
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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( 5.0, 2.0 )
23+
~91.67
24+
> m = accumulator( 5.0, NaN )
25+
~91.67
26+
> m = accumulator()
27+
~91.67
28+
29+
See Also
30+
--------
31+

0 commit comments

Comments
 (0)