# Harmonic mean with zero value

How does harmonic mean handle zero values? what would the harmonic mean of {3, 4, 5, 0} be since $1/0=\infty$?

• Well, for your data, the harmonic mean is not defined! Why do you want to use an harmonic mean? You should give details of what you want to do. Harmonic mean is mostly used for situations when zero observations are logically impossible, so what is producing your zeros? truncation? true zeros? Answer will depend! – kjetil b halvorsen Sep 19 '12 at 21:39
• I have a bunch of numbers and I'm feeding "characteristics" about them into a neural network type classifier. What i did was to exclude zero values. – Dez Udezue Sep 19 '12 at 23:59

Just as the geometric mean of anything and $0$ is $0$, it is usually natural to define the harmonic mean of anything and $0$ to be $0$.

One physical interpretation of the harmonic mean is that if you have resistors in parallel, the total resistance is as though each resistor had the harmonic mean resistance. If one of the resistors has no resistance, there is no resistance over all (a short), and this is the same as if all resistors had no resistance.

If for some reason you are considering the harmonic means of numbers so that some are negative and some are positive, then it might be better to say that a harmonic mean of $0$ with itself is not defined. However, in the applications I know for the harmonic mean, it is used on nonnegative numbers.

• The resistor analogy is useful. – Amrinder Arora Apr 9 at 2:28

If you're working in a language that supports Infinity in computations properly, like R, you can define the harmonic mean like so:

harm <- function(x) 1/mean(1/x)


Then it will deal properly with zeroes in a natural way:

> harm(c(6, 2, 9, 4, 3, 1))
[1] 2.541176
> harm(c(6, 2, 9, 4, 0, 3, 1))
[1] 0

• It's a matter of opinion, but I don't think that is supporting infinity "properly". – Neil G Sep 11 '15 at 22:24
• What's wrong with it? It's just using 1/0==Inf, and 1/Inf==0, which is standard IEEE arithmetic. – Ken Williams Sep 11 '15 at 22:27
• The reason that IEEE allowed that was that regular calculations can underflow and this way at least you recover a sign from your computation. I think it's better for languages to raise exceptions on divide by zero and lets the user explicitly ignore them. If the computation that led to your x was a subtraction (a-b), e.g., then your result 1/x is nonsense. – Neil G Sep 11 '15 at 22:32
• This is a dreadful misunderstanding of limits. Let $\dfrac{1}{0} = \infty$ and $\dfrac{1}{\infty} = 0$. Then $$1 = 1 \cdot 1 = (\infty \cdot 0) \cdot (0 \cdot \infty) = \infty \cdot (0 \cdot 0) \cdot \infty = \infty \cdot 0 \cdot \infty = \infty \cdot \dfrac{1}{\infty} \cdot \infty = \infty,$$ and that's obviously nonsense. – Björn Friedrich Apr 9 '16 at 11:26
• I understand limits pretty well, actually. As for your calculation, that's neither how math works (nonconvergents are not associative, and $0 \cdot \infty \neq 1$) nor how IEEE arithmetic works ($\dfrac{1}{\infty} \cdot \infty$ is not equal to $1$, it's $NaN$). – Ken Williams Apr 14 '16 at 22:20