Skip to content

Commit acb99e6

Browse files
committed
feat: add stats/incr/nanwmean
--- 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: na - 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 18ad933 commit acb99e6

File tree

9 files changed

+751
-0
lines changed

9 files changed

+751
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2019 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+
# incrwmean
22+
23+
> Compute a [weighted arithmetic mean][weighted-arithmetic-mean] incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [weighted arithmetic mean][weighted-arithmetic-mean] is defined as
28+
29+
<!-- <equation class="equation" label="eq:weighted_arithmetic_mean" align="center" raw="\bar{x} = \frac{\displaystyle\sum_{i=0}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0}^{n-1} w_{i}}" alt="Equation for the weighted arithmetic mean."> -->
30+
31+
```math
32+
\bar{x} = \frac{\displaystyle\sum_{i=0}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0}^{n-1} w_{i}}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{\displaystyle\sum_{i=0}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0}^{n-1} w_{i}}" data-equation="eq:weighted_arithmetic_mean">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@adbea9806383f70c982e3191475c874efba1296b/lib/node_modules/@stdlib/stats/incr/wmean/docs/img/equation_weighted_arithmetic_mean.svg" alt="Equation for the weighted 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 incrnanwmean = require( '@stdlib/stats/incr/nanwmean' );
52+
```
53+
54+
#### incrnanwmean()
55+
56+
Returns an accumulator `function` which incrementally computes a [weighted arithmetic mean][weighted-arithmetic-mean], ignoring `NaN` values.
57+
58+
```javascript
59+
var accumulator = incrnanwmean();
60+
```
61+
62+
#### accumulator( \[x, w] )
63+
64+
If provided an input value `x` and a weight `w`, the accumulator function returns an updated weighted mean. If not provided any input values, the accumulator function returns the current mean.
65+
66+
```javascript
67+
var accumulator = incrnanwmean();
68+
69+
var mu = accumulator( 2.0, 1.0 );
70+
// returns 2.0
71+
72+
mu = accumulator( 2.0, 0.5 );
73+
// returns 2.0
74+
75+
mu = accumulator(2.0, NaN );
76+
// returns 2.0
77+
78+
mu = accumulator( 3.0, 1.5 );
79+
// returns 2.5
80+
81+
mu = accumulator(2.0, NaN );
82+
// returns 2.5
83+
84+
mu = accumulator();
85+
// returns 2.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 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.
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 incrnanwmean = require( '@stdlib/stats/incr/nanwmean' );
111+
112+
var accumulator;
113+
var v;
114+
var w;
115+
var i;
116+
117+
// Initialize an accumulator:
118+
accumulator = incrnanwmean();
119+
120+
// For each simulated datum, update the weighted mean...
121+
for ( i = 0; i < 100; i++ ) {
122+
if ( randu() < 0.2 ) {
123+
v = NaN;
124+
} else {
125+
v = randu() * 100.0;
126+
}
127+
if ( randu() < 0.2 ) {
128+
w = NaN;
129+
}
130+
else {
131+
w = randu() * 100.0;
132+
}
133+
accumulator( v, w );
134+
}
135+
console.log( accumulator() );
136+
```
137+
138+
</section>
139+
140+
<!-- /.examples -->
141+
142+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
143+
144+
<section class="related">
145+
146+
* * *
147+
148+
## See Also
149+
150+
- <span class="package-name">[`@stdlib/stats/incr/ewmean`][@stdlib/stats/incr/ewmean]</span><span class="delimiter">: </span><span class="description">compute an exponentially weighted mean incrementally.</span>
151+
- <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>
152+
- <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>
153+
154+
</section>
155+
156+
<!-- /.related -->
157+
158+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
159+
160+
<section class="links">
161+
162+
[weighted-arithmetic-mean]: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean
163+
164+
<!-- <related-links> -->
165+
166+
[@stdlib/stats/incr/ewmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/ewmean
167+
168+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
169+
170+
[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean
171+
172+
<!-- </related-links> -->
173+
174+
</section>
175+
176+
<!-- /.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) 2019 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 incrnanwmean = 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 = incrnanwmean();
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 = incrnanwmean();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu(), 1.0 );
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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 both arguments, returns an updated weighted arithmetic mean; otherwise, returns the current weighted arithmetic mean.
25+
*
26+
* @param x - value
27+
* @param w - weight
28+
* @returns weighted arithmetic mean
29+
*/
30+
type accumulator = ( x?: number, w?: number ) => number | null;
31+
32+
/**
33+
* Returns an accumulator function which incrementally computes a weighted arithmetic mean.
34+
*
35+
* @returns accumulator function
36+
*
37+
* @example
38+
* var accumulator = incrnanwmean();
39+
*
40+
* var mu = accumulator( 2.0, 1.0 );
41+
* // returns 2.0
42+
*
43+
* mu = accumulator( 2.0, 0.5 );
44+
* // returns 2.0
45+
*
46+
* mu = accumulator (2.0, NaN );
47+
* // returns 2.0
48+
*
49+
* mu = accumulator( 3.0, 1.5 );
50+
* // returns 2.5
51+
*
52+
* mu = accumulator (2.0, NaN );
53+
* // returns 2.5
54+
*
55+
* mu = accumulator();
56+
* // returns 2.5
57+
*/
58+
declare function incrnanwmean(): accumulator;
59+
60+
61+
// EXPORTS //
62+
63+
export = incrnanwmean;
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) 2019 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+
import incrnanwmean = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrnanwmean(); // $ExpectType accumulator
27+
}
28+
29+
// The compiler throws an error if the function is provided arguments...
30+
{
31+
incrnanwmean( '5' ); // $ExpectError
32+
incrnanwmean( 5 ); // $ExpectError
33+
incrnanwmean( true ); // $ExpectError
34+
incrnanwmean( false ); // $ExpectError
35+
incrnanwmean( null ); // $ExpectError
36+
incrnanwmean( undefined ); // $ExpectError
37+
incrnanwmean( [] ); // $ExpectError
38+
incrnanwmean( {} ); // $ExpectError
39+
incrnanwmean( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The function returns an accumulator function which returns an accumulated result...
43+
{
44+
const acc = incrnanwmean();
45+
46+
acc(); // $ExpectType number | null
47+
acc( 3.14, 1.0 ); // $ExpectType number | null
48+
}
49+
50+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
51+
{
52+
const acc = incrnanwmean();
53+
54+
acc( '5', 1.0 ); // $ExpectError
55+
acc( true, 1.0 ); // $ExpectError
56+
acc( false, 1.0 ); // $ExpectError
57+
acc( null, 1.0 ); // $ExpectError
58+
acc( [], 1.0 ); // $ExpectError
59+
acc( {}, 1.0 ); // $ExpectError
60+
acc( ( x: number ): number => x, 1.0 ); // $ExpectError
61+
62+
acc( 3.14, '5' ); // $ExpectError
63+
acc( 3.14, true ); // $ExpectError
64+
acc( 3.14, false ); // $ExpectError
65+
acc( 3.14, null ); // $ExpectError
66+
acc( 3.14, [] ); // $ExpectError
67+
acc( 3.14, {} ); // $ExpectError
68+
acc( 3.14, ( x: number ): number => x ); // $ExpectError
69+
}

0 commit comments

Comments
 (0)