Skip to content

Commit c0c5867

Browse files
committed
feat(stats): add nanmgmean package
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 9b2b107 commit c0c5867

File tree

11 files changed

+895
-0
lines changed

11 files changed

+895
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
# incrnanmgmean
22+
23+
> Compute a moving [geometric mean][geometric-mean] incrementally, ignoring `NaN` value .
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@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mgmean/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 incrnanmgmean = require( '@stdlib/stats/incr/nanmgmean' );
52+
```
53+
54+
#### incrnanmgmean( window )
55+
56+
Returns an accumulator `function` which incrementally computes a moving [geometric mean][geometric-mean]. The `window` parameter defines the number of values over which to compute the moving [geometric mean][geometric-mean].
57+
58+
```javascript
59+
var accumulator = incrnanmgmean( 3 );
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 = incrnanmgmean( 3 );
68+
69+
var v = accumulator();
70+
// returns null
71+
72+
// Fill the window...
73+
v = accumulator( 2.0 ); // [2.0]
74+
// returns 2.0
75+
76+
v = accumulator( 1.0 ); // [2.0, 1.0]
77+
// returns ~1.41
78+
79+
v = accumulator( 3.0 ); // [2.0, 1.0, 3.0]
80+
// returns ~1.82
81+
82+
v = accumulator( NaN ); // [2.0, 1.0, 3.0]
83+
// returns ~1.82
84+
85+
// Window begins sliding...
86+
v = accumulator( 7.0 ); // [1.0, 3.0, 7.0]
87+
// returns ~2.76
88+
89+
v = accumulator( 5.0 ); // [3.0, 7.0, 5.0]
90+
// returns ~4.72
91+
92+
v = accumulator();
93+
// returns ~4.72
94+
```
95+
96+
</section>
97+
98+
<!-- /.usage -->
99+
100+
<section class="notes">
101+
102+
## Notes
103+
104+
- Input values are **not** type checked. If a NaN value is provided, the accumulator skips the update and continues to return the previous state. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
105+
- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values.
106+
107+
</section>
108+
109+
<!-- /.notes -->
110+
111+
<section class="examples">
112+
113+
## Examples
114+
115+
<!-- eslint no-undef: "error" -->
116+
117+
```javascript
118+
var randu = require( '@stdlib/random/base/randu' );
119+
var incrnanmgmean = require( '@stdlib/stats/incr/nanmgmean' );
120+
121+
var accumulator;
122+
var v;
123+
var i;
124+
125+
// Initialize an accumulator:
126+
accumulator = incrnanmgmean( 5 );
127+
128+
// For each simulated datum, update the moving geometric mean...
129+
for ( i = 0; i < 100; i++ ) {
130+
v = randu() * 100.0;
131+
accumulator( v );
132+
}
133+
console.log( accumulator() );
134+
```
135+
136+
</section>
137+
138+
<!-- /.examples -->
139+
140+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
141+
142+
<section class="related">
143+
144+
* * *
145+
146+
## See Also
147+
148+
- <span class="package-name">[`@stdlib/stats/incr/gmean`][@stdlib/stats/incr/gmean]</span><span class="delimiter">: </span><span class="description">compute a geometric mean incrementally.</span>
149+
- <span class="package-name">[`@stdlib/stats/incr/mhmean`][@stdlib/stats/incr/mhmean]</span><span class="delimiter">: </span><span class="description">compute a moving harmonic mean incrementally.</span>
150+
- <span class="package-name">[`@stdlib/stats/incr/mmean`][@stdlib/stats/incr/mmean]</span><span class="delimiter">: </span><span class="description">compute a moving arithmetic mean incrementally.</span>
151+
152+
</section>
153+
154+
<!-- /.related -->
155+
156+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
157+
158+
<section class="links">
159+
160+
[geometric-mean]: https://en.wikipedia.org/wiki/Geometric_mean
161+
162+
<!-- <related-links> -->
163+
164+
[@stdlib/stats/incr/gmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/gmean
165+
166+
[@stdlib/stats/incr/mhmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mhmean
167+
168+
[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean
169+
170+
<!-- </related-links> -->
171+
172+
</section>
173+
174+
<!-- /.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 incrnanmgmean = 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 = incrnanmgmean( (i%5)+1 );
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 = incrnanmgmean( 5 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( W )
3+
Returns an accumulator function which incrementally computes a moving
4+
geometric mean, ignoring `NaN` value.
5+
6+
The `W` parameter defines the number of values over which to compute the
7+
moving geometric mean.
8+
9+
If provided a value, the accumulator function returns an updated moving
10+
geometric mean. If not provided a value, the accumulator function returns
11+
the current moving geometric mean.
12+
13+
Parameters
14+
----------
15+
W: integer
16+
Window size.
17+
18+
Returns
19+
-------
20+
acc: Function
21+
Accumulator function.
22+
23+
Examples
24+
--------
25+
> var accumulator = {{alias}}( 3 );
26+
> var v = accumulator()
27+
null
28+
> v = accumulator( 2.0 )
29+
2.0
30+
> v = accumulator( 5.0 )
31+
~3.16
32+
> v = accumulator( 3.0 )
33+
~3.11
34+
> v = accumulator( NaN )
35+
~3.11
36+
> v = accumulator( 5.0 )
37+
~4.22
38+
> v = accumulator()
39+
~4.22
40+
41+
See Also
42+
--------
43+

0 commit comments

Comments
 (0)