Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/sprinkler1.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 % Lawn sprinker example from Russell and Norvig p454 | |
2 % For a picture, see http://www.cs.berkeley.edu/~murphyk/Bayes/usage.html#basics | |
3 | |
4 N = 4; | |
5 dag = zeros(N,N); | |
6 C = 1; S = 2; R = 3; W = 4; | |
7 dag(C,[R S]) = 1; | |
8 dag(R,W) = 1; | |
9 dag(S,W)=1; | |
10 | |
11 false = 1; true = 2; | |
12 ns = 2*ones(1,N); % binary nodes | |
13 | |
14 %bnet = mk_bnet(dag, ns); | |
15 bnet = mk_bnet(dag, ns, 'names', {'cloudy','S','R','W'}, 'discrete', 1:4); | |
16 names = bnet.names; | |
17 %C = names{'cloudy'}; | |
18 bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]); | |
19 bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]); | |
20 bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]); | |
21 bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]); | |
22 | |
23 | |
24 CPD{C} = reshape([0.5 0.5], 2, 1); | |
25 CPD{R} = reshape([0.8 0.2 0.2 0.8], 2, 2); | |
26 CPD{S} = reshape([0.5 0.9 0.5 0.1], 2, 2); | |
27 CPD{W} = reshape([1 0.1 0.1 0.01 0 0.9 0.9 0.99], 2, 2, 2); | |
28 joint = zeros(2,2,2,2); | |
29 for c=1:2 | |
30 for r=1:2 | |
31 for s=1:2 | |
32 for w=1:2 | |
33 joint(c,s,r,w) = CPD{C}(c) * CPD{S}(c,s) * CPD{R}(c,r) * ... | |
34 CPD{W}(s,r,w); | |
35 end | |
36 end | |
37 end | |
38 end | |
39 | |
40 joint2 = repmat(reshape(CPD{C}, [2 1 1 1]), [1 2 2 2]) .* ... | |
41 repmat(reshape(CPD{S}, [2 2 1 1]), [1 1 2 2]) .* ... | |
42 repmat(reshape(CPD{R}, [2 1 2 1]), [1 2 1 2]) .* ... | |
43 repmat(reshape(CPD{W}, [1 2 2 2]), [2 1 1 1]); | |
44 | |
45 assert(approxeq(joint, joint2)); | |
46 | |
47 | |
48 engine = jtree_inf_engine(bnet); | |
49 | |
50 evidence = cell(1,N); | |
51 evidence{W} = true; | |
52 | |
53 [engine, ll] = enter_evidence(engine, evidence); | |
54 | |
55 m = marginal_nodes(engine, S); | |
56 p1 = m.T(true) % P(S=true|W=true) = 0.4298 | |
57 lik1 = exp(ll); % P(W=true) = 0.6471 | |
58 assert(approxeq(p1, 0.4298)); | |
59 assert(approxeq(lik1, 0.6471)); | |
60 | |
61 pSandW = sumv(joint(:,true,:,true), [C R]); % P(S,W) = sum_cr P(CSRW) | |
62 pW = sumv(joint(:,:,:,true), [C S R]); | |
63 pSgivenW = pSandW / pW; % P(S=t|W=t) = P(S=t,W=t)/P(W=t) | |
64 assert(approxeq(pW, lik1)) | |
65 assert(approxeq(pSgivenW, p1)) | |
66 | |
67 | |
68 m = marginal_nodes(engine, R); | |
69 p2 = m.T(true) % P(R=true|W=true) = 0.7079 | |
70 | |
71 pRandW = sumv(joint(:,:,true,true), [C S]); % P(R,W) = sum_cr P(CSRW) | |
72 pRgivenW = pRandW / pW; % P(R=t|W=t) = P(R=t,W=t)/P(W=t) | |
73 assert(approxeq(pRgivenW, p2)) | |
74 | |
75 | |
76 % Add extra evidence that R=true | |
77 evidence{R} = true; | |
78 | |
79 [engine, ll] = enter_evidence(engine, evidence); | |
80 | |
81 m = marginal_nodes(engine, S); | |
82 p3 = m.T(true) % P(S=true|W=true,R=true) = 0.1945 | |
83 assert(approxeq(p3, 0.1945)) | |
84 | |
85 | |
86 pSandRandW = sumv(joint(:,true,true,true), [C]); % P(S,R,W) = sum_c P(cSRW) | |
87 pRandW = sumv(joint(:,:,true,true), [C S]); % P(R,W) = sum_cs P(cSRW) | |
88 pSgivenWR = pSandRandW / pRandW; % P(S=t|W=t,R=t) = P(S=t,R=t,W=t)/P(W=t,R=t) | |
89 assert(approxeq(pSgivenWR, p3)) | |
90 | |
91 % So the sprinkler is less likely to be on if we know that | |
92 % it is raining, since the rain can "explain away" the fact | |
93 % that the grass is wet. | |
94 | |
95 lik3 = exp(ll); % P(W=true, R=true) = 0.4581 | |
96 % So the combined evidence is less likely (of course) | |
97 | |
98 | |
99 | |
100 | |
101 % Joint distributions | |
102 | |
103 evidence = cell(1,N); | |
104 [engine, ll] = enter_evidence(engine, evidence); | |
105 m = marginal_nodes(engine, [S R W]); | |
106 | |
107 evidence{R} = 2; | |
108 [engine, ll] = enter_evidence(engine, evidence); | |
109 m = marginal_nodes(engine, [S R W]); | |
110 | |
111 | |
112 |