Hakaru10 is designed to address two main challenges of probabilistic programming: performance and correctness. It implements the incremental Metropolis-Hastings method, avoiding all redundant computations. In the presence of conditional branches, efficiently maintaining dependencies and correctly computing the acceptance ratio are non-trivial problems, solved in Hakaru10. The implementation is unique in being explicitly designed to satisfy the common equational laws of probabilistic programs. Hakaru10 is typed; specifically, its type system statically prevents meaningless conditioning, enforcing that the values to condition upon must indeed come from outside the model.
Syntax.hs [12K]
Syntax sugar: making the library more convenient to use
Distribution.hs [14K]
Primitive distributions
Util.hs [2K]
Utilities: computing histograms, momenta, KL divergence, etc.
Memory.hs [4K]
First-class storage
grass = do rain <- dist bern 0.3 sprinkler <- dist bern 0.5 -- dist (nor 0.9 0.8 0.1) rain sprinkler grass_is_wet <- dist (True `condition` nor 0.9 0.8 0.1) rain sprinkler return rain -- noisy-or function nor :: Double -> Double -> Double -> Bool -> Bool -> Distribution Bool nor strengthX strengthY noise x y = bern $ 1 - nnot (1-strengthX) x * nnot (1-strengthY) y * (1-noise) -- noisy not function nnot :: Num a => a -> Bool -> a nnot p True = p nnot p False = 1
The model is an ordinary Haskell function grass
whose inferred type
is Model Bool
. The function nor
is a custom parameterized distribution,
the noisy-or function. Sampling (20000 times) from the model --
evaluating mcmC 20000 grass
-- and counting the number of True
gives the posterior estimate of rain
having observed that the
grass is wet. See the introduction part of the paper for more
explanation of the example.
Bench.hs [2K]
The benchmark code
``An urn contains an unknown number of balls--say, a number chosen from a Poisson or a uniform distributions. Balls are equally likely to be blue or green. We draw some balls from the urn, observing the color of each and replacing it. We cannot tell two identically colored balls apart; furthermore, observed colors are wrong with probability 0.2. How many balls are in the urn? Was the same ball drawn twice?''
ColoredBalls.hs [7K]
The implementation of the model and sample inferences: e.g., of the posterior distribution of the number of balls in the urn given that we drew ten balls and all appeared blue. Our results seem in excellent agreement with those by BLOG.
oleg-at-okmij.org
Your comments, problem reports, questions are very welcome!
Converted from HSXML by HSXML->HTML