Skip to content

Commit eb8b850

Browse files
anxhukumarkgryte
andauthoredMar 18, 2025
feat: add stats/incr/nanmeanabs
PR-URL: #6068 Closes: #5572 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Anshu Kumar <contact.anshukumar@protonmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent cec0ff2 commit eb8b850

File tree

11 files changed

+737
-0
lines changed

11 files changed

+737
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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 i;
115+
for ( i = 0; i < 100; i++ ) {
116+
accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -100.0, 100.0 ) );
117+
}
118+
console.log( accumulator() );
119+
```
120+
121+
</section>
122+
123+
<!-- /.examples -->
124+
125+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
126+
127+
<section class="related">
128+
129+
</section>
130+
131+
<!-- /.related -->
132+
133+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
134+
135+
<section class="links">
136+
137+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
138+
139+
</section>
140+
141+
<!-- /.links -->
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 incrnanmeanabs = 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 = incrnanmeanabs();
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 = incrnanmeanabs();
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+
});
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
/**
22+
* If provided a value, returns an updated arithmetic mean; otherwise, returns the current arithmetic mean.
23+
*
24+
* @param x - value
25+
* @returns arithmetic mean of absolute values
26+
*/
27+
type accumulator = ( x?: number ) => number | null;
28+
29+
/**
30+
* Returns an accumulator function which incrementally computes an arithmetic mean of absolute values, ignoring `NaN` values.
31+
*
32+
* @returns accumulator function
33+
*
34+
* @example
35+
* var accumulator = incrnanmeanabs();
36+
*
37+
* var mu = accumulator();
38+
* // returns null
39+
*
40+
* mu = accumulator( 2.0 );
41+
* // returns 2.0
42+
*
43+
* mu = accumulator( NaN );
44+
* // returns 2.0
45+
*
46+
* mu = accumulator( -3.0 );
47+
* // returns 2.5
48+
*
49+
* mu = accumulator( -4.0 );
50+
* // returns 3.0
51+
*
52+
* mu = accumulator();
53+
* // returns 3.0
54+
*/
55+
declare function incrnanmeanabs(): accumulator;
56+
57+
58+
// EXPORTS //
59+
60+
export = incrnanmeanabs;

0 commit comments

Comments
 (0)
Failed to load comments.