From 185297fa8ebb033d8d7ebb6fc7895335e58f985b Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 17 Nov 2009 02:17:16 +0100
Subject: [PATCH] Collapse the statistical functions into one

This allows us to get rid of two duplicate list length computations,
with a minor speedup.
---
 Ganeti/HTools/Utils.hs | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/Ganeti/HTools/Utils.hs b/Ganeti/HTools/Utils.hs
index 66722806f..66498db70 100644
--- a/Ganeti/HTools/Utils.hs
+++ b/Ganeti/HTools/Utils.hs
@@ -78,23 +78,18 @@ fst3 (a, _, _) = a
 
 -- * Mathematical functions
 
--- Simple and slow statistical functions, please replace with better versions
+-- Simple and slow statistical functions, please replace with better
+-- versions
 
--- | Mean value of a list.
-meanValue :: [Double] -> Double
-meanValue lst = sum lst / fromIntegral (length lst)
-
--- | Standard deviation.
-stdDev :: [Double] -> Double
-stdDev lst =
-    let mv = meanValue lst
-        av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst
-        bv = sqrt (av / fromIntegral (length lst))
-    in bv
-
--- | Coefficient of variation.
+-- | The covariance of the list
 varianceCoeff :: [Double] -> Double
-varianceCoeff lst = stdDev lst / fromIntegral (length lst)
+varianceCoeff lst =
+    let ll = fromIntegral (length lst)::Double -- length of list
+        mv = sum lst / ll   -- mean value
+        av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst
+        bv = sqrt (av / ll) -- stddev
+        cv = bv / ll        -- covariance
+    in cv
 
 -- * JSON-related functions
 
-- 
GitLab