Skip to content

Commit d8fc3a6

Browse files
anxhukumarkgryte
andauthored
feat: add stats/incr/nanmean
PR-URL: #5976 Closes: #5571 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent c9af5b6 commit d8fc3a6

File tree

11 files changed

+726
-0
lines changed

11 files changed

+726
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
# incrnanmean
22+
23+
> Compute an [arithmetic mean][arithmetic-mean] 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="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the arithmetic mean."> -->
30+
31+
```math
32+
\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\mu = \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@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/nanmean/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean.">
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 incrnanmean = require( '@stdlib/stats/incr/nanmean' );
52+
```
53+
54+
#### incrnanmean()
55+
56+
Returns an accumulator function which incrementally computes an [arithmetic mean][arithmetic-mean], ignoring `NaN` values.
57+
58+
```javascript
59+
var accumulator = incrnanmean();
60+
```
61+
62+
#### accumulator( \[x] )
63+
64+
If provided an input value `x`, the accumulator function returns an updated mean. If not provided an input value `x`, the accumulator function returns the current mean.
65+
66+
```javascript
67+
var accumulator = incrnanmean();
68+
69+
var mu = accumulator( 2.0 );
70+
// returns 2.0
71+
72+
mu = accumulator( NaN );
73+
// returns 2.0
74+
75+
mu = accumulator( 1.0 );
76+
// returns 1.5
77+
78+
mu = accumulator( 3.0 );
79+
// returns 2.0
80+
81+
mu = accumulator();
82+
// returns 2.0
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+
95+
</section>
96+
97+
<!-- /.notes -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var uniform = require( '@stdlib/random/base/uniform' );
107+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
108+
var incrnanmean = require( '@stdlib/stats/incr/nanmean' );
109+
110+
// Initialize an accumulator:
111+
var accumulator = incrnanmean();
112+
113+
// For each simulated datum, update the mean...
114+
var i;
115+
for ( i = 0; i < 100; i++ ) {
116+
accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ) );
117+
}
118+
console.log( accumulator() );
119+
```
120+
121+
</section>
122+
123+
<!-- /.examples -->
124+
125+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
126+
127+
<section class="related">
128+
129+
</section>
130+
131+
<!-- /.related -->
132+
133+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
134+
135+
<section class="links">
136+
137+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
138+
139+
</section>
140+
141+
<!-- /.links -->
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 pkg = require( './../package.json' ).name;
25+
var incrnanmean = require( './../lib' );
26+
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 = incrnanmean();
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 = incrnanmean();
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = acc( i );
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+
});
Lines changed: 42 additions & 0 deletions
Loading
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes an arithmetic
4+
mean, ignoring `NaN` values.
5+
6+
If provided a value, the accumulator function returns an updated mean. If
7+
not provided a value, the accumulator function returns the current mean.
8+
9+
Returns
10+
-------
11+
acc: Function
12+
Accumulator function.
13+
14+
Examples
15+
--------
16+
> var accumulator = {{alias}}();
17+
> var mu = accumulator()
18+
null
19+
> mu = accumulator( 2.0 )
20+
2.0
21+
> mu = accumulator( NaN )
22+
2.0
23+
> mu = accumulator( -5.0 )
24+
-1.5
25+
> mu = accumulator()
26+
-1.5
27+
28+
See Also
29+
--------
30+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* If provided a value, returns an updated arithmetic mean; otherwise, returns the current arithmetic mean.
25+
*
26+
* @param x - value
27+
* @returns arithmetic mean
28+
*/
29+
type accumulator = ( x?: number ) => number | null;
30+
31+
/**
32+
* Returns an accumulator function which incrementally computes an arithmetic mean, ignoring `NaN` values.
33+
*
34+
* @returns accumulator function
35+
*
36+
* @example
37+
* var accumulator = incrnanmean();
38+
*
39+
* var mu = accumulator();
40+
* // returns null
41+
*
42+
* mu = accumulator( 2.0 );
43+
* // returns 2.0
44+
*
45+
* mu = accumulator( NaN );
46+
* // returns 2.0
47+
*
48+
* mu = accumulator( 3.0 );
49+
* // returns 2.5
50+
*
51+
* mu = accumulator( 4.0 );
52+
* // returns 3.0
53+
*
54+
* mu = accumulator();
55+
* // returns 3.0
56+
*/
57+
declare function incrnanmean(): accumulator;
58+
59+
60+
// EXPORTS //
61+
62+
export = incrnanmean;

0 commit comments

Comments
 (0)