Skip to content

Commit 536ec86

Browse files
[Add]: Create new package stats/incr/nanwmean to handle NaN values
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: ---
1 parent 934c483 commit 536ec86

File tree

11 files changed

+842
-0
lines changed

11 files changed

+842
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
# incrnanwmean
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_nan" align="center" raw="\bar{x} = \frac{\displaystyle\sum_{i=0, x_i \neq \text{NaN}}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0, x_i \neq \text{NaN}}^{n-1} w_{i}}" alt="Equation for the weighted arithmetic mean ignoring NaNs."> -->
30+
31+
```math
32+
\bar{x} = \frac{\displaystyle\sum_{i=0, x_i \neq \text{NaN}}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0, x_i \neq \text{NaN}}^{n-1} w_{i}}
33+
```
34+
<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{\displaystyle\sum_{i=0, x_i \neq \text{NaN}}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0, x_i \neq \text{NaN}}^{n-1} w_{i}}" data-equation="eq:weighted_arithmetic_mean_nan">
35+
<img src="docs/img/equation_weighted_arithmetic_mean_nan.svg" alt="Equation for the weighted arithmetic mean ignoring NaNs.">
36+
<br>
37+
</div> -->
38+
39+
<!-- </equation> -->
40+
41+
</section>
42+
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 while ignoring `NaN` values. 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();
70+
// returns null
71+
72+
mu = accumulator( 2.0, 1.0 );
73+
// returns 2.0
74+
75+
mu = accumulator( 2.0, 0.5 );
76+
// returns 2.0
77+
78+
mu = accumulator( 3.0, 1.5 );
79+
// returns 2.5
80+
81+
mu = accumulator();
82+
// returns 2.5
83+
```
84+
85+
86+
</section>
87+
88+
<!-- /.usage -->
89+
90+
<section class="notes">
91+
92+
## Notes
93+
94+
- Input values are **not** type checked. If provided `NaN` for the value `x`, the accumulator **ignores** it and does not update the weighted mean. If provided `NaN` for the weight `w`, the corresponding value is ignored. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
95+
96+
</section>
97+
98+
<!-- /.notes -->
99+
100+
101+
<section class="examples">
102+
103+
## Examples
104+
105+
<!-- eslint no-undef: "error" -->
106+
107+
```javascript
108+
var randu = require( '@stdlib/random/base/randu' );
109+
var incrnanwmean = require( '@stdlib/stats/incr/nanwmean' );
110+
111+
var accumulator;
112+
var v;
113+
var w;
114+
var i;
115+
116+
accumulator = incrnanwmean();
117+
118+
for ( i = 0; i < 100; i++ ) {
119+
v = ( randu() < 0.1 ) ? NaN : randu() * 100.0; // Introduce NaNs randomly
120+
w = ( randu() < 0.1 ) ? NaN : randu() * 100.0; // Introduce NaNs randomly
121+
accumulator( v, w );
122+
}
123+
console.log( accumulator() );
124+
```
125+
126+
</section>
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 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+
});

lib/node_modules/@stdlib/stats/incr/nanwmean/docs/img/equation_weighted_arithmetic_mean_nan.svg

Lines changed: 39 additions & 0 deletions
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{{alias}}()
2+
Returns an accumulator function which incrementally computes a weighted
3+
arithmetic mean while ignoring NaN values.
4+
5+
If provided arguments, the accumulator function returns an updated weighted
6+
mean. If not provided arguments, the accumulator function returns the
7+
current weighted mean.
8+
9+
If `x` is `NaN`, the function ignores it and does not update the mean.
10+
If `w` is `NaN` or less than or equal to zero, the function ignores the input.
11+
If all values provided are `NaN`, the accumulated value remains `null`.
12+
13+
The accumulator function accepts two arguments:
14+
15+
- x: value.
16+
- w: weight.
17+
18+
Returns
19+
-------
20+
acc: Function
21+
Accumulator function.
22+
23+
Examples
24+
--------
25+
> var accumulator = {{alias}}();
26+
> var mean = accumulator()
27+
null
28+
> mean = accumulator(2.0, 3.0)
29+
2.0
30+
> mean = accumulator(NaN, 4.0)
31+
2.0 (NaN value is ignored)
32+
> mean = accumulator(3.0, NaN)
33+
2.0 (NaN weight is ignored)
34+
> mean = accumulator(5.0, 2.0)
35+
3.5
36+
> mean = accumulator()
37+
3.5
38+
39+
See Also
40+
--------
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* If provided both arguments, returns an updated weighted arithmetic mean while ignoring NaN values; otherwise, returns the current weighted arithmetic mean.
25+
*
26+
* ## Notes
27+
*
28+
* - If `x` is `NaN`, the function ignores it and does not update the mean.
29+
* - If `w` is `NaN` or less than or equal to zero, the function ignores the input.
30+
* - If all values provided are `NaN`, the accumulated value remains `null`.
31+
*
32+
* @param x - value
33+
* @param w - weight
34+
* @returns weighted arithmetic mean (ignoring NaN values)
35+
*/
36+
type accumulator = ( x?: number, w?: number ) => number | null;
37+
38+
/**
39+
* Returns an accumulator function which incrementally computes a weighted arithmetic mean while ignoring NaN values.
40+
*
41+
* @returns accumulator function
42+
*
43+
* @example
44+
* var incrnanwmean = require('@stdlib/stats/incr/nanwmean');
45+
*
46+
* var accumulator = incrnanwmean();
47+
*
48+
* var mean = accumulator();
49+
* // returns null
50+
*
51+
* mean = accumulator(2.0, 3.0);
52+
* // returns 2.0
53+
*
54+
* mean = accumulator(NaN, 4.0);
55+
* // returns 2.0 (Skips NaN)
56+
*
57+
* mean = accumulator(3.0 , NaN);
58+
* // returns 2.0 (Skips NaN)
59+
*
60+
* mean = accumulator(5.0, 2.0);
61+
* // returns 3.5
62+
*
63+
* mean = accumulator();
64+
* // returns 3.5
65+
*/
66+
declare function incrnanwmean(): accumulator;
67+
68+
// EXPORTS //
69+
70+
export = incrnanwmean;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
// TESTS //
22+
23+
// The function returns an accumulator function...
24+
{
25+
incrnanwmean(); // $ExpectType accumulator
26+
}
27+
28+
// The compiler throws an error if the function is provided arguments...
29+
{
30+
incrnanwmean( '5' ); // $ExpectError
31+
incrnanwmean( 5 ); // $ExpectError
32+
incrnanwmean( true ); // $ExpectError
33+
incrnanwmean( false ); // $ExpectError
34+
incrnanwmean( null ); // $ExpectError
35+
incrnanwmean( undefined ); // $ExpectError
36+
incrnanwmean( [] ); // $ExpectError
37+
incrnanwmean( {} ); // $ExpectError
38+
incrnanwmean( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The function returns an accumulator function which returns an accumulated result...
42+
{
43+
const acc = incrnanwmean();
44+
45+
acc(); // $ExpectType number | null
46+
acc( 3.14, 1.0 ); // $ExpectType number | null
47+
acc( NaN, 2.0 ); // $ExpectType number | null
48+
acc( 4.5, NaN ); // $ExpectType number | null
49+
}
50+
51+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
52+
{
53+
const acc = incrnanwmean();
54+
55+
acc( '5', 1.0 ); // $ExpectError
56+
acc( true, 1.0 ); // $ExpectError
57+
acc( false, 1.0 ); // $ExpectError
58+
acc( null, 1.0 ); // $ExpectError
59+
acc( [], 1.0 ); // $ExpectError
60+
acc( {}, 1.0 ); // $ExpectError
61+
acc( ( x: number ): number => x, 1.0 ); // $ExpectError
62+
63+
acc( 3.14, '5' ); // $ExpectError
64+
acc( 3.14, true ); // $ExpectError
65+
acc( 3.14, false ); // $ExpectError
66+
acc( 3.14, null ); // $ExpectError
67+
acc( 3.14, [] ); // $ExpectError
68+
acc( 3.14, {} ); // $ExpectError
69+
acc( 3.14, ( x: number ): number => x ); // $ExpectError
70+
}

0 commit comments

Comments
 (0)