Select Evidence Variables
Cloudy
Sprinkler
Rainy
Wet Grass
K value for Gibbs Sampling

Original Probability Tables

P(C)

+c 0.5
-c 0.5

P(S | C)

+c +s 0.1
-s 0.9
-c +s 0.5
-s 0.5

P(R | C)

+c +r 0.8
-r 0.2
-c +r 0.2
-r 0.8

P(W | S,R)

+s +r +w 0.99
-w 0.01
-r +w 0.9
-w 0.1
-s +r +w 0.9
-w 0.1
-r +w 0.99
-w 0.01

P(C | S,R,W)

? +c ?
-c ?

P(S | C,R,W)

? +s ?
-s ?

P(R | C,S,W)

? +r ?
-r ?

P(W | C,S,R)

? +w ?
-w ?

Code Snippet

def gibbs_sampling(graph,N):

for n in range(N):

randomly initialize the sample

for gibbs_iter in range(k):

set Xi to a random non-evidence node

sample Xi from P(Xi | all nodes)

record (x1,x2,x3,x4)

Cloudy Sprinkler Rain Wet Grass Count
N(C,S,R,W)
Approximate
P(S,W|+c,-r)
Exact
P(S,W|+c,-r)
+c +s +r +w 0 0 0
+c +s +r -w 0 0 0
+c +s -r +w 0 0 0
+c +s -r -w 0 0 0
+c -s +r +w 0 0 0
+c -s +r -w 0 0 0
+c -s -r +w 0 0 0
+c -s -r -w 0 0 0
-c +s +r +w 0 0 0
-c +s +r -w 0 0 0
-c +s -r +w 0 0 0
-c +s -r -w 0 0 0
-c -s +r +w 0 0 0
-c -s +r -w 0 0 0
-c -s -r +w 0 0 0
-c -s -r -w 0 0 0
Local Variables
Current Sample (?,?,?,?)
Current Node None
Current Iteration (gibbs_iter) 0
Sample Number (n) 0
Cloudy Sprinkler Rain Wet Grass Count
N(C,S,R,W)
Number of Samples Less Cycles Gibbs Probability
(gibbs_iter = 5)
Approximate Prob of Sample Exact Prob of Sample
+c +s +r +w 0 0 0 0 0
+c +s +r -w 0 0 0 0 0
+c +s -r +w 0 0 0 0 0
+c +s -r -w 0 0 0 0 0
+c -s +r +w 0 0 0 0 0
+c -s +r -w 0 0 0 0 0
+c -s -r +w 0 0 0 0 0
+c -s -r -w 0 0 0 0 0
-c +s +r +w 0 0 0 0 0
-c +s +r -w 0 0 0 0 0
-c +s -r +w 0 0 0 0 0
-c +s -r -w 0 0 0 0 0
-c -s +r +w 0 0 0 0 0
-c -s +r -w 0 0 0 0 0
-c -s -r +w 0 0 0 0 0
-c -s -r -w 0 0 0 0 0