Skip to content

Commit 692e1d7

Browse files
authored
feat: add stats/incr/nanmse
PR-URL: #12349 Closes: #5608 Ref: #5599 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: wilmerdooley <wilmerdooley1@gmail.com>
1 parent a2a20a2 commit 692e1d7

11 files changed

Lines changed: 877 additions & 0 deletions

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
# incrnanmse
22+
23+
> Compute the [mean squared error][mean-squared-error] (MSE) incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [mean squared error][mean-squared-error] is defined as
28+
29+
<!-- <equation class="equation" label="eq:mean_squared_error" align="center" raw="\operatorname{MSE} = \frac{1}{n} \sum_{i=0}^{n-1} (y_i - x_i)^2" alt="Equation for the mean squared error."> -->
30+
31+
```math
32+
\mathop{\mathrm{MSE}} = \frac{1}{n} \sum_{i=0}^{n-1} (y_i - x_i)^2
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{MSE} = \frac{1}{n} \sum_{i=0}^{n-1} (y_i - x_i)^2" data-equation="eq:mean_squared_error">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@f5d4f0cac0a117ba1e0c70706a2fb284f69e7291/lib/node_modules/@stdlib/stats/incr/mse/docs/img/equation_mean_squared_error.svg" alt="Equation for the mean squared 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 incrnanmse = require( '@stdlib/stats/incr/nanmse' );
52+
```
53+
54+
#### incrnanmse()
55+
56+
Returns an accumulator `function` which incrementally computes the [mean squared error][mean-squared-error], ignoring `NaN` values.
57+
58+
```javascript
59+
var accumulator = incrnanmse();
60+
```
61+
62+
#### accumulator( \[x, y] )
63+
64+
If provided input values `x` and `y`, the accumulator function returns an updated [mean squared error][mean-squared-error]. If not provided input values `x` and `y`, the accumulator function returns the current [mean squared error][mean-squared-error].
65+
66+
```javascript
67+
var accumulator = incrnanmse();
68+
69+
var m = accumulator( 2.0, 3.0 );
70+
// returns 1.0
71+
72+
m = accumulator( NaN, 5.0 );
73+
// returns 1.0
74+
75+
m = accumulator( -1.0, -4.0 );
76+
// returns 5.0
77+
78+
m = accumulator( -3.0, 5.0 );
79+
// returns ~24.67
80+
81+
m = accumulator();
82+
// returns ~24.67
83+
```
84+
85+
</section>
86+
87+
<!-- /.usage -->
88+
89+
<section class="notes">
90+
91+
## Notes
92+
93+
- 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.
94+
- If either input value is `NaN`, the accumulator function ignores the input value and returns the current [mean squared error][mean-squared-error].
95+
96+
</section>
97+
98+
<!-- /.notes -->
99+
100+
<section class="examples">
101+
102+
## Examples
103+
104+
<!-- eslint no-undef: "error" -->
105+
106+
```javascript
107+
var randu = require( '@stdlib/random/base/randu' );
108+
var incrnanmse = require( '@stdlib/stats/incr/nanmse' );
109+
110+
var accumulator;
111+
var v1;
112+
var v2;
113+
var i;
114+
115+
// Initialize an accumulator:
116+
accumulator = incrnanmse();
117+
118+
// For each simulated datum, update the mean squared error...
119+
for ( i = 0; i < 100; i++ ) {
120+
if ( randu() < 0.2 ) {
121+
v1 = NaN;
122+
v2 = NaN;
123+
} else {
124+
v1 = ( randu()*100.0 ) - 50.0;
125+
v2 = ( randu()*100.0 ) - 50.0;
126+
}
127+
accumulator( v1, v2 );
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+
</section>
141+
142+
<!-- /.related -->
143+
144+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
145+
146+
<section class="links">
147+
148+
[mean-squared-error]: https://en.wikipedia.org/wiki/Mean_squared_error
149+
150+
<!-- <related-links> -->
151+
152+
<!-- </related-links> -->
153+
154+
</section>
155+
156+
<!-- /.links -->
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 incrnanmse = 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 = incrnanmse();
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 = incrnanmse();
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+
});
Lines changed: 60 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
4+
squared error (MSE), ignoring `NaN` values.
5+
6+
If provided input values, the accumulator function returns an updated mean
7+
squared error. If not provided input values, the accumulator function
8+
returns the current mean squared error.
9+
10+
If either input value is `NaN`, the input value is ignored.
11+
12+
Returns
13+
-------
14+
acc: Function
15+
Accumulator function.
16+
17+
Examples
18+
--------
19+
> var accumulator = {{alias}}();
20+
> var m = accumulator()
21+
null
22+
> m = accumulator( 2.0, 3.0 )
23+
1.0
24+
> m = accumulator( NaN, 2.0 )
25+
1.0
26+
> m = accumulator( -5.0, 2.0 )
27+
25.0
28+
> m = accumulator()
29+
25.0
30+
31+
See Also
32+
--------
33+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 squared error. If not provided input values, the accumulator function returns the current mean squared error.
25+
*
26+
* @param x - input value
27+
* @param y - input value
28+
* @returns mean squared 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 squared error, ignoring `NaN` values.
34+
*
35+
* ## Notes
36+
*
37+
* - If either input value is `NaN`, the accumulator function ignores the input value and returns the current mean squared error.
38+
*
39+
* @returns accumulator function
40+
*
41+
* @example
42+
* var accumulator = incrnanmse();
43+
*
44+
* var m = accumulator();
45+
* // returns null
46+
*
47+
* m = accumulator( 2.0, 3.0 );
48+
* // returns 1.0
49+
*
50+
* m = accumulator( NaN, 2.0 );
51+
* // returns 1.0
52+
*
53+
* m = accumulator( -5.0, 2.0 );
54+
* // returns 25.0
55+
*
56+
* m = accumulator();
57+
* // returns 25.0
58+
*/
59+
declare function incrnanmse(): accumulator;
60+
61+
62+
// EXPORTS //
63+
64+
export = incrnanmse;

0 commit comments

Comments
 (0)