package prbnmcn-dagger
Install
Dune Dependency
Authors
Maintainers
Sources
md5=3fb82cc81ef81557f3325ef2c8611e35
sha512=41498634a1872cfd98afea4e35d8ffff08e444cd1a8dffd5d2a8aea57dc7578129c4832929fb6bf03c0f2edbfe1f85949533cf2e574273449ff526af1975aa28
doc/index.html
Dagger
dagger is a embedded domain-specific language for probabilistic programming in OCaml. Models are written in monadic style. Sampling from the posterior is performed using one of the following algorithms:
- Single-site Metropolis-Hastings, a.k.a. Lightweight Metropolis-Hastings
- Incremental single-site MH, similar to that implemented in Hakaru10
- Sequential Monte Carlo with programmable resampling
A basic example
Let us consider the classical "sprinkler" example (drawn from Functional Programming for Modular Bayesian Inference).
Our lawn can become wet either because it rained or because the sprinkler is activated. We construct a simple model that encodes our belief that the lawn becomes wet knowing that it rained or not and the sprinkler state.
open Lmh_inference
open Infix
let model : bool t =
let open Infix in
let* rain = sample (Stats_dist.bernoulli ~bias:0.2) in
let* sprinkler = sample (Stats_dist.bernoulli ~bias:0.1) in
let prob_lawn_wet =
match (rain, sprinkler) with
| (true, true) -> 0.99
| (true, false) -> 0.7
| (false, true) -> 0.9
| (false, false) -> 0.01
in
let+ () = score prob_lawn_wet in
rain
Each execution trace is weighted by the density of the samples taken in that trace and by calls to the score
construct. The inference algorithm samples execution traces proportionally to their total weight. Scoring therefore amounts to a form of soft conditioning (and assigning a score of 0 to a trace amounts to rejecting that trace). Let us sample from the posterior implicitly defined by our model
.
let rng_state = RNG.make [| 0x1337; 0x533D |]
let nsamples = 100_000
let samples =
Lmh_inference.stream_samples model rng_state
|> Seq.take nsamples
let freq =
(samples
|> Seq.map (fun x -> if x then 1 else 0)
|> List.of_seq |> List.fold_left ( + ) 0 |> float_of_int)
/. float_of_int nsamples
The result is that the probability that it rains knowing that the lawn is wet is approximately equal to 0.64.
More examples
- Polynomial regression using SMC
- More advanced examples can be found in the
examples
subdirectory of the source distribution.
Companion libraries
In the interest of modularity, sampleable distributions (such as bernoulli
) are defined in auxilliary libraries. Currently, we have:
prbnmcn-dagger-gsl
, which relies on the OCaml bindings to the GNUgsl
library. Note thatprbnmcn-dagger-gsl
is licensed under the GNU GPL.prbnmcn-dagger-stats
, which relies onprbnmcn-stats
(MIT license).
API
Dagger.Intf
Module type definitionsDagger.Lmh_inference
Lightweight Metropolis-HastingsDagger.Lmh_incremental_inference
Incremental lightweight Metropolis-HastingsDagger.Smc_inference
Sequential Monte CarloDagger.Dist
DistributionsDagger.Resampling
Resampling functions