Skip to content

Commit e6abaec

Browse files
kanikasharma-18Planeshifterstdlib-bot
authored
feat: add stats/incr/nanmme
PR-URL: #9460 Closes: #5587 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: stdlib-bot <noreply@stdlib.io> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 320c37a commit e6abaec

11 files changed

Lines changed: 854 additions & 0 deletions

File tree

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
# incrnanmme
22+
23+
> Compute a moving [mean error][mean-absolute-error] (ME) incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, the [mean error][mean-absolute-error] is defined as
28+
29+
<!-- <equation class="equation" label="eq:mean_error" align="center" raw="\mathop{\mathrm{ME}} = \frac{1}{W} \sum_{i=0}^{W-1} (y_i - x_i)" alt="Equation for the mean error."> -->
30+
31+
```math
32+
\mathop{\mathrm{ME}} = \frac{1}{W} \sum_{i=0}^{W-1} (y_i - x_i)
33+
```
34+
35+
<!-- <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">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@634ac3689760e2f57fd51085f387d8dc5bb3b927/lib/node_modules/@stdlib/stats/incr/nanmme/docs/img/equation_mean_error.svg" alt="Equation for the mean error.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var incrnanmme = require( '@stdlib/stats/incr/nanmme' );
52+
```
53+
54+
#### incrnanmme( window )
55+
56+
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].
57+
58+
```javascript
59+
var accumulator = incrnanmme( 3 );
60+
```
61+
62+
#### accumulator( \[x, y] )
63+
64+
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].
65+
66+
```javascript
67+
var accumulator = incrnanmme( 3 );
68+
69+
var m = accumulator();
70+
// returns null
71+
72+
// Fill the window...
73+
m = accumulator( 2.0, 3.0 ); // [(2.0, 3.0)]
74+
// returns 1.0
75+
76+
m = accumulator( -5.0, 2.0 ); // [(2.0, 3.0), (-5.0, 2.0)]
77+
// returns 4.0
78+
79+
m = accumulator( 3.0, NaN ); // NaN pair ignored: [(2.0, 3.0), (-5.0, 2.0)]
80+
// returns 4.0
81+
82+
// Window begins sliding...
83+
m = accumulator( -7.0, 3.0 ); // [(2.0, 3.0), (-5.0, 2.0), (-7.0, 3.0)]
84+
// returns 6.0
85+
86+
m = accumulator( -5.0, -3.0 ); // [(-5.0, 2.0), (-7.0, 3.0), (-5.0, -3.0)]
87+
// returns ~6.33
88+
89+
m = accumulator();
90+
// returns ~6.33
91+
```
92+
93+
</section>
94+
95+
<!-- /.usage -->
96+
97+
<section class="notes">
98+
99+
## Notes
100+
101+
- Input values are **not** type checked. Any `(x, y)` pair containing `NaN` is ignored and does **not** update the window.
102+
- 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.
103+
- 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.
104+
- **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.
105+
106+
</section>
107+
108+
<!-- /.notes -->
109+
110+
<section class="examples">
111+
112+
## Examples
113+
114+
<!-- eslint no-undef: "error" -->
115+
116+
```javascript
117+
var randu = require( '@stdlib/random/base/randu' );
118+
var incrnanmme = require( '@stdlib/stats/incr/nanmme' );
119+
120+
var accumulator;
121+
var v1;
122+
var v2;
123+
var i;
124+
125+
// Initialize an accumulator:
126+
accumulator = incrnanmme( 5 );
127+
128+
// For each simulated datum, update the moving mean error...
129+
for ( i = 0; i < 100; i++ ) {
130+
v1 = ( randu()*100.0 ) - 50.0;
131+
v2 = ( randu()*100.0 ) - 50.0;
132+
accumulator( v1, v2 );
133+
}
134+
console.log( accumulator() );
135+
```
136+
137+
</section>
138+
139+
<!-- /.examples -->
140+
141+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
142+
143+
<section class="links">
144+
145+
[mean-absolute-error]: https://en.wikipedia.org/wiki/Mean_absolute_error
146+
147+
<!-- <related-links> -->
148+
149+
<!-- </related-links> -->
150+
151+
</section>
152+
153+
<!-- /.links -->
154+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 incrnanmme = 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 = incrnanmme( ( 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 = incrnanmme( 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+
});
71+
72+
bench( format( '%s::accumulator-nan', pkg ), function benchmark( b ) {
73+
var acc;
74+
var v;
75+
var i;
76+
77+
acc = incrnanmme( 5 );
78+
79+
b.tic();
80+
for ( i = 0; i < b.iterations; i++ ) {
81+
if ( i % 10 === 0 ) {
82+
v = acc( NaN, randu() - 0.5 );
83+
} else {
84+
v = acc( randu() - 0.5, randu() - 0.5 );
85+
}
86+
if ( v !== v ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
}
90+
b.toc();
91+
if ( v !== v ) {
92+
b.fail( 'should not return NaN' );
93+
}
94+
b.pass( 'benchmark finished' );
95+
b.end();
96+
});

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

Loading
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
{{alias}}( W )
3+
Returns an accumulator function which incrementally computes a moving
4+
mean error (ME), ignoring `NaN` values.
5+
6+
The `W` parameter defines the number of values over which to compute the
7+
moving mean error.
8+
9+
If provided input values, the accumulator function returns an updated
10+
moving mean error. If not provided input values, the accumulator function
11+
returns the current moving mean error.
12+
13+
As `W` (x,y) pairs are needed to fill the window buffer, the first `W-1`
14+
returned values are calculated from smaller sample sizes. Until the window
15+
is full, each returned value is calculated from all provided values which
16+
are not `NaN`.
17+
18+
Parameters
19+
----------
20+
W: integer
21+
Window size.
22+
23+
Returns
24+
-------
25+
acc: Function
26+
Accumulator function.
27+
28+
Examples
29+
--------
30+
> var accumulator = {{alias}}( 3 );
31+
> var m = accumulator()
32+
null
33+
> m = accumulator( 2.0, 3.0 )
34+
1.0
35+
> m = accumulator( -5.0, NaN )
36+
1.0
37+
> m = accumulator( 3.0, 2.0 )
38+
0.0
39+
> m = accumulator( 5.0, -2.0 )
40+
~-2.33
41+
> m = accumulator()
42+
~-2.33
43+
44+
See Also
45+
--------
46+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 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 pair is ignored and the accumulator 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 (ME), 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` (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.
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, NaN );
61+
* // returns 4.0
62+
*
63+
* m = accumulator( -7.0, 3.0 );
64+
* // returns 6.0
65+
*
66+
* m = accumulator( -5.0, -3.0 );
67+
* // returns ~6.33
68+
*
69+
* m = accumulator();
70+
* // returns ~6.33
71+
*/
72+
declare function incrnanmme( W: number ): accumulator;
73+
74+
75+
// EXPORTS //
76+
77+
export = incrnanmme;

0 commit comments

Comments
 (0)