Skip to content

Commit 2b82801

Browse files
feat: add stats/incr/nanmpe
--- 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 d660cab commit 2b82801

11 files changed

Lines changed: 826 additions & 0 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
# 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/nampe/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( NaN, 4.0 );
75+
// returns ~33.33
76+
77+
m = accumulator( 1.0, 4.0 );
78+
// returns ~54.17
79+
80+
m = accumulator( 3.0, NaN );
81+
// returns ~54.17
82+
83+
m = accumulator( 3.0, 5.0 );
84+
// returns ~49.44
85+
86+
m = accumulator();
87+
// returns ~49.44
88+
```
89+
90+
</section>
91+
92+
<!-- /.usage -->
93+
94+
<section class="notes">
95+
96+
## Notes
97+
98+
- Input values are **not** type checked. If provided `NaN`, the accumulated value is `NaN` for **all** future invocations **until** a non-`NaN` value pair is provided. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
99+
- Note that if either `f` or `a` is `NaN`, the input value pair is **skipped** (i.e., the pair does not contribute to the accumulated value).
100+
- 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.
101+
- **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).
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 bernoulli = require( '@stdlib/random/base/bernoulli' );
115+
var randu = require( '@stdlib/random/base/randu' );
116+
var incrnanmpe = require( '@stdlib/stats/incr/nanmpe' );
117+
118+
var accumulator;
119+
var v1;
120+
var v2;
121+
var i;
122+
123+
// Initialize an accumulator:
124+
accumulator = incrnanmpe();
125+
126+
// For each simulated datum, update the mean percentage error...
127+
for ( i = 0; i < 100; i++ ) {
128+
v1 = ( bernoulli( 0.8 ) === 0 ) ? NaN : ( randu()*100.0 ) + 50.0;
129+
v2 = ( bernoulli( 0.8 ) === 0 ) ? NaN : ( randu()*100.0 ) + 50.0;
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+
</section>
144+
145+
<!-- /.related -->
146+
147+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="links">
150+
151+
[mean-percentage-error]: https://en.wikipedia.org/wiki/Mean_percentage_error
152+
153+
</section>
154+
155+
<!-- /.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) 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 pkg = require( './../package.json' ).name;
26+
var incrnanmpe = 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 = incrnanmpe();
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 = incrnanmpe();
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: 65 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+
percentage error (MPE), ignoring `NaN` values.
5+
6+
If provided input values, the accumulator function returns an updated mean
7+
percentage error. If not provided input values, the accumulator function
8+
returns the current mean 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+
~-58.33
26+
> m = accumulator()
27+
~-58.33
28+
29+
See Also
30+
--------
31+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* If provided input values, the accumulator function returns an updated mean percentage error. If not provided input values, the accumulator function returns the current mean percentage error.
25+
*
26+
* ## Notes
27+
*
28+
* - If either `f` or `a` is `NaN`, the input value pair is skipped (i.e., the pair does not contribute to the accumulated value).
29+
*
30+
* @param f - input value (forecast)
31+
* @param a - input value (actual)
32+
* @returns mean percentage error or null
33+
*/
34+
type accumulator = ( f?: number, a?: number ) => number | null;
35+
36+
/**
37+
* Returns an accumulator function which incrementally computes the mean percentage error, ignoring `NaN` values.
38+
*
39+
* @returns accumulator function
40+
*
41+
* @example
42+
* var accumulator = incrnanmpe();
43+
*
44+
* var m = accumulator();
45+
* // returns null
46+
*
47+
* m = accumulator( 2.0, 3.0 );
48+
* // returns ~33.33
49+
*
50+
* m = accumulator( NaN, 5.0 );
51+
* // returns ~33.33
52+
*
53+
* m = accumulator( 5.0, 2.0 );
54+
* // returns ~-58.33
55+
*
56+
* m = accumulator();
57+
* // returns ~-58.33
58+
*/
59+
declare function incrnanmpe(): accumulator;
60+
61+
62+
// EXPORTS //
63+
64+
export = incrnanmpe;

0 commit comments

Comments
 (0)