Skip to content

Commit f6beb42

Browse files
committed
Add iterator utility to iteratively compute a cumulative geometric mean
1 parent cac9844 commit f6beb42

8 files changed

Lines changed: 911 additions & 0 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2019 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+
# itercugmean
22+
23+
> Create an [iterator][mdn-iterator-protocol] which iteratively computes a cumulative [geometric mean][geometric-mean].
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+
<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">
32+
<img src="" alt="Equation for the geometric mean.">
33+
<br>
34+
</div>
35+
36+
<!-- </equation> -->
37+
38+
</section>
39+
40+
<!-- /.intro -->
41+
42+
<!-- Package usage documentation. -->
43+
44+
<section class="usage">
45+
46+
## Usage
47+
48+
```javascript
49+
var itercugmean = require( '@stdlib/stats/iter/cugmean' );
50+
```
51+
52+
#### itercugmean( iterator )
53+
54+
Returns an [iterator][mdn-iterator-protocol] which iteratively computes a cumulative [geometric mean][geometric-mean].
55+
56+
```javascript
57+
var array2iterator = require( '@stdlib/array/to-iterator' );
58+
59+
var arr = array2iterator( [ 2.0, 1.0, 3.0, 7.0, 5.0 ] );
60+
var it = itercugmean( arr );
61+
62+
var v = it.next().value;
63+
// returns 2.0
64+
65+
v = it.next().value;
66+
// returns ~1.41
67+
68+
v = it.next().value;
69+
// returns ~1.82
70+
71+
v = it.next().value;
72+
// returns ~2.55
73+
74+
v = it.next().value;
75+
// returns ~2.91
76+
```
77+
78+
</section>
79+
80+
<!-- /.usage -->
81+
82+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
83+
84+
<section class="notes">
85+
86+
## Notes
87+
88+
- If an iterated value is non-numeric (including `NaN`) or negative, the function returns `NaN` for **all** future iterations. If non-numeric and/or negative iterated values are possible, you are advised to provide an [`iterator`][mdn-iterator-protocol] which type checks and handles such values accordingly.
89+
90+
</section>
91+
92+
<!-- /.notes -->
93+
94+
<!-- Package usage examples. -->
95+
96+
<section class="examples">
97+
98+
## Examples
99+
100+
<!-- eslint no-undef: "error" -->
101+
102+
```javascript
103+
var runif = require( '@stdlib/random/iter/uniform' );
104+
var itercugmean = require( '@stdlib/stats/iter/cugmean' );
105+
106+
// Create an iterator for generating uniformly distributed pseudorandom numbers:
107+
var rand = runif( 0.0, 10.0, {
108+
'seed': 1234,
109+
'iter': 100
110+
});
111+
112+
// Create an iterator for iteratively computing a cumulative geometric mean:
113+
var it = itercugmean( rand );
114+
115+
// Perform manual iteration...
116+
var v;
117+
while ( true ) {
118+
v = it.next();
119+
if ( typeof v.value === 'number' ) {
120+
console.log( 'gmean: %d', v.value );
121+
}
122+
if ( v.done ) {
123+
break;
124+
}
125+
}
126+
```
127+
128+
</section>
129+
130+
<!-- /.examples -->
131+
132+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
133+
134+
<section class="references">
135+
136+
</section>
137+
138+
<!-- /.references -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
[geometric-mean]: https://en.wikipedia.org/wiki/Geometric_mean
145+
146+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
147+
148+
</section>
149+
150+
<!-- /.links -->
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
26+
var randu = require( '@stdlib/random/iter/randu' );
27+
var pkg = require( './../package.json' ).name;
28+
var itercugmean = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var rand;
35+
var iter;
36+
var i;
37+
38+
rand = randu();
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
iter = itercugmean( rand );
43+
if ( typeof iter !== 'object' ) {
44+
b.fail( 'should return an object' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isIteratorLike( iter ) ) {
49+
b.fail( 'should return an iterator protocol-compliant object' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+'::iteration', function benchmark( b ) {
56+
var iter;
57+
var rand;
58+
var v;
59+
var i;
60+
61+
rand = randu();
62+
iter = itercugmean( rand );
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
v = iter.next().value;
67+
if ( isnan( v ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( v ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
{{alias}}( iterator )
3+
Returns an iterator which iteratively computes a cumulative geometric mean.
4+
5+
If provided a negative value, the iterated value is `NaN` for all future
6+
invocations.
7+
8+
If an environment supports Symbol.iterator, the returned iterator is
9+
iterable.
10+
11+
Parameters
12+
----------
13+
iterator: Object
14+
Input iterator.
15+
16+
Returns
17+
-------
18+
iterator: Object
19+
Iterator.
20+
21+
iterator.next(): Function
22+
Returns an iterator protocol-compliant object containing the next
23+
iterated value (if one exists) and a boolean flag indicating whether the
24+
iterator is finished.
25+
26+
iterator.return( [value] ): Function
27+
Finishes an iterator and returns a provided value.
28+
29+
Examples
30+
--------
31+
> var arr = {{alias:@stdlib/array/to-iterator}}( [ 2.0, 5.0, 3.0, 5.0 ] );
32+
> var it = {{alias}}( arr );
33+
> var v = it.next().value
34+
2.0
35+
> v = it.next().value
36+
~3.16
37+
> v = it.next().value
38+
~3.11
39+
> v = it.next().value
40+
~3.50
41+
42+
See Also
43+
--------
44+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
var runif = require( '@stdlib/random/iter/uniform' );
22+
var itercugmean = require( './../lib' );
23+
24+
// Create an iterator for generating uniformly distributed pseudorandom numbers:
25+
var rand = runif( 0.0, 10.0, {
26+
'seed': 1234,
27+
'iter': 100
28+
});
29+
30+
// Create an iterator for iteratively computing a cumulative geometric mean:
31+
var it = itercugmean( rand );
32+
33+
// Perform manual iteration...
34+
var v;
35+
while ( true ) {
36+
v = it.next();
37+
if ( typeof v.value === 'number' ) {
38+
console.log( 'gmean: %d', v.value );
39+
}
40+
if ( v.done ) {
41+
break;
42+
}
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
/**
22+
* Create an iterator which iteratively computes a cumulative geometric mean.
23+
*
24+
* @module @stdlib/stats/iter/cugmean
25+
*
26+
* @example
27+
* var runif = require( '@stdlib/random/iter/uniform' );
28+
* var itercugmean = require( '@stdlib/stats/iter/cugmean' );
29+
*
30+
* var rand = runif( 0.0, 10.0, {
31+
* 'iter': 100
32+
* });
33+
*
34+
* var it = itercugmean( rand );
35+
*
36+
* var v = it.next().value;
37+
* // returns <number>
38+
*
39+
* v = it.next().value;
40+
* // returns <number>
41+
*
42+
* v = it.next().value;
43+
* // returns <number>
44+
*
45+
* // ...
46+
*/
47+
48+
// MODULES //
49+
50+
var iterator = require( './main.js' );
51+
52+
53+
// EXPORTS //
54+
55+
module.exports = iterator;

0 commit comments

Comments
 (0)