Forgot to commit ChangeLog in last commit
[libdai.git] / swig / example_sprinkler.m
1 % This file is part of libDAI - http://www.libdai.org/
2 %
3 % Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 %
5 % Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6
7
8 % This example program illustrates how to construct a factorgraph
9 % by means of the sprinkler network example discussed at
10 % http://www.cs.ubc.ca/~murphyk/Bayes/bnintro.html
11 % using the SWIG octave wrapper of libDAI
12
13 dai
14
15 C = dai.Var(0, 2); % Define binary variable Cloudy (with label 0)
16 S = dai.Var(1, 2); % Define binary variable Sprinkler (with label 1)
17 R = dai.Var(2, 2); % Define binary variable Rain (with label 2)
18 W = dai.Var(3, 2); % Define binary variable Wetgrass (with label 3)
19
20 % Define probability distribution for C
21 P_C = dai.Factor(C);
22 P_C(0) = 0.5; % C = 0
23 P_C(1) = 0.5; % C = 1
24
25 % Define conditional probability of S given C
26 P_S_given_C = dai.Factor(dai.VarSet(S,C));
27 P_S_given_C(0) = 0.5; % C = 0, S = 0
28 P_S_given_C(1) = 0.9; % C = 1, S = 0
29 P_S_given_C(2) = 0.5; % C = 0, S = 1
30 P_S_given_C(3) = 0.1; % C = 1, S = 1
31
32 % Define conditional probability of R given C
33 P_R_given_C = dai.Factor(dai.VarSet(R,C));
34 P_R_given_C(0) = 0.8; % C = 0, R = 0
35 P_R_given_C(1) = 0.2; % C = 1, R = 0
36 P_R_given_C(2) = 0.2; % C = 0, R = 1
37 P_R_given_C(3) = 0.8; % C = 1, R = 1
38
39 % Define conditional probability of W given S and R
40 SRW = dai.VarSet(S,R);
41 SRW.append(W);
42 P_W_given_S_R = dai.Factor(SRW);
43 P_W_given_S_R(0) = 1.0; % S = 0, R = 0, W = 0
44 P_W_given_S_R(1) = 0.1; % S = 1, R = 0, W = 0
45 P_W_given_S_R(2) = 0.1; % S = 0, R = 1, W = 0
46 P_W_given_S_R(3) = 0.01; % S = 1, R = 1, W = 0
47 P_W_given_S_R(4) = 0.0; % S = 0, R = 0, W = 1
48 P_W_given_S_R(5) = 0.9; % S = 1, R = 0, W = 1
49 P_W_given_S_R(6) = 0.9; % S = 0, R = 1, W = 1
50 P_W_given_S_R(7) = 0.99; % S = 1, R = 1, W = 1
51
52 % Build factor graph consisting of those four factors
53 SprinklerFactors = dai.VecFactor();
54 SprinklerFactors.append(P_C);
55 SprinklerFactors.append(P_R_given_C);
56 SprinklerFactors.append(P_S_given_C);
57 SprinklerFactors.append(P_W_given_S_R);
58 SprinklerNetwork = dai.FactorGraph(SprinklerFactors);
59
60 % Write factorgraph to a file
61 SprinklerNetwork.WriteToFile('sprinkler.fg');
62 fprintf('Sprinkler network written to sprinkler.fg\n');
63
64 % Output some information about the factorgraph
65 fprintf('%d variables\n', SprinklerNetwork.nrVars());
66 fprintf('%d factors\n', SprinklerNetwork.nrFactors());
67
68 % Calculate joint probability of all four variables
69 P = dai.Factor();
70 for I = 0:(SprinklerNetwork.nrFactors()-1)
71 P *= SprinklerNetwork.factor(I);
72 end
73 P.normalize(); % Not necessary: a Bayesian network is already normalized by definition
74
75 % Calculate some probabilities
76 denom = P.marginal(dai.VarSet(W))(1);
77 fprintf('P(W=1) = %f\n', denom);
78 fprintf('P(S=1 | W=1) = %f\n', P.marginal(dai.VarSet(S,W))(3) / denom);
79 fprintf('P(R=1 | W=1) = %f\n', P.marginal(dai.VarSet(R,W))(3) / denom);