distribution 1.1.0.0 → 1.1.1.0
raw patch · 2 files changed
+35/−1 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.Distribution.Core: observing :: (a -> Distribution Bool) -> Distribution a -> Distribution a
Files
- Data/Distribution/Core.hs +34/−0
- distribution.cabal +1/−1
Data/Distribution/Core.hs view
@@ -35,6 +35,7 @@ -- ** Transformation , select , assuming+ , observing -- ** Combination , combineWith -- ** Sequences@@ -271,6 +272,39 @@ adjust x = x * (1 / total) total = sum $ Map.elems filtered ++-- | Returns a new distribution using the Bayesian update rule.+--+-- Using this example:+-- https://en.wikipedia.org/wiki/Bayesian_inference#Probability_of_a_hypothesis+--+-- > data CookieBowl = Bowl1 | Bowl2 deriving (Eq,Ord)+-- > data CookieType = Plain | ChocolateChip deriving (Eq,Ord)+-- >+-- > assumption :: Distribution CookieBowl+-- > assumption = uniform [Bowl1,Bowl2]+-- >+-- > update :: Cookie -> Distribution CookieBowl -> Distribution CookieBowl+-- > update c = observing f where+-- > f b = case b of+-- > -- Bowl #1 contains 10 chocolate chip cookies and 30 plain cookies+-- > Bowl1 -> fromList [(c == ChocolateChip,10),(c == Plain,30)]+-- > -- Bowl #2 contains 20 of each flavour of cookie+-- > Bowl2 -> fromList [(c == ChocolateChip,20),(c == Plain,20)]+--+-- The "update" function in this example can be used to update the probability+-- distribution of which bowl you have based on observing a random cookie inside+-- the bowl.+observing :: (a -> Distribution Bool) -> Distribution a -> Distribution a+observing f (Distribution xs) = Distribution $ fmap adjust filtered+ where+ filtered = Map.filter (/= 0) $ Map.mapWithKey tweak xs+ tweak x p = let+ Distribution px = f x+ pt = fromMaybe 0 $ Map.lookup True px+ in pt * p+ adjust x = x * (1 / total)+ total = sum $ Map.elems filtered -- Combination
distribution.cabal view
@@ -8,7 +8,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 1.1.0.0+version: 1.1.1.0 -- A short (one-line) description of the package. synopsis: Finite discrete probability distributions.