|
| 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 | +# incrnanmmidrange |
| 22 | + |
| 23 | +> Compute a moving [mid-range][mid-range] incrementally, ignoring `NaN` values. |
| 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 incrnanmmidrange = require( '@stdlib/stats/incr/nanmmidrange' ); |
| 39 | +``` |
| 40 | + |
| 41 | +#### incrnanmmidrange( window ) |
| 42 | + |
| 43 | +Returns an accumulator `function` which incrementally computes a moving [mid-range][mid-range], ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving [mid-range][mid-range]. |
| 44 | + |
| 45 | +```javascript |
| 46 | +var accumulator = incrnanmmidrange( 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 = incrnanmmidrange( 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( NaN ); // [1.0, 3.0, -7.0] |
| 74 | +// returns -2.0 |
| 75 | + |
| 76 | +mr = accumulator( -5.0 ); // [3.0, -7.0, -5.0] |
| 77 | +// returns -2.0 |
| 78 | + |
| 79 | +mr = accumulator(); |
| 80 | +// returns -2.0 |
| 81 | +``` |
| 82 | + |
| 83 | +</section> |
| 84 | + |
| 85 | +<!-- /.usage --> |
| 86 | + |
| 87 | +<section class="notes"> |
| 88 | + |
| 89 | +## Notes |
| 90 | + |
| 91 | +- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. |
| 92 | +- 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. |
| 93 | + |
| 94 | +</section> |
| 95 | + |
| 96 | +<!-- /.notes --> |
| 97 | + |
| 98 | +<section class="examples"> |
| 99 | + |
| 100 | +## Examples |
| 101 | + |
| 102 | +<!-- eslint no-undef: "error" --> |
| 103 | + |
| 104 | +```javascript |
| 105 | +var randu = require( '@stdlib/random/base/randu' ); |
| 106 | +var incrmmidrange = require( '@stdlib/stats/incr/mmidrange' ); |
| 107 | + |
| 108 | +var accumulator; |
| 109 | +var v; |
| 110 | +var i; |
| 111 | + |
| 112 | +// Initialize an accumulator: |
| 113 | +accumulator = incrmmidrange( 5 ); |
| 114 | + |
| 115 | +// For each simulated datum, update the moving mid-range... |
| 116 | +for ( i = 0; i < 100; i++ ) { |
| 117 | + v = randu() * 100.0; |
| 118 | + accumulator( v ); |
| 119 | +} |
| 120 | +console.log( accumulator() ); |
| 121 | +``` |
| 122 | + |
| 123 | +</section> |
| 124 | + |
| 125 | +<!-- /.examples --> |
| 126 | + |
| 127 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 128 | + |
| 129 | +<section class="related"> |
| 130 | + |
| 131 | +* * * |
| 132 | + |
| 133 | +## See Also |
| 134 | + |
| 135 | +- <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> |
| 136 | +- <span class="package-name">[`@stdlib/stats/incr/mmax`][@stdlib/stats/incr/mmax]</span><span class="delimiter">: </span><span class="description">compute a moving maximum incrementally.</span> |
| 137 | +- <span class="package-name">[`@stdlib/stats/incr/mmin`][@stdlib/stats/incr/mmin]</span><span class="delimiter">: </span><span class="description">compute a moving minimum incrementally.</span> |
| 138 | +- <span class="package-name">[`@stdlib/stats/incr/mrange`][@stdlib/stats/incr/mrange]</span><span class="delimiter">: </span><span class="description">compute a moving range incrementally.</span> |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.related --> |
| 143 | + |
| 144 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 145 | + |
| 146 | +<section class="links"> |
| 147 | + |
| 148 | +[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29 |
| 149 | + |
| 150 | +[mid-range]: https://en.wikipedia.org/wiki/Mid-range |
| 151 | + |
| 152 | +<!-- <related-links> --> |
| 153 | + |
| 154 | +[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean |
| 155 | + |
| 156 | +[@stdlib/stats/incr/mmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmax |
| 157 | + |
| 158 | +[@stdlib/stats/incr/mmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmin |
| 159 | + |
| 160 | +[@stdlib/stats/incr/mrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mrange |
| 161 | + |
| 162 | +<!-- </related-links> --> |
| 163 | + |
| 164 | +</section> |
| 165 | + |
| 166 | +<!-- /.links --> |
0 commit comments