| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Bayes.Examples.Sampling
Contents
Description
Example of sampling
Two samplers are availables : the discreteAncestralSampler and the gibbsSampler.
Only the gibbsSampler can be used with evidence.
In this example, we have a very simple network.
simple :: ([TDVBool],SBNCPT) simple =runBN$ do a <-variable"a" (t:: Bool) b <-variable"b" (t:: Bool) --probaa~~[0.4,0.6]cptb [a]~~[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 gibbsSampler is
generate a stream of samples. From this stream, we need to compute a probability distribution. For this, we use
the samplingHistograms histogram function which is generating a list : the probability values of each vertex.
let (vars@[a,b],exampleG) = simple
n <- runSampling 5000 200 (gibbsSampler exampleG [b =: True])
let h = samplingHistograms n
print $ h
Then, we compare this result with the exact one we get with a junction tree.
let jt =createJunctionTreenodeComparisonForTriangulationexampleG jt' =changeEvidence[b=: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.
n <-runSampling500 (discreteAncestralSamplerexampleG)
- testSampling :: IO ()
Test function
testSampling :: IO () Source