Fixed regression FBP and bugs in TRWBP
[libdai.git] / include / dai / doc.h
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-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 */
9
10
11 /** \file
12 * \brief Contains additional doxygen documentation
13 *
14 * \todo Write a concept/notations page for the documentation,
15 * explaining the concepts of "state" (index into a
16 * multi-dimensional array, e.g., one corresponding
17 * to the Cartesian product of statespaces of variables)
18 * and "linear index". This should make it easier to
19 * document index.h and varset.h
20 *
21 * \todo Document tests and utils
22 *
23 * \todo Implement routines for UAI probabilistic inference evaluation data
24 *
25 * \todo Improve SWIG interfaces
26 *
27 * \idea Adapt (part of the) guidelines in http://www.boost.org/development/requirements.html#Design_and_Programming
28 *
29 * \idea Use "gcc -MM" to generate dependencies for targets: http://make.paulandlesley.org/autodep.html
30 *
31 * \todo Replace VarSets by SmallSet<size_t> where appropriate, in order to minimize the use of FactorGraph::findVar().
32 *
33 * \idea Disentangle structures. In particular, ensure that graphical properties are not
34 * entangled with probabilistic properties. For example, a FactorGraph contains several components:
35 * - a BipartiteGraph
36 * - an array of variable labels
37 * - an array of variable state space sizes
38 * - an array of pointers to factor value vectors
39 * In this way, each factor could be implemented differently, e.g., we could have
40 * some sparse factors, some noisy-OR factors, some dense factors, some arbitrary
41 * precision factors, etcetera.
42 *
43 * \idea Use boost::uBLAS framework to deal with matrices, especially, with 2D sparse matrices.
44 * See http://www.boost.org/libs/numeric/ublas/doc/matrix_sparse.htm
45 * However: I read somewhere that boost::uBLAS concentrates more on correct implementation than on performance.
46 *
47 * \idea Introduce naming scheme:
48 * - all Vars should be named v_..., e.g. v_i instead of i
49 * - all VarSets should be named vs_..., e.g. v_i instead of i
50 * - all Factors should be named f_..., e.g. f_I instead of I
51 * - all indices should be named _..., e.g. _k instead of k
52 * - all iterators should be named i_, e.g. i_i is an iterator to i
53 * - all const_iterators should be named ci_, e.g. ci_i is an iterator to i
54 **/
55
56
57 /** \mainpage Reference manual for libDAI - A free/open source C++ library for Discrete Approximate Inference methods
58 * \author Joris Mooij
59 * \version git HEAD
60 * \date January 12, 2010 - or later
61 *
62 * <hr size="1">
63 * \section about About libDAI
64 * libDAI is a free/open source C++ library (licensed under GPL 2+) that provides
65 * implementations of various (approximate) inference methods for discrete
66 * graphical models. libDAI supports arbitrary factor graphs with discrete
67 * variables; this includes discrete Markov Random Fields and Bayesian
68 * Networks.
69 *
70 * The library is targeted at researchers. To be able to use the library, a
71 * good understanding of graphical models is needed.
72 *
73 * The best way to use libDAI is by writing C++ code that invokes the library;
74 * in addition, part of the functionality is accessibly by using the
75 * - command line interface
76 * - (limited) MatLab interface
77 * - (experimental) python interface
78 * - (experimental) octave interface.
79 *
80 * libDAI can be used to implement novel (approximate) inference algorithms
81 * and to easily compare the accuracy and performance with existing algorithms
82 * that have been implemented already.
83 *
84 * \section features Features
85 * Currently, libDAI supports the following (approximate) inference methods:
86 * - Exact inference by brute force enumeration;
87 * - Exact inference by junction-tree methods;
88 * - Mean Field;
89 * - Loopy Belief Propagation [\ref KFL01];
90 * - Fractional Belief Propagation [\ref WiH03];
91 * - Tree-Reweighted Belief Propagation [\ref WJW03];
92 * - Tree Expectation Propagation [\ref MiQ04];
93 * - Generalized Belief Propagation [\ref YFW05];
94 * - Double-loop GBP [\ref HAK03];
95 * - Various variants of Loop Corrected Belief Propagation
96 * [\ref MoK07, \ref MoR05];
97 * - Gibbs sampler;
98 * - Clamped Belief Propagation [\ref EaG09].
99 *
100 * These inference methods can be used to calculate partition sums, marginals
101 * over subsets of variables, and MAP states (the joint state of variables that
102 * has maximum probability).
103 *
104 * In addition, libDAI supports parameter learning of conditional probability
105 * tables by Expectation Maximization.
106 *
107 * \section limitations Limitations
108 * libDAI is not intended to be a complete package for approximate inference.
109 * Instead, it should be considered as an "inference engine", providing
110 * various inference methods. In particular, it contains no GUI, currently
111 * only supports its own file format for input and output (although support
112 * for standard file formats may be added later), and provides very limited
113 * visualization functionalities. The only learning method supported currently
114 * is Expectation Maximization (or Maximum Likelihood if no data is missing)
115 * for learning factor parameters.
116 *
117 * \section rationale Rationale
118 *
119 * In my opinion, the lack of open source "reference" implementations hampers
120 * progress in research on approximate inference. Methods differ widely in terms
121 * of quality and performance characteristics, which also depend in different
122 * ways on various properties of the graphical models. Finding the best
123 * approximate inference method for a particular application therefore often
124 * requires empirical comparisons. However, implementing and debugging these
125 * methods takes a lot of time which could otherwise be spent on research. I hope
126 * that this code will aid researchers to be able to easily compare various
127 * (existing as well as new) approximate inference methods, in this way
128 * accelerating research and stimulating real-world applications of approximate
129 * inference.
130 *
131 * \section language Language
132 * Because libDAI is implemented in C++, it is very fast compared with
133 * implementations in MatLab (a factor 1000 faster is not uncommon).
134 * libDAI does provide a (limited) MatLab interface for easy integration with MatLab.
135 * It also provides a command line interface and experimental python and octave
136 * interfaces (thanks to Patrick Pletscher).
137 *
138 * \section compatibility Compatibility
139 *
140 * The code has been developed under Debian GNU/Linux with the GCC compiler suite.
141 * libDAI compiles successfully with g++ versions 3.4, 4.1, 4.2 and 4.3.
142 *
143 * libDAI has also been successfully compiled with MS Visual Studio 2008 under Windows
144 * (but not all build targets are supported yet) and with Cygwin under Windows.
145 *
146 * Finally, libDAI has been compiled successfully on MacOS X.
147 *
148 * \section download Downloading libDAI
149 * The libDAI sources and documentation can be downloaded from the libDAI website:
150 * http://www.libdai.org.
151 *
152 * \section support Mailing list
153 * The Google group "libDAI" (http://groups.google.com/group/libdai)
154 * can be used for getting support and discussing development issues.
155 */
156
157
158 /** \page license License
159 * <hr size="1">
160 * \section license-license License
161 *
162 * libDAI is free software; you can redistribute it and/or modify
163 * it under the terms of the GNU General Public License as published by
164 * the Free Software Foundation; either version 2 of the License, or
165 * (at your option) any later version.
166 *
167 * libDAI is distributed in the hope that it will be useful,
168 * but WITHOUT ANY WARRANTY; without even the implied warranty of
169 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
170 * GNU General Public License for more details.
171 *
172 * <hr size="1">
173 * \section license-gpl GNU General Public License version 2
174 *
175 * \verbinclude COPYING
176 */
177
178
179 /** \page citations Citing libDAI
180 * <hr size="1">
181 * \section citations-citations Citing libDAI
182 *
183 * If you write a scientific paper describing research that made substantive use
184 * of this program, please:
185 * - mention the fashion in which this software was
186 * used, including the version number, with a citation to the literature,
187 * to allow replication;
188 * - mention this software in the Acknowledgements section.
189 *
190 * An appropriate citation would be:\n
191 * J. M. Mooij (2009) "libDAI 0.2.3: A free/open source C++ library for Discrete
192 * Approximate Inference", http://www.libdai.org
193 *
194 * Moreover, as a personal note, I would appreciate it if you would email
195 * (citations of) papers referencing this work to joris dot mooij at libdai dot org.
196 */
197
198
199 /** \page authors Authors
200 * \section authors-authors People who contributed to libDAI
201 *
202 * \verbinclude AUTHORS
203 */
204
205
206 /** \page build Building libDAI
207 * <hr size="1">
208 * \section build-unix Building libDAI under UNIX variants (Linux / Cygwin / Mac OS X)
209 *
210 * You need:
211 * - a recent version of gcc (at least version 3.4)
212 * - GNU make
213 * - doxygen
214 * - graphviz
215 * - recent boost C++ libraries (at least version 1.34, or 1.37 for cygwin;
216 * version 1.37 shipped with Ubuntu 9.04 is known not to work)
217 *
218 * On Debian/Ubuntu, you can easily install all these packages with a single command:
219 * <pre> apt-get install g++ make doxygen graphviz libboost-dev libboost-graph-dev libboost-program-options-dev</pre>
220 * (root permissions needed).
221 *
222 * On Mac OS X (10.4 is known to work), these packages can be installed easily via MacPorts.
223 * If MacPorts is not already installed, install it according to the instructions at http://www.macports.org/.
224 * Then, a simple
225 * <pre> sudo port install gmake boost doxygen graphviz</pre>
226 * should be enough to install everything that is needed.
227 *
228 * On Cygwin, the prebuilt Cygwin package boost-1.33.1-x is known not to work.
229 * You can however obtain the latest boost version (you need at least 1.37.0)
230 * from http://www.boost.org/ and compile/install it with:
231 *
232 * <pre> ./configure
233 * make
234 * make install
235 * </pre>
236 *
237 * To build the libDAI source, first copy a template Makefile.* to Makefile.conf
238 * (for example, copy Makefile.LINUX to Makefile.conf if you use GNU/Linux).
239 * Then, edit the Makefile.conf template to adapt it to your local setup.
240 * Especially directories may differ from system to system. Finally, run
241 * <pre> make</pre>
242 * The build includes a regression test, which may take a while to complete.
243 *
244 * If the build was successful, you can test the example program:
245 * <pre> examples/example tests/alarm.fg</pre>
246 * or the more elaborate test program:
247 * <pre> tests/testdai --aliases tests/aliases.conf --filename tests/alarm.fg --methods JTREE_HUGIN BP_SEQMAX</pre>
248 *
249 *
250 * <hr size="1">
251 * \section build-windows Building libDAI under Windows
252 *
253 * You need:
254 * - A recent version of MicroSoft Visual Studio (2008 works)
255 * - recent boost C++ libraries (version 1.34 or higher)
256 * - GNU make (can be obtained from http://gnuwin32.sourceforge.net)
257 *
258 * For the regression test, you need:
259 * - GNU diff, GNU sed (can be obtained from http://gnuwin32.sourceforge.net)
260 *
261 * To build the source, copy Makefile.WINDOWS to Makefile.conf. Then, edit
262 * Makefile.conf to adapt it to your local setup. Finally, run (from the command line)
263 * <pre> make</pre>
264 * The build includes a regression test, which may take a while to complete.
265 *
266 * If the build was successful, you can test the example program:
267 * <pre> examples\\example tests\\alarm.fg</pre>
268 * or the more elaborate test program:
269 * <pre> tests\\testdai --aliases tests\\aliases.conf --filename tests\\alarm.fg --methods JTREE_HUGIN BP_SEQMAX</pre>
270 *
271 *
272 * <hr size="1">
273 * \section build-matlab Building the libDAI MatLab interface
274 *
275 * You need:
276 * - MatLab
277 * - The platform-dependent requirements described above
278 *
279 * First, you need to build the libDAI source as described above for your
280 * platform. By default, the MatLab interface is disabled, so before compiling the
281 * source, you have to enable it in the Makefile.conf by setting
282 * <pre> WITH_MATLAB=true</pre>
283 * Also, you have to configure the MatLab-specific parts of
284 * Makefile.conf to match your system (in particular, the Makefile variables ME,
285 * MATLABDIR and MEX). The MEX file extension depends on your platform; for a
286 * 64-bit linux x86_64 system this would be "ME=.mexa64", for a 32-bit linux x86
287 * system "ME=.mexglx". If you are unsure about your MEX file
288 * extension: it needs to be the same as what the MatLab command "mexext" returns.
289 * The required MEX files are built by issuing
290 * <pre> make</pre>
291 * from the command line. The MatLab interface is much less powerful than using
292 * libDAI from C++. There are two reasons for this: (i) it is boring to write MEX
293 * files; (ii) the large performance penalty paid when large data structures (like
294 * factor graphs) have to be converted between their native C++ data structure to
295 * something that MatLab understands.
296 *
297 * A simple example of how to use the MatLab interface is the following (entered
298 * at the MatLab prompt), which performs exact inference by the junction tree
299 * algorithm and approximate inference by belief propagation on the ALARM network:
300 * <pre> cd path_to_libdai/matlab
301 * [psi] = dai_readfg ('../examples/alarm.fg');
302 * [logZ,q,md,qv,qf] = dai (psi, 'JTREE', '[updates=HUGIN,verbose=0]')
303 * [logZ,q,md,qv,qf] = dai (psi, 'BP', '[updates=SEQMAX,tol=1e-9,maxiter=10000,logdomain=0]')</pre>
304 * where "path_to_libdai" has to be replaced with the directory in which libDAI
305 * was installed. For other algorithms and some default parameters, see the file
306 * tests/aliases.conf.
307 *
308 * <hr size="1">
309 * \section build-doxygen Building the documentation
310 *
311 * Install doxygen, graphviz and a TeX distribution and use
312 * <pre> make doc</pre>
313 * to build the documentation. If the documentation is not clear enough, feel free
314 * to send me an email (or even better, to improve the documentation and send a patch!).
315 * The documentation can also be browsed online at http://www.libdai.org.
316 */
317
318
319 /** \page changelog Change Log
320 * \verbinclude ChangeLog
321 */
322
323
324 /** \page inference Graphical models and approximate inference
325 *
326 * \section inference-graphicalmodels Graphical models
327 *
328 * Commonly used graphical models are Bayesian networks and Markov random fields.
329 * In libDAI, both types of graphical models are represented by a slightly more
330 * general type of graphical model: a factor graph [\ref KFL01].
331 *
332 * An example of a Bayesian network is:
333 * \dot
334 * digraph bayesnet {
335 * size="1,1";
336 * x0 [label="0"];
337 * x1 [label="1"];
338 * x2 [label="2"];
339 * x3 [label="3"];
340 * x4 [label="4"];
341 * x0 -> x1;
342 * x0 -> x2;
343 * x1 -> x3;
344 * x1 -> x4;
345 * x2 -> x4;
346 * }
347 * \enddot
348 * The probability distribution of a Bayesian network factorizes as:
349 * \f[ P(\mathbf{x}) = \prod_{i\in\mathcal{V}} P(x_i \,|\, x_{\mathrm{pa}(i)}) \f]
350 * where \f$\mathrm{pa}(i)\f$ are the parents of node \a i in a DAG.
351 *
352 * The same probability distribution can be represented as a Markov random field:
353 * \dot
354 * graph mrf {
355 * size="1.5,1.5";
356 * x0 [label="0"];
357 * x1 [label="1"];
358 * x2 [label="2"];
359 * x3 [label="3"];
360 * x4 [label="4"];
361 * x0 -- x1;
362 * x0 -- x2;
363 * x1 -- x2;
364 * x1 -- x3;
365 * x1 -- x4;
366 * x2 -- x4;
367 * }
368 * \enddot
369 *
370 * The probability distribution of a Markov random field factorizes as:
371 * \f[ P(\mathbf{x}) = \frac{1}{Z} \prod_{C\in\mathcal{C}} \psi_C(x_C) \f]
372 * where \f$ \mathcal{C} \f$ are the cliques of an undirected graph,
373 * \f$ \psi_C(x_C) \f$ are "potentials" or "compatibility functions", and
374 * \f$ Z \f$ is the partition sum which properly normalizes the probability
375 * distribution.
376 *
377 * Finally, the same probability distribution can be represented as a factor graph:
378 * \dot
379 * graph factorgraph {
380 * size="1.8,1";
381 * x0 [label="0"];
382 * x1 [label="1"];
383 * x2 [label="2"];
384 * x3 [label="3"];
385 * x4 [label="4"];
386 * f01 [shape="box",label=""];
387 * f02 [shape="box",label=""];
388 * f13 [shape="box",label=""];
389 * f124 [shape="box",label=""];
390 * x0 -- f01;
391 * x1 -- f01;
392 * x0 -- f02;
393 * x2 -- f02;
394 * x1 -- f13;
395 * x3 -- f13;
396 * x1 -- f124;
397 * x2 -- f124;
398 * x4 -- f124;
399 * }
400 * \enddot
401 *
402 * The probability distribution of a factor graph factorizes as:
403 * \f[ P(\mathbf{x}) = \frac{1}{Z} \prod_{I\in \mathcal{F}} f_I(x_I) \f]
404 * where \f$ \mathcal{F} \f$ are the factor nodes of a factor graph (a
405 * bipartite graph consisting of variable nodes and factor nodes),
406 * \f$ f_I(x_I) \f$ are the factors, and \f$ Z \f$ is the partition sum
407 * which properly normalizes the probability distribution.
408 *
409 * Looking at the expressions for the joint probability distributions,
410 * it is obvious that Bayesian networks and Markov random fields can
411 * both be easily represented as factor graphs. Factor graphs most
412 * naturally express the factorization structure of a probability
413 * distribution, and hence are a convenient representation for approximate
414 * inference algorithms, which all try to exploit this factorization.
415 * This is why libDAI uses a factor graph as representation of a
416 * graphical model, implemented in the dai::FactorGraph class.
417 *
418 * \section inference-inference Inference tasks
419 *
420 * Given a factor graph, specified by the variable nodes \f$\{x_i\}_{i\in\mathcal{V}}\f$
421 * the factor nodes \f$ \mathcal{F} \f$, the graph structure, and the factors
422 * \f$\{f_I(x_I)\}_{I\in\mathcal{F}}\f$, the following tasks are important:
423 *
424 * - Calculating the partition sum:
425 * \f[ Z = \sum_{\mathbf{x}_{\mathcal{V}}} \prod_{I \in \mathcal{F}} f_I(x_I) \f]
426 * - Calculating the marginal distribution of a subset of variables
427 * \f$\{x_i\}_{i\in A}\f$:
428 * \f[ P(\mathbf{x}_{A}) = \frac{1}{Z} \sum_{\mathbf{x}_{\mathcal{V}\setminus A}} \prod_{I \in \mathcal{F}} f_I(x_I) \f]
429 * - Calculating the MAP state which has the maximum probability mass:
430 * \f[ \mathrm{argmax}_{\mathbf{x}}\,\prod_{I\in\mathcal{F}} f_I(x_I) \f]
431 *
432 * libDAI offers several inference algorithms, which solve (a subset of) these tasks either
433 * approximately or exactly, for factor graphs with discrete variables. The following
434 * algorithms are implemented:
435 *
436 * Exact inference:
437 * - Brute force enumeration: dai::ExactInf
438 * - Junction-tree method: dai::JTree
439 *
440 * Approximate inference:
441 * - Mean Field: dai::MF
442 * - (Loopy) Belief Propagation: dai::BP [\ref KFL01]
443 * - Fractional Belief Propagation: dai::FBP [\ref WiH03]
444 * - Tree-Reweighted Belief Propagation: dai::TRWBP [\ref WJW03]
445 * - Tree Expectation Propagation: dai::TreeEP [\ref MiQ04]
446 * - Generalized Belief Propagation: dai::HAK [\ref YFW05]
447 * - Double-loop GBP: dai::HAK [\ref HAK03]
448 * - Loop Corrected Belief Propagation: dai::MR [\ref MoR05] and dai::LC [\ref MoK07]
449 * - Gibbs sampling: dai::Gibbs
450 * - Clamped Belief Propagation: dai::CBP [\ref EaG09]
451 *
452 * Not all inference tasks are implemented by each method: calculating MAP states
453 * is only possible with dai::JTree and dai::BP, calculating partition sums is
454 * not possible with dai::MR, dai::LC and dai::Gibbs.
455 *
456 * \section inference-learning Parameter learning
457 *
458 * In addition, libDAI supports parameter learning of conditional probability
459 * tables by Expectation Maximization (or Maximum Likelihood, if there is no
460 * missing data). This is implemented in dai::EMAlg.
461 *
462 */
463
464
465 /** \page fileformats libDAI file formats
466 *
467 * \section fileformats-factorgraph Factor graph (.fg) file format
468 *
469 * This section describes the .fg file format used in libDAI to store factor graphs.
470 * Markov Random Fields are special cases of factor graphs, as are Bayesian
471 * networks. A factor graph can be specified as follows: for each factor, one has
472 * to specify which variables occur in the factor, what their respective
473 * cardinalities (i.e., number of possible values) are, and a table listing all
474 * the values of that factor for all possible configurations of these variables.
475 *
476 * A .fg file is not much more than that. It starts with a line containing the
477 * number of factors in that graph, followed by an empty line. Then all factors
478 * are specified, using one block for each factor, where the blocks are seperated
479 * by empty lines. Each variable occurring in the factor graph has a unique
480 * identifier, its label (which should be a nonnegative integer). Comment lines
481 * which start with # are ignored.
482 *
483 * \subsection fileformats-factorgraph-factor Factor block format
484 *
485 * Each block describing a factor starts with a line containing the number of
486 * variables in that factor. The second line contains the labels of these
487 * variables, seperated by spaces (labels are nonnegative integers and to avoid
488 * confusion, it is suggested to start counting at 0). The third line contains
489 * the number of possible values of each of these variables, also seperated by
490 * spaces. Note that there is some redundancy here, since if a variable appears
491 * in more than one factor, the cardinality of that variable appears several
492 * times in the .fg file; obviously, these cardinalities should be consistent.
493 * The fourth line contains the number of nonzero entries
494 * in the factor table. The rest of the lines contain these nonzero entries;
495 * each line consists of a table index, followed by white-space, followed by the
496 * value corresponding to that table index. The most difficult part is getting
497 * the indexing right. The convention that is used is that the left-most
498 * variables cycle through their values the fastest (similar to MatLab indexing
499 * of multidimensional arrays).
500 *
501 * \subsubsection fileformats-factorgraph-factor-example Example
502 *
503 * An example block describing one factor is:
504 *
505 * <pre>
506 * 3
507 * 4 8 7
508 * 3 2 2
509 * 11
510 * 0 0.1
511 * 1 3.5
512 * 2 2.8
513 * 3 6.3
514 * 4 8.4
515 * 6 7.4
516 * 7 2.4
517 * 8 8.9
518 * 9 1.3
519 * 10 1.6
520 * 12 6.4
521 * 11 2.6
522 * </pre>
523 *
524 * which corresponds to the following factor:
525 *
526 * \f[
527 * \begin{array}{ccc|c}
528 * x_4 & x_8 & x_7 & \mbox{value}\\
529 * \hline
530 * 0 & 0 & 0 & 0.1\\
531 * 1 & 0 & 0 & 3.5\\
532 * 2 & 0 & 0 & 2.8\\
533 * 0 & 1 & 0 & 6.3\\
534 * 1 & 1 & 0 & 8.4\\
535 * 2 & 1 & 0 & 0.0\\
536 * 0 & 0 & 1 & 7.4\\
537 * 1 & 0 & 1 & 2.4\\
538 * 2 & 0 & 1 & 8.9\\
539 * 0 & 1 & 1 & 1.3\\
540 * 1 & 1 & 1 & 1.6\\
541 * 2 & 1 & 1 & 2.6
542 * \end{array}
543 * \f]
544 *
545 * Note that the value of \f$x_4\f$ changes fastest, followed by that of \f$x_8\f$, and \f$x_7\f$
546 * varies the slowest, corresponding to the second line of the block ("4 8 7").
547 * Further, \f$x_4\f$ can take on three values, and \f$x_8\f$ and \f$x_7\f$ each have two possible
548 * values, as described in the third line of the block ("3 2 2"). The table
549 * contains 11 non-zero entries (all except for the fifth entry). Note that the
550 * eleventh and twelveth entries are interchanged.
551 *
552 * A final note: the internal representation in libDAI of the factor above is
553 * different, because the variables are ordered according to their indices
554 * (i.e., the ordering would be \f$x_4 x_7 x_8\f$) and the values of the table are
555 * stored accordingly, with the variable having the smallest index changing
556 * fastest:
557 *
558 * \f[
559 * \begin{array}{ccc|c}
560 * x_4 & x_7 & x_8 & \mbox{value}\\
561 * \hline
562 * 0 & 0 & 0 & 0.1\\
563 * 1 & 0 & 0 & 3.5\\
564 * 2 & 0 & 0 & 2.8\\
565 * 0 & 1 & 0 & 7.4\\
566 * 1 & 1 & 0 & 2.4\\
567 * 2 & 1 & 0 & 8.9\\
568 * 0 & 0 & 1 & 6.3\\
569 * 1 & 0 & 1 & 8.4\\
570 * 2 & 0 & 1 & 0.0\\
571 * 0 & 1 & 1 & 1.3\\
572 * 1 & 1 & 1 & 1.6\\
573 * 2 & 1 & 1 & 2.6
574 * \end{array}
575 * \f]
576 *
577 *
578 * \section fileformats-evidence Evidence (.tab) file format
579 *
580 * This section describes the .tab fileformat used in libDAI to store "evidence",
581 * i.e., a data set consisting of multiple samples, where each sample is the
582 * observed joint state of some variables.
583 *
584 * A .tab file is a tabular data file, consisting of a header line, followed by
585 * an empty line, followed by the data points, with one line for each data point.
586 * Each line (apart from the empty one) should have the same number of columns,
587 * where columns are separated by one tab character. Each column corresponds to
588 * a variable. The header line consists of the variable labels (corresponding to
589 * dai::Var::label()). The other lines are observed joint states of the variables, i.e.,
590 * each line corresponds to a joint observation of the variables, and each column
591 * of a line contains the state of the variable associated with that column.
592 * Missing data is handled simply by having two consecutive tab characters,
593 * without any characters in between.
594 *
595 * \subsection fileformats-evidence-example Example
596 *
597 * <pre>
598 * 1 3 2
599 *
600 * 0 0 1
601 * 1 0 1
602 * 1 1
603 * </pre>
604 *
605 * This would correspond to a data set consisting of three observations concerning
606 * the variables with labels 1, 3 and 2; the first observation being
607 * \f$x_1 = 0, x_3 = 0, x_2 = 1\f$, the second observation being
608 * \f$x_1 = 1, x_3 = 0, x_2 = 1\f$, and the third observation being
609 * \f$x_1 = 1, x_2 = 1\f$ (where the state of \f$x_3\f$ is missing).
610 *
611 * \section fileformats-emalg Expectation Maximization (.em) file format
612 *
613 * This section describes the file format of .em files, which are used
614 * to specify a particular EM algorithm. The .em files are complementary
615 * to .fg files; in other words, an .em file without a corresponding .fg
616 * file is useless. Furthermore, one also needs a corresponding .tab file
617 * containing the data used for parameter learning.
618 *
619 * An .em file starts with a line specifying the number of maximization steps,
620 * followed by an empty line. Then, each maximization step is described in a
621 * block, which should satisfy the format described in the next subsection.
622 *
623 * \subsection fileformats-emalg-maximizationstep Maximization Step block format
624 *
625 * A maximization step block of an .em file starts with a single line
626 * describing the number of shared parameters blocks that will follow.
627 * Then, each shared parameters block follows, in the format described in
628 * the next subsection.
629 *
630 * \subsection fileformats-emalg-sharedparameters Shared parameters block format
631 *
632 * A shared parameters block of an .em file starts with a single line
633 * consisting of the name of a ParameterEstimation subclass
634 * and its parameters in the format of a PropertySet. For example:
635 * <pre> CondProbEstimation [target_dim=2,total_dim=4,pseudo_count=1]</pre>
636 * The next line contains the number of factors that share their parameters.
637 * Then, each of these factors is specified on separate lines (possibly
638 * seperated by empty lines), where each line consists of several fields
639 * seperated by a space or a tab character. The first field contains
640 * the index of the factor in the factor graph. The following fields should
641 * contain the variable labels of the variables on which that factor depends,
642 * in a specific ordering. This ordering can be different from the canonical
643 * ordering of the variables used internally in libDAI (which would be sorted
644 * ascendingly according to the variable labels). The ordering of the variables
645 * specifies the implicit ordering of the shared parameters: when iterating
646 * over all shared parameters, the corresponding index of the first variable
647 * changes fastest (in the inner loop), and the corresponding index of the
648 * last variable changes slowest (in the outer loop). By choosing the right
649 * ordering, it is possible to let different factors (depending on different
650 * variables) share parameters in parameter learning using EM. This convention
651 * is similar to the convention used in factor blocks in a factor graph .fg
652 * file (see \ref fileformats-factorgraph-factor).
653 *
654 * \section fileformats-aliases Aliases file format
655 *
656 * An aliases file is basically a list of "macros" and the strings that they
657 * should be substituted with.
658 *
659 * Each line of the aliases file can be either empty, contain a comment
660 * (if the first character is a '#') or contain an alias. In the latter case,
661 * the line should contain a colon; the part before the colon contains the
662 * name of the alias, the part after the colon the string that it should be
663 * substituted with. Any whitespace before and after the colon is ignored.
664 *
665 * For example, the following line would define the alias \c BP_SEQFIX
666 * as a shorthand for "BP[updates=SEQFIX,tol=1e-9,maxiter=10000,logdomain=0]":
667 * <pre>
668 * BP_SEQFIX: BP[updates=SEQFIX,tol=1e-9,maxiter=10000,logdomain=0]
669 * </pre>
670 *
671 * Aliases files can be used to store default options for algorithms.
672 */
673
674 /** \page bibliography Bibliography
675 * \anchor EaG09 \ref EaG09
676 * F. Eaton and Z. Ghahramani (2009):
677 * "Choosing a Variable to Clamp",
678 * <em>Proceedings of the Twelfth International Conference on Artificial Intelligence and Statistics (AISTATS 2009)</em> 5:145-152,
679 * http://jmlr.csail.mit.edu/proceedings/papers/v5/eaton09a/eaton09a.pdf
680 *
681 * \anchor EMK06 \ref EMK06
682 * G. Elidan and I. McGraw and D. Koller (2006):
683 * "Residual Belief Propagation: Informed Scheduling for Asynchronous Message Passing",
684 * <em>Proceedings of the 22nd Annual Conference on Uncertainty in Artificial Intelligence (UAI-06)</em>,
685 * http://uai.sis.pitt.edu/papers/06/UAI2006_0091.pdf
686 *
687 * \anchor HAK03 \ref HAK03
688 * T. Heskes and C. A. Albers and H. J. Kappen (2003):
689 * "Approximate Inference and Constrained Optimization",
690 * <em>Proceedings of the 19th Annual Conference on Uncertainty in Artificial Intelligence (UAI-03)</em> pp. 313-320,
691 * http://www.snn.ru.nl/reports/Heskes.uai2003.ps.gz
692 *
693 * \anchor KFL01 \ref KFL01
694 * F. R. Kschischang and B. J. Frey and H.-A. Loeliger (2001):
695 * "Factor Graphs and the Sum-Product Algorithm",
696 * <em>IEEE Transactions on Information Theory</em> 47(2):498-519,
697 * http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=910572
698 *
699 * \anchor KoF09 \ref KoF09
700 * D. Koller and N. Friedman (2009):
701 * <em>Probabilistic Graphical Models - Principles and Techniques</em>,
702 * The MIT Press, Cambridge, Massachusetts, London, England.
703
704 * \anchor Min05 \ref Min05
705 * T. Minka (2005):
706 * "Divergence measures and message passing",
707 * <em>MicroSoft Research Technical Report</em> MSR-TR-2005-173,
708 * http://research.microsoft.com/en-us/um/people/minka/papers/message-passing/minka-divergence.pdf
709 *
710 * \anchor MiQ04 \ref MiQ04
711 * T. Minka and Y. Qi (2004):
712 * "Tree-structured Approximations by Expectation Propagation",
713 * <em>Advances in Neural Information Processing Systems</em> (NIPS) 16,
714 * http://books.nips.cc/papers/files/nips16/NIPS2003_AA25.pdf
715 *
716 * \anchor MoK07 \ref MoK07
717 * J. M. Mooij and H. J. Kappen (2007):
718 * "Loop Corrections for Approximate Inference on Factor Graphs",
719 * <em>Journal of Machine Learning Research</em> 8:1113-1143,
720 * http://www.jmlr.org/papers/volume8/mooij07a/mooij07a.pdf
721 *
722 * \anchor MoK07b \ref MoK07b
723 * J. M. Mooij and H. J. Kappen (2007):
724 * "Sufficient Conditions for Convergence of the Sum-Product Algorithm",
725 * <em>IEEE Transactions on Information Theory</em> 53(12):4422-4437,
726 * http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=4385778
727 *
728 * \anchor MoR05 \ref MoR05
729 * A. Montanari and T. Rizzo (2005):
730 * "How to Compute Loop Corrections to the Bethe Approximation",
731 * <em>Journal of Statistical Mechanics: Theory and Experiment</em> 2005(10)-P10011,
732 * http://stacks.iop.org/1742-5468/2005/P10011
733 *
734 * \anchor StW99 \ref StW99
735 * A. Steger and N. C. Wormald (1999):
736 * "Generating Random Regular Graphs Quickly",
737 * <em>Combinatorics, Probability and Computing</em> Vol 8, Issue 4, pp. 377-396,
738 * http://www.math.uwaterloo.ca/~nwormald/papers/randgen.pdf
739 *
740 * \anchor WiH03 \ref WiH03
741 * W. Wiegerinck and T. Heskes (2003):
742 * "Fractional Belief Propagation",
743 * <em>Advances in Neural Information Processing Systems</em> (NIPS) 15, pp. 438-445,
744 * http://books.nips.cc/papers/files/nips15/LT16.pdf
745 *
746 * \anchor WJW03 \ref WJW03
747 * M. J. Wainwright, T. S. Jaakkola and A. S. Willsky (2003):
748 * "Tree-reweighted belief propagation algorithms and approximate ML estimation by pseudo-moment matching",
749 * <em>9th Workshop on Artificial Intelligence and Statistics</em>,
750 * http://www.eecs.berkeley.edu/~wainwrig/Papers/WJW_AIStat03.pdf
751 *
752 * \anchor YFW05 \ref YFW05
753 * J. S. Yedidia and W. T. Freeman and Y. Weiss (2005):
754 * "Constructing Free-Energy Approximations and Generalized Belief Propagation Algorithms",
755 * <em>IEEE Transactions on Information Theory</em> 51(7):2282-2312,
756 * http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=1459044
757 */
758
759
760 /** \page discussion Ideas not worth exploring
761 * \section discuss_extendedgraphs Extended factorgraphs/regiongraphs
762 *
763 * A FactorGraph and a RegionGraph are often equipped with
764 * additional properties for nodes and edges. The code to initialize those
765 * is often quite similar. Maybe one could abstract this, e.g.:
766 * \code
767 * template <typename Node1Properties, typename Node2Properties, typename EdgeProperties>
768 * class ExtFactorGraph : public FactorGraph {
769 * public:
770 * std::vector<Node1Properties> node1Props;
771 * std::vector<Node2Properties> node2Props;
772 * std::vector<std::vector<EdgeProperties> > edgeProps;
773 * // ...
774 * }
775 * \endcode
776 *
777 * Advantages:
778 * - Less code duplication.
779 * - Easier maintainability.
780 * - Easier to write new inference algorithms.
781 *
782 * Disadvantages:
783 * - Cachability may be worse.
784 * - A problem is the case where there are no properties for either type of nodes or for edges.
785 * Maybe this can be solved using specializations, or using variadac template arguments?
786 * Another possible solution would be to define a "class Empty {}", and add some code
787 * that checks for the typeid, comparing it with Empty, and doing something special in that case
788 * (e.g., not allocating memory).
789 * - The main disadvantage of this approach seems to be that it leads to even more entanglement.
790 * Therefore this is probably a bad idea.
791 *
792 * \section discuss_templates Polymorphism by template parameterization
793 *
794 * Instead of polymorphism by inheritance, use polymorphism by template parameterization.
795 * For example, the real reason for introducing the complicated inheritance scheme of dai::InfAlg
796 * was for functions like dai::calcMarginal. Instead, one could use a template function:
797 * \code
798 * template<typename InfAlg>
799 * Factor calcMarginal( const InfAlg &obj, const VarSet &ns, bool reInit );
800 * \endcode
801 * This would assume that the type InfAlg supports certain methods. Ideally, one would use
802 * concepts to define different classes of inference algorithms with different capabilities,
803 * for example the ability to calculate logZ, the ability to calculate marginals, the ability to
804 * calculate bounds, the ability to calculate MAP states, etc. Then, one would use traits
805 * classes in order to be able to query the capabilities of the model. For example, one would be
806 * able to query whether the inference algorithm supports calculation of logZ. Unfortunately,
807 * this is compile-time polymorphism, whereas tests/testdai needs runtime polymorphism.
808 * Therefore this is probably a bad idea.
809 */