Skip to content

Commit 9bec44a

Browse files
coehemangPlaneshifterstdlib-bot
authored
feat: add stats/incr/nanmstdev
PR-URL: #6297 Closes: #5609 Co-authored-by: Philipp Burckhardt <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]>
1 parent 053722e commit 9bec44a

File tree

11 files changed

+1324
-0
lines changed

11 files changed

+1324
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
# incrnanmstdev
22+
23+
> Compute a moving [corrected sample standard deviation][standard-deviation] incrementally, ignoring NaN values.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, the [corrected sample standard deviation][standard-deviation] is defined as
28+
29+
<!-- <equation class="equation" label="eq:corrected_sample_standard_deviation" align="center" raw="s = \sqrt{\frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2}" alt="Equation for the corrected sample standard deviation."> -->
30+
31+
```math
32+
s = \sqrt{\frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="s = \sqrt{\frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2}" data-equation="eq:corrected_sample_standard_deviation">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mstdev/docs/img/equation_corrected_sample_standard_deviation.svg" alt="Equation for the corrected sample standard deviation.">
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 incrnanmstdev = require( '@stdlib/stats/incr/nanmstdev' );
52+
```
53+
54+
#### incrnanmstdev( window\[, mean] )
55+
56+
Returns an accumulator `function` which incrementally computes a moving [corrected sample standard deviation][standard-deviation], ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving [corrected sample standard deviation][standard-deviation].
57+
58+
```javascript
59+
var accumulator = incrnanmstdev( 3 );
60+
```
61+
62+
If the mean is already known, provide a `mean` argument.
63+
64+
```javascript
65+
var accumulator = incrnanmstdev( 3, 5.0 );
66+
```
67+
68+
#### accumulator( \[x] )
69+
70+
If provided an input value `x`, the accumulator function returns an updated [corrected sample standard deviation][standard-deviation]. If not provided an input value `x`, the accumulator function returns the current [corrected sample standard deviation][standard-deviation]. If provided `NaN`, the accumulator function ignores the value and returns the current [corrected sample standard deviation][standard-deviation] without updating the window.
71+
72+
```javascript
73+
var accumulator = incrnanmstdev( 3 );
74+
75+
var s = accumulator();
76+
// returns null
77+
78+
// Fill the window with non-NaN values...
79+
s = accumulator( 2.0 ); // [2.0]
80+
// returns 0.0
81+
82+
s = accumulator( NaN ); // [2.0]
83+
// returns 0.0
84+
85+
s = accumulator( 3.0 ); // [2.0, 3.0]
86+
// returns ~0.7071
87+
88+
s = accumulator( 5.0 ); // [2.0, 3.0, 5.0]
89+
// returns ~1.53
90+
91+
// Window begins sliding...
92+
s = accumulator( -7.0 ); // [3.0, 5.0, -7.0]
93+
// returns ~6.43
94+
95+
s = accumulator( NaN ); // [3.0, 5.0, -7.0]
96+
// returns ~6.43
97+
98+
s = accumulator( -5.0 ); // [5.0, -7.0, -5.0]
99+
// returns ~6.43
100+
101+
s = accumulator();
102+
// returns ~6.43
103+
```
104+
105+
</section>
106+
107+
<!-- /.usage -->
108+
109+
<section class="notes">
110+
111+
## Notes
112+
113+
- Input values are **not** type checked. If provided `NaN`, the value is ignored and the accumulator function returns the current [corrected sample standard deviation][standard-deviation] without updating the window. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
114+
- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values (excluding NaN values).
115+
116+
</section>
117+
118+
<!-- /.notes -->
119+
120+
<section class="examples">
121+
122+
## Examples
123+
124+
<!-- eslint no-undef: "error" -->
125+
126+
```javascript
127+
var randu = require( '@stdlib/random/base/randu' );
128+
var incrnanmstdev = require( '@stdlib/stats/incr/nanmstdev' );
129+
130+
var accumulator;
131+
var v;
132+
var i;
133+
134+
// Initialize an accumulator:
135+
accumulator = incrnanmstdev( 5 );
136+
137+
// For each simulated datum, update the moving corrected sample standard deviation...
138+
for ( i = 0; i < 100; i++ ) {
139+
if ( randu() < 0.2 ) {
140+
v = NaN;
141+
} else {
142+
v = randu() * 100.0;
143+
}
144+
accumulator( v );
145+
}
146+
console.log( accumulator() );
147+
```
148+
149+
</section>
150+
151+
<!-- /.examples -->
152+
153+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
154+
155+
<section class="related">
156+
157+
</section>
158+
159+
<!-- /.related -->
160+
161+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
162+
163+
<section class="links">
164+
165+
[standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation
166+
167+
<!-- <related-links> -->
168+
169+
[@stdlib/stats/incr/mstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mstdev
170+
171+
[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean
172+
173+
[@stdlib/stats/incr/msummary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/msummary
174+
175+
[@stdlib/stats/incr/mvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mvariance
176+
177+
[@stdlib/stats/incr/stdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/stdev
178+
179+
<!-- </related-links> -->
180+
181+
</section>
182+
183+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 incrnanmstdev = 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 = incrnanmstdev( (i%5)+1 );
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 = incrnanmstdev( 5 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
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+
});
70+
71+
bench( pkg+'::accumulator,known_mean', function benchmark( b ) {
72+
var acc;
73+
var v;
74+
var i;
75+
76+
acc = incrnanmstdev( 5, 3.0 );
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
v = acc( randu() );
81+
if ( v !== v ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
}
85+
b.toc();
86+
if ( v !== v ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
});

0 commit comments

Comments
 (0)