Example of sampling
In this example, we have a very simple network.
simple :: ([
CPT) simple =
runBN$ do a <-
t:: Bool) b <-
t:: Bool) --
~~[0.8,0.2,0.2,0.8] -- return [a,b]
This network is representing a sensor b. We observe the value of b and we want to infer the value of a.
We use the
gibbsSampler for this with an initial period of 200 samples which are dropped. The
generate a stream of samples. From this stream, we need to compute a probability distribution. For this, we use
samplingHistograms histogram function which is generating a list : the probability values of each vertex.
let (vars@[a,b],exampleG) = simple n <-
runSampling5000 200 (
=:True]) let h =
samplingHistogramsn print $ h
Then, we compare this result with the exact one we get with a junction tree.
let jt =
nodeComparisonForTriangulationexampleG jt' =
=:True] jt mapM_ (x -> print .
posteriorjt' $ [x]) vars
We can also use the
discreteAncestralSampler to compute the posterior but it is not supporting the use of evidence in this
version. The syntax is similar.