Revision 394a5cb9
b/src/Ganeti/Utils.hs | ||
---|---|---|
26 | 26 |
, debugFn |
27 | 27 |
, debugXy |
28 | 28 |
, sepSplit |
29 |
, Statistics |
|
30 |
, getSumStatistics |
|
31 |
, getStdDevStatistics |
|
32 |
, getStatisticValue |
|
33 |
, updateStatistics |
|
29 | 34 |
, stdDev |
30 | 35 |
, if' |
31 | 36 |
, select |
... | ... | |
144 | 149 |
av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst |
145 | 150 |
in sqrt (av / ll) -- stddev |
146 | 151 |
|
152 |
-- | Abstract type of statistical accumulations. They behave as if the given |
|
153 |
-- statistics were computed on the list of values, but they allow a potentially |
|
154 |
-- more efficient update of a given value. |
|
155 |
-- For the time being, we only update the sum efficiently whereas the standard |
|
156 |
-- deviation is recomputed on demand. |
|
157 |
data Statistics = SumStatistics Double | StdDevStatistics [Double] deriving Show |
|
158 |
|
|
159 |
-- | Get a statistics that sums up the values. |
|
160 |
getSumStatistics :: [Double] -> Statistics |
|
161 |
getSumStatistics = SumStatistics . sum |
|
162 |
|
|
163 |
-- | Get a statistics for the standard deviation. |
|
164 |
getStdDevStatistics :: [Double] -> Statistics |
|
165 |
getStdDevStatistics = StdDevStatistics |
|
166 |
|
|
167 |
-- | Obtain the value of a statistics. |
|
168 |
getStatisticValue :: Statistics -> Double |
|
169 |
getStatisticValue (SumStatistics s) = s |
|
170 |
getStatisticValue (StdDevStatistics xs) = stdDev xs |
|
171 |
|
|
172 |
-- | In a given statistics replace on value by another. This |
|
173 |
-- will only give meaningful results, if the original value |
|
174 |
-- was actually part of the statistics. |
|
175 |
updateStatistics :: Statistics -> (Double, Double) -> Statistics |
|
176 |
updateStatistics (SumStatistics s) (x, y) = SumStatistics $ s + (y - x) |
|
177 |
updateStatistics (StdDevStatistics xs) (x, y) = |
|
178 |
StdDevStatistics $ (xs \\ [x]) ++ [y] |
|
179 |
|
|
147 | 180 |
-- * Logical functions |
148 | 181 |
|
149 | 182 |
-- Avoid syntactic sugar and enhance readability. These functions are proposed |
Also available in: Unified diff