|
| 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 | +# incrnanhmean |
| 22 | + |
| 23 | +> Compute a [harmonic mean][harmonic-mean] incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [harmonic mean][harmonic-mean] of positive real numbers `x_0, x_1, ..., x_{n-1}` is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:harmonic_mean" align="center" raw="\begin{align}H &= \frac{n}{\frac{1}{x_0} + \frac{1}{x_1} + \cdots + \frac{1}{x_{n-1}}} \\ &= \frac{\displaystyle n}{\displaystyle\sum_{i=0}^{n-1} \frac{1}{x_i}} \\ &= \biggl( \frac{\displaystyle\sum_{i=0}^{n-1} \frac{1}{x_i}}{\displaystyle n} \biggr)^{-1}\end{align}" alt="Equation for the harmonic mean."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\begin{align}H &= \frac{n}{\frac{1}{x_0} + \frac{1}{x_1} + \cdots + \frac{1}{x_{n-1}}} \\ &= \frac{\displaystyle n}{\displaystyle\sum_{i=0}^{n-1} \frac{1}{x_i}} \\ &= \biggl( \frac{\displaystyle\sum_{i=0}^{n-1} \frac{1}{x_i}}{\displaystyle n} \biggr)^{-1}\end{align} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\begin{align}H &= \frac{n}{\frac{1}{x_0} + \frac{1}{x_1} + \cdots + \frac{1}{x_{n-1}}} \\ &= \frac{\displaystyle n}{\displaystyle\sum_{i=0}^{n-1} \frac{1}{x_i}} \\ &= \biggl( \frac{\displaystyle\sum_{i=0}^{n-1} \frac{1}{x_i}}{\displaystyle n} \biggr)^{-1}\end{align}" data-equation="eq:harmonic_mean"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@2b632747053b9e357a4663369528fe62b29a6d55/lib/node_modules/@stdlib/stats/incr/nanhmean/docs/img/equation_harmonic_mean.svg" alt="Equation for the harmonic mean."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +</section> |
| 43 | + |
| 44 | +<!-- /.intro --> |
| 45 | + |
| 46 | +<section class="usage"> |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +```javascript |
| 51 | +var incrnanhmean = require( '@stdlib/stats/incr/nanhmean' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### incrnanhmean() |
| 55 | + |
| 56 | +Returns an accumulator `function` which incrementally computes a [harmonic mean][harmonic-mean], ignoring `NaN` values. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var accumulator = incrnanhmean(); |
| 60 | +``` |
| 61 | + |
| 62 | +#### accumulator( \[x] ) |
| 63 | + |
| 64 | +If provided an input value `x`, the accumulator function returns an updated [harmonic mean][harmonic-mean]. If not provided an input value `x`, the accumulator function returns the current [harmonic mean][harmonic-mean]. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var accumulator = incrnanhmean(); |
| 68 | + |
| 69 | +var v = accumulator( 2.0 ); |
| 70 | +// returns 2.0 |
| 71 | + |
| 72 | +v = accumulator( 1.0 ); |
| 73 | +// returns ~1.33 |
| 74 | + |
| 75 | +v = accumulator( NaN ); |
| 76 | +// returns ~1.33 |
| 77 | + |
| 78 | +v = accumulator( 3.0 ); |
| 79 | +// returns ~1.64 |
| 80 | + |
| 81 | +v = accumulator(); |
| 82 | +// returns ~1.64 |
| 83 | +``` |
| 84 | + |
| 85 | +</section> |
| 86 | + |
| 87 | +<!-- /.usage --> |
| 88 | + |
| 89 | +<section class="notes"> |
| 90 | + |
| 91 | +## Notes |
| 92 | + |
| 93 | +- 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. |
| 94 | + |
| 95 | +</section> |
| 96 | + |
| 97 | +<!-- /.notes --> |
| 98 | + |
| 99 | +<section class="examples"> |
| 100 | + |
| 101 | +## Examples |
| 102 | + |
| 103 | +<!-- eslint no-undef: "error" --> |
| 104 | + |
| 105 | +```javascript |
| 106 | +var randu = require( '@stdlib/random/base/randu' ); |
| 107 | +var incrnanhmean = require( '@stdlib/stats/incr/nanhmean' ); |
| 108 | + |
| 109 | +var accumulator; |
| 110 | +var v; |
| 111 | +var i; |
| 112 | + |
| 113 | +// Initialize an accumulator: |
| 114 | +accumulator = incrnanhmean(); |
| 115 | + |
| 116 | +// For each simulated datum, update the harmonic mean... |
| 117 | +for ( i = 0; i < 100; i++ ) { |
| 118 | + if ( randu() < 0.2 ) { |
| 119 | + v = NaN; |
| 120 | + } else { |
| 121 | + v = ( randu()*100.0 ); |
| 122 | + } |
| 123 | + accumulator( v ); |
| 124 | +} |
| 125 | +console.log( accumulator() ); |
| 126 | +``` |
| 127 | + |
| 128 | +</section> |
| 129 | + |
| 130 | +<!-- /.examples --> |
| 131 | + |
| 132 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 133 | + |
| 134 | +<section class="related"> |
| 135 | + |
| 136 | +* * * |
| 137 | + |
| 138 | +## See Also |
| 139 | + |
| 140 | +- <span class="package-name">[`@stdlib/stats/incr/gmean`][@stdlib/stats/incr/gmean]</span><span class="delimiter">: </span><span class="description">compute a geometric mean incrementally.</span> |
| 141 | +- <span class="package-name">[`@stdlib/stats/incr/mean`][@stdlib/stats/incr/mean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally.</span> |
| 142 | +- <span class="package-name">[`@stdlib/stats/incr/mnanhmean`][@stdlib/stats/incr/mnanhmean]</span><span class="delimiter">: </span><span class="description">compute a moving harmonic mean incrementally.</span> |
| 143 | +- <span class="package-name">[`@stdlib/stats/incr/summary`][@stdlib/stats/incr/summary]</span><span class="delimiter">: </span><span class="description">compute a statistical summary incrementally.</span> |
| 144 | + |
| 145 | +</section> |
| 146 | + |
| 147 | +<!-- /.related --> |
| 148 | + |
| 149 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 150 | + |
| 151 | +<section class="links"> |
| 152 | + |
| 153 | +[harmonic-mean]: https://en.wikipedia.org/wiki/Harmonic_mean |
| 154 | + |
| 155 | +<!-- <related-links> --> |
| 156 | + |
| 157 | +[@stdlib/stats/incr/gmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/gmean |
| 158 | + |
| 159 | +[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean |
| 160 | + |
| 161 | +[@stdlib/stats/incr/mnanhmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mnanhmean |
| 162 | + |
| 163 | +[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary |
| 164 | + |
| 165 | +<!-- </related-links> --> |
| 166 | + |
| 167 | +</section> |
| 168 | + |
| 169 | +<!-- /.links --> |
0 commit comments