Skip to content

Commit 787645a

Browse files
committed
feat feat: add stats/incr/nangmean
1 parent 9e91f81 commit 787645a

File tree

11 files changed

+850
-0
lines changed

11 files changed

+850
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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} \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} \biggr)^{\frac{1}{n}} = \sqrt[n]{x_0 x_1 \cdots x_{n-1}}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\biggl( \prod_{i=0}^{n-1} \biggr)^{\frac{1}{n}} = \sqrt[n]{x_0 x_1 \cdots x_{n-1}}" data-equation="eq:geometric_mean">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@cb802bd5cb07ef925c8a3ce9c34db0fb68040d12/lib/node_modules/@stdlib/stats/incr/gmean/docs/img/equation_geometric_mean.svg" alt="Equation for the geometric 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 incrnangmean = require( '@stdlib/stats/incr/gmean' );
52+
```
53+
54+
#### incrnangmean()
55+
56+
Returns an accumulator `function` which incrementally computes a [geometric mean][geometric-mean], ignoring `NaN` values.
57+
58+
```javascript
59+
var accumulator = incrnangmean();
60+
```
61+
62+
#### accumulator( \[x] )
63+
64+
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].
65+
66+
```javascript
67+
var accumulator = incrnangmean();
68+
69+
var prod = accumulator( 2.0 );
70+
// returns 2.0
71+
72+
prod = accumulator( 1.0 );
73+
// returns ~1.414
74+
75+
prod = accumulator( NaN );
76+
// returns ~1.414
77+
78+
prod = accumulator( 3.0 );
79+
// returns ~1.817
80+
81+
prod = accumulator();
82+
// returns ~1.817
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 randu = require( '@stdlib/random/base/randu' );
107+
var incrnangmean = require( '@stdlib/stats/incr/nangmean' );
108+
109+
var accumulator;
110+
var v;
111+
var i;
112+
113+
// Initialize an accumulator:
114+
accumulator = incrnangmean();
115+
116+
// For each simulated value, update the geometric mean...
117+
for ( i = 0; i < 100; i++ ) {
118+
if ( randu() < 0.2 ) {
119+
v = NaN;
120+
} else {
121+
v = ( randu()*100.0 );
122+
}
123+
accumulator( v );
124+
}
125+
console.log( accumulator() );
126+
```
127+
128+
</section>
129+
130+
<!-- /.examples -->
131+
132+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
133+
134+
<section class="related">
135+
136+
* * *
137+
138+
## See Also
139+
140+
- <span class="package-name">[`@stdlib/stats/incr/hmean`][@stdlib/stats/incr/hmean]</span><span class="delimiter">: </span><span class="description">compute a harmonic mean incrementally.</span>
141+
- <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>
142+
- <span class="package-name">[`@stdlib/stats/incr/mgmean`][@stdlib/stats/incr/mgmean]</span><span class="delimiter">: </span><span class="description">compute a moving geometric mean incrementally.</span>
143+
- <span class="package-name">[`@stdlib/stats/incr/summary`][@stdlib/stats/incr/summary]</span><span class="delimiter">: </span><span class="description">compute a statistical summary incrementally.</span>
144+
145+
</section>
146+
147+
<!-- /.related -->
148+
149+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="links">
152+
153+
[geometric-mean]: https://en.wikipedia.org/wiki/Geometric_mean
154+
155+
<!-- <related-links> -->
156+
157+
[@stdlib/stats/incr/hmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/hmean
158+
159+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
160+
161+
[@stdlib/stats/incr/mgmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mgmean
162+
163+
[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary
164+
165+
<!-- </related-links> -->
166+
167+
</section>
168+
169+
<!-- /.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+

0 commit comments

Comments
 (0)