Search Icon

Ryan Harrison My blog, portfolio and technology related ramblings

Java - Calculate the Geometric Mean

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

The geometric mean is defined as:

Formula


 
public static double geometricMean(double[] data)  
{
	double sum = data[0];

	for (int i = 1; i < data.length; i++) {
		sum *= data[i]; 
	}
	return Math.pow(sum, 1.0 / data.length); 
}