Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add geometric mean function #31

Closed
Closed
Assignees
Labels
completedIssue completed and committed to develop. To be closed on next release enhancementNew feature or request
@delphidabbler

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:

  1. For N +ve numbers a1, a2, ... aN, GeoMean = Nth root of (a1 ×ばつ a2 ×ばつ ... aN).
  2. 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

Labels

completedIssue completed and committed to develop. To be closed on next release enhancementNew feature or request

Projects

Status

Completed

Milestone

No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      AltStyle によって変換されたページ (->オリジナル) /