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