Skip to content

Commit 9075c13

Browse files
committed
feat: add stats/incr/nanmmaape #5581
--- 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 57ab662 commit 9075c13

13 files changed

Lines changed: 1020 additions & 0 deletions

File tree

lib/node_modules/@stdlib/stats/incr/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ var incr = ns;
118118
- <span class="signature">[`incrnanmaxabs()`][@stdlib/stats/incr/nanmaxabs]</span><span class="delimiter">: </span><span class="description">compute a maximum absolute value incrementally, ignoring `NaN` values.</span>
119119
- <span class="signature">[`incrnanmean()`][@stdlib/stats/incr/nanmean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally, ignoring `NaN` values.</span>
120120
- <span class="signature">[`incrnanmeanabs()`][@stdlib/stats/incr/nanmeanabs]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean of absolute values incrementally, ignoring `NaN` values.</span>
121+
- <span class="signature">[`incrnanmmaape( window )`][@stdlib/stats/incr/nanmmaape]</span><span class="delimiter">: </span><span class="description">compute a moving mean arctangent absolute percentage error (MAAPE) incrementally, ignoring NaN values.</span>
121122
- <span class="signature">[`incrnanmstdev( window[, mean] )`][@stdlib/stats/incr/nanmstdev]</span><span class="delimiter">: </span><span class="description">compute a moving corrected sample standard deviation incrementally, ignoring NaN values.</span>
122123
- <span class="signature">[`incrnanmsum( window )`][@stdlib/stats/incr/nanmsum]</span><span class="delimiter">: </span><span class="description">compute a moving sum incrementally, ignoring `NaN` values.</span>
123124
- <span class="signature">[`incrnanskewness()`][@stdlib/stats/incr/nanskewness]</span><span class="delimiter">: </span><span class="description">compute a corrected sample skewness incrementally, ignoring `NaN` values.</span>
@@ -336,6 +337,8 @@ console.log( getKeys( ns ) );
336337

337338
[@stdlib/stats/incr/nanmeanabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmeanabs
338339

340+
[@stdlib/stats/incr/nanmmaape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmmaape
341+
339342
[@stdlib/stats/incr/nanmstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmstdev
340343

341344
[@stdlib/stats/incr/nanmsum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmsum

lib/node_modules/@stdlib/stats/incr/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,15 @@ setReadOnly( ns, 'incrnanmean', require( '@stdlib/stats/incr/nanmean' ) );
711711
*/
712712
setReadOnly( ns, 'incrnanmeanabs', require( '@stdlib/stats/incr/nanmeanabs' ) );
713713

714+
/**
715+
* @name incrnanmmaape
716+
* @memberof ns
717+
* @readonly
718+
* @type {Function}
719+
* @see {@link module:@stdlib/stats/incr/nanmmaape}
720+
*/
721+
setReadOnly( ns, 'incrnanmmaape', require( '@stdlib/stats/incr/nanmmaape' ) );
722+
714723
/**
715724
* @name incrnanmstdev
716725
* @memberof ns
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# incrmmaape
22+
23+
> Compute a moving [mean arctangent absolute percentage error][@kim:2016a] (MAAPE) incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, the [mean arctangent absolute percentage error][@kim:2016a] is defined as
28+
29+
<!-- <equation class="equation" label="eq:mean_arctangent_absolute_percentage_error" align="center" raw="\operatorname{MAAPE} = \frac{1}{W} \sum_{i=0}^{W-1} \operatorname{arctan}\biggl( \biggl| \frac{a_i - f_i}{a_i} \biggr| \biggr)" alt="Equation for the mean arctangent absolute percentage error."> -->
30+
31+
```math
32+
\mathop{\mathrm{MAAPE}} = \frac{1}{W} \sum_{i=0}^{W-1} \mathop{\mathrm{arctan}}\biggl( \biggl| \frac{a_i - f_i}{a_i} \biggr| \biggr)
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{MAAPE} = \frac{1}{W} \sum_{i=0}^{W-1} \operatorname{arctan} \biggl( \biggl| \frac{a_i - f_i}{a_i} \biggr| \biggr)" data-equation="eq:mean_arctangent_absolute_percentage_error">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@677f6828831c61cabc84859897a5ee7e079d6ddf/lib/node_modules/@stdlib/stats/incr/mmaape/docs/img/equation_mean_arctangent_absolute_percentage_error.svg" alt="Equation for the mean arctangent 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 incrnanmmaape = require( '@stdlib/stats/incr/nanmmaape' );
54+
```
55+
56+
#### incrnanmmaape( window )
57+
58+
Returns an accumulator `function` which incrementally computes a moving [mean arctangent absolute percentage error][@kim:2016a]. The `window` parameter defines the number of values over which to compute the moving [mean arctangent absolute percentage error][@kim:2016a].
59+
60+
```javascript
61+
var accumulator = incrnanmmaape( 3 );
62+
```
63+
64+
#### accumulator( \[f, a] )
65+
66+
If provided input values `f` and `a`, the accumulator function returns an updated [mean arctangent absolute percentage error][@kim:2016a]. If not provided input values `f` and `a`, the accumulator function returns the current [mean arctangent absolute percentage error][@kim:2016a].
67+
68+
```javascript
69+
var accumulator = incrnanmmaape( 3 );
70+
71+
var m = accumulator();
72+
// returns null
73+
74+
// Fill the window...
75+
m = accumulator( 2.0, 3.0 );
76+
// returns ~0.32
77+
78+
m = accumulator( NaN, 4.0 );
79+
// returns ~0.32
80+
81+
m = accumulator( 1.0, 4.0 );
82+
// returns ~0.48
83+
84+
m = accumulator( 3.0, 9.0 );
85+
// returns ~0.52
86+
87+
// Window begins sliding...
88+
m = accumulator( 7.0, 3.0 );
89+
// returns ~0.72
90+
91+
m = accumulator();
92+
// returns ~0.72
93+
```
94+
95+
</section>
96+
97+
<!-- /.usage -->
98+
99+
<section class="notes">
100+
101+
## Notes
102+
103+
- 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.
104+
- 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.
105+
- Note that, unlike the [mean absolute percentage error][@stdlib/stats/incr/mape] (MAPE), the [mean arctangent absolute percentage error][@kim:2016a] is expressed in radians on the interval \[0,π/2].
106+
107+
</section>
108+
109+
<!-- /.notes -->
110+
111+
<section class="examples">
112+
113+
## Examples
114+
115+
<!-- eslint no-undef: "error" -->
116+
117+
```javascript
118+
var randu = require( '@stdlib/random/base/randu' );
119+
var incrnanmmaape = require( '@stdlib/stats/incr/nanmmaape' );
120+
121+
var accumulator;
122+
var v1;
123+
var v2;
124+
var i;
125+
126+
// Initialize an accumulator:
127+
accumulator = incrnanmmaape( 5 );
128+
129+
// For each simulated datum, update the moving mean arctangent absolute percentage error...
130+
for ( i = 0; i < 100; i++ ) {
131+
if ( randu() < 0.2 ) {
132+
v1 = NaN;
133+
} else {
134+
v1 = ( randu()*100.0 ) + 50.0;
135+
}
136+
if ( randu() < 0.2 ) {
137+
v2 = NaN;
138+
} else {
139+
v2 = ( randu()*100.0 ) + 50.0;
140+
}
141+
accumulator( v1, v2 );
142+
}
143+
console.log( accumulator() );
144+
```
145+
146+
</section>
147+
148+
<!-- /.examples -->
149+
150+
<section class="references">
151+
152+
</section>
153+
154+
<!-- /.references -->
155+
156+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
157+
158+
<section class="related">
159+
160+
* * *
161+
162+
## See Also
163+
164+
- <span class="package-name">[`@stdlib/stats/incr/mmaape`][@stdlib/stats/incr/mmaape]</span><span class="delimiter">: </span><span class="description">compute a moving mean arctangent absolute percentage error (MAAPE) incrementally..</span>
165+
166+
</section>
167+
168+
<!-- /.related -->
169+
170+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
171+
172+
<section class="links">
173+
174+
[@kim:2016a]: https://www.sciencedirect.com/science/article/pii/S0169207016000121
175+
176+
[@stdlib/stats/incr/mape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mape
177+
178+
179+
<!-- <related-links> -->
180+
181+
[@stdlib/stats/incr/mmaape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmaape
182+
183+
<!-- </related-links> -->
184+
185+
</section>
186+
187+
<!-- /.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) 2018 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 incrmmaape = 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 = incrmmaape( (i%5)+1 );
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 = incrmmaape( 5 );
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+
});

0 commit comments

Comments
 (0)