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