README.md (3341B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # incrmmidrange 22 23 > Compute a moving [mid-range][mid-range] incrementally. 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 <section class="usage"> 34 35 ## Usage 36 37 ```javascript 38 var incrmmidrange = require( '@stdlib/stats/incr/mmidrange' ); 39 ``` 40 41 #### incrmmidrange( window ) 42 43 Returns an accumulator `function` which incrementally computes a moving [mid-range][mid-range]. The `window` parameter defines the number of values over which to compute the moving [mid-range][mid-range]. 44 45 ```javascript 46 var accumulator = incrmmidrange( 3 ); 47 ``` 48 49 #### accumulator( \[x] ) 50 51 If provided an input value `x`, the accumulator function returns an updated [mid-range][mid-range]. If not provided an input value `x`, the accumulator function returns the current [mid-range][mid-range]. 52 53 ```javascript 54 var accumulator = incrmmidrange( 3 ); 55 56 var mr = accumulator(); 57 // returns null 58 59 // Fill the window... 60 mr = accumulator( 2.0 ); // [2.0] 61 // returns 2.0 62 63 mr = accumulator( 1.0 ); // [2.0, 1.0] 64 // returns 1.5 65 66 mr = accumulator( 3.0 ); // [2.0, 1.0, 3.0] 67 // returns 2.0 68 69 // Window begins sliding... 70 mr = accumulator( -7.0 ); // [1.0, 3.0, -7.0] 71 // returns -2.0 72 73 mr = accumulator( -5.0 ); // [3.0, -7.0, -5.0] 74 // returns -2.0 75 76 mr = accumulator(); 77 // returns -2.0 78 ``` 79 80 </section> 81 82 <!-- /.usage --> 83 84 <section class="notes"> 85 86 ## Notes 87 88 - Input values are **not** type checked. If provided `NaN`, the accumulated value is `NaN` for **at least** `W-1` future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. 89 - 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. 90 91 </section> 92 93 <!-- /.notes --> 94 95 <section class="examples"> 96 97 ## Examples 98 99 <!-- eslint no-undef: "error" --> 100 101 ```javascript 102 var randu = require( '@stdlib/random/base/randu' ); 103 var incrmmidrange = require( '@stdlib/stats/incr/mmidrange' ); 104 105 var accumulator; 106 var v; 107 var i; 108 109 // Initialize an accumulator: 110 accumulator = incrmmidrange( 5 ); 111 112 // For each simulated datum, update the moving mid-range... 113 for ( i = 0; i < 100; i++ ) { 114 v = randu() * 100.0; 115 accumulator( v ); 116 } 117 console.log( accumulator() ); 118 ``` 119 120 </section> 121 122 <!-- /.examples --> 123 124 <section class="links"> 125 126 [range]: https://en.wikipedia.org/wiki/Range_%28statistics%29 127 128 [mid-range]: https://en.wikipedia.org/wiki/Mid-range 129 130 </section> 131 132 <!-- /.links -->