Skip to content

Commit 8481f49

Browse files
committed
feat(stats/incr): add incrnanmae 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 3da41b7 commit 8481f49

File tree

11 files changed

+879
-0
lines changed

11 files changed

+879
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
# incrnanmae
22+
23+
> Compute the [mean absolute error][mean-absolute-error] (MAE) incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [mean absolute error][mean-absolute-error] is defined as
28+
29+
<!-- <equation class="equation" label="eq:mean_absolute_error" align="center" raw="\operatorname{MAE} = \frac{\displaystyle\sum_{i=0}^{n-1} |y_i - x_i|}{n}" alt="Equation for the mean absolute error."> -->
30+
31+
```math
32+
\mathop{\mathrm{MAE}} = \frac{\displaystyle\sum_{i=0}^{n-1} |y_i - x_i|}{n}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{MAE} = \frac{\displaystyle\sum_{i=0}^{n-1} |y_i - x_i|}{n}" data-equation="eq:mean_absolute_error">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mae/docs/img/equation_mean_absolute_error.svg" alt="Equation for the mean absolute 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 incrnanmae = require( '@stdlib/stats/incr/nanmae' );
52+
```
53+
54+
#### incrnanmae()
55+
56+
Returns an accumulator `function` which incrementally computes the [mean absolute error][mean-absolute-error] ignoring NaN values.
57+
58+
```javascript
59+
var accumulator = incrnanmae();
60+
```
61+
62+
#### accumulator( \[x, y] )
63+
64+
If provided input values `x` and `y`, the accumulator function returns an updated [mean absolute error][mean-absolute-error]. If not provided input values `x` and `y`, the accumulator function returns the current [mean absolute error][mean-absolute-error].
65+
66+
```javascript
67+
var accumulator = incrnanmae();
68+
69+
var m = accumulator();
70+
// returns null
71+
72+
m = accumulator( 2.0, 3.0 );
73+
// returns 1.0
74+
75+
m = accumulator( NaN, 2.0 );
76+
// returns 1.0
77+
78+
m = accumulator( -5.0, NaN );
79+
// returns 1.0
80+
81+
m = accumulator( NaN, NaN );
82+
// returns 1.0
83+
84+
m = accumulator( -5.0, 2.0 );
85+
// returns 4.0
86+
87+
m = accumulator();
88+
// returns 4.0
89+
```
90+
91+
</section>
92+
93+
<!-- /.usage -->
94+
95+
<section class="notes">
96+
97+
## Notes
98+
99+
- Input values are not type checked beyond checking for NaN. If provided values which are not numbers or NaN (e.g., strings, booleans), the behavior of the accumulator is undefined. If non-numeric inputs are possible, you are advised to type check and handle accordingly before passing the value to the accumulator function.
100+
- If both provided values are NaN, the accumulated value is unchanged. The accumulator only updates when both inputs are valid numbers.
101+
- **Warning**: the [mean absolute error][mean-absolute-error] is scale-dependent and, thus, the measure should **not** be used to make comparisons between datasets having different scales.
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 randu = require( '@stdlib/random/base/randu' );
115+
var incrnanmae = require( '@stdlib/stats/incr/nanmae' );
116+
117+
var accumulator;
118+
var v1;
119+
var v2;
120+
var i;
121+
122+
// Initialize an accumulator:
123+
accumulator = incrnanmae();
124+
125+
// For each simulated datum, update the mean absolute error...
126+
for ( i = 0; i < 100; i++ ) {
127+
v1 = ( randu() < 0.2 ) ? NaN : ( randu()*100.0 ) - 50.0;
128+
v2 = ( randu() < 0.2 ) ? NaN : ( randu()*100.0 ) - 50.0;
129+
accumulator( v1, v2 );
130+
}
131+
console.log( accumulator() );
132+
```
133+
134+
</section>
135+
136+
<!-- /.examples -->
137+
138+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139+
140+
<section class="related">
141+
142+
* * *
143+
144+
## See Also
145+
146+
- <span class="package-name">[`@stdlib/stats/incr/mape`][@stdlib/stats/incr/mape]</span><span class="delimiter">: </span><span class="description">compute the mean absolute percentage error (MAPE) incrementally.</span>
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/mean`][@stdlib/stats/incr/mean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally.</span>
149+
- <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>
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/mape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mape
164+
165+
[@stdlib/stats/incr/me]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/me
166+
167+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
168+
169+
[@stdlib/stats/incr/mmae]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmae
170+
171+
<!-- </related-links> -->
172+
173+
</section>
174+
175+
<!-- /.links -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var incrnanmae = require( './../lib' ); // Corrected from incrmae
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 = incrnanmae();
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( pkg+'::accumulator', function benchmark( b ) {
51+
var acc;
52+
var v;
53+
var i;
54+
var x;
55+
var y;
56+
57+
acc = incrnanmae();
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
x = ( randu() < 0.2 ) ? NaN : randu()-0.5;
62+
y = ( randu() < 0.2 ) ? NaN : randu()-0.5;
63+
v = acc( x, y );
64+
if ( v !== v && !isnan( x ) && !isnan( y ) ) {
65+
b.fail( 'should not return NaN when inputs are not NaN' );
66+
}
67+
}
68+
b.toc();
69+
if ( v !== v && !isnan( x ) && !isnan( y ) ) {
70+
b.fail( 'should not return NaN when inputs are not NaN' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
});
75+
76+
bench( pkg+'::accumulator,nan_handling', function benchmark( b ) {
77+
var acc;
78+
var v;
79+
var i;
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
acc = incrnanmae(); // Reset accumulator for each iteration
84+
v = acc(); // Initial state
85+
if ( v !== null ) {
86+
b.fail( 'initial value should be null' );
87+
}
88+
acc( 2.0, 3.0 ); // Set a baseline MAE
89+
v = acc( NaN, randu()-0.5 );
90+
if ( typeof v !== 'number' || isnan( v ) ) {
91+
b.fail( 'should return a number when one input is NaN' );
92+
}
93+
v = acc( randu()-0.5, NaN );
94+
if ( typeof v !== 'number' || isnan( v ) ) {
95+
b.fail( 'should return a number when one input is NaN' );
96+
}
97+
v = acc( NaN, NaN );
98+
if ( typeof v !== 'number' || isnan( v ) ) {
99+
b.fail( 'should return a number when both inputs are NaN' );
100+
}
101+
}
102+
b.toc();
103+
b.pass( 'benchmark finished' );
104+
b.end();
105+
});
Lines changed: 54 additions & 0 deletions
Loading
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes the mean
4+
absolute error, ignoring `NaN` values.
5+
6+
If provided two values, the accumulator function returns an updated mean
7+
absolute error. If not provided values, the accumulator function returns
8+
the current mean absolute error.
9+
10+
Returns
11+
-------
12+
acc: Function
13+
Accumulator function.
14+
15+
Examples
16+
--------
17+
> var incrnanmae = require( '@stdlib/stats/incr/nanmae' );
18+
> var accumulator = incrnanmae();
19+
> var m = accumulator()
20+
null
21+
> m = accumulator( 2.0, 3.0 )
22+
1.0
23+
> m = accumulator( NaN, 2.0 )
24+
1.0
25+
> m = accumulator( -5.0, NaN )
26+
1.0
27+
> m = accumulator( NaN, NaN )
28+
1.0
29+
> m = accumulator( -5.0, 2.0 )
30+
4.0
31+
> m = accumulator()
32+
4.0
33+
34+
See Also
35+
--------
36+

0 commit comments

Comments
 (0)