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