Added examples example_sprinkler_gibbs and example_sprinkler_em
[libdai.git] / examples / example_sprinkler.cpp
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) 2008-2010 Joris Mooij [joris dot mooij at libdai dot org]
8 */
9
10
11 #include <dai/factorgraph.h>
12 #include <iostream>
13 #include <fstream>
14
15 using namespace std;
16 using namespace dai;
17
18 int main() {
19 // This example program illustrates how to construct a factorgraph
20 // by means of the sprinkler network example discussed at
21 // http://www.cs.ubc.ca/~murphyk/Bayes/bnintro.html
22
23 Var C(0, 2); // Define binary variable Cloudy (with label 0)
24 Var S(1, 2); // Define binary variable Sprinkler (with label 1)
25 Var R(2, 2); // Define binary variable Rain (with label 2)
26 Var W(3, 2); // Define binary variable Wetgrass (with label 3)
27
28 // Define probability distribution for C
29 Factor P_C( C );
30 P_C[0] = 0.5; // C = 0
31 P_C[1] = 0.5; // C = 1
32
33 // Define conditional probability of S given C
34 Factor P_S_given_C( VarSet( S, C ) );
35 P_S_given_C[0] = 0.5; // C = 0, S = 0
36 P_S_given_C[1] = 0.9; // C = 1, S = 0
37 P_S_given_C[2] = 0.5; // C = 0, S = 1
38 P_S_given_C[3] = 0.1; // C = 1, S = 1
39
40 // Define conditional probability of R given C
41 Factor P_R_given_C( VarSet( R, C ) );
42 P_R_given_C[0] = 0.8; // C = 0, R = 0
43 P_R_given_C[1] = 0.2; // C = 1, R = 0
44 P_R_given_C[2] = 0.2; // C = 0, R = 1
45 P_R_given_C[3] = 0.8; // C = 1, R = 1
46
47 // Define conditional probability of W given S and R
48 Factor P_W_given_S_R( VarSet( S, R ) | W );
49 P_W_given_S_R[0] = 1.0; // S = 0, R = 0, W = 0
50 P_W_given_S_R[1] = 0.1; // S = 1, R = 0, W = 0
51 P_W_given_S_R[2] = 0.1; // S = 0, R = 1, W = 0
52 P_W_given_S_R[3] = 0.01; // S = 1, R = 1, W = 0
53 P_W_given_S_R[4] = 0.0; // S = 0, R = 0, W = 1
54 P_W_given_S_R[5] = 0.9; // S = 1, R = 0, W = 1
55 P_W_given_S_R[6] = 0.9; // S = 0, R = 1, W = 1
56 P_W_given_S_R[7] = 0.99; // S = 1, R = 1, W = 1
57
58 // Build factor graph consisting of those four factors
59 vector<Factor> SprinklerFactors;
60 SprinklerFactors.push_back( P_C );
61 SprinklerFactors.push_back( P_R_given_C );
62 SprinklerFactors.push_back( P_S_given_C );
63 SprinklerFactors.push_back( P_W_given_S_R );
64 FactorGraph SprinklerNetwork( SprinklerFactors );
65
66 // Write factorgraph to a file
67 SprinklerNetwork.WriteToFile( "sprinkler.fg" );
68 cout << "Sprinkler network written to sprinkler.fg" << endl;
69
70 // Output some information about the factorgraph
71 cout << SprinklerNetwork.nrVars() << " variables" << endl;
72 cout << SprinklerNetwork.nrFactors() << " factors" << endl;
73
74 // Calculate joint probability of all four variables
75 Factor P;
76 for( size_t I = 0; I < SprinklerNetwork.nrFactors(); I++ )
77 P *= SprinklerNetwork.factor( I );
78 // P.normalize(); // Not necessary: a Bayesian network is already normalized by definition
79
80 // Calculate some probabilities
81 Real denom = P.marginal( W )[1];
82 cout << "P(W=1) = " << denom << endl;
83 cout << "P(S=1 | W=1) = " << P.marginal( VarSet( S, W ) )[3] / denom << endl;
84 cout << "P(R=1 | W=1) = " << P.marginal( VarSet( R, W ) )[3] / denom << endl;
85
86 return 0;
87 }