Commit 394a5cb9 authored by Klaus Aehlig's avatar Klaus Aehlig
Browse files

Add data type for abstract statistics



Our cluster score is a weighted sum of certain sums and
standard deviations of node characteristics. When placing
a single instance, the cluster score of a big number of
quite similar clusters are computed: that of the original
cluster with one instance added, for each possible placement.
To speed up these computations, add an abstract type of
statistics that allows a potentially more efficient update
of a single value.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarPetr Pudlak <pudlak@google.com>
parent 2b296ad4
......@@ -26,6 +26,11 @@ module Ganeti.Utils
, debugFn
, debugXy
, sepSplit
, Statistics
, getSumStatistics
, getStdDevStatistics
, getStatisticValue
, updateStatistics
, stdDev
, if'
, select
......@@ -144,6 +149,34 @@ stdDev lst =
av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst
in sqrt (av / ll) -- stddev
-- | Abstract type of statistical accumulations. They behave as if the given
-- statistics were computed on the list of values, but they allow a potentially
-- more efficient update of a given value.
-- For the time being, we only update the sum efficiently whereas the standard
-- deviation is recomputed on demand.
data Statistics = SumStatistics Double | StdDevStatistics [Double] deriving Show
-- | Get a statistics that sums up the values.
getSumStatistics :: [Double] -> Statistics
getSumStatistics = SumStatistics . sum
-- | Get a statistics for the standard deviation.
getStdDevStatistics :: [Double] -> Statistics
getStdDevStatistics = StdDevStatistics
-- | Obtain the value of a statistics.
getStatisticValue :: Statistics -> Double
getStatisticValue (SumStatistics s) = s
getStatisticValue (StdDevStatistics xs) = stdDev xs
-- | In a given statistics replace on value by another. This
-- will only give meaningful results, if the original value
-- was actually part of the statistics.
updateStatistics :: Statistics -> (Double, Double) -> Statistics
updateStatistics (SumStatistics s) (x, y) = SumStatistics $ s + (y - x)
updateStatistics (StdDevStatistics xs) (x, y) =
StdDevStatistics $ (xs \\ [x]) ++ [y]
-- * Logical functions
-- Avoid syntactic sugar and enhance readability. These functions are proposed
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment