Skip to content

Commit bc80ce2

Browse files
committed
feat(stats): add nanmeanabs package
1 parent 39e505f commit bc80ce2

File tree

11 files changed

+731
-0
lines changed

11 files changed

+731
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
# incrnanmeanabs
22+
23+
> Compute an [arithmetic mean][arithmetic-mean] of absolute values incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
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="\mu = \frac{1}{n} \sum_{i=0}^{n-1} |x_i|" alt="Equation for the arithmetic mean of absolute values."> -->
30+
31+
```math
32+
\mu = \frac{1}{n} \sum_{i=0}^{n-1} |x_i|
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:arithmetic_mean_absolute_values">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/nanmeanabs/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 incrnanmeanabs = require( '@stdlib/stats/incr/nanmeanabs' );
52+
```
53+
54+
#### incrnanmeanabs()
55+
56+
Returns an accumulator `function` which incrementally computes an [arithmetic mean][arithmetic-mean] of absolute values, ignoring `NaN` values.
57+
58+
```javascript
59+
var accumulator = incrnanmeanabs();
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 = incrnanmeanabs();
68+
69+
var mu = accumulator( 2.0 );
70+
// returns 2.0
71+
72+
mu = accumulator( NaN );
73+
// returns 2.0
74+
75+
mu = accumulator( -1.0 );
76+
// returns 1.5
77+
78+
mu = accumulator( -3.0 );
79+
// returns 2.0
80+
81+
mu = accumulator();
82+
// returns 2.0
83+
```
84+
85+
</section>
86+
87+
<!-- /.usage -->
88+
89+
<section class="notes">
90+
91+
## Notes
92+
93+
- 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.
94+
95+
</section>
96+
97+
<!-- /.notes -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var uniform = require( '@stdlib/random/base/uniform' );
107+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
108+
var incrnanmeanabs = require( '@stdlib/stats/incr/nanmeanabs' );
109+
110+
// Initialize an accumulator:
111+
var accumulator = incrnanmeanabs();
112+
113+
// For each simulated datum, update the mean...
114+
var v;
115+
var i;
116+
117+
for ( i = 0; i < 100; i++ ) {
118+
accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ) );
119+
}
120+
console.log( accumulator() );
121+
```
122+
123+
</section>
124+
125+
<!-- /.examples -->
126+
127+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
128+
129+
<section class="related">
130+
131+
</section>
132+
133+
<!-- /.links -->
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 pkg = require( './../package.json' ).name;
25+
var incrnanmeanabs = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg, function benchmark( b ) {
31+
var f;
32+
var i;
33+
b.tic();
34+
for ( i = 0; i < b.iterations; i++ ) {
35+
f = incrnanmeanabs();
36+
if ( typeof f !== 'function' ) {
37+
b.fail( 'should return a function' );
38+
}
39+
}
40+
b.toc();
41+
if ( typeof f !== 'function' ) {
42+
b.fail( 'should return a function' );
43+
}
44+
b.pass( 'benchmark finished' );
45+
b.end();
46+
});
47+
48+
bench( pkg+'::accumulator', function benchmark( b ) {
49+
var acc;
50+
var v;
51+
var i;
52+
53+
acc = incrnanmeanabs();
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = acc( i );
58+
if ( v !== v ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( v !== v ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});
Lines changed: 45 additions & 0 deletions
Loading
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes an arithmetic
4+
mean of absolute values, ignoring `NaN` values.
5+
6+
If provided a value, the accumulator function returns an updated mean. If
7+
not provided a value, the accumulator function returns the current mean.
8+
9+
Returns
10+
-------
11+
acc: Function
12+
Accumulator function.
13+
14+
Examples
15+
--------
16+
> var accumulator = {{alias}}();
17+
> var mu = accumulator()
18+
null
19+
> mu = accumulator( 2.0 )
20+
2.0
21+
> mu = accumulator( NaN )
22+
2.0
23+
> mu = accumulator( -5.0 )
24+
3.5
25+
> mu = accumulator()
26+
3.5
27+
28+
See Also
29+
--------
30+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 a value, returns an updated arithmetic mean; otherwise, returns the current arithmetic mean.
25+
*
26+
* @param x - value
27+
* @returns arithmetic mean of absolute values
28+
*/
29+
type accumulator = ( x?: number ) => number | null;
30+
31+
/**
32+
* Returns an accumulator function which incrementally computes an arithmetic mean of absolute values, ignoring `NaN` values.
33+
*
34+
* @returns accumulator function
35+
*
36+
* @example
37+
* var accumulator = incrnanmeanabs();
38+
*
39+
* var mu = accumulator();
40+
* // returns null
41+
*
42+
* mu = accumulator( 2.0 );
43+
* // returns 2.0
44+
*
45+
* mu = accumulator( NaN );
46+
* // returns 2.0
47+
*
48+
* mu = accumulator( -3.0 );
49+
* // returns 2.5
50+
*
51+
* mu = accumulator( -4.0 );
52+
* // returns 3.0
53+
*
54+
* mu = accumulator();
55+
* // returns 3.0
56+
*/
57+
declare function incrnanmeanabs(): accumulator;
58+
59+
60+
// EXPORTS //
61+
62+
export = incrnanmeanabs;

0 commit comments

Comments
 (0)