Skip to content

Commit 21c4e0f

Browse files
feat: add stats/incr/nangmean
PR-URL: #6020 Closes: #5559 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 32709f5 commit 21c4e0f

File tree

11 files changed

+826
-0
lines changed

11 files changed

+826
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
# incrnangmean
22+
23+
> Compute a [geometric mean][geometric-mean] incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [geometric mean][geometric-mean] is defined as the nth root of a product of _n_ numbers.
28+
29+
<!-- <equation class="equation" label="eq:geometric_mean" align="center" raw="\biggl( \prod_{i=0}^{n-1} x_i \biggr)^{\frac{1}{n}} = \sqrt[n]{x_0 x_1 \cdots x_{n-1}}" alt="Equation for the geometric mean."> -->
30+
31+
```math
32+
\biggl( \prod_{i=0}^{n-1} x_i \biggr)^{\frac{1}{n}} = \sqrt[n]{x_0 x_1 \cdots x_{n-1}}
33+
```
34+
35+
36+
<!-- </equation> -->
37+
38+
</section>
39+
40+
<!-- /.intro -->
41+
42+
<section class="usage">
43+
44+
## Usage
45+
46+
```javascript
47+
var incrnangmean = require( '@stdlib/stats/incr/nangmean' );
48+
```
49+
50+
#### incrnangmean()
51+
52+
Returns an accumulator `function` which incrementally computes a [geometric mean][geometric-mean], ignoring `NaN` values.
53+
54+
```javascript
55+
var accumulator = incrnangmean();
56+
```
57+
58+
#### accumulator( \[x] )
59+
60+
If provided an input value `x`, the accumulator function returns an updated [geometric mean][geometric-mean]. If not provided an input value `x`, the accumulator function returns the current [geometric mean][geometric-mean].
61+
62+
```javascript
63+
var accumulator = incrnangmean();
64+
65+
var prod = accumulator( 2.0 );
66+
// returns 2.0
67+
68+
prod = accumulator( 1.0 );
69+
// returns ~1.414
70+
71+
prod = accumulator( NaN );
72+
// returns ~1.414
73+
74+
prod = accumulator( 3.0 );
75+
// returns ~1.817
76+
77+
prod = accumulator();
78+
// returns ~1.817
79+
```
80+
81+
</section>
82+
83+
<!-- /.usage -->
84+
85+
<section class="notes">
86+
87+
## Notes
88+
89+
- 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.
90+
91+
</section>
92+
93+
<!-- /.notes -->
94+
95+
<section class="examples">
96+
97+
## Examples
98+
99+
<!-- eslint no-undef: "error" -->
100+
101+
```javascript
102+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
103+
var uniform = require( '@stdlib/random/base/uniform' );
104+
var incrnangmean = require( '@stdlib/stats/incr/nangmean' );
105+
106+
var accumulator;
107+
var v;
108+
var i;
109+
110+
// Initialize an accumulator:
111+
accumulator = incrnangmean();
112+
113+
// For each simulated value, update the geometric mean...
114+
for ( i = 0; i < 100; i++ ) {
115+
if ( bernoulli( 0.2 ) ) {
116+
v = NaN;
117+
} else {
118+
v = uniform( 0.0, 100.0 );
119+
}
120+
accumulator( v );
121+
}
122+
console.log( accumulator() );
123+
```
124+
125+
</section>
126+
127+
<!-- /.examples -->
128+
129+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
130+
131+
<section class="related">
132+
133+
</section>
134+
135+
<!-- /.related -->
136+
137+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
138+
139+
<section class="links">
140+
141+
[geometric-mean]: https://en.wikipedia.org/wiki/Geometric_mean
142+
143+
<!-- <related-links> -->
144+
145+
<!-- </related-links> -->
146+
147+
</section>
148+
149+
<!-- /.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) 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 incrnangmean = 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 = incrnangmean();
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 = incrnangmean();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu()*10.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: 68 additions & 0 deletions
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes a geometric
4+
mean, ignoring `NaN` values.
5+
6+
If provided a value, the accumulator function returns an updated geometric
7+
mean. If not provided a value, the accumulator function returns the current
8+
geometric mean.
9+
10+
If provided a negative value, the accumulated value is `NaN` for all future
11+
invocations.
12+
13+
Returns
14+
-------
15+
acc: Function
16+
Accumulator function.
17+
18+
Examples
19+
--------
20+
> var accumulator = {{alias}}();
21+
> var v = accumulator()
22+
null
23+
> v = accumulator( 2.0 )
24+
2.0
25+
> v = accumulator( NaN )
26+
2.0
27+
> v = accumulator( 5.0 )
28+
~3.16
29+
> v = accumulator()
30+
~3.16
31+
32+
See Also
33+
--------
34+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 geometric mean; otherwise, returns the current geometric mean.
25+
*
26+
* @param x - value
27+
* @returns geometric mean
28+
*/
29+
type accumulator = ( x?: number ) => number | null;
30+
31+
/**
32+
* Returns an accumulator function which incrementally computes a geometric mean, ignoring `NaN` values.
33+
*
34+
* @returns accumulator function
35+
*
36+
* @example
37+
* var accumulator = incrnangmean();
38+
*
39+
* var v = accumulator();
40+
* // returns null
41+
*
42+
* v = accumulator( 2.0 );
43+
* // returns 2.0
44+
*
45+
* v = accumulator( NaN );
46+
* // returns 2.0
47+
*
48+
* v = accumulator( 5.0 );
49+
* // returns ~3.16
50+
*
51+
* v = accumulator();
52+
* // returns ~3.16
53+
*/
54+
declare function incrnangmean(): accumulator;
55+
56+
57+
// EXPORTS //
58+
59+
export = incrnangmean;

0 commit comments

Comments
 (0)