diff --git a/mathem/conversions.go b/mathem/conversions.go index 6f17a09..98fceb0 100644 --- a/mathem/conversions.go +++ b/mathem/conversions.go @@ -132,6 +132,24 @@ func GeometricMean(values ...float64) float64 { return math.Pow(product, float64(1.0/len(values))) } +/* +Calculates the Harmonic mean of the user given values. + +Example: + +func main() { + mathem.HarmonicMean(1,2,3,4) // [4/(1/1 + 1/2 + 1/3 + 1/4)] == 1.92 +} +*/ +func HarmonicMean(values ...float64) float64 { + n := len(values) + var div float64 + for i := 0; i < n; i++ { + div += 1 / values[i] + } + return float64(n) / div +} + /* Variance is the ratio between the square deviations of each value from the mean and the number of values.