Skip to content

Commit 0565fc5

Browse files
committed
feat: add stats/incr/nanmse
Signed-off-by: wilmerdooley <wilmerdooley1@gmail.com>
1 parent cf7b40a commit 0565fc5

14 files changed

Lines changed: 918 additions & 0 deletions

File tree

lib/node_modules/@stdlib/stats/incr/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ var incr = ns;
118118
- <span class="signature">[`incrnanmaxabs()`][@stdlib/stats/incr/nanmaxabs]</span><span class="delimiter">: </span><span class="description">compute a maximum absolute value incrementally, ignoring `NaN` values.</span>
119119
- <span class="signature">[`incrnanmean()`][@stdlib/stats/incr/nanmean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally, ignoring `NaN` values.</span>
120120
- <span class="signature">[`incrnanmeanabs()`][@stdlib/stats/incr/nanmeanabs]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean of absolute values incrementally, ignoring `NaN` values.</span>
121+
- <span class="signature">[`incrnanmse()`][@stdlib/stats/incr/nanmse]</span><span class="delimiter">: </span><span class="description">compute the mean squared error (MSE) incrementally, ignoring `NaN` values.</span>
121122
- <span class="signature">[`incrnanmstdev( window[, mean] )`][@stdlib/stats/incr/nanmstdev]</span><span class="delimiter">: </span><span class="description">compute a moving corrected sample standard deviation incrementally, ignoring NaN values.</span>
122123
- <span class="signature">[`incrnanmsum( window )`][@stdlib/stats/incr/nanmsum]</span><span class="delimiter">: </span><span class="description">compute a moving sum incrementally, ignoring `NaN` values.</span>
123124
- <span class="signature">[`incrnanskewness()`][@stdlib/stats/incr/nanskewness]</span><span class="delimiter">: </span><span class="description">compute a corrected sample skewness incrementally, ignoring `NaN` values.</span>
@@ -336,6 +337,8 @@ console.log( getKeys( ns ) );
336337

337338
[@stdlib/stats/incr/nanmeanabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmeanabs
338339

340+
[@stdlib/stats/incr/nanmse]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmse
341+
339342
[@stdlib/stats/incr/nanmstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmstdev
340343

341344
[@stdlib/stats/incr/nanmsum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmsum

lib/node_modules/@stdlib/stats/incr/docs/types/index.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import incrnancount = require( '@stdlib/stats/incr/nancount' );
9595
import incrnanmaxabs = require( '@stdlib/stats/incr/nanmaxabs' );
9696
import incrnanmean = require( '@stdlib/stats/incr/nanmean' );
9797
import incrnanmeanabs = require( '@stdlib/stats/incr/nanmeanabs' );
98+
import incrnanmse = require( '@stdlib/stats/incr/nanmse' );
9899
import incrnanmstdev = require( '@stdlib/stats/incr/nanmstdev' );
99100
import incrnanmsum = require( '@stdlib/stats/incr/nanmsum' );
100101
import incrnanskewness = require( '@stdlib/stats/incr/nanskewness' );
@@ -2404,6 +2405,31 @@ interface Namespace {
24042405
*/
24052406
incrnanmeanabs: typeof incrnanmeanabs;
24062407

2408+
/**
2409+
* Returns an accumulator function which incrementally computes the mean squared error, ignoring `NaN` values.
2410+
*
2411+
* @returns accumulator function
2412+
*
2413+
* @example
2414+
* var accumulator = ns.incrnanmse();
2415+
*
2416+
* var m = accumulator();
2417+
* // returns null
2418+
*
2419+
* m = accumulator( 2.0, 3.0 );
2420+
* // returns 1.0
2421+
*
2422+
* m = accumulator( NaN, 2.0 );
2423+
* // returns 1.0
2424+
*
2425+
* m = accumulator( -5.0, 2.0 );
2426+
* // returns 25.0
2427+
*
2428+
* m = accumulator();
2429+
* // returns 25.0
2430+
*/
2431+
incrnanmse: typeof incrnanmse;
2432+
24072433
/**
24082434
* Returns an accumulator function which incrementally computes a moving corrected sample standard deviation, ignoring NaN values.
24092435
*

lib/node_modules/@stdlib/stats/incr/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,15 @@ setReadOnly( ns, 'incrnanmean', require( '@stdlib/stats/incr/nanmean' ) );
711711
*/
712712
setReadOnly( ns, 'incrnanmeanabs', require( '@stdlib/stats/incr/nanmeanabs' ) );
713713

714+
/**
715+
* @name incrnanmse
716+
* @memberof ns
717+
* @readonly
718+
* @type {Function}
719+
* @see {@link module:@stdlib/stats/incr/nanmse}
720+
*/
721+
setReadOnly( ns, 'incrnanmse', require( '@stdlib/stats/incr/nanmse' ) );
722+
714723
/**
715724
* @name incrnanmstdev
716725
* @memberof ns
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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
20+
'use strict';
21+
22+
// MODULES //
23+
24+
var bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var incrnanmse = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var f;
35+
var i;
36+
b.tic();
37+
for ( i = 0; i < b.iterations; i++ ) {
38+
f = incrnanmse();
39+
if ( typeof f !== 'function' ) {
40+
b.fail( 'should return a function' );
41+
}
42+
}
43+
b.toc();
44+
if ( typeof f !== 'function' ) {
45+
b.fail( 'should return a function' );
46+
}
47+
b.pass( 'benchmark finished' );
48+
b.end();
49+
});
50+
51+
bench( format( '%s::accumulator', pkg ), function benchmark( b ) {
52+
var acc;
53+
var v;
54+
var i;
55+
56+
acc = incrnanmse();
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
v = acc( randu()-0.5, randu()-0.5 );
61+
if ( v !== v ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
}
65+
b.toc();
66+
if ( v !== v ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
});
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+

0 commit comments

Comments
 (0)