Skip to content

Commit 992d620

Browse files
AaryaBalwadkarAaryaBalwadkar-PHCPlaneshifter
authored
feat: add stats/incr/nanmeanstdev
PR-URL: #6148 Closes: #5574 Co-authored-by: AaryaBalwadkarPHC <aarya.dev@projecthumancity.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent c33d0b9 commit 992d620

12 files changed

Lines changed: 1048 additions & 0 deletions

File tree

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
# incrnanmeanstdev
22+
23+
> Compute an [arithmetic mean][arithmetic-mean] and a [corrected sample standard deviation][sample-stdev] incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [arithmetic mean][arithmetic-mean] is defined as
28+
29+
<!-- <equation class="equation" label="eq:arithmetic_mean" align="center" raw="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the arithmetic mean."> -->
30+
31+
```math
32+
\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:arithmetic_mean">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@5ca648efa8739942a21b07ad7df1947cdd01b662/lib/node_modules/@stdlib/stats/incr/meanstdev/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
and the [corrected sample standard deviation][sample-stdev] is defined as
43+
44+
<!-- <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."> -->
45+
46+
```math
47+
s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2}
48+
```
49+
50+
<!-- <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">
51+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@5ca648efa8739942a21b07ad7df1947cdd01b662/lib/node_modules/@stdlib/stats/incr/meanstdev/docs/img/equation_corrected_sample_standard_deviation.svg" alt="Equation for the corrected sample standard deviation.">
52+
<br>
53+
</div> -->
54+
55+
<!-- </equation> -->
56+
57+
</section>
58+
59+
<!-- /.intro -->
60+
61+
<section class="usage">
62+
63+
## Usage
64+
65+
```javascript
66+
var incrnanmeanstdev = require( '@stdlib/stats/incr/nanmeanstdev' );
67+
```
68+
69+
#### incrnanmeanstdev( \[out] )
70+
71+
Returns an accumulator `function` which incrementally computes an [arithmetic mean][arithmetic-mean] and [corrected sample standard deviation][sample-stdev], ignoring `NaN` values.
72+
73+
```javascript
74+
var accumulator = incrnanmeanstdev();
75+
```
76+
77+
By default, the returned accumulator `function` returns the accumulated values as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object.
78+
79+
```javascript
80+
var Float64Array = require( '@stdlib/array/float64' );
81+
82+
var accumulator = incrnanmeanstdev( new Float64Array( 2 ) );
83+
```
84+
85+
#### accumulator( \[x] )
86+
87+
If provided an input value `x`, the accumulator function returns updated accumulated values. If not provided an input value `x`, the accumulator function returns the current accumulated values.
88+
89+
```javascript
90+
var accumulator = incrnanmeanstdev();
91+
92+
var ms = accumulator();
93+
// returns null
94+
95+
ms = accumulator( NaN );
96+
// returns null
97+
98+
ms = accumulator( 2.0 );
99+
// returns [ 2.0, 0.0 ]
100+
101+
ms = accumulator( 1.0 );
102+
// returns [ 1.5, ~0.71 ]
103+
104+
ms = accumulator( 3.0 );
105+
// returns [ 2.0, 1.0 ]
106+
107+
ms = accumulator( -7.0 );
108+
// returns [ -0.25, ~4.57 ]
109+
110+
ms = accumulator( -5.0 );
111+
// returns [ -1.2, ~4.49 ]
112+
113+
ms = accumulator();
114+
// returns [ -1.2, ~4.49 ]
115+
```
116+
117+
</section>
118+
119+
<!-- /.usage -->
120+
121+
<section class="notes">
122+
123+
## Notes
124+
125+
- 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.
126+
127+
</section>
128+
129+
<!-- /.notes -->
130+
131+
<section class="examples">
132+
133+
## Examples
134+
135+
<!-- eslint no-undef: "error" -->
136+
137+
```javascript
138+
var randu = require( '@stdlib/random/base/randu' );
139+
var Float64Array = require( '@stdlib/array/float64' );
140+
var ArrayBuffer = require( '@stdlib/array/buffer' );
141+
var incrnanmeanstdev = require( '@stdlib/stats/incr/nanmeanstdev' );
142+
143+
var offset;
144+
var acc;
145+
var buf;
146+
var out;
147+
var ms;
148+
var N;
149+
var v;
150+
var i;
151+
var j;
152+
153+
// Define the number of accumulators:
154+
N = 5;
155+
156+
// Create an array buffer for storing accumulator output:
157+
buf = new ArrayBuffer( N*2*8 ); // 8 bytes per element
158+
159+
// Initialize accumulators:
160+
acc = [];
161+
for ( i = 0; i < N; i++ ) {
162+
// Compute the byte offset:
163+
offset = i * 2 * 8; // stride=2, bytes_per_element=8
164+
165+
// Create a new view for storing accumulated values:
166+
out = new Float64Array( buf, offset, 2 );
167+
168+
// Initialize an accumulator which will write results to the view:
169+
acc.push( incrnanmeanstdev( out ) );
170+
}
171+
172+
// Simulate data and update the sample means and standard deviations...
173+
for ( i = 0; i < 100; i++ ) {
174+
for ( j = 0; j < N; j++ ) {
175+
if ( randu() < 0.1 ) {
176+
v = NaN;
177+
} else {
178+
v = randu() * 100.0 * (j+1);
179+
}
180+
acc[ j ]( v );
181+
}
182+
}
183+
184+
// Print the final results:
185+
console.log( 'Mean\tStDev' );
186+
for ( i = 0; i < N; i++ ) {
187+
ms = acc[ i ]();
188+
console.log( '%d\t%d', ms[ 0 ].toFixed( 3 ), ms[ 1 ].toFixed( 3 ) );
189+
}
190+
```
191+
192+
</section>
193+
194+
<!-- /.examples -->
195+
196+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
197+
198+
<section class="related">
199+
200+
</section>
201+
202+
<!-- /.related -->
203+
204+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
205+
206+
<section class="links">
207+
208+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
209+
210+
[sample-stdev]: https://en.wikipedia.org/wiki/Standard_deviation
211+
212+
<!-- <related-links> -->
213+
214+
<!-- </related-links> -->
215+
216+
</section>
217+
218+
<!-- /.links -->
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var format = require( '@stdlib/string/format' );
26+
var pkg = require( './../package.json' ).name;
27+
var incrnanmeanstdev = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var f;
34+
var i;
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
f = incrnanmeanstdev();
38+
if ( typeof f !== 'function' ) {
39+
b.fail( 'should return a function' );
40+
}
41+
}
42+
b.toc();
43+
if ( typeof f !== 'function' ) {
44+
b.fail( 'should return a function' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( format( '%s::accumulator', pkg ), function benchmark( b ) {
51+
var acc;
52+
var v;
53+
var i;
54+
55+
acc = incrnanmeanstdev();
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
v = acc( randu() );
60+
if ( v.length !== 2 ) {
61+
b.fail( 'should contain two elements' );
62+
}
63+
}
64+
b.toc();
65+
if ( v[ 0 ] !== v[ 0 ] || v[ 1 ] !== v[ 1 ] ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});
Lines changed: 43 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)