module Simulation.Aivika.Statistics
(
SamplingStats(..),
SamplingData(..),
combineSamplingStatsEither,
samplingStatsVariance,
samplingStatsDeviation,
samplingStatsSummary,
returnSamplingStats,
listSamplingStats,
fromIntSamplingStats,
TimingStats(..),
TimingData(..),
timingStatsDeviation,
timingStatsSummary,
returnTimingStats,
fromIntTimingStats,
normTimingStats,
SamplingCounter(..),
emptySamplingCounter,
incSamplingCounter,
decSamplingCounter,
setSamplingCounter,
returnSamplingCounter,
TimingCounter(..),
emptyTimingCounter,
incTimingCounter,
decTimingCounter,
setTimingCounter,
returnTimingCounter) where
import GHC.Generics (Generic)
import Control.DeepSeq
import Data.Monoid
import Data.Typeable
import Data.Binary
class Ord a => ConvertableToDouble a where
convertToDouble :: a -> Double
instance ConvertableToDouble Double where
convertToDouble = id
instance ConvertableToDouble Int where
convertToDouble = fromIntegral
data SamplingStats a =
SamplingStats { samplingStatsCount :: !Int,
samplingStatsMin :: !a,
samplingStatsMax :: !a,
samplingStatsMean :: !Double,
samplingStatsMean2 :: !Double
}
deriving (Eq, Ord, Typeable, Generic)
instance NFData a => NFData (SamplingStats a)
instance Binary a => Binary (SamplingStats a)
class Num a => SamplingData a where
emptySamplingStats :: SamplingStats a
addSamplingStats :: a -> SamplingStats a -> SamplingStats a
combineSamplingStats :: SamplingStats a -> SamplingStats a -> SamplingStats a
instance SamplingData a => Monoid (SamplingStats a) where
mempty = emptySamplingStats
mappend = combineSamplingStats
instance SamplingData Double where
emptySamplingStats =
SamplingStats { samplingStatsCount = 0,
samplingStatsMin = 1 / 0,
samplingStatsMax = (1) / 0,
samplingStatsMean = 0 / 0,
samplingStatsMean2 = 0 / 0 }
addSamplingStats = addSamplingStatsGeneric
combineSamplingStats = combineSamplingStatsGeneric
instance SamplingData Int where
emptySamplingStats =
SamplingStats { samplingStatsCount = 0,
samplingStatsMin = maxBound,
samplingStatsMax = minBound,
samplingStatsMean = 0 / 0,
samplingStatsMean2 = 0 / 0 }
addSamplingStats = addSamplingStatsGeneric
combineSamplingStats = combineSamplingStatsGeneric
addSamplingStatsGeneric :: ConvertableToDouble a => a -> SamplingStats a -> SamplingStats a
addSamplingStatsGeneric a stats
| isNaN x = stats
| count == 1 = SamplingStats { samplingStatsCount = 1,
samplingStatsMin = a,
samplingStatsMax = a,
samplingStatsMean = x,
samplingStatsMean2 = x * x }
| otherwise = SamplingStats { samplingStatsCount = count,
samplingStatsMin = minX,
samplingStatsMax = maxX,
samplingStatsMean = meanX,
samplingStatsMean2 = meanX2 }
where count = 1 + samplingStatsCount stats
minX = a `seq` min a (samplingStatsMin stats)
maxX = a `seq` max a (samplingStatsMax stats)
meanX = k1 * x + k2 * samplingStatsMean stats
meanX2 = k1 * x * x + k2 * samplingStatsMean2 stats
n = fromIntegral count
x = convertToDouble a
k1 = 1.0 / n
k2 = (n 1.0) / n
combineSamplingStatsGeneric :: ConvertableToDouble a =>
SamplingStats a -> SamplingStats a -> SamplingStats a
combineSamplingStatsGeneric stats1 stats2
| c1 == 0 = stats2
| c2 == 0 = stats1
| otherwise = SamplingStats { samplingStatsCount = c,
samplingStatsMin = minZ,
samplingStatsMax = maxZ,
samplingStatsMean = meanZ,
samplingStatsMean2 = meanZ2 }
where c1 = samplingStatsCount stats1
c2 = samplingStatsCount stats2
c = c1 + c2
n1 = fromIntegral c1
n2 = fromIntegral c2
n = n1 + n2
minX = samplingStatsMin stats1
minY = samplingStatsMin stats2
minZ = min minX minY
maxX = samplingStatsMax stats1
maxY = samplingStatsMax stats2
maxZ = max maxX maxY
meanX = samplingStatsMean stats1
meanY = samplingStatsMean stats2
meanZ = k1 * meanX + k2 * meanY
meanX2 = samplingStatsMean2 stats1
meanY2 = samplingStatsMean2 stats2
meanZ2 = k1 * meanX2 + k2 * meanY2
k1 = n1 / n
k2 = n2 / n
combineSamplingStatsEither :: SamplingData a => Either a (SamplingStats a) -> SamplingStats a -> SamplingStats a
combineSamplingStatsEither (Left a) stats2 = addSamplingStats a stats2
combineSamplingStatsEither (Right stats1) stats2 = combineSamplingStats stats1 stats2
samplingStatsVariance :: SamplingStats a -> Double
samplingStatsVariance stats
| count == 1 = 0
| otherwise = (meanX2 meanX * meanX) * (n / (n 1))
where count = samplingStatsCount stats
meanX = samplingStatsMean stats
meanX2 = samplingStatsMean2 stats
n = fromIntegral count
samplingStatsDeviation :: SamplingStats a -> Double
samplingStatsDeviation = sqrt . samplingStatsVariance
returnSamplingStats :: SamplingData a => a -> SamplingStats a
returnSamplingStats x = addSamplingStats x emptySamplingStats
listSamplingStats :: SamplingData a => [a] -> SamplingStats a
listSamplingStats = foldr addSamplingStats emptySamplingStats
fromIntSamplingStats :: SamplingStats Int -> SamplingStats Double
fromIntSamplingStats stats =
stats { samplingStatsMin = fromIntegral $ samplingStatsMin stats,
samplingStatsMax = fromIntegral $ samplingStatsMax stats }
showSamplingStats :: (Show a) => SamplingStats a -> ShowS
showSamplingStats stats =
showString "{ count = " . shows (samplingStatsCount stats) .
showString ", mean = " . shows (samplingStatsMean stats) .
showString ", std = " . shows (samplingStatsDeviation stats) .
showString ", min = " . shows (samplingStatsMin stats) .
showString ", max = " . shows (samplingStatsMax stats) .
showString " }"
instance Show a => Show (SamplingStats a) where
showsPrec prec = showSamplingStats
samplingStatsSummary :: (Show a) => SamplingStats a -> Int -> ShowS
samplingStatsSummary stats indent =
let tab = replicate indent ' '
in showString tab .
showString "count = " . shows (samplingStatsCount stats) .
showString "\n" .
showString tab .
showString "mean = " . shows (samplingStatsMean stats) .
showString "\n" .
showString tab .
showString "std = " . shows (samplingStatsDeviation stats) .
showString "\n" .
showString tab .
showString "min = " . shows (samplingStatsMin stats) .
showString "\n" .
showString tab .
showString "max = " . shows (samplingStatsMax stats)
data TimingStats a =
TimingStats { timingStatsCount :: !Int,
timingStatsMin :: !a,
timingStatsMax :: !a,
timingStatsLast :: !a,
timingStatsMinTime :: !Double,
timingStatsMaxTime :: !Double,
timingStatsStartTime :: !Double,
timingStatsLastTime :: !Double,
timingStatsSum :: !Double,
timingStatsSum2 :: !Double
} deriving (Eq, Ord, Typeable, Generic)
instance NFData a => NFData (TimingStats a)
instance Binary a => Binary (TimingStats a)
class Num a => TimingData a where
emptyTimingStats :: TimingStats a
addTimingStats :: Double -> a -> TimingStats a -> TimingStats a
timingStatsMean :: TimingStats a -> Double
timingStatsMean2 :: TimingStats a -> Double
timingStatsVariance :: TimingStats a -> Double
instance TimingData Double where
emptyTimingStats =
TimingStats { timingStatsCount = 0,
timingStatsMin = 1 / 0,
timingStatsMax = (1) / 0,
timingStatsLast = 0 / 0,
timingStatsMinTime = 1 / 0,
timingStatsMaxTime = (1) / 0,
timingStatsStartTime = 1 / 0,
timingStatsLastTime = (1) / 0,
timingStatsSum = 0 / 0,
timingStatsSum2 = 0 / 0 }
addTimingStats = addTimingStatsGeneric
timingStatsMean = timingStatsMeanGeneric
timingStatsMean2 = timingStatsMean2Generic
timingStatsVariance = timingStatsVarianceGeneric
instance TimingData Int where
emptyTimingStats =
TimingStats { timingStatsCount = 0,
timingStatsMin = maxBound,
timingStatsMax = minBound,
timingStatsLast = 0,
timingStatsMinTime = 1 / 0,
timingStatsMaxTime = (1) / 0,
timingStatsStartTime = 1 / 0,
timingStatsLastTime = (1) / 0,
timingStatsSum = 0 / 0,
timingStatsSum2 = 0 / 0 }
addTimingStats = addTimingStatsGeneric
timingStatsMean = timingStatsMeanGeneric
timingStatsMean2 = timingStatsMean2Generic
timingStatsVariance = timingStatsVarianceGeneric
addTimingStatsGeneric :: ConvertableToDouble a => Double -> a -> TimingStats a -> TimingStats a
addTimingStatsGeneric t a stats
| t < t' = error "The current time cannot be less than the previous one: addTimingStats"
| isNaN x = stats
| count == 1 = TimingStats { timingStatsCount = 1,
timingStatsMin = a,
timingStatsMax = a,
timingStatsLast = a,
timingStatsMinTime = t,
timingStatsMaxTime = t,
timingStatsStartTime = t,
timingStatsLastTime = t,
timingStatsSum = 0,
timingStatsSum2 = 0 }
| otherwise = TimingStats { timingStatsCount = count,
timingStatsMin = minX,
timingStatsMax = maxX,
timingStatsLast = a,
timingStatsMinTime = minT,
timingStatsMaxTime = maxT,
timingStatsStartTime = t0,
timingStatsLastTime = t,
timingStatsSum = sumX,
timingStatsSum2 = sumX2 }
where count = 1 + timingStatsCount stats
minX' = timingStatsMin stats
maxX' = timingStatsMax stats
minX = a `seq` min a minX'
maxX = a `seq` max a maxX'
minT | a < minX' = t
| otherwise = timingStatsMinTime stats
maxT | a > maxX' = t
| otherwise = timingStatsMaxTime stats
t0 = timingStatsStartTime stats
t' = timingStatsLastTime stats
a' = timingStatsLast stats
x = convertToDouble a
x' = convertToDouble a'
sumX' = timingStatsSum stats
sumX = sumX' + (t t') * x'
sumX2' = timingStatsSum2 stats
sumX2 = sumX2' + (t t') * x' * x'
timingStatsMeanGeneric :: ConvertableToDouble a => TimingStats a -> Double
timingStatsMeanGeneric stats
| count == 0 = 0 / 0
| t1 > t0 = sumX / (t1 t0)
| otherwise = minX
where t0 = timingStatsStartTime stats
t1 = timingStatsLastTime stats
sumX = timingStatsSum stats
minX = convertToDouble $ timingStatsMin stats
count = timingStatsCount stats
timingStatsMean2Generic :: ConvertableToDouble a => TimingStats a -> Double
timingStatsMean2Generic stats
| count == 0 = 0 / 0
| t1 > t0 = sumX2 / (t1 t0)
| otherwise = minX * minX
where t0 = timingStatsStartTime stats
t1 = timingStatsLastTime stats
sumX2 = timingStatsSum2 stats
minX = convertToDouble $ timingStatsMin stats
count = timingStatsCount stats
timingStatsVarianceGeneric :: ConvertableToDouble a => TimingStats a -> Double
timingStatsVarianceGeneric stats = ex2 ex * ex
where ex = timingStatsMeanGeneric stats
ex2 = timingStatsMean2Generic stats
timingStatsDeviation :: TimingData a => TimingStats a -> Double
timingStatsDeviation = sqrt . timingStatsVariance
returnTimingStats :: TimingData a => Double -> a -> TimingStats a
returnTimingStats t a = addTimingStats t a emptyTimingStats
fromIntTimingStats :: TimingStats Int -> TimingStats Double
fromIntTimingStats stats =
stats { timingStatsMin = fromIntegral $ timingStatsMin stats,
timingStatsMax = fromIntegral $ timingStatsMax stats,
timingStatsLast = fromIntegral $ timingStatsLast stats }
normTimingStats :: TimingData a => Int -> TimingStats a -> SamplingStats a
normTimingStats n stats =
SamplingStats { samplingStatsCount = n,
samplingStatsMin = timingStatsMin stats,
samplingStatsMax = timingStatsMax stats,
samplingStatsMean = timingStatsMean stats,
samplingStatsMean2 = timingStatsMean2 stats }
showTimingStats :: (Show a, TimingData a) => TimingStats a -> ShowS
showTimingStats stats =
showString "{ count = " . shows (timingStatsCount stats) .
showString ", mean = " . shows (timingStatsMean stats) .
showString ", std = " . shows (timingStatsDeviation stats) .
showString ", min = " . shows (timingStatsMin stats) .
showString " (t = " . shows (timingStatsMinTime stats) .
showString "), max = " . shows (timingStatsMax stats) .
showString " (t = " . shows (timingStatsMaxTime stats) .
showString "), t in [" . shows (timingStatsStartTime stats) .
showString ", " . shows (timingStatsLastTime stats) .
showString "] }"
instance (Show a, TimingData a) => Show (TimingStats a) where
showsPrec prec = showTimingStats
timingStatsSummary :: (Show a, TimingData a) => TimingStats a -> Int -> ShowS
timingStatsSummary stats indent =
let tab = replicate indent ' '
in showString tab .
showString "count = " . shows (timingStatsCount stats) .
showString "\n" .
showString tab .
showString "mean = " . shows (timingStatsMean stats) .
showString "\n" .
showString tab .
showString "std = " . shows (timingStatsDeviation stats) .
showString "\n" .
showString tab .
showString "min = " . shows (timingStatsMin stats) .
showString " (t = " . shows (timingStatsMinTime stats) .
showString ")\n" .
showString tab .
showString "max = " . shows (timingStatsMax stats) .
showString " (t = " . shows (timingStatsMaxTime stats) .
showString ")\n" .
showString tab .
showString "t in [" . shows (timingStatsStartTime stats) .
showString ", " . shows (timingStatsLastTime stats) .
showString "]"
data SamplingCounter a =
SamplingCounter { samplingCounterValue :: a,
samplingCounterStats :: SamplingStats a
} deriving (Eq, Ord, Show, Typeable, Generic)
instance NFData a => NFData (SamplingCounter a)
instance Binary a => Binary (SamplingCounter a)
emptySamplingCounter :: SamplingData a => SamplingCounter a
emptySamplingCounter =
SamplingCounter { samplingCounterValue = 0,
samplingCounterStats = emptySamplingStats }
incSamplingCounter :: SamplingData a => a -> SamplingCounter a -> SamplingCounter a
incSamplingCounter a counter =
SamplingCounter { samplingCounterValue = a',
samplingCounterStats = addSamplingStats a' (samplingCounterStats counter) }
where a' = samplingCounterValue counter + a
decSamplingCounter :: SamplingData a => a -> SamplingCounter a -> SamplingCounter a
decSamplingCounter a counter =
SamplingCounter { samplingCounterValue = a',
samplingCounterStats = addSamplingStats a' (samplingCounterStats counter) }
where a' = samplingCounterValue counter a
setSamplingCounter :: SamplingData a => a -> SamplingCounter a -> SamplingCounter a
setSamplingCounter a counter =
SamplingCounter { samplingCounterValue = a,
samplingCounterStats = addSamplingStats a (samplingCounterStats counter) }
returnSamplingCounter :: SamplingData a => a -> SamplingCounter a
returnSamplingCounter a =
SamplingCounter { samplingCounterValue = a,
samplingCounterStats = returnSamplingStats a }
data TimingCounter a =
TimingCounter { timingCounterValue :: a,
timingCounterStats :: TimingStats a
} deriving (Eq, Ord, Show, Typeable, Generic)
instance NFData a => NFData (TimingCounter a)
instance Binary a => Binary (TimingCounter a)
emptyTimingCounter :: TimingData a => TimingCounter a
emptyTimingCounter =
TimingCounter { timingCounterValue = 0,
timingCounterStats = emptyTimingStats }
incTimingCounter :: TimingData a => Double -> a -> TimingCounter a -> TimingCounter a
incTimingCounter t a counter =
TimingCounter { timingCounterValue = a',
timingCounterStats = addTimingStats t a' (timingCounterStats counter) }
where a' = timingCounterValue counter + a
decTimingCounter :: TimingData a => Double -> a -> TimingCounter a -> TimingCounter a
decTimingCounter t a counter =
TimingCounter { timingCounterValue = a',
timingCounterStats = addTimingStats t a' (timingCounterStats counter) }
where a' = timingCounterValue counter a
setTimingCounter :: TimingData a => Double -> a -> TimingCounter a -> TimingCounter a
setTimingCounter t a counter =
TimingCounter { timingCounterValue = a,
timingCounterStats = addTimingStats t a (timingCounterStats counter) }
returnTimingCounter :: TimingData a => Double -> a -> TimingCounter a
returnTimingCounter t a =
TimingCounter { timingCounterValue = a,
timingCounterStats = returnTimingStats t a }