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