|
| 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 | + |
| 36 | +<!-- </equation> --> |
| 37 | + |
| 38 | +</section> |
| 39 | + |
| 40 | +<!-- /.intro --> |
| 41 | + |
| 42 | +<section class="usage"> |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +```javascript |
| 47 | +var incrnanhmean = require( '@stdlib/stats/incr/nanhmean' ); |
| 48 | +``` |
| 49 | + |
| 50 | +#### incrnanhmean() |
| 51 | + |
| 52 | +Returns an accumulator `function` which incrementally computes a [harmonic mean][harmonic-mean], ignoring `NaN` values. |
| 53 | + |
| 54 | +```javascript |
| 55 | +var accumulator = incrnanhmean(); |
| 56 | +``` |
| 57 | + |
| 58 | +#### accumulator( \[x] ) |
| 59 | + |
| 60 | +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]. |
| 61 | + |
| 62 | +```javascript |
| 63 | +var accumulator = incrnanhmean(); |
| 64 | + |
| 65 | +var v = accumulator( 2.0 ); |
| 66 | +// returns 2.0 |
| 67 | + |
| 68 | +v = accumulator( 1.0 ); |
| 69 | +// returns ~1.33 |
| 70 | + |
| 71 | +v = accumulator( NaN ); |
| 72 | +// returns ~1.33 |
| 73 | + |
| 74 | +v = accumulator( 3.0 ); |
| 75 | +// returns ~1.64 |
| 76 | + |
| 77 | +v = accumulator(); |
| 78 | +// returns ~1.64 |
| 79 | +``` |
| 80 | + |
| 81 | +</section> |
| 82 | + |
| 83 | +<!-- /.usage --> |
| 84 | + |
| 85 | +<section class="notes"> |
| 86 | + |
| 87 | +## Notes |
| 88 | + |
| 89 | +- 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. |
| 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 uniform = require( '@stdlib/random/base/uniform' ); |
| 103 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 104 | +var incrnanhmean = require( '@stdlib/stats/incr/nanhmean' ); |
| 105 | + |
| 106 | +var accumulator; |
| 107 | +var v; |
| 108 | +var i; |
| 109 | + |
| 110 | +// Initialize an accumulator: |
| 111 | +accumulator = incrnanhmean(); |
| 112 | + |
| 113 | +// For each simulated datum, update the harmonic mean... |
| 114 | +for ( i = 0; i < 100; i++ ) { |
| 115 | + if ( bernoulli( 0.2 ) ) { |
| 116 | + v = NaN; |
| 117 | + } else { |
| 118 | + v = uniform( 1.0, 100.0 ); |
| 119 | + } |
| 120 | + accumulator( v ); |
| 121 | +} |
| 122 | +console.log( accumulator() ); |
| 123 | +``` |
| 124 | + |
| 125 | +</section> |
| 126 | + |
| 127 | +<!-- /.examples --> |
| 128 | + |
| 129 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 130 | + |
| 131 | +<section class="related"> |
| 132 | + |
| 133 | +</section> |
| 134 | + |
| 135 | +<!-- /.related --> |
| 136 | + |
| 137 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 138 | + |
| 139 | +<section class="links"> |
| 140 | + |
| 141 | +[harmonic-mean]: https://en.wikipedia.org/wiki/Harmonic_mean |
| 142 | + |
| 143 | +<!-- <related-links> --> |
| 144 | + |
| 145 | +<!-- </related-links> --> |
| 146 | + |
| 147 | +</section> |
| 148 | + |
| 149 | +<!-- /.links --> |
0 commit comments