FUNCTION
Harmonic
Calculates the harmonic mean of a given population.
You're viewing an older version of this page (#5367). View the current version.
Interface
#include <codecogs/statistics/moments/harmonic.h>
using namespace Statistics::Moments;
The harmonic mean of N numbers $x_i$ (where, $i=1,2...,N$) is defined by
$$H=\frac{n}{\sum_{i=1}^N\frac{1}{x_i}}$$
(1)
The special cases of $N=2$ and $N=3$ are therefore given by
$$H(x_1,x_2)=\frac{2x_1x_2}{x_1+x_2}$$
(2)
$$H(x_1,x_2,x_3)=\frac{3x_1x_2x_3}{x_1x_2+x_1x_3+x_2x_3},$$
(3)
and so on.
For $N=2$, the harmonic mean is related to the arithmetic mean, A, and the geometric mean, G, by
$$H=\frac{G^2}{A}$$
(4)
The harmonic mean is the special case $M_{-1}$ of the power mean and is one of the Pythagorean means.
Example 1
#include <codecogs/statistics/moments/harmonic.h>
#include <iostream>
int main()
{
double x[4] = {3.5 , 6.5 , 5.9 , 8.8};
double harm = Statistics::Moments::harmonic<double>(4, x);
std::cout << "The population harmonic mean is: " << harm << std::endl;
return 0;
}Output:
The population harmonic mean is:5.53489Parameters
n
the size of the population
data
the actual population data given as an array
Returns
the harmonic mean of a given population
This function's source code is only visible to registered users — documentation and the calculator above are free to use either way. Sign in to see it.