Search Icon

Ryan Harrison My blog, portfolio and technology related ramblings

Java - Calculate the Harmonic Mean

Here is a small snippet to calculate the harmonic mean of a data set.

The harmonic mean is defined as:


public static double harmonicMean(double[] data)  
{  
	double sum = 0.0;

	for (int i = 0; i < data.length; i++) { 
		sum += 1.0 / data[i]; 
	} 
	return data.length / sum; 
}