Skip to content

Commit fc6b89c

Browse files
refactor: add stats/incr/nanmmeanabs
--- 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: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 442efb4 commit fc6b89c

File tree

11 files changed

+854
-0
lines changed

11 files changed

+854
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
# incrnanmmeanabs
22+
23+
> Compute a moving [arithmetic mean][arithmetic-mean] of absolute values incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, the [arithmetic mean][arithmetic-mean] of absolute values is defined as
28+
29+
<!-- <equation class="equation" label="eq:arithmetic_mean_absolute_values" align="center" raw="\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} |x_i|" alt="Equation for the arithmetic mean of absolute values."> -->
30+
31+
```math
32+
\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} |x_i|
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} |x_i|" data-equation="eq:arithmetic_mean_absolute_values">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@320a89534d4f59b82d162f31e968222555dae2f7/lib/node_modules/@stdlib/stats/incr/mmeanabs/docs/img/equation_arithmetic_mean_absolute_values.svg" alt="Equation for the arithmetic mean of absolute values.">
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 incrnanmmeanabs = require( '@stdlib/stats/incr/nanmmeanabs' );
52+
```
53+
54+
#### incrnanmmeanabs( window )
55+
56+
Returns an accumulator `function` which incrementally computes a moving [arithmetic mean][arithmetic-mean] of absolute values. The `window` parameter defines the number of values over which to compute the moving mean.
57+
58+
```javascript
59+
var accumulator = incrnanmmeanabs( 3 );
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 = incrnanmmeanabs( 3 );
68+
69+
var mu = accumulator();
70+
// returns null
71+
72+
// Fill the window...
73+
mu = accumulator( 2.0 ); // [2.0]
74+
// returns 2.0
75+
76+
mu = accumulator( -5.0 ); // [2.0, -5.0]
77+
// returns 3.5
78+
79+
mu = accumulator( NaN); // [2.0, -5.0]
80+
// returns 3.5
81+
82+
mu = accumulator( 3.0 ); // [2.0, -5.0, 3.0]
83+
// returns ~3.33
84+
85+
mu = accumulator( 5.0 ); // [-5.0, 3.0, 5.0]
86+
// returns ~4.33
87+
88+
mu = accumulator();
89+
// returns ~4.33
90+
```
91+
92+
</section>
93+
94+
<!-- /.usage -->
95+
96+
<section class="notes">
97+
98+
## Notes
99+
100+
- Input values are **not type-checked**. If a `NaN` or any non-numeric value is provided, the accumulator will ignore the `NaN` and return the current mean.
101+
- When a `NaN` is encountered, the mean remains unchanged for **at least** the next `W-1` calls, where `W` is the window size.
102+
- During the initial phase, when fewer than `W` values are available, the returned mean is calculated using the available values.
103+
104+
105+
</section>
106+
107+
<!-- /.notes -->
108+
109+
<section class="examples">
110+
111+
## Examples
112+
113+
<!-- eslint no-undef: "error" -->
114+
115+
```javascript
116+
var randu = require( '@stdlib/random/base/randu' );
117+
var incrnanmmeanabs = require( '@stdlib/stats/incr/nanmmeanabs' );
118+
119+
var accumulator;
120+
var mu;
121+
var v;
122+
var i;
123+
124+
// Initialize an accumulator:
125+
accumulator = incrnanmmeanabs( 5 );
126+
127+
// For each simulated datum, update the moving mean...
128+
console.log( '\nValue\tMean\n' );
129+
for ( i = 0; i < 100; i++ ) {
130+
if ( randu() < 0.2 ) {
131+
v = NaN;
132+
} else {
133+
v = ( randu()*100.0 ) - 50.0;
134+
}
135+
mu = accumulator( v );
136+
console.log( '%d\t%d', v.toFixed( 3 ), mu.toFixed( 3 ) );
137+
}
138+
```
139+
140+
</section>
141+
142+
<!-- /.examples -->
143+
144+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
145+
146+
<section class="related">
147+
148+
* * *
149+
150+
## See Also
151+
152+
- <span class="package-name">[`@stdlib/stats/incr/meanabs`][@stdlib/stats/incr/meanabs]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean of absolute values incrementally.</span>
153+
- <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>
154+
- <span class="package-name">[`@stdlib/stats/incr/msumabs`][@stdlib/stats/incr/msumabs]</span><span class="delimiter">: </span><span class="description">compute a moving sum of absolute values incrementally.</span>
155+
156+
</section>
157+
158+
<!-- /.related -->
159+
160+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="links">
163+
164+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
165+
166+
<!-- <related-links> -->
167+
168+
[@stdlib/stats/incr/meanabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/meanabs
169+
170+
[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean
171+
172+
[@stdlib/stats/incr/msumabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/msumabs
173+
174+
<!-- </related-links> -->
175+
176+
</section>
177+
178+
<!-- /.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 incrnanmmeanabs = 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 = incrnanmmeanabs( (i%5)+1 );
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 = incrnanmmeanabs( 5 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu()-0.5 );
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: 46 additions & 0 deletions
Loading
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
{{alias}}( W )
3+
Returns an accumulator function which incrementally computes a moving
4+
arithmetic mean of absolute values, ignoring `NaN` values.
5+
6+
The `W` parameter defines the number of values over which to compute the
7+
moving mean.
8+
9+
If provided a value, the accumulator function returns an updated moving
10+
mean. If not provided a value, the accumulator function returns the current
11+
moving mean.
12+
13+
As `W` values are needed to fill the window buffer, the first `W-1` returned
14+
values are calculated from smaller sample sizes. Until the window is full,
15+
each returned value is calculated from all provided values.
16+
17+
Parameters
18+
----------
19+
W: integer
20+
Window size.
21+
22+
Returns
23+
-------
24+
acc: Function
25+
Accumulator function.
26+
27+
Examples
28+
--------
29+
> var accumulator = {{alias}}( 3 );
30+
> var mu = accumulator()
31+
null
32+
> mu = accumulator( 2.0 )
33+
2.0
34+
> mu = accumulator( -5.0 )
35+
3.5
36+
> mu = accumulator( NaN )
37+
3.5
38+
> mu = accumulator( 3.0 )
39+
~3.33
40+
> mu = accumulator( 5.0 )
41+
~4.33
42+
> mu = accumulator()
43+
~4.33
44+
45+
See Also
46+
--------
47+

0 commit comments

Comments
 (0)