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