Skip to content

Commit 05d2e1d

Browse files
committed
feat: add incremental corrected sample standard deviation function that ignores NaN values
1 parent f9bb0d7 commit 05d2e1d

File tree

12 files changed

+906
-0
lines changed

12 files changed

+906
-0
lines changed

lib/node_modules/@stdlib/stats/incr/nanapcorr/lib/main.js

Whitespace-only changes.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
# incrstdev
22+
23+
> Compute a [corrected sample standard deviation][sample-stdev] incrementally.
24+
25+
<section class="intro">
26+
27+
The [corrected sample standard deviation][sample-stdev] is defined as
28+
29+
<!-- <equation class="equation" label="eq:corrected_sample_standard_deviation" align="center" raw="s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2}" alt="Equation for the corrected sample standard deviation."> -->
30+
31+
```math
32+
s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-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/stdev/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 incrstdev = require( '@stdlib/stats/incr/nanstdev' );
52+
```
53+
54+
#### incrstdev( \[mean] )
55+
56+
Returns an accumulator `function` which incrementally computes a [corrected sample standard deviation][sample-stdev].
57+
58+
```javascript
59+
var accumulator = incrnanstdev();
60+
```
61+
62+
If the mean is already known, provide a `mean` argument.
63+
64+
```javascript
65+
var accumulator = incrnanstdev( 3.0 );
66+
```
67+
68+
#### accumulator( \[x] )
69+
70+
If provided an input value `x`, the accumulator function returns an updated [corrected sample standard deviation][sample-stdev]. If not provided an input value `x`, the accumulator function returns the current [corrected sample standard deviation][sample-stdev].
71+
72+
```javascript
73+
var accumulator = incrnanstdev();
74+
75+
var s = accumulator( 2.0 );
76+
// returns 0.0
77+
78+
s = accumulator( 1.0 ); // => sqrt(((2-1.5)^2+(1-1.5)^2) / (2-1))
79+
// returns ~0.7071
80+
81+
s = accumulator( NaN ); // Ignored
82+
// return ~0.7071
83+
84+
s = accumulator( 3.0 ); // => sqrt(((2-2)^2+(1-2)^2+(3-2)^2) / (3-1))
85+
// returns 1.0
86+
87+
s = accumulator();
88+
// returns 1.0
89+
```
90+
91+
</section>
92+
93+
<!-- /.usage -->
94+
95+
<section class="notes">
96+
97+
## Notes
98+
99+
- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, It will be Ignored Automatically without any errors but you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
100+
101+
</section>
102+
103+
<!-- /.notes -->
104+
105+
<section class="examples">
106+
107+
## Examples
108+
109+
<!-- eslint no-undef: "error" -->
110+
111+
```javascript
112+
var randu = require( '@stdlib/random/base/randu' );
113+
var incrnanstdev = require( './../lib' );
114+
115+
var accumulator;
116+
var s;
117+
var v;
118+
var i;
119+
120+
// Initialize an accumulator:
121+
accumulator = incrnanstdev();
122+
123+
// For each simulated datum, update the corrected sample standard deviation...
124+
for ( i = 0; i < 100; i++ ) {
125+
v = randu() * 100.0;
126+
127+
// Introduce NaN values randomly to test the accumulator
128+
if ( randu() < 0.1 ) { // 10% chance to insert NaN
129+
v = NaN;
130+
}
131+
132+
s = accumulator( v );
133+
}
134+
console.log( '\nFinal standard deviation: %d\n', accumulator() );
135+
```
136+
137+
</section>
138+
139+
<!-- /.examples -->
140+
141+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
142+
143+
<section class="related">
144+
145+
* * *
146+
147+
## See Also
148+
149+
- <span class="package-name">[`@stdlib/stats/incr/kurtosis`][@stdlib/stats/incr/kurtosis]</span><span class="delimiter">: </span><span class="description">compute a corrected sample excess kurtosis incrementally.</span>
150+
- <span class="package-name">[`@stdlib/stats/incr/mean`][@stdlib/stats/incr/mean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally.</span>
151+
- <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>
152+
- <span class="package-name">[`@stdlib/stats/incr/skewness`][@stdlib/stats/incr/skewness]</span><span class="delimiter">: </span><span class="description">compute a corrected sample skewness incrementally.</span>
153+
- <span class="package-name">[`@stdlib/stats/incr/summary`][@stdlib/stats/incr/summary]</span><span class="delimiter">: </span><span class="description">compute a statistical summary incrementally.</span>
154+
- <span class="package-name">[`@stdlib/stats/incr/variance`][@stdlib/stats/incr/variance]</span><span class="delimiter">: </span><span class="description">compute an unbiased sample variance incrementally.</span>
155+
156+
</section>
157+
158+
<!-- /.related -->
159+
160+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="links">
163+
164+
[sample-stdev]: https://en.wikipedia.org/wiki/Standard_deviation
165+
166+
<!-- <related-links> -->
167+
168+
[@stdlib/stats/incr/kurtosis]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/kurtosis
169+
170+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
171+
172+
[@stdlib/stats/incr/mstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mstdev
173+
174+
[@stdlib/stats/incr/skewness]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/skewness
175+
176+
[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary
177+
178+
[@stdlib/stats/incr/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/variance
179+
180+
<!-- </related-links> -->
181+
182+
</section>
183+
184+
<!-- /.links -->
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 incrnanstdev = require( './../lib' );
27+
28+
// MAIN //
29+
30+
bench( pkg, function benchmark( b ) {
31+
var f;
32+
var i;
33+
b.tic();
34+
for ( i = 0; i < b.iterations; i++ ) {
35+
f = incrnanstdev();
36+
if ( typeof f !== 'function' ) {
37+
b.fail( 'should return a function' );
38+
}
39+
}
40+
b.toc();
41+
if ( typeof f !== 'function' ) {
42+
b.fail( 'should return a function' );
43+
}
44+
b.pass( 'benchmark finished' );
45+
b.end();
46+
});
47+
48+
bench( pkg+'::accumulator', function benchmark( b ) {
49+
var acc;
50+
var v;
51+
var i;
52+
53+
acc = incrnanstdev();
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = acc( randu() );
58+
if ( v !== v ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( v !== v ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});
69+
70+
bench( pkg+'::accumulator,known_mean', function benchmark( b ) {
71+
var acc;
72+
var v;
73+
var i;
74+
75+
acc = incrnanstdev( 3.0 );
76+
77+
b.tic();
78+
for ( i = 0; i < b.iterations; i++ ) {
79+
v = acc( randu() );
80+
if ( v !== v ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
}
84+
b.toc();
85+
if ( v !== v ) {
86+
b.fail( 'should not return NaN' );
87+
}
88+
b.pass( 'benchmark finished' );
89+
b.end();
90+
});
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( [mean] )
3+
Returns an accumulator function which incrementally computes a corrected
4+
sample standard deviation, ignoring `NaN` values.
5+
6+
If provided a value, the accumulator function returns an updated corrected
7+
sample standard deviation. If not provided a value, the accumulator function
8+
returns the current corrected sample standard deviation.
9+
10+
If provided `NaN` or a value which, when used in computations, results in
11+
`NaN`, the value is ignored and does not affect future calculations.
12+
13+
Parameters
14+
----------
15+
mean: number (optional)
16+
Known mean.
17+
18+
Returns
19+
-------
20+
acc: Function
21+
Accumulator function.
22+
23+
Examples
24+
--------
25+
> var accumulator = {{alias}}();
26+
> var s = accumulator()
27+
null
28+
> s = accumulator( 2.0 )
29+
0.0
30+
> s = accumulator( NaN )
31+
0.0
32+
> s = accumulator( -5.0 )
33+
~4.95
34+
> s = accumulator()
35+
~4.95
36+
37+
See Also
38+
--------
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) 2019 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 a value, returns an updated corrected sample standard deviation; otherwise, returns the current corrected sample standard deviation.
25+
*
26+
* ## Notes
27+
*
28+
* - If provided `NaN` or a value which, when used in computations, the value is ignored and does not affect future calculations.
29+
*
30+
* @param x - value
31+
* @returns corrected sample standard deviation
32+
*/
33+
type accumulator = ( x?: number ) => number | null;
34+
35+
/**
36+
* Returns an accumulator function which incrementally computes a corrected sample standard deviation.
37+
*
38+
* @param mu - known mean
39+
* @returns accumulator function
40+
*
41+
* @example
42+
* var accumulator = incrstdev();
43+
*
44+
* var s = accumulator();
45+
* // returns null
46+
*
47+
* s = accumulator( 2.0 );
48+
* // returns 0.0
49+
*
50+
* s = accumulator( NaN );
51+
* // returns 0.0
52+
*
53+
* s = accumulator( -5.0 );
54+
* // returns ~4.95
55+
*
56+
* s = accumulator();
57+
* // returns ~4.95
58+
*/
59+
declare function incrnanstdev( mu?: number ): accumulator;
60+
61+
62+
// EXPORTS //
63+
64+
export = incrnanstdev;

0 commit comments

Comments
 (0)