Skip to content

Commit

Permalink
Rename Count, it's not really a count
Browse files Browse the repository at this point in the history
  • Loading branch information
olorin committed Mar 31, 2016
1 parent eb98764 commit 947f13b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
20 changes: 12 additions & 8 deletions src/Warden/Data/Numeric.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{-# LANGUAGE DeriveGeneric #-}

module Warden.Data.Numeric (
Count(..)
KAcc(..)
, Maximum(..)
, Mean(..)
, MeanAcc(..)
Expand Down Expand Up @@ -71,12 +71,14 @@ instance Monoid Maximum where
else Maximum prev
{-# INLINE mappend #-}

newtype Count =
Count {
getCount :: Int
-- | Counter param for mean/stddev calculation. Equal to one plus the number
-- of records seen.
newtype KAcc =
KAcc {
getKAcc :: Int
} deriving (Eq, Show, ToJSON, FromJSON, Generic, Num)

instance NFData Count
instance NFData KAcc

-- | Preliminary mean, still accumulating.
newtype MeanAcc =
Expand All @@ -101,8 +103,10 @@ data Median =

instance NFData Median

newtype StdDevAcc = StdDevAcc { unStdDevAcc :: Double }
deriving (Eq, Show, ToJSON, FromJSON, Generic)
newtype StdDevAcc =
StdDevAcc {
unStdDevAcc :: Double
} deriving (Eq, Show, ToJSON, FromJSON, Generic)

instance NFData StdDevAcc

Expand Down Expand Up @@ -138,7 +142,7 @@ instance NFData NumericSummary

data MeanDevAcc =
MeanDevInitial
| MeanDevAcc {-# UNPACK #-} !MeanAcc !(Maybe StdDevAcc) {-# UNPACK #-} !Count
| MeanDevAcc {-# UNPACK #-} !MeanAcc !(Maybe StdDevAcc) {-# UNPACK #-} !KAcc
deriving (Eq, Show, Generic)

instance NFData MeanDevAcc
Expand Down
14 changes: 7 additions & 7 deletions src/Warden/Numeric.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE BangPatterns #-}

module Warden.Numeric (
Expand Down Expand Up @@ -46,17 +46,17 @@ updateMeanDev :: Real a
updateMeanDev !macc x =
let x' = (fromRational . toRational) x in case macc of
MeanDevInitial ->
let i = Count 1
let i = KAcc 1
m = MeanAcc 0
s = Nothing
in update' m s i x'
(MeanDevAcc m s i) ->
update' m s i x'
where
update' (MeanAcc m) s (Count i) v =
update' (MeanAcc m) s (KAcc i) v =
let delta = v - m
m' = MeanAcc $ m + delta / (fromIntegral i)
i' = Count $ i + 1
i' = KAcc $ i + 1
s' = case s of
Nothing ->
Just $ StdDevAcc 0
Expand Down Expand Up @@ -100,7 +100,7 @@ combineMeanDevAcc (MeanDevAcc mu1 s1 c1) (MeanDevAcc mu2 s2 c2) =
sda' = combineStdDevAcc mu' (mu1, s1, c1) (mu2, s2, c2)
-- KAccs are off-by-one from the actual number of values seen, so
-- subtract one from the sum to prevent it becoming off-by-two.
c' = c1 + c2 - (Count 1) in
c' = c1 + c2 - (KAcc 1) in
MeanDevAcc mu' sda' c'
{-# INLINE combineMeanDevAcc #-}

Expand Down Expand Up @@ -144,8 +144,8 @@ combineVariance (MeanAcc muHat) (MeanAcc mu1, Variance var1, KAcc c1) (MeanAcc m
{-# INLINE combineVariance #-}

-- | Combine mean of two subsets, given subset means and size.
combineMeanAcc :: (MeanAcc, Count) -> (MeanAcc, Count) -> MeanAcc
combineMeanAcc (MeanAcc mu1, Count c1) (MeanAcc mu2, Count c2) =
combineMeanAcc :: (MeanAcc, KAcc) -> (MeanAcc, KAcc) -> MeanAcc
combineMeanAcc (MeanAcc mu1, KAcc c1) (MeanAcc mu2, KAcc c2) =
let c1' = fromIntegral c1
c2' = fromIntegral c2 in
MeanAcc $ ((mu1 * c1') + (mu2 * c2')) / (c1' + c2')
Expand Down
6 changes: 3 additions & 3 deletions test/Test/Warden/Numeric.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ prop_updateMeanDev (NPlus n) = forAll (vectorOf n (arbitrary :: Gen Double)) $ \
var = textbookVariance mu xs
sd = StdDev $ sqrt var
uMeanDev = (Mean mu, sd) in
(nsMeanDev, Just (n+1)) ~~~ (uMeanDev, meanDevCount mda)
(nsMeanDev, Just (n+1)) ~~~ (uMeanDev, meanDevKAcc mda)
where
meanDevCount MeanDevInitial = Nothing
meanDevCount (MeanDevAcc _ _ (Count c)) = Just c
meanDevKAcc MeanDevInitial = Nothing
meanDevKAcc (MeanDevAcc _ _ (KAcc c)) = Just c

prop_updateMeanDev_associative :: Int -> Property
prop_updateMeanDev_associative n = forAll (vectorOf n (arbitrary :: Gen Double)) $ \xs ->
Expand Down

0 comments on commit 947f13b

Please sign in to comment.