Skip to content

Commit da44350

Browse files
committed
feat: add stats/incr/nanewmean
1 parent 9e91f81 commit da44350

File tree

11 files changed

+847
-0
lines changed

11 files changed

+847
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
# incrnanewmean
22+
23+
> Compute an [exponentially weighted mean][moving-average] incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
An [exponentially weighted mean][moving-average] can be defined recursively as
28+
29+
<!-- <equation class="equation" label="eq:exponentially_weighted_mean" align="center" raw="\mu_t = \begin{cases} x_0 & \textrm{if}\ t = 0 \\ \alpha x_t + (1-\alpha) \mu_{t-1} & \textrm{if}\ t > 0 \end{cases}" alt="Recursive definition for computing an exponentially weighted mean."> -->
30+
31+
```math
32+
\mu_t = \begin{cases} x_0 & \textrm{if}\ t = 0 \\ \alpha x_t + (1-\alpha) \mu_{t-1} & \textrm{if}\ t > 0 \end{cases}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\mu_t = \begin{cases} x_0 &amp; \textrm{if}\ t = 0 \\ \alpha x_t + (1-\alpha) \mu_{t-1} &amp; \textrm{if}\ t &gt; 0 \end{cases}" data-equation="eq:exponentially_weighted_mean">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@1445ad5c454bc3c1a86bde2be87d6cec87781174/lib/node_modules/@stdlib/stats/incr/ewmean/docs/img/equation_exponentially_weighted_mean.svg" alt="Recursive definition for computing an exponentially weighted 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 incrnanewmean = require( '@stdlib/stats/incr/nanewmean' );
52+
```
53+
54+
#### incrnanewmean( alpha )
55+
56+
Returns an accumulator `function` which incrementally computes an [exponentially weighted mean][moving-average], where `alpha` is a smoothing factor between `0` and `1`, ignoring `NaN` values.
57+
58+
```javascript
59+
var accumulator = incrnanewmean( 0.5 );
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 = incrnanewmean( 0.5 );
68+
69+
var v = accumulator();
70+
// returns null
71+
72+
v = accumulator( 2.0 );
73+
// returns 2.0
74+
75+
v = accumulator( 1.0 );
76+
// returns 1.5
77+
78+
v = accumulator( NaN );
79+
// returns 1.5
80+
81+
v = accumulator( 3.0 );
82+
// returns 2.25
83+
84+
v = accumulator();
85+
// returns 2.25
86+
```
87+
88+
</section>
89+
90+
<!-- /.usage -->
91+
92+
<section class="notes">
93+
94+
## Notes
95+
96+
- 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.
97+
98+
</section>
99+
100+
<!-- /.notes -->
101+
102+
<section class="examples">
103+
104+
## Examples
105+
106+
<!-- eslint no-undef: "error" -->
107+
108+
```javascript
109+
var randu = require( '@stdlib/random/base/randu' );
110+
var incrnanewmean = require( '@stdlib/stats/incr/nanewmean' );
111+
112+
var accumulator;
113+
var v;
114+
var i;
115+
116+
// Initialize an accumulator:
117+
accumulator = incrnanewmean( 0.5 );
118+
119+
// For each simulated datum, update the exponentially weighted mean...
120+
for ( i = 0; i < 100; i++ ) {
121+
if ( randu() < 0.2 ) {
122+
v = NaN;
123+
} else {
124+
v = randu() * 100.0;
125+
}
126+
accumulator( v );
127+
}
128+
console.log( accumulator() );
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
136+
137+
<section class="related">
138+
139+
* * *
140+
141+
## See Also
142+
143+
- <span class="package-name">[`@stdlib/stats/incr/ewvariance`][@stdlib/stats/incr/ewvariance]</span><span class="delimiter">: </span><span class="description">compute an exponentially weighted variance incrementally.</span>
144+
- <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>
145+
- <span class="package-name">[`@stdlib/stats/incr/mmean`][@stdlib/stats/incr/mmean]</span><span class="delimiter">: </span><span class="description">compute a moving arithmetic mean incrementally.</span>
146+
- <span class="package-name">[`@stdlib/stats/incr/wmean`][@stdlib/stats/incr/wmean]</span><span class="delimiter">: </span><span class="description">compute a weighted arithmetic mean incrementally.</span>
147+
148+
</section>
149+
150+
<!-- /.related -->
151+
152+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="links">
155+
156+
[moving-average]: https://en.wikipedia.org/wiki/Moving_average
157+
158+
<!-- <related-links> -->
159+
160+
[@stdlib/stats/incr/ewvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/ewvariance
161+
162+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
163+
164+
[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean
165+
166+
[@stdlib/stats/incr/wmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/wmean
167+
168+
<!-- </related-links> -->
169+
170+
</section>
171+
172+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 incrnanewmean = 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 = incrnanewmean( 0.5 );
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 = incrnanewmean( 0.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+
});
Lines changed: 74 additions & 0 deletions
Loading
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( α )
3+
Returns an accumulator function which incrementally computes an
4+
exponentially weighted mean, where α is a smoothing factor between 0 and 1, ignoring
5+
`NaN` values.
6+
7+
If provided a value, the accumulator function returns an updated mean. If
8+
not provided a value, the accumulator function returns the current mean.
9+
10+
Parameters
11+
----------
12+
α: number
13+
Smoothing factor (value between 0 and 1).
14+
15+
Returns
16+
-------
17+
acc: Function
18+
Accumulator function.
19+
20+
Examples
21+
--------
22+
> var accumulator = {{alias}}( 0.5 );
23+
> var v = accumulator()
24+
null
25+
> v = accumulator( 2.0 )
26+
2.0
27+
> v = accumulator( NaN )
28+
2.0
29+
> v = accumulator( -5.0 )
30+
-1.5
31+
> v = accumulator()
32+
-1.5
33+
34+
See Also
35+
--------
36+

0 commit comments

Comments
 (0)