Harmonic mean (source code)

= Harmonic mean
{title2=$H=n/(\sum_i1/x_i)$}
{wiki}

The harmonic mean of a nonempty finite list of positive <real numbers> is the <multiplicative inverse> of the <arithmetic mean> of their inverses:
$$
H=\frac{n}{\sum_{i=1}^n1/x_i},\qquad \frac nH=\sum_{i=1}^n\frac1{x_i}.
$$
For two inputs, $H=2x_1x_2/(x_1+x_2)$. Replacing each input by $H$ preserves the <sum> of inverses, making it useful when a fixed amount divided by a rate determines a cost or duration. This positive-input definition avoids division by zero; negative inputs need not retain the usual averaging or ordering properties. https://dlmf.nist.gov/1.2.iv[NIST Digital Library of Mathematical Functions, §1.2(iv), equation 1.2.19: harmonic mean]{ref}.

Travel two legs of equal <length> $d$ at positive <speeds> $v_1,v_2$, with no stops. Their travel <times> are $d/v_1$ and $d/v_2$, so the average <speed> is
$$
v_{\mathrm{avg}}=\frac{2d}{d/v_1+d/v_2}=\frac{2}{1/v_1+1/v_2}.
$$
For two $60\,\mathrm{km}$ legs at $30$ and $60\,\mathrm{km/h}$, the <times> are two hours and one hour. \b[The average <speed> is $120/3=40\,\mathrm{km/h}$.] The same <speeds> maintained for one hour each instead cover $90\,\mathrm{km}$ in two hours, giving the <arithmetic mean> $45\,\mathrm{km/h}$. A slower equal-distance leg occupies more <time>, so the data being averaged must include what is held equal. This is total distance traveled divided by elapsed <time>; a return journey can have zero average <velocity> while keeping a positive average <speed>. https://undergroundmathematics.org/introducing-calculus/average-speed/averages[University of Cambridge, Underground Mathematics: equal-distance and equal-time speed averages]{ref}.

\Image[/harmonic-mean-equal-distance-travel.png]
{title=Equal-distance travel gives a <harmonic mean>; equal-time travel gives an <arithmetic mean>}
{description=Two original idealized journeys use the same <speeds>, $30$ and $60\,\mathrm{km/h}$. On the left each leg has <length> $60\,\mathrm{km}$, taking a total of three hours. On the right each leg lasts one hour, taking two hours and covering $90\,\mathrm{km}$. Dashed straight lines show the corresponding average <speeds>. The diagrams omit stops and transition duration and show cumulative distance traveled rather than signed <displacement>.}

The <arithmetic-geometric mean inequality> applied to the inverses gives $H\leq G$; together with $G\leq A$ it yields the comparison in <geometric mean>. For two inputs the arithmetic–harmonic gap has the exact form
$$
A-H=\frac{(x_1-x_2)^2}{2(x_1+x_2)}\geq0.
$$
For $30$ and $60$ the gap is five. With one positive input held fixed and another approaching zero, the <harmonic mean> approaches zero too: a very slow fixed-distance leg dominates the travel <time>. Unequal leg <lengths> instead require a <weighted harmonic mean>.