# Computing the harmonic mean of an image collection in the Google Earth Engine

How can I compute the harmonic instead of arithmetic mean for a series of images in Google Earth Engine? There is a build-in function to compute the arithmetic mean and median, but there is no harmonic/geometric mean available. I tried to build my own function by making use of the reduce function, but with this I did not succeed. Any ideas/examples on how to proceed?

This should be doable as follows:

``````// a random point on the map
var point = ee.Geometry.Point([-106.95381215107625, 39.46780712207983]);
// Make some constant images and put them together in a image collection
var list = ee.List([1,1,1, 2,2,2,3,4,5,5,6,6,6,6,7,7,8,9,9]);
var imageCol = ee.ImageCollection.fromImages(list.map(function(integer){
return ee.Image.constant(integer).toInt();
}));

// Calculate the harmonic mean based on: https://en.wikipedia.org/wiki/Harmonic_mean
var n = imageCol.size(); // number of observations (19)
var harmonicMean = imageCol.map(function(image){
// Note that function image.pow() cannot handle 0 values
return image.pow(-1);
}).sum().divide(n).pow(-1);
print('harmonic mean image collection',
harmonicMean.reduceRegion('first', point, 1000).get('constant'));

// Calculate the harmonic mean based on: https://en.wikipedia.org/wiki/Harmonic_mean
// All data points should be possitive
var geometricMean = imageCol.product().pow(ee.Number(1).divide(n));
print('geometric mean image collection',
geometricMean.reduceRegion('first', point, 1000).get('constant'));
``````

According to Wikipedia, I checked how to calculate the harmonic and geometric mean. And according to this site, the outcomes coincide. Link to code.

• works like a charm, thank you! Apr 10, 2019 at 8:52