README.md (4134B)
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 -->