Skip to content

Commit a76815f

Browse files
committed
feat(stats): add nanme package
1 parent 38b39db commit a76815f

File tree

11 files changed

+843
-0
lines changed

11 files changed

+843
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
# incrnanme
22+
23+
> Compute the [mean error][mean-absolute-error] (ME) incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [mean error][mean-absolute-error] is defined as
28+
29+
<!-- <equation class="equation" label="eq:mean_error" align="center" raw="\operatorname{ME} = \frac{1}{n} \sum_{i=0}^{n-1} (y_i - x_i)" alt="Equation for the mean error."> -->
30+
31+
```math
32+
\mathop{\mathrm{ME}} = \frac{1}{n} \sum_{i=0}^{n-1} (y_i - x_i)
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{ME} = \frac{1}{n} \sum_{i=0}^{n-1} (y_i - x_i)" data-equation="eq:mean_error">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@7d6e6319f451be0997d35a6cf491b08e1f2cb5cf/lib/node_modules/@stdlib/stats/incr/nanme/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 incrnanme = require( '@stdlib/stats/incr/nanme' );
52+
```
53+
54+
#### incrnanme()
55+
56+
Returns an accumulator `function` which incrementally computes the [mean error][mean-absolute-error], ignoring `NaN` values.
57+
58+
```javascript
59+
var accumulator = incrnanme();
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 = incrnanme();
68+
69+
var m = accumulator( 2.0, 3.0 );
70+
// returns 1.0
71+
72+
m = accumulator( -1.0, -4.0 );
73+
// returns -1.0
74+
75+
m = accumulator( -3.0, 5.0 );
76+
// returns 2.0
77+
78+
m = accumulator();
79+
// returns 2.0
80+
```
81+
82+
</section>
83+
84+
<!-- /.usage -->
85+
86+
<section class="notes">
87+
88+
## Notes
89+
90+
- 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.
91+
- 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.
92+
- **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.
93+
94+
</section>
95+
96+
<!-- /.notes -->
97+
98+
<section class="examples">
99+
100+
## Examples
101+
102+
<!-- eslint no-undef: "error" -->
103+
104+
```javascript
105+
var randu = require( '@stdlib/random/base/randu' );
106+
var incrnanme = require( '@stdlib/stats/incr/nanme' );
107+
108+
var accumulator;
109+
var v1;
110+
var v2;
111+
var i;
112+
113+
// Initialize an accumulator:
114+
accumulator = incrnanme();
115+
116+
// For each simulated datum, update the mean error...
117+
for ( i = 0; i < 100; i++ ) {
118+
if( randu() < 0.2 ){
119+
v1 = NaN;
120+
} else {
121+
v1 = ( randu()*100.0 ) - 50.0;
122+
}
123+
if( randu() < 0.2 ){
124+
v2 = NaN;
125+
} else {
126+
v2 = ( randu()*100.0 ) - 50.0;
127+
}
128+
}
129+
console.log( accumulator() );
130+
```
131+
132+
</section>
133+
134+
<!-- /.examples -->
135+
136+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
137+
138+
<section class="related">
139+
140+
* * *
141+
142+
## See Also
143+
144+
- <span class="package-name">[`@stdlib/stats/incr/mae`][@stdlib/stats/incr/mae]</span><span class="delimiter">: </span><span class="description">compute the mean absolute error (MAE) incrementally.</span>
145+
- <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>
146+
- <span class="package-name">[`@stdlib/stats/incr/mme`][@stdlib/stats/incr/mme]</span><span class="delimiter">: </span><span class="description">compute a moving mean error (ME) incrementally.</span>
147+
148+
</section>
149+
150+
<!-- /.related -->
151+
152+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="links">
155+
156+
[mean-absolute-error]: https://en.wikipedia.org/wiki/Mean_absolute_error
157+
158+
<!-- <related-links> -->
159+
160+
[@stdlib/stats/incr/mae]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mae
161+
162+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
163+
164+
[@stdlib/stats/incr/mme]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mme
165+
166+
<!-- </related-links> -->
167+
168+
</section>
169+
170+
<!-- /.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) 2025 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 incrnanme = require( '@stdlib/stats/incr/nanme/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 = incrnanme();
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 = incrnanme();
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: 53 additions & 0 deletions
Loading
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes the mean error
4+
(ME), ignoring NaN values.
5+
6+
If provided input values, the accumulator function returns an updated mean
7+
error. If not provided input values, the accumulator function returns the
8+
current mean 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+
1.0
22+
> m = accumulator( -5.0, 2.0 )
23+
4.0
24+
> m = accumulator( -3.0, NaN )
25+
4.0
26+
> m = accumulator( NaN, 2.0 )
27+
4.0
28+
> m = accumulator()
29+
4.0
30+
31+
See Also
32+
--------
33+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
* @param x - input value
27+
* @param y - input value
28+
* @returns mean error or null
29+
*/
30+
type accumulator = ( x?: number, y?: number ) => number | null;
31+
32+
/**
33+
* Returns an accumulator function which incrementally computes the mean error, ignoring `NaN` values.
34+
*
35+
* @returns accumulator function
36+
*
37+
* @example
38+
* var accumulator = incrnanme();
39+
*
40+
* var m = accumulator();
41+
* // returns null
42+
*
43+
* m = accumulator( 2.0, 3.0 );
44+
* // returns 1.0
45+
*
46+
* m = accumulator( -5.0, 2.0 );
47+
* // returns 4.0
48+
*
49+
* m = accumulator( -3.0, NaN );
50+
* // returns 4.0
51+
*
52+
* m = accumulator( NaN, 2.0 );
53+
* // returns 4.0
54+
*
55+
* m = accumulator();
56+
* // returns 4.0
57+
*/
58+
declare function incrnanme(): accumulator;
59+
60+
61+
// EXPORTS //
62+
63+
export = incrnanme;

0 commit comments

Comments
 (0)