Finished a new release: libDAI 0.2.7.
[libdai.git] / swig / example_sprinkler.m
1 % This file is part of libDAI - http://www.libdai.org/
2 %
3 % libDAI is licensed under the terms of the GNU General Public License version
4 % 2, or (at your option) any later version. libDAI is distributed without any
5 % warranty. See the file COPYING for more details.
6 %
7 % Copyright (C) 2009 Joris Mooij [joris dot mooij at libdai dot org]
8
9
10 % This example program illustrates how to construct a factorgraph
11 % by means of the sprinkler network example discussed at
12 % http://www.cs.ubc.ca/~murphyk/Bayes/bnintro.html
13 % using the SWIG octave wrapper of libDAI
14
15 dai
16
17 C = dai.Var(0, 2); % Define binary variable Cloudy (with label 0)
18 S = dai.Var(1, 2); % Define binary variable Sprinkler (with label 1)
19 R = dai.Var(2, 2); % Define binary variable Rain (with label 2)
20 W = dai.Var(3, 2); % Define binary variable Wetgrass (with label 3)
21
22 % Define probability distribution for C
23 P_C = dai.Factor(C);
24 P_C(0) = 0.5; % C = 0
25 P_C(1) = 0.5; % C = 1
26
27 % Define conditional probability of S given C
28 P_S_given_C = dai.Factor(dai.VarSet(S,C));
29 P_S_given_C(0) = 0.5; % C = 0, S = 0
30 P_S_given_C(1) = 0.9; % C = 1, S = 0
31 P_S_given_C(2) = 0.5; % C = 0, S = 1
32 P_S_given_C(3) = 0.1; % C = 1, S = 1
33
34 % Define conditional probability of R given C
35 P_R_given_C = dai.Factor(dai.VarSet(R,C));
36 P_R_given_C(0) = 0.8; % C = 0, R = 0
37 P_R_given_C(1) = 0.2; % C = 1, R = 0
38 P_R_given_C(2) = 0.2; % C = 0, R = 1
39 P_R_given_C(3) = 0.8; % C = 1, R = 1
40
41 % Define conditional probability of W given S and R
42 SRW = dai.VarSet(S,R);
43 SRW.append(W);
44 P_W_given_S_R = dai.Factor(SRW);
45 P_W_given_S_R(0) = 1.0; % S = 0, R = 0, W = 0
46 P_W_given_S_R(1) = 0.1; % S = 1, R = 0, W = 0
47 P_W_given_S_R(2) = 0.1; % S = 0, R = 1, W = 0
48 P_W_given_S_R(3) = 0.01; % S = 1, R = 1, W = 0
49 P_W_given_S_R(4) = 0.0; % S = 0, R = 0, W = 1
50 P_W_given_S_R(5) = 0.9; % S = 1, R = 0, W = 1
51 P_W_given_S_R(6) = 0.9; % S = 0, R = 1, W = 1
52 P_W_given_S_R(7) = 0.99; % S = 1, R = 1, W = 1
53
54 % Build factor graph consisting of those four factors
55 SprinklerFactors = dai.VecFactor();
56 SprinklerFactors.append(P_C);
57 SprinklerFactors.append(P_R_given_C);
58 SprinklerFactors.append(P_S_given_C);
59 SprinklerFactors.append(P_W_given_S_R);
60 SprinklerNetwork = dai.FactorGraph(SprinklerFactors);
61
62 % Write factorgraph to a file
63 SprinklerNetwork.WriteToFile('sprinkler.fg');
64 fprintf('Sprinkler network written to sprinkler.fg\n');
65
66 % Output some information about the factorgraph
67 fprintf('%d variables\n', SprinklerNetwork.nrVars());
68 fprintf('%d factors\n', SprinklerNetwork.nrFactors());
69
70 % Calculate joint probability of all four variables
71 P = dai.Factor();
72 for I = 0:(SprinklerNetwork.nrFactors()-1)
73 P *= SprinklerNetwork.factor(I);
74 end
75 P.normalize(); % Not necessary: a Bayesian network is already normalized by definition
76
77 % Calculate some probabilities
78 denom = P.marginal(dai.VarSet(W))(1);
79 fprintf('P(W=1) = %f\n', denom);
80 fprintf('P(S=1 | W=1) = %f\n', P.marginal(dai.VarSet(S,W))(3) / denom);
81 fprintf('P(R=1 | W=1) = %f\n', P.marginal(dai.VarSet(R,W))(3) / denom);