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