Two coins in Fun
(See file Samples\Fun\Coins\Coins.fs.)
Suppose you toss two coins, observe that not both of them are
heads, and would like to infer the posterior probability of each of
them being heads. To do so using Infer.NET Fun you start by writing
a model as follows:
open MicrosoftResearch.Infer.Fun.FSharp.Syntax
[<ReflectedDefinition>]
let coins () =
let c1 = random (Bernoulli(0.5))
let c2 = random (Bernoulli(0.5))
let bothHeads = c1 && c2
observe (bothHeads = false)
c1, c2, bothHeads |
The model uses two special functions defined in
MicrosoftResearch.Infer.Fun.FSharp.Syntax: the function
random returns a random sample from a given distribution and
the function observe marks the execution as failed if
the condition is not satisfied. The goal of the inference is to
produce the distribution of the output variables of the model across
all executions in which no observation fails. Sampling from a model
is as simple as calling the function:
printf "Sample: %O\n" (coins ()) |
To perform inference the quoted version of the function is given to
the function inferFun3 which returns a triple of
distributions, one for each return value of
open
MicrosoftResearch.Infer.Fun.FSharp.Inference
open
MicrosoftResearch.Infer.Distributions
let (c1D,
c2D, bothD)
: IDistribution<bool>
* IDistribution<bool>
* IDistribution<bool>
= infer <@ coins
@> ()
printf
"coins
distribution \n%O\n%O\n%O\n"
c1D c2D bothD |