Skip to content

Commit 12be486

Browse files
committed
Add pkg to incrementally compute an exponentially weighted moving variance
1 parent 4a86fb9 commit 12be486

File tree

9 files changed

+725
-0
lines changed

9 files changed

+725
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
# increwvariance
22+
23+
> Compute an [exponentially weighted variance][moving-average] incrementally.
24+
25+
<section class="intro">
26+
27+
An [exponentially weighted variance][moving-average] can be defined recursively as
28+
29+
<!-- <equation class="equation" label="eq:exponentially_weighted_variance" align="center" raw="S_n = \begin{cases} 0 & \textrm{if}\ n = 0 \\ (1 - \alpha) (S_{n-1} + \alpha(x_n - \mu_{n-1})^2) & \textrm{if}\ n > 0 \end{cases}" alt="Recursive definition for computing an exponentially weighted variance."> -->
30+
31+
<div class="equation" align="center" data-raw-text="S_n = \begin{cases} 0 &amp; \textrm{if}\ n = 0 \\ (1 - \alpha) (S_{n-1} + \alpha(x_n - \mu_{n-1})^2) &amp; \textrm{if}\ n &gt; 0 \end{cases}" data-equation="eq:exponentially_weighted_variance">
32+
<img src="" alt="Recursive definition for computing an exponentially weighted variance.">
33+
<br>
34+
</div>
35+
36+
<!-- </equation> -->
37+
38+
</section>
39+
40+
<!-- /.intro -->
41+
42+
<section class="usage">
43+
44+
## Usage
45+
46+
```javascript
47+
var increwvariance = require( '@stdlib/stats/incr/ewvariance' );
48+
```
49+
50+
#### increwvariance( alpha )
51+
52+
Returns an accumulator `function` which incrementally computes an [exponentially weighted variance][moving-average], where `alpha` is a smoothing factor between `0` and `1`.
53+
54+
```javascript
55+
var accumulator = increwvariance( 0.5 );
56+
```
57+
58+
#### accumulator( \[x] )
59+
60+
If provided an input value `x`, the accumulator function returns an updated variance. If not provided an input value `x`, the accumulator function returns the current variance.
61+
62+
```javascript
63+
var accumulator = increwvariance( 0.5 );
64+
65+
var v = accumulator();
66+
// returns null
67+
68+
v = accumulator( 2.0 );
69+
// returns 0.0
70+
71+
v = accumulator( 1.0 );
72+
// returns 0.25
73+
74+
v = accumulator( 3.0 );
75+
// returns 0.6875
76+
77+
v = accumulator();
78+
// returns 0.6875
79+
```
80+
81+
</section>
82+
83+
<!-- /.usage -->
84+
85+
<section class="notes">
86+
87+
## Notes
88+
89+
- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
90+
91+
</section>
92+
93+
<!-- /.notes -->
94+
95+
<section class="examples">
96+
97+
## Examples
98+
99+
<!-- eslint no-undef: "error" -->
100+
101+
```javascript
102+
var randu = require( '@stdlib/random/base/randu' );
103+
var increwvariance = require( '@stdlib/stats/incr/ewvariance' );
104+
105+
var accumulator;
106+
var v;
107+
var i;
108+
109+
// Initialize an accumulator:
110+
accumulator = increwvariance( 0.5 );
111+
112+
// For each simulated datum, update the exponentially weighted variance...
113+
for ( i = 0; i < 100; i++ ) {
114+
v = randu() * 100.0;
115+
accumulator( v );
116+
}
117+
console.log( accumulator() );
118+
```
119+
120+
</section>
121+
122+
<!-- /.examples -->
123+
124+
<section class="links">
125+
126+
[moving-average]: https://en.wikipedia.org/wiki/Moving_average
127+
128+
</section>
129+
130+
<!-- /.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) 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 increwvariance = 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 = increwvariance( 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 = increwvariance( 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: 89 additions & 0 deletions
Loading
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( α )
3+
Returns an accumulator function which incrementally computes an
4+
exponentially weighted variance, where α is a smoothing factor between 0 and
5+
1.
6+
7+
If provided a value, the accumulator function returns an updated variance.
8+
If not provided a value, the accumulator function returns the current
9+
variance.
10+
11+
If provided `NaN` or a value which, when used in computations, results in
12+
`NaN`, the accumulated value is `NaN` for all future invocations.
13+
14+
Returns
15+
-------
16+
acc: Function
17+
Accumulator function.
18+
19+
Examples
20+
--------
21+
> var accumulator = {{alias}}( 0.5 );
22+
> var v = accumulator()
23+
null
24+
> v = accumulator( 2.0 )
25+
0.0
26+
> v = accumulator( -5.0 )
27+
12.25
28+
> v = accumulator()
29+
12.25
30+
31+
See Also
32+
--------
33+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
var randu = require( '@stdlib/random/base/randu' );
22+
var increwvariance = require( './../lib' );
23+
24+
var accumulator;
25+
var s2;
26+
var v;
27+
var i;
28+
29+
// Initialize an accumulator:
30+
accumulator = increwvariance( 0.5 );
31+
32+
// For each simulated datum, update the exponentially weighted variance...
33+
console.log( '\nValue\tVariance\n' );
34+
for ( i = 0; i < 100; i++ ) {
35+
v = randu() * 100.0;
36+
s2 = accumulator( v );
37+
console.log( '%d\t%d', v.toFixed( 4 ), s2.toFixed( 4 ) );
38+
}
39+
console.log( '\nFinal variance: %d\n', accumulator() );

0 commit comments

Comments
 (0)