Skip to content

Commit b2bc7a5

Browse files
fix(stats/incr/nanmme): handle NaN in nanmme accumulator
--- 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 b2bc7a5

11 files changed

Lines changed: 835 additions & 0 deletions

File tree

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
# incrnanmme
22+
23+
> Compute a moving [mean error][mean-absolute-error] (ME) incrementally.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, the [mean error][mean-absolute-error] is defined **ignoring any `(x, y)` pairs containing NaN**.
28+
29+
```math
30+
\mathop{\mathrm{ME}} = \frac{1}{W} \sum_{i=0}^{W-1} (y_i - x_i)
31+
```
32+
33+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{ME} = \frac{1}{W} \sum_{i=0}^{W-1} (y_i - x_i)" data-equation="eq:mean_error">
34+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@634ac3689760e2f57fd51085f387d8dc5bb3b927/lib/node_modules/@stdlib/stats/incr/mme/docs/img/equation_mean_error.svg" alt="Equation for the mean error.">
35+
<br>
36+
</div> -->
37+
38+
<!-- </equation> -->
39+
40+
</section>
41+
42+
<!-- /.intro -->
43+
44+
<section class="usage">
45+
46+
## Usage
47+
48+
```javascript
49+
var incrnanmme = require( '@stdlib/stats/incr/nanmme' );
50+
```
51+
52+
#### incrnanmme( window )
53+
54+
Returns an accumulator `function` which incrementally computes a moving [mean error][mean-absolute-error], ignoring any `(x, y)` pairs containing `NaN`. The `window` parameter defines the number of values over which to compute the moving [mean error][mean-absolute-error].
55+
56+
```javascript
57+
var accumulator = incrnanmme( 3 );
58+
```
59+
60+
#### accumulator( \[x, y] )
61+
62+
If provided input values `x` and `y`, the accumulator function returns an updated [mean error][mean-absolute-error]. If not provided input values `x` and `y`, the accumulator function returns the current [mean error][mean-absolute-error].
63+
64+
```javascript
65+
var accumulator = incrnanmme( 3 );
66+
67+
var m = accumulator();
68+
// returns null
69+
70+
// Fill the window...
71+
m = accumulator( 2.0, 3.0 ); // [(2.0,3.0)]
72+
// returns 1.0
73+
74+
m = accumulator( -1.0, 4.0 ); // [(2.0,3.0), (-1.0,4.0)]
75+
// returns 3.0
76+
77+
m = accumulator( 3.0, NaN ); // ignored
78+
// returns 3.0
79+
80+
// Window begins sliding...
81+
m = accumulator( -7.0, 3.0 );
82+
// returns 5.333333333333334
83+
84+
m = accumulator( -5.0, -3.0 );
85+
// returns 5.666666666666667
86+
87+
m = accumulator();
88+
// returns 5.666666666666667
89+
```
90+
91+
</section>
92+
93+
<!-- /.usage -->
94+
95+
<section class="notes">
96+
97+
## Notes
98+
99+
- Input values are **not** type checked. Any `(x, y)` pair containing `NaN` is ignored and does **not** update the window.
100+
- As `W` (x,y) 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.
101+
- Be careful when interpreting the [mean error][mean-absolute-error] as errors can cancel. This stated, that errors can cancel makes the [mean error][mean-absolute-error] suitable for measuring the bias in forecasts.
102+
- **Warning**: the [mean error][mean-absolute-error] is scale-dependent and, thus, the measure should **not** be used to make comparisons between datasets having different scales.
103+
104+
</section>
105+
106+
<!-- /.notes -->
107+
108+
<section class="examples">
109+
110+
## Examples
111+
112+
<!-- eslint no-undef: "error" -->
113+
114+
```javascript
115+
var randu = require( '@stdlib/random/base/randu' );
116+
var incrnanmme = require( '@stdlib/stats/incr/nanmme' );
117+
118+
var accumulator;
119+
var v1;
120+
var v2;
121+
var i;
122+
123+
// Initialize an accumulator:
124+
accumulator = incrnanmme( 5 );
125+
126+
// For each simulated datum, update the moving mean error...
127+
for ( i = 0; i < 100; i++ ) {
128+
v1 = ( randu()*100.0 ) - 50.0;
129+
v2 = ( 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+
* * *
144+
145+
## See Also
146+
147+
- <span class="package-name">[`@stdlib/stats/incr/me`][@stdlib/stats/incr/me]</span><span class="delimiter">: </span><span class="description">compute the mean error (ME) incrementally.</span>
148+
- <span class="package-name">[`@stdlib/stats/incr/mmae`][@stdlib/stats/incr/mmae]</span><span class="delimiter">: </span><span class="description">compute a moving mean absolute error (MAE) incrementally.</span>
149+
- <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>
150+
151+
</section>
152+
153+
<!-- /.related -->
154+
155+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
156+
157+
<section class="links">
158+
159+
[mean-absolute-error]: https://en.wikipedia.org/wiki/Mean_absolute_error
160+
161+
<!-- <related-links> -->
162+
163+
[@stdlib/stats/incr/me]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/me
164+
165+
[@stdlib/stats/incr/mmae]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmae
166+
167+
[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean
168+
169+
<!-- </related-links> -->
170+
171+
</section>
172+
173+
<!-- /.links -->
174+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 incrnanmme = 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 = incrnanmme( (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-nan', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanmme( 5 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
if ( i%10 === 0 ) {
59+
v = acc( NaN, randu()-0.5 );
60+
} else {
61+
v = acc( randu()-0.5, randu()-0.5 );
62+
}
63+
if ( v !== v ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
}
67+
b.toc();
68+
if ( v !== v ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});

lib/node_modules/@stdlib/stats/incr/nanmme/docs/img/equation_mean_error.svg

Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{{alias}}( W )
2+
Returns an accumulator function which incrementally computes a moving
3+
mean error (ME), ignoring NaN value.
4+
5+
The `W` parameter defines the number of values over which to compute the
6+
moving mean error
7+
8+
If provided a value, the accumulator function returns an updated moving
9+
mean error. If not provided a value, the accumulator function returns the
10+
current moving mean error.
11+
12+
As `W` values are needed to fill the window buffer, the first `W-1` returned
13+
values are calculated from smaller sample sizes. Until the window is full,
14+
each returned value is calculated from all provided values.
15+
16+
Parameters
17+
----------
18+
W: integer
19+
Window size.
20+
21+
Returns
22+
-------
23+
acc: Function
24+
Accumulator function.
25+
26+
Examples
27+
--------
28+
> var accumulator = {{alias}}( 3 );
29+
> var m = accumulator()
30+
null
31+
> m = accumulator( 2.0, 3.0 )
32+
1
33+
> m = accumulator( -5.0, NaN )
34+
1
35+
> m = accumulator( 3.0, 2.0 )
36+
0
37+
> m = accumulator( 5.0, -2.0 )
38+
-2.3333333333333335
39+
> m = accumulator()
40+
-2.3333333333333335
41+
42+
See Also
43+
--------
44+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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 error. If not provided input values, the accumulator function returns the current mean error.
25+
*
26+
* ## Notes
27+
*
28+
* - If either `x` or `y` is `NaN`, the accumulator ignores the input values and returns the previously accumulated value.
29+
*
30+
* @param x - input value
31+
* @param y - input value
32+
* @returns mean error or null
33+
*/
34+
type accumulator = ( x?: number, y?: number ) => number | null;
35+
36+
/**
37+
* Returns an accumulator function which incrementally computes a moving mean error, ignoring 'NaN' values.
38+
*
39+
* ## Notes
40+
*
41+
* - The `W` parameter defines the number of values over which to compute the moving mean error.
42+
* - As `W` values 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.
43+
*
44+
* @param W - window size
45+
* @throws must provide a positive integer
46+
* @returns accumulator function
47+
*
48+
* @example
49+
* var accumulator = incrnanmme( 3 );
50+
*
51+
* var m = accumulator();
52+
* // returns null
53+
*
54+
* m = accumulator( 2.0, 3.0 );
55+
* // returns 1.0
56+
*
57+
* m = accumulator( -5.0, 2.0 );
58+
* // returns 4.0
59+
*
60+
* m = accumulator( 3.0, 2.0 );
61+
* // returns ~2.33
62+
*
63+
* m = accumulator( 5.0, -2.0 );
64+
* // returns ~-0.33
65+
*
66+
* m = accumulator();
67+
* // returns ~-0.33
68+
*/
69+
declare function incrnanmme( W: number ): accumulator;
70+
71+
72+
// EXPORTS //
73+
74+
export = incrnanmme;
75+

0 commit comments

Comments
 (0)