Skip to main content
Collects values into a list

Calculating Geometric Mean using COLLECT_LIST and REDUCE

Geometric Mean is a measure of central tendency that calculates an average of the data using multiplication instead of addition. For a set of n numbers, the geometric mean is the nth root of the product of those numbers. For example, the geometric mean of the numbers 2, 3, and 14 equals (2 * 3 * 14)1/3 = (84)1/3 = 4.37952. Geometric Mean is particularly useful when the data is skewed and a simple average does not work. Suppose we are interested in the Geometric Mean Income of all the people who live in the same city as Fatima:
match path = ALL  (a {_id:'fatima'})-[:lives_in]->(b)<-[:lives_in]-(c)
return COLLECT_LIST(c) as residents
next
return reduce (geo_mean_product = 1, resident in residents | resident.income * geo_mean_product)  ^ (1.0 / LENGTH(residents)) as income_geometric_mean;
Note that we first use the COLLECT_LIST to get all the people who live in the same city as Fatima in a LIST. Next we use the REDUCE to calculate the Geometric Mean. An alternate way to calculate Geometric Mean would be:
match path = ALL  (a {_id:'fatima'})-[:lives_in]->(b)<-[:lives_in]-(c)
return exp(AVG(ln(c.income))) as income_geometric_mean;