wolffd@0
|
1 % Burglar alarm example
|
wolffd@0
|
2
|
wolffd@0
|
3 N = 5;
|
wolffd@0
|
4 dag = zeros(N,N);
|
wolffd@0
|
5 E = 1; B = 2; R = 3; A = 4; C = 5;
|
wolffd@0
|
6 dag(E,[R A]) = 1;
|
wolffd@0
|
7 dag(B,A) = 1;
|
wolffd@0
|
8 dag(A,C)=1;
|
wolffd@0
|
9
|
wolffd@0
|
10 % true = state 1, false = state 2
|
wolffd@0
|
11 ns = 2*ones(1,N); % binary nodes
|
wolffd@0
|
12 bnet = mk_bnet(dag, ns);
|
wolffd@0
|
13
|
wolffd@0
|
14 bnet.CPD{E} = tabular_CPD(bnet, E, [0.1 0.9]);
|
wolffd@0
|
15 bnet.CPD{B} = tabular_CPD(bnet, B, [0.01 0.99]);
|
wolffd@0
|
16 %bnet.CPD{R} = tabular_CPD(bnet, R, [0.65 0.00001 0.35 0.99999]);
|
wolffd@0
|
17 bnet.CPD{R} = tabular_CPD(bnet, R, [0.65 0.01 0.35 0.99]);
|
wolffd@0
|
18 bnet.CPD{A} = tabular_CPD(bnet, A, [0.95 0.8 0.3 0.001 0.05 0.2 0.7 0.999]);
|
wolffd@0
|
19 bnet.CPD{C} = tabular_CPD(bnet, C, [0.7 0.05 0.3 0.95]);
|
wolffd@0
|
20
|
wolffd@0
|
21
|
wolffd@0
|
22 engine = jtree_inf_engine(bnet);
|
wolffd@0
|
23 ev = cell(1,N);
|
wolffd@0
|
24 ev{C} = 1;
|
wolffd@0
|
25 engine = enter_evidence(engine, ev);
|
wolffd@0
|
26 mE = marginal_nodes(engine, E);
|
wolffd@0
|
27 mB = marginal_nodes(engine, B);
|
wolffd@0
|
28 fprintf('P(E|c)=%5.3f, P(B|c)=%5.3f\n', mE.T(1), mB.T(1))
|
wolffd@0
|
29
|
wolffd@0
|
30 ev{C} = 1;
|
wolffd@0
|
31 ev{R} = 1;
|
wolffd@0
|
32 engine = enter_evidence(engine, ev);
|
wolffd@0
|
33 mE = marginal_nodes(engine, E);
|
wolffd@0
|
34 mB = marginal_nodes(engine, B);
|
wolffd@0
|
35 fprintf('P(E|c,r)=%5.3f, P(B|c,r)=%5.3f\n', mE.T(1), mB.T(1))
|
wolffd@0
|
36
|
wolffd@0
|
37
|
wolffd@0
|
38 if 0
|
wolffd@0
|
39 nsamples = 100;
|
wolffd@0
|
40 samples = zeros(nsamples, 5);
|
wolffd@0
|
41 for i=1:nsamples
|
wolffd@0
|
42 samples(i,:) = cell2num(sample_bnet(bnet))';
|
wolffd@0
|
43 end
|
wolffd@0
|
44 end
|