Commits

Aleksey Khudyakov  committed 954c62b

Avoid overflow of exponent in calculation of geometric mean

Fixes #38 by Alex Lang

  • Participants
  • Parent commits 09f19e7

Comments (0)

Files changed (1)

File Statistics/Sample.hs

 
 -- | /O(n)/ Geometric mean of a sample containing no negative values.
 geometricMean :: (G.Vector v Double) => v Double -> Double
-geometricMean = fini . G.foldl' go (T 1 0)
-  where
-    fini (T p n) = p ** (1 / fromIntegral n)
-    go (T p n) a = T (p * a) (n + 1)
+geometricMean = exp . mean . G.map log
 {-# INLINE geometricMean #-}
 
 -- | Compute the /k/th central moment of a sample.  The central moment