Skip to content

Commit 271147b

Browse files
committed
Add iterator utility to compute a moving mid-range
1 parent deee5b4 commit 271147b

8 files changed

Lines changed: 969 additions & 0 deletions

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
# itermmidrange
22+
23+
> Create an [iterator][mdn-iterator-protocol] which iteratively computes a moving [mid-range][mid-range].
24+
25+
<section class="intro">
26+
27+
The [**mid-range**][mid-range], or **mid-extreme**, is the arithmetic mean of maximum and minimum values. Accordingly, the [mid-range][mid-range] is the midpoint of the [range][range] and a measure of central tendency.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var itermmidrange = require( '@stdlib/stats/iter/mmidrange' );
41+
```
42+
43+
#### itermmidrange( iterator, W )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which iteratively computes a moving [mid-range][mid-range]. The `W` parameter defines the number of iterated values over which to compute the moving [mid-range][mid-range].
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
var arr = array2iterator( [ 2.0, 1.0, 3.0, -7.0, -5.0 ] );
51+
var it = itermmidrange( arr, 3 );
52+
53+
// Fill the window...
54+
var v = it.next().value; // [2.0]
55+
// returns 2.0
56+
57+
v = it.next().value; // [2.0, 1.0]
58+
// returns 1.5
59+
60+
v = it.next().value; // [2.0, 1.0, 3.0]
61+
// returns 2.0
62+
63+
// Window begins sliding...
64+
v = it.next().value; // [1.0, 3.0, -7.0]
65+
// returns -2.0
66+
67+
v = it.next().value; // [3.0, -7.0, -5.0]
68+
// returns -2.0
69+
```
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
76+
77+
<section class="notes">
78+
79+
## Notes
80+
81+
- If an iterated value is non-numeric (including `NaN`), the function returns `NaN` for **at least** `W-1` future invocations. If non-numeric iterated values are possible, you are advised to provide an [`iterator`][mdn-iterator-protocol] which type checks and handles non-numeric values accordingly.
82+
- 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 previously iterated values.
83+
84+
</section>
85+
86+
<!-- /.notes -->
87+
88+
<!-- Package usage examples. -->
89+
90+
<section class="examples">
91+
92+
## Examples
93+
94+
<!-- eslint no-undef: "error" -->
95+
96+
```javascript
97+
var runif = require( '@stdlib/random/iter/uniform' );
98+
var itermmidrange = require( '@stdlib/stats/iter/mmidrange' );
99+
100+
// Create an iterator for generating uniformly distributed pseudorandom numbers:
101+
var rand = runif( -10.0, 10.0, {
102+
'seed': 1234,
103+
'iter': 100
104+
});
105+
106+
// Create an iterator for iteratively computing a moving mid-range:
107+
var it = itermmidrange( rand, 3 );
108+
109+
// Perform manual iteration...
110+
var v;
111+
while ( true ) {
112+
v = it.next();
113+
if ( typeof v.value === 'number' ) {
114+
console.log( 'mid-range: %d', v.value );
115+
}
116+
if ( v.done ) {
117+
break;
118+
}
119+
}
120+
```
121+
122+
</section>
123+
124+
<!-- /.examples -->
125+
126+
<!-- 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. -->
127+
128+
<section class="references">
129+
130+
</section>
131+
132+
<!-- /.references -->
133+
134+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
135+
136+
<section class="links">
137+
138+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
139+
140+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
141+
142+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
143+
144+
</section>
145+
146+
<!-- /.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 itermmidrange = 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 = itermmidrange( rand, 3 );
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 = itermmidrange( rand, 3 );
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
{{alias}}( iterator, W )
3+
Returns an iterator which iteratively computes a moving mid-range.
4+
5+
The mid-range is the arithmetic mean of maximum and minimum values.
6+
Accordingly, the mid-range is the midpoint of the range and a measure of
7+
central tendency.
8+
9+
The `W` parameter defines the number of iterated values over which to
10+
compute the moving mid-range.
11+
12+
As `W` values are needed to fill the window buffer, the first `W-1` returned
13+
values are calculated from smaller sample sizes. Until the window is full,
14+
each returned value is calculated from all previously iterated values.
15+
16+
If an environment supports Symbol.iterator, the returned iterator is
17+
iterable.
18+
19+
Parameters
20+
----------
21+
iterator: Object
22+
Input iterator.
23+
24+
W: integer
25+
Window size.
26+
27+
Returns
28+
-------
29+
iterator: Object
30+
Iterator.
31+
32+
iterator.next(): Function
33+
Returns an iterator protocol-compliant object containing the next
34+
iterated value (if one exists) and a boolean flag indicating whether the
35+
iterator is finished.
36+
37+
iterator.return( [value] ): Function
38+
Finishes an iterator and returns a provided value.
39+
40+
Examples
41+
--------
42+
> var arr = {{alias:@stdlib/array/to-iterator}}( [ 2.0, -5.0, 3.0, 5.0 ] );
43+
> var it = {{alias}}( arr, 3 );
44+
> var v = it.next().value
45+
2.0
46+
> v = it.next().value
47+
-1.5
48+
> v = it.next().value
49+
-1.0
50+
> v = it.next().value
51+
0.0
52+
53+
See Also
54+
--------
55+
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 itermmidrange = require( './../lib' );
23+
24+
// Create an iterator for generating uniformly distributed pseudorandom numbers:
25+
var rand = runif( -10.0, 10.0, {
26+
'seed': 1234,
27+
'iter': 100
28+
});
29+
30+
// Create an iterator for iteratively computing a moving mid-range:
31+
var it = itermmidrange( rand, 3 );
32+
33+
// Perform manual iteration...
34+
var v;
35+
while ( true ) {
36+
v = it.next();
37+
if ( typeof v.value === 'number' ) {
38+
console.log( 'mid-range: %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 moving mid-range.
23+
*
24+
* @module @stdlib/stats/iter/mmidrange
25+
*
26+
* @example
27+
* var runif = require( '@stdlib/random/iter/uniform' );
28+
* var itermmidrange = require( '@stdlib/stats/iter/mmidrange' );
29+
*
30+
* var rand = runif( -10.0, 10.0, {
31+
* 'iter': 100
32+
* });
33+
*
34+
* var it = itermmidrange( rand, 3 );
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)