Closed
Description
The geometric mean, sometimes also called geometric average, is an average calculated by multiplying a set of positive values and taking the nth root, where n is the number of values.
The geometric mean is used to minimize the effects of extreme values; for instance, when averaging growth rates.
--- Source: Eurostat
Geometric mean requires that all numbers are > 0.
There are two formula:
- For N +ve numbers a1, a2, ... aN, GeoMean = Nth root of (a1 × a2 × ... aN).
- For N +ve numbers a1, a2, ... aN, GeoMean = exp(sum(ln(a1)), ln(a2), ... ĺn(aN)) / N).
--- Source: Wikipedia
Note that formula 1 could overflow easily, so formula 2 seems the best algorithm.
E.g.:
function GeoMean(const A: array of Double): Double;
begin
Assert(Length(A) > 0); // replace with exception
var SumOfLogs: Double := 0.0;
for var D in A do
begin
if Sign(D) <> PositiveValue then
raise EArgumentOutOfRangeException.Create('Non positive value passed to GeoMean');
SumOfLogs := SumOfLogs + Ln(A);
end;
Result := Exp(SumOfLogs / Length(A));
end;
This function was extracted from issue #16
Metadata
Metadata
Assignees
Projects
Status
Completed