File size: 35,605 Bytes
9375c9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - bayes_net_ex.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
</font><font color='#009900'>/*
This is an example illustrating the use of the Bayesian Network
inference utilities found in the dlib C++ library.
In this example all the nodes in the Bayesian network are
boolean variables. That is, they take on either the value
0 or the value 1.
The network contains 4 nodes and looks as follows:
B C
\\ //
\/ \/
A
||
\/
D
The probabilities of each node are summarized below. (The probability
of each node being 0 is not listed since it is just P(X=0) = 1-p(X=1) )
p(B=1) = 0.01
p(C=1) = 0.001
p(A=1 | B=0, C=0) = 0.01
p(A=1 | B=0, C=1) = 0.5
p(A=1 | B=1, C=0) = 0.9
p(A=1 | B=1, C=1) = 0.99
p(D=1 | A=0) = 0.2
p(D=1 | A=1) = 0.5
*/</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>bayes_utils.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>graph_utils.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>graph.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>directed_graph.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>iostream<font color='#5555FF'>></font>
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>try</font>
<b>{</b>
<font color='#009900'>// There are many useful convenience functions in this namespace. They all
</font> <font color='#009900'>// perform simple access or modify operations on the nodes of a bayesian network.
</font> <font color='#009900'>// You don't have to use them but they are convenient and they also will check for
</font> <font color='#009900'>// various errors in your bayesian network when your application is built with
</font> <font color='#009900'>// the DEBUG or ENABLE_ASSERTS preprocessor definitions defined. So their use
</font> <font color='#009900'>// is recommended. In fact, most of the global functions used in this example
</font> <font color='#009900'>// program are from this namespace.
</font> <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> bayes_node_utils;
<font color='#009900'>// This statement declares a bayesian network called bn. Note that a bayesian network
</font> <font color='#009900'>// in the dlib world is just a directed_graph object that contains a special kind
</font> <font color='#009900'>// of node called a bayes_node.
</font> directed_graph<font color='#5555FF'><</font>bayes_node<font color='#5555FF'>></font>::kernel_1a_c bn;
<font color='#009900'>// Use an enum to make some more readable names for our nodes.
</font> <font color='#0000FF'>enum</font> <b><a name='nodes'></a>nodes</b>
<b>{</b>
A <font color='#5555FF'>=</font> <font color='#979000'>0</font>,
B <font color='#5555FF'>=</font> <font color='#979000'>1</font>,
C <font color='#5555FF'>=</font> <font color='#979000'>2</font>,
D <font color='#5555FF'>=</font> <font color='#979000'>3</font>
<b>}</b>;
<font color='#009900'>// The next few blocks of code setup our bayesian network.
</font>
<font color='#009900'>// The first thing we do is tell the bn object how many nodes it has
</font> <font color='#009900'>// and also add the three edges. Again, we are using the network
</font> <font color='#009900'>// shown in ASCII art at the top of this file.
</font> bn.<font color='#BB00BB'>set_number_of_nodes</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>;
bn.<font color='#BB00BB'>add_edge</font><font face='Lucida Console'>(</font>A, D<font face='Lucida Console'>)</font>;
bn.<font color='#BB00BB'>add_edge</font><font face='Lucida Console'>(</font>B, A<font face='Lucida Console'>)</font>;
bn.<font color='#BB00BB'>add_edge</font><font face='Lucida Console'>(</font>C, A<font face='Lucida Console'>)</font>;
<font color='#009900'>// Now we inform all the nodes in the network that they are binary
</font> <font color='#009900'>// nodes. That is, they only have two possible values.
</font> <font color='#BB00BB'>set_node_num_values</font><font face='Lucida Console'>(</font>bn, A, <font color='#979000'>2</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_num_values</font><font face='Lucida Console'>(</font>bn, B, <font color='#979000'>2</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_num_values</font><font face='Lucida Console'>(</font>bn, C, <font color='#979000'>2</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_num_values</font><font face='Lucida Console'>(</font>bn, D, <font color='#979000'>2</font><font face='Lucida Console'>)</font>;
assignment parent_state;
<font color='#009900'>// Now we will enter all the conditional probability information for each node.
</font> <font color='#009900'>// Each node's conditional probability is dependent on the state of its parents.
</font> <font color='#009900'>// To specify this state we need to use the assignment object. This assignment
</font> <font color='#009900'>// object allows us to specify the state of each nodes parents.
</font>
<font color='#009900'>// Here we specify that p(B=1) = 0.01
</font> <font color='#009900'>// parent_state is empty in this case since B is a root node.
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, B, <font color='#979000'>1</font>, parent_state, <font color='#979000'>0.01</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Here we specify that p(B=0) = 1-0.01
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, B, <font color='#979000'>0</font>, parent_state, <font color='#979000'>1</font><font color='#5555FF'>-</font><font color='#979000'>0.01</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Here we specify that p(C=1) = 0.001
</font> <font color='#009900'>// parent_state is empty in this case since B is a root node.
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, C, <font color='#979000'>1</font>, parent_state, <font color='#979000'>0.001</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Here we specify that p(C=0) = 1-0.001
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, C, <font color='#979000'>0</font>, parent_state, <font color='#979000'>1</font><font color='#5555FF'>-</font><font color='#979000'>0.001</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// This is our first node that has parents. So we set the parent_state
</font> <font color='#009900'>// object to reflect that A has both B and C as parents.
</font> parent_state.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font>B, <font color='#979000'>1</font><font face='Lucida Console'>)</font>;
parent_state.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font>C, <font color='#979000'>1</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Here we specify that p(A=1 | B=1, C=1) = 0.99
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, A, <font color='#979000'>1</font>, parent_state, <font color='#979000'>0.99</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Here we specify that p(A=0 | B=1, C=1) = 1-0.99
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, A, <font color='#979000'>0</font>, parent_state, <font color='#979000'>1</font><font color='#5555FF'>-</font><font color='#979000'>0.99</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Here we use the [] notation because B and C have already
</font> <font color='#009900'>// been added into parent state.
</font> parent_state[B] <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
parent_state[C] <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#009900'>// Here we specify that p(A=1 | B=1, C=0) = 0.9
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, A, <font color='#979000'>1</font>, parent_state, <font color='#979000'>0.9</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, A, <font color='#979000'>0</font>, parent_state, <font color='#979000'>1</font><font color='#5555FF'>-</font><font color='#979000'>0.9</font><font face='Lucida Console'>)</font>;
parent_state[B] <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
parent_state[C] <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
<font color='#009900'>// Here we specify that p(A=1 | B=0, C=1) = 0.5
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, A, <font color='#979000'>1</font>, parent_state, <font color='#979000'>0.5</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, A, <font color='#979000'>0</font>, parent_state, <font color='#979000'>1</font><font color='#5555FF'>-</font><font color='#979000'>0.5</font><font face='Lucida Console'>)</font>;
parent_state[B] <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
parent_state[C] <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#009900'>// Here we specify that p(A=1 | B=0, C=0) = 0.01
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, A, <font color='#979000'>1</font>, parent_state, <font color='#979000'>0.01</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, A, <font color='#979000'>0</font>, parent_state, <font color='#979000'>1</font><font color='#5555FF'>-</font><font color='#979000'>0.01</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Here we set probabilities for node D.
</font> <font color='#009900'>// First we clear out parent state so that it doesn't have any of
</font> <font color='#009900'>// the assignments for the B and C nodes used above.
</font> parent_state.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
parent_state.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font>A,<font color='#979000'>1</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Here we specify that p(D=1 | A=1) = 0.5
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, D, <font color='#979000'>1</font>, parent_state, <font color='#979000'>0.5</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, D, <font color='#979000'>0</font>, parent_state, <font color='#979000'>1</font><font color='#5555FF'>-</font><font color='#979000'>0.5</font><font face='Lucida Console'>)</font>;
parent_state[A] <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#009900'>// Here we specify that p(D=1 | A=0) = 0.2
</font> <font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, D, <font color='#979000'>1</font>, parent_state, <font color='#979000'>0.2</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_probability</font><font face='Lucida Console'>(</font>bn, D, <font color='#979000'>0</font>, parent_state, <font color='#979000'>1</font><font color='#5555FF'>-</font><font color='#979000'>0.2</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// We have now finished setting up our bayesian network. So let's compute some
</font> <font color='#009900'>// probability values. The first thing we will do is compute the prior probability
</font> <font color='#009900'>// of each node in the network. To do this we will use the join tree algorithm which
</font> <font color='#009900'>// is an algorithm for performing exact inference in a bayesian network.
</font>
<font color='#009900'>// First we need to create an undirected graph which contains set objects at each node and
</font> <font color='#009900'>// edge. This long declaration does the trick.
</font> <font color='#0000FF'>typedef</font> dlib::set<font color='#5555FF'><</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>></font>::compare_1b_c set_type;
<font color='#0000FF'>typedef</font> graph<font color='#5555FF'><</font>set_type, set_type<font color='#5555FF'>></font>::kernel_1a_c join_tree_type;
join_tree_type join_tree;
<font color='#009900'>// Now we need to populate the join_tree with data from our bayesian network. The next
</font> <font color='#009900'>// function calls do this. Explaining exactly what they do is outside the scope of this
</font> <font color='#009900'>// example. Just think of them as filling join_tree with information that is useful
</font> <font color='#009900'>// later on for dealing with our bayesian network.
</font> <font color='#BB00BB'>create_moral_graph</font><font face='Lucida Console'>(</font>bn, join_tree<font face='Lucida Console'>)</font>;
<font color='#BB00BB'>create_join_tree</font><font face='Lucida Console'>(</font>join_tree, join_tree<font face='Lucida Console'>)</font>;
<font color='#009900'>// Now that we have a proper join_tree we can use it to obtain a solution to our
</font> <font color='#009900'>// bayesian network. Doing this is as simple as declaring an instance of
</font> <font color='#009900'>// the bayesian_network_join_tree object as follows:
</font> bayesian_network_join_tree <font color='#BB00BB'>solution</font><font face='Lucida Console'>(</font>bn, join_tree<font face='Lucida Console'>)</font>;
<font color='#009900'>// now print out the probabilities for each node
</font> cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>Using the join tree algorithm:\n</font>";
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(A=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>A<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(A=0) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>A<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(B=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>B<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(B=0) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>B<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>C<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(C=0) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>C<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(D=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>D<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(D=0) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>D<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\n\n</font>";
<font color='#009900'>// Now to make things more interesting let's say that we have discovered that the C
</font> <font color='#009900'>// node really has a value of 1. That is to say, we now have evidence that
</font> <font color='#009900'>// C is 1. We can represent this in the network using the following two function
</font> <font color='#009900'>// calls.
</font> <font color='#BB00BB'>set_node_value</font><font face='Lucida Console'>(</font>bn, C, <font color='#979000'>1</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_as_evidence</font><font face='Lucida Console'>(</font>bn, C<font face='Lucida Console'>)</font>;
<font color='#009900'>// Now we want to compute the probabilities of all the nodes in the network again
</font> <font color='#009900'>// given that we now know that C is 1. We can do this as follows:
</font> bayesian_network_join_tree <font color='#BB00BB'>solution_with_evidence</font><font face='Lucida Console'>(</font>bn, join_tree<font face='Lucida Console'>)</font>;
<font color='#009900'>// now print out the probabilities for each node
</font> cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>Using the join tree algorithm:\n</font>";
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(A=1 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution_with_evidence.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>A<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(A=0 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution_with_evidence.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>A<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(B=1 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution_with_evidence.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>B<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(B=0 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution_with_evidence.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>B<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(C=1 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution_with_evidence.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>C<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(C=0 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution_with_evidence.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>C<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(D=1 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution_with_evidence.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>D<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(D=0 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> solution_with_evidence.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>D<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\n\n</font>";
<font color='#009900'>// Note that when we made our solution_with_evidence object we reused our join_tree object.
</font> <font color='#009900'>// This saves us the time it takes to calculate the join_tree object from scratch. But
</font> <font color='#009900'>// it is important to note that we can only reuse the join_tree object if we haven't changed
</font> <font color='#009900'>// the structure of our bayesian network. That is, if we have added or removed nodes or
</font> <font color='#009900'>// edges from our bayesian network then we must recompute our join_tree. But in this example
</font> <font color='#009900'>// all we did was change the value of a bayes_node object (we made node C be evidence)
</font> <font color='#009900'>// so we are ok.
</font>
<font color='#009900'>// Next this example will show you how to use the bayesian_network_gibbs_sampler object
</font> <font color='#009900'>// to perform approximate inference in a bayesian network. This is an algorithm
</font> <font color='#009900'>// that doesn't give you an exact solution but it may be necessary to use in some
</font> <font color='#009900'>// instances. For example, the join tree algorithm used above, while fast in many
</font> <font color='#009900'>// instances, has exponential runtime in some cases. Moreover, inference in bayesian
</font> <font color='#009900'>// networks is NP-Hard for general networks so sometimes the best you can do is
</font> <font color='#009900'>// find an approximation.
</font> <font color='#009900'>// However, it should be noted that the gibbs sampler does not compute the correct
</font> <font color='#009900'>// probabilities if the network contains a deterministic node. That is, if any
</font> <font color='#009900'>// of the conditional probability tables in the bayesian network have a probability
</font> <font color='#009900'>// of 1.0 for something the gibbs sampler should not be used.
</font>
<font color='#009900'>// This Gibbs sampler algorithm works by randomly sampling possibles values of the
</font> <font color='#009900'>// network. So to use it we should set the network to some initial state.
</font>
<font color='#BB00BB'>set_node_value</font><font face='Lucida Console'>(</font>bn, A, <font color='#979000'>0</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_value</font><font face='Lucida Console'>(</font>bn, B, <font color='#979000'>0</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_node_value</font><font face='Lucida Console'>(</font>bn, D, <font color='#979000'>0</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// We will leave the C node with a value of 1 and keep it as an evidence node.
</font>
<font color='#009900'>// First create an instance of the gibbs sampler object
</font> bayesian_network_gibbs_sampler sampler;
<font color='#009900'>// To use this algorithm all we do is go into a loop for a certain number of times
</font> <font color='#009900'>// and each time through we sample the bayesian network. Then we count how
</font> <font color='#009900'>// many times a node has a certain state. Then the probability of that node
</font> <font color='#009900'>// having that state is just its count/total times through the loop.
</font>
<font color='#009900'>// The following code illustrates the general procedure.
</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> A_count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> B_count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> C_count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> D_count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#009900'>// The more times you let the loop run the more accurate the result will be. Here we loop
</font> <font color='#009900'>// 2000 times.
</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> rounds <font color='#5555FF'>=</font> <font color='#979000'>2000</font>;
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> rounds; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
<b>{</b>
sampler.<font color='#BB00BB'>sample_graph</font><font face='Lucida Console'>(</font>bn<font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>node_value</font><font face='Lucida Console'>(</font>bn, A<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
<font color='#5555FF'>+</font><font color='#5555FF'>+</font>A_count;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>node_value</font><font face='Lucida Console'>(</font>bn, B<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
<font color='#5555FF'>+</font><font color='#5555FF'>+</font>B_count;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>node_value</font><font face='Lucida Console'>(</font>bn, C<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
<font color='#5555FF'>+</font><font color='#5555FF'>+</font>C_count;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>node_value</font><font face='Lucida Console'>(</font>bn, D<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
<font color='#5555FF'>+</font><font color='#5555FF'>+</font>D_count;
<b>}</b>
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>Using the approximate Gibbs Sampler algorithm:\n</font>";
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(A=1 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font><font face='Lucida Console'>)</font>A_count<font color='#5555FF'>/</font><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font><font face='Lucida Console'>)</font>rounds <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(B=1 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font><font face='Lucida Console'>)</font>B_count<font color='#5555FF'>/</font><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font><font face='Lucida Console'>)</font>rounds <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(C=1 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font><font face='Lucida Console'>)</font>C_count<font color='#5555FF'>/</font><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font><font face='Lucida Console'>)</font>rounds <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>p(D=1 | C=1) = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font><font face='Lucida Console'>)</font>D_count<font color='#5555FF'>/</font><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font><font face='Lucida Console'>)</font>rounds <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
<b>}</b>
<font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>std::exception<font color='#5555FF'>&</font> e<font face='Lucida Console'>)</font>
<b>{</b>
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>exception thrown: </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> e.<font color='#BB00BB'>what</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>hit enter to terminate</font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cin.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
</pre></body></html> |