Skip to content

Commit 4cde934

Browse files
committed
feat: add stats/incr/nanmse
1 parent 9e91f81 commit 4cde934

File tree

11 files changed

+841
-0
lines changed

11 files changed

+841
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
# 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/nanmse/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( 1.0, NaN );
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+
95+
</section>
96+
97+
<!-- /.notes -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var randu = require( '@stdlib/random/base/randu' );
107+
var incrnanmse = require( '@stdlib/stats/incr/nanmse' );
108+
109+
var accumulator;
110+
var v1;
111+
var v2;
112+
var i;
113+
114+
// Initialize an accumulator:
115+
accumulator = incrnanmse();
116+
117+
// For each simulated datum, update the mean squared error...
118+
for ( i = 0; i < 100; i++ ) {
119+
if ( randu() < 0.2 ) {
120+
v1 = NaN;
121+
v2 = NaN;
122+
} else {
123+
v1 = ( randu()*100.0 ) - 50.0;
124+
v2 = ( randu()*100.0 ) - 50.0;
125+
}
126+
accumulator( v1, v2 );
127+
}
128+
console.log( accumulator() );
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
136+
137+
<section class="related">
138+
139+
* * *
140+
141+
## See Also
142+
143+
- <span class="package-name">[`@stdlib/stats/incr/mmse`][@stdlib/stats/incr/mmse]</span><span class="delimiter">: </span><span class="description">compute a moving mean squared error (MSE) incrementally.</span>
144+
- <span class="package-name">[`@stdlib/stats/incr/rmse`][@stdlib/stats/incr/rmse]</span><span class="delimiter">: </span><span class="description">compute the root mean squared error (RMSE) incrementally.</span>
145+
- <span class="package-name">[`@stdlib/stats/incr/rss`][@stdlib/stats/incr/rss]</span><span class="delimiter">: </span><span class="description">compute the residual sum of squares (RSS) incrementally.</span>
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
[mean-squared-error]: https://en.wikipedia.org/wiki/Mean_squared_error
156+
157+
<!-- <related-links> -->
158+
159+
[@stdlib/stats/incr/mmse]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmse
160+
161+
[@stdlib/stats/incr/rmse]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/rmse
162+
163+
[@stdlib/stats/incr/rss]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/rss
164+
165+
<!-- </related-links> -->
166+
167+
</section>
168+
169+
<!-- /.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 incrnanmse = 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 = incrnanmse();
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 = incrnanmse();
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: 60 additions & 0 deletions
Loading
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
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( 1.0, NaN )
23+
1.0
24+
> m = accumulator( -5.0, 2.0 )
25+
25.0
26+
> m = accumulator()
27+
25.0
28+
29+
See Also
30+
--------
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 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+
* @returns accumulator function
36+
*
37+
* @example
38+
* var accumulator = incrnanmse();
39+
*
40+
* var m = accumulator();
41+
* // returns null
42+
*
43+
* m = accumulator( 2.0, 3.0 );
44+
* // returns 1.0
45+
*
46+
*
47+
* m = accumulator( 1.0, NaN );
48+
* // returns 1.0
49+
*
50+
* m = accumulator( -5.0, 2.0 );
51+
* // returns 25.0
52+
*
53+
* m = accumulator();
54+
* // returns 25.0
55+
*/
56+
declare function incrnanmse(): accumulator;
57+
58+
59+
// EXPORTS //
60+
61+
export = incrnanmse;

0 commit comments

Comments
 (0)