Skip to content

Commit 12967cb

Browse files
committed
Added nanmstdev
1 parent 551a3e9 commit 12967cb

File tree

11 files changed

+1317
-0
lines changed

11 files changed

+1317
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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].
71+
72+
```javascript
73+
var accumulator = incrnanmstdev( 3 );
74+
75+
var s = accumulator();
76+
// returns null
77+
78+
// Fill the window...
79+
s = accumulator( 2.0 ); // [2.0]
80+
// returns 0.0
81+
82+
s = accumulator( NaN ); // [2.0, NaN]
83+
// returns 0.0
84+
85+
s = accumulator( 3.0 ); // [2.0, NaN, 3.0]
86+
// returns ~0.7071
87+
88+
// Window begins sliding...
89+
s = accumulator( -7.0 ); // [NaN, 3.0, -7.0]
90+
// returns ~7.07
91+
92+
s = accumulator( -5.0 ); // [3.0, -7.0, -5.0]
93+
// returns ~5.29
94+
95+
s = accumulator();
96+
// returns ~5.29
97+
```
98+
99+
</section>
100+
101+
<!-- /.usage -->
102+
103+
<section class="notes">
104+
105+
## Notes
106+
107+
- 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]. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
108+
- 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).
109+
110+
</section>
111+
112+
<!-- /.notes -->
113+
114+
<section class="examples">
115+
116+
## Examples
117+
118+
<!-- eslint no-undef: "error" -->
119+
120+
```javascript
121+
var randu = require( '@stdlib/random/base/randu' );
122+
var incrnanmstdev = require( '@stdlib/stats/incr/nanmstdev' );
123+
124+
var accumulator;
125+
var v;
126+
var i;
127+
128+
// Initialize an accumulator:
129+
accumulator = incrnanmstdev( 5 );
130+
131+
// For each simulated datum, update the moving corrected sample standard deviation...
132+
for ( i = 0; i < 100; i++ ) {
133+
if ( randu() < 0.2 ) {
134+
v = NaN;
135+
} else {
136+
v = randu() * 100.0;
137+
}
138+
accumulator( v );
139+
}
140+
console.log( accumulator() );
141+
```
142+
143+
</section>
144+
145+
<!-- /.examples -->
146+
147+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
148+
149+
<section class="related">
150+
151+
* * *
152+
153+
## See Also
154+
155+
- <span class="package-name">[`@stdlib/stats/incr/mstdev`][@stdlib/stats/incr/mstdev]</span><span class="delimiter">: </span><span class="description">compute a moving corrected sample standard deviation incrementally.</span>
156+
- <span class="package-name">[`@stdlib/stats/incr/nanmmean`][@stdlib/stats/incr/nanmmean]</span><span class="delimiter">: </span><span class="description">compute a moving arietic mean incrementally, iging NaN values.</span>
157+
- <span class="package-name">[`@stdlib/stats/incr/msummary`][@stdlib/stats/incr/msummary]</span><span class="delimiter">: </span><span class="description">compute a moving statistical summary incrementally, ignoring NaN values.</span>
158+
- <span class="package-name">[`@stdlib/stats/incr/mvariance`][@stdlib/stats/incr/mvariance]</span><span class="delimiter">: </span><span class="description">compute a moving unbiased sample variance incrementally, ignoring NaN values.</span>
159+
- <span class="package-name">[`@stdlib/stats/incr/stdev`][@stdlib/stats/incr/stdev]</span><span class="delimiter">: </span><span class="description">compute a corrected sample standard deviation incrementally, ignoring NaN values.</span>
160+
161+
</section>
162+
163+
<!-- /.related -->
164+
165+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
166+
167+
<section class="links">
168+
169+
[standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation
170+
171+
<!-- <related-links> -->
172+
173+
[@stdlib/stats/incr/mstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mstdev
174+
175+
[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean
176+
177+
[@stdlib/stats/incr/msummary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/msummary
178+
179+
[@stdlib/stats/incr/mvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mvariance
180+
181+
[@stdlib/stats/incr/stdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/stdev
182+
183+
<!-- </related-links> -->
184+
185+
</section>
186+
187+
<!-- /.links -->
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
});
Lines changed: 73 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)