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