Skip to content

Commit 99177b3

Browse files
committed
test(stats/incr): update test cases for nanewvariance2
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent f9bb0d7 commit 99177b3

File tree

11 files changed

+818
-0
lines changed

11 files changed

+818
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
# nanewvariance
22+
23+
> Compute an [exponentially weighted variance][exponentially weighted variance] incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [exponentially weighted variance][exponentially weighted variance] is defined as
28+
29+
<!-- <equation class="equation" label="eq:ew_variance" align="center" raw="s_t^2 = (1-\alpha) \left( s_{t-1}^2 + \alpha (x_t - \mu_{t-1})^2 \right)" alt="Equation for the exponentially weighted variance."> -->
30+
31+
```math
32+
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}
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:exponentially weighted variance">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/nanmean/docs/img/equation_exponentially_weighted_variance.svg" alt="exponentially weighted variance">
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 nanewvariance = require( '@stdlib/stats/incr/nanewvariance' );
52+
```
53+
54+
#### nanewvariance()
55+
56+
Returns an accumulator function which incrementally computes an [exponentially weighted variance][exponentially weighted variance], ignoring `NaN` values.
57+
58+
```javascript
59+
var accumulator = nanewvariance( 0.5 );
60+
```
61+
62+
#### accumulator( \[x] )
63+
64+
If provided an input value `x`, the accumulator function returns an updated exponentially weighted variance. If not provided an input value `x`, the accumulator function returns the current exponentially weighted variance.
65+
66+
```javascript
67+
var accumulator = nanewvariance( 0.5 );
68+
69+
var v = accumulator();
70+
// returns null
71+
72+
v = accumulator( 2.0 );
73+
// returns 0
74+
75+
v = accumulator( 4.0 );
76+
// returns 1
77+
78+
v = accumulator( NaN );
79+
// returns 1
80+
81+
v = accumulator( 5.0 );
82+
// returns 1.5
83+
84+
v = accumulator();
85+
// returns 1.5
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 nanewvariance = require( '@stdlib/stats/incr/nanewvariance' );
111+
112+
var accumulator;
113+
var v;
114+
var i;
115+
116+
// Initialize an accumulator with smoothing factor α = 0.5:
117+
accumulator = nanewvariance( 0.5 );
118+
119+
// For each simulated datum, update the exponentially weighted variance...
120+
for ( i = 0; i < 100; i++ ) {
121+
// With a 20% chance, provide NaN to test that invalid inputs are ignored:
122+
v = ( randu() < 0.2 ) ? NaN : randu() * 100.0;
123+
accumulator( v );
124+
}
125+
console.log( accumulator() );
126+
```
127+
128+
</section>
129+
130+
<!-- /.examples -->
131+
132+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
133+
134+
<section class="related">
135+
136+
</section>
137+
138+
<!-- /.related -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
[exponentially weighted variance]: https://en.wikipedia.org/wiki/EWMA_chart
145+
146+
</section>
147+
148+
<!-- /.links -->
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 nanewvariance = 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 = nanewvariance( 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+
acc = nanewvariance( 0.5 );
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
v = acc( randu() );
57+
if ( v !== v ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( v !== v ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});
Lines changed: 89 additions & 0 deletions
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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. This accumulator ignores any input values that are `NaN`.
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`, that input is ignored and the accumulated variance remains unchanged.
13+
14+
Parameters
15+
----------
16+
α: number
17+
Smoothing factor (value between 0 and 1).
18+
19+
Returns
20+
-------
21+
acc: Function
22+
Accumulator function.
23+
24+
Examples
25+
--------
26+
> var accumulator = {{alias}}( 0.5 );
27+
> var v = accumulator()
28+
null
29+
> v = accumulator( 2.0 )
30+
0
31+
> v = accumulator( 4.0 )
32+
1
33+
> v = accumulator( NaN )
34+
1
35+
> v = accumulator( 5.0 )
36+
1.5
37+
> v = accumulator()
38+
1.5
39+
40+
See Also
41+
--------
42+

0 commit comments

Comments
 (0)