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 */

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 **/

42 /** \mainpage Reference manual for libDAI - A free/open source C++ library for Discrete Approximate Inference methods

43 * \author Joris Mooij, Frederik Eaton

44 * \version git HEAD

45 * \date February 11, 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 */

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 */

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 *

177 * Joris M. Mooij et al. (2010) "libDAI 0.2.4: A free/open source C++ library for Discrete

178 * Approximate Inference", http://www.libdai.org

179 *

180 * or in BiBTeX format:

181 *

182 * <pre>

183 * \@misc{mooij2010libdai,

184 * author = "Joris M. Mooij et al.",

185 * title = "lib{DAI} 0.2.4: A free/open source {C}++ library for {D}iscrete {A}pproximate {I}nference",

186 * howpublished = "http://www.libdai.org/",

187 * year = 2010

188 * }

189 * </pre>

190 *

191 * Moreover, as a personal note, I would appreciate it if you would email

192 * (citations of) papers referencing this work to joris dot mooij at libdai dot org.

193 */

196 /** \page authors Authors

197 * \section authors-authors People who contributed to libDAI

198 *

199 * \verbinclude AUTHORS

200 */

203 /** \page build Building libDAI

204 * <hr size="1">

205 * \section build-unix Building libDAI under UNIX variants (Linux / Cygwin / Mac OS X)

206 *

207 * \subsection build-unix-preparations Preparations

208 *

209 * You need:

210 * - a recent version of gcc (at least version 3.4)

211 * - GNU make

212 * - doxygen

213 * - graphviz

214 * - recent boost C++ libraries (at least version 1.34 if you have

215 * a recent version of GCC, otherwise at least version 1.37; however,

216 * version 1.37 shipped with Ubuntu 9.04 is known not to work)

217 *

218 * On Debian/Ubuntu, you can easily install all these packages with a single command:

219 * <pre> apt-get install g++ make doxygen graphviz libboost-dev libboost-graph-dev libboost-program-options-dev</pre>

220 * (root permissions needed).

221 *

222 * On Mac OS X (10.4 is known to work), these packages can be installed easily via MacPorts.

223 * If MacPorts is not already installed, install it according to the instructions at http://www.macports.org/.

224 * Then, a simple

225 * <pre> sudo port install gmake boost doxygen graphviz</pre>

226 * should be enough to install everything that is needed.

227 *

228 * On Cygwin, the prebuilt Cygwin package boost-1.33.1-x is known not to work.

229 * You can however obtain the latest boost version (you need at least 1.37.0)

230 * from http://www.boost.org/ and compile/install it with:

231 *

232 * <pre> ./configure

233 * make

234 * make install

235 * </pre>

236 *

237 * \subsection build-unix-libdai Building libDAI

238 *

239 * To build the libDAI source, first copy a template Makefile.* to Makefile.conf

240 * (for example, copy Makefile.LINUX to Makefile.conf if you use GNU/Linux).

241 * Then, edit the Makefile.conf template to adapt it to your local setup.

242 * Especially directories may differ from system to system. Platform independent

243 * build options can be set in Makefile.ALL. Finally, run

244 * <pre> make</pre>

245 * The build includes a regression test, which may take a while to complete.

246 *

247 * If the build is successful, you can test the example program:

248 * <pre> examples/example tests/alarm.fg</pre>

249 * or the more extensive test program:

250 * <pre> tests/testdai --aliases tests/aliases.conf --filename tests/alarm.fg --methods JTREE_HUGIN BP_SEQMAX</pre>

251 *

252 *

253 * <hr size="1">

254 * \section build-windows Building libDAI under Windows

255 *

256 * \subsection build-windows-preparations Preparations

257 *

258 * You need:

259 * - A recent version of MicroSoft Visual Studio (2008 is known to work)

260 * - recent boost C++ libraries (version 1.37 or higher)

261 * - GNU make (can be obtained from http://gnuwin32.sourceforge.net)

262 *

263 * For the regression test, you need:

264 * - GNU diff, GNU sed (can be obtained from http://gnuwin32.sourceforge.net)

265 *

266 * \subsection build-windows-libdai Building libDAI

267 *

268 * To build the source, copy Makefile.WINDOWS to Makefile.conf. Then, edit

269 * Makefile.conf to adapt it to your local setup. Platform independent

270 * build options can be set in Makefile.ALL. Finally, run (from the command line)

271 * <pre> make</pre>

272 * The build includes a regression test, which may take a while to complete.

273 *

274 * If the build is successful, you can test the example program:

275 * <pre> examples\\example tests\\alarm.fg</pre>

276 * or the more extensive test program:

277 * <pre> tests\\testdai --aliases tests\\aliases.conf --filename tests\\alarm.fg --methods JTREE_HUGIN BP_SEQMAX</pre>

278 *

279 *

280 * <hr size="1">

281 * \section build-matlab Building the libDAI MatLab interface

282 *

283 * You need:

284 * - MatLab

285 * - The platform-dependent requirements described above

286 *

287 * First, you need to build the libDAI source as described above for your

288 * platform. By default, the MatLab interface is disabled, so before compiling the

289 * source, you have to enable it in Makefile.ALL by setting

290 * <pre> WITH_MATLAB=true</pre>

291 * Also, you have to configure the MatLab-specific parts of

292 * Makefile.conf to match your system (in particular, the Makefile variables ME,

293 * MATLABDIR and MEX). The MEX file extension depends on your platform; for a

294 * 64-bit linux x86_64 system this would be "ME=.mexa64", for a 32-bit linux x86

295 * system "ME=.mexglx". If you are unsure about your MEX file

296 * extension: it needs to be the same as what the MatLab command "mexext" returns.

297 * The required MEX files are built by issuing

298 * <pre> make</pre>

299 * from the command line. The MatLab interface is much less powerful than using

300 * libDAI from C++. There are two reasons for this: (i) it is boring to write MEX

301 * files; (ii) the large performance penalty paid when large data structures (like

302 * factor graphs) have to be converted between their native C++ data structure to

303 * something that MatLab understands.

304 *

305 * A simple example of how to use the MatLab interface is the following (entered

306 * at the MatLab prompt), which performs exact inference by the junction tree

307 * algorithm and approximate inference by belief propagation on the ALARM network:

308 * <pre> cd path_to_libdai/matlab

309 * [psi] = dai_readfg ('../examples/alarm.fg');

310 * [logZ,q,md,qv,qf] = dai (psi, 'JTREE', '[updates=HUGIN,verbose=0]')

311 * [logZ,q,md,qv,qf] = dai (psi, 'BP', '[updates=SEQMAX,tol=1e-9,maxiter=10000,logdomain=0]')</pre>

312 * where "path_to_libdai" has to be replaced with the directory in which libDAI

313 * was installed. For other algorithms and some default parameters, see the file

314 * tests/aliases.conf.

315 *

316 * <hr size="1">

317 * \section build-doxygen Building the documentation

318 *

319 * Install doxygen, graphviz and a TeX distribution and use

320 * <pre> make doc</pre>

321 * to build the documentation. If the documentation is not clear enough, feel free

322 * to send me an email (or even better, to improve the documentation and send a patch!).

323 * The documentation can also be browsed online at http://www.libdai.org.

324 */

327 /** \page changelog Change Log

328 * \verbinclude ChangeLog

329 */

332 /** \page terminology Terminology and conventions

333 *

334 * \section terminology-graphicalmodels Graphical models

335 *

336 * Commonly used graphical models are Bayesian networks and Markov random fields.

337 * In libDAI, both types of graphical models are represented by a slightly more

338 * general type of graphical model: a factor graph [\ref KFL01].

339 *

340 * An example of a Bayesian network is:

341 * \dot

342 * digraph bayesnet {

343 * size="1,1";

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 -> x3;

352 * x1 -> x4;

353 * x2 -> x4;

354 * }

355 * \enddot

356 * The probability distribution of a Bayesian network factorizes as:

357 * \f[ P(\mathbf{x}) = \prod_{i\in\mathcal{V}} P(x_i \,|\, x_{\mathrm{pa}(i)}) \f]

358 * where \f$\mathrm{pa}(i)\f$ are the parents of node \a i in a DAG.

359 *

360 * The same probability distribution can be represented as a Markov random field:

361 * \dot

362 * graph mrf {

363 * size="1.5,1.5";

364 * x0 [label="0"];

365 * x1 [label="1"];

366 * x2 [label="2"];

367 * x3 [label="3"];

368 * x4 [label="4"];

369 * x0 -- x1;

370 * x0 -- x2;

371 * x1 -- x2;

372 * x1 -- x3;

373 * x1 -- x4;

374 * x2 -- x4;

375 * }

376 * \enddot

377 *

378 * The probability distribution of a Markov random field factorizes as:

379 * \f[ P(\mathbf{x}) = \frac{1}{Z} \prod_{C\in\mathcal{C}} \psi_C(x_C) \f]

380 * where \f$ \mathcal{C} \f$ are the cliques of an undirected graph,

381 * \f$ \psi_C(x_C) \f$ are "potentials" or "compatibility functions", and

382 * \f$ Z \f$ is the partition sum which properly normalizes the probability

383 * distribution.

384 *

385 * Finally, the same probability distribution can be represented as a factor graph:

386 * \dot

387 * graph factorgraph {

388 * size="1.8,1";

389 * x0 [label="0"];

390 * x1 [label="1"];

391 * x2 [label="2"];

392 * x3 [label="3"];

393 * x4 [label="4"];

394 * f01 [shape="box",label=""];

395 * f02 [shape="box",label=""];

396 * f13 [shape="box",label=""];

397 * f124 [shape="box",label=""];

398 * x0 -- f01;

399 * x1 -- f01;

400 * x0 -- f02;

401 * x2 -- f02;

402 * x1 -- f13;

403 * x3 -- f13;

404 * x1 -- f124;

405 * x2 -- f124;

406 * x4 -- f124;

407 * }

408 * \enddot

409 *

410 * The probability distribution of a factor graph factorizes as:

411 * \f[ P(\mathbf{x}) = \frac{1}{Z} \prod_{I\in \mathcal{F}} f_I(x_I) \f]

412 * where \f$ \mathcal{F} \f$ are the factor nodes of a factor graph (a

413 * bipartite graph consisting of variable nodes and factor nodes),

414 * \f$ f_I(x_I) \f$ are the factors, and \f$ Z \f$ is the partition sum

415 * which properly normalizes the probability distribution.

416 *

417 * Looking at the expressions for the joint probability distributions,

418 * it is obvious that Bayesian networks and Markov random fields can

419 * both be easily represented as factor graphs. Factor graphs most

420 * naturally express the factorization structure of a probability

421 * distribution, and hence are a convenient representation for approximate

422 * inference algorithms, which all try to exploit this factorization.

423 * This is why libDAI uses a factor graph as representation of a

424 * graphical model, implemented in the dai::FactorGraph class.

425 *

426 * \section terminology-inference Inference tasks

427 *

428 * Given a factor graph, specified by the variable nodes \f$\{x_i\}_{i\in\mathcal{V}}\f$

429 * the factor nodes \f$ \mathcal{F} \f$, the graph structure, and the factors

430 * \f$\{f_I(x_I)\}_{I\in\mathcal{F}}\f$, the following tasks are important:

431 *

432 * - Calculating the partition sum:

433 * \f[ Z = \sum_{\mathbf{x}_{\mathcal{V}}} \prod_{I \in \mathcal{F}} f_I(x_I) \f]

434 * - Calculating the marginal distribution of a subset of variables

435 * \f$\{x_i\}_{i\in A}\f$:

436 * \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]

437 * - Calculating the MAP state which has the maximum probability mass:

438 * \f[ \mathrm{argmax}_{\mathbf{x}}\,\prod_{I\in\mathcal{F}} f_I(x_I) \f]

439 *

440 * libDAI offers several inference algorithms, which solve (a subset of) these tasks either

441 * approximately or exactly, for factor graphs with discrete variables. The following

442 * algorithms are implemented:

443 *

444 * Exact inference:

445 * - Brute force enumeration: dai::ExactInf

446 * - Junction-tree method: dai::JTree

447 *

448 * Approximate inference:

449 * - Mean Field: dai::MF

450 * - (Loopy) Belief Propagation: dai::BP [\ref KFL01]

451 * - Fractional Belief Propagation: dai::FBP [\ref WiH03]

452 * - Tree-Reweighted Belief Propagation: dai::TRWBP [\ref WJW03]

453 * - Tree Expectation Propagation: dai::TreeEP [\ref MiQ04]

454 * - Generalized Belief Propagation: dai::HAK [\ref YFW05]

455 * - Double-loop GBP: dai::HAK [\ref HAK03]

456 * - Loop Corrected Belief Propagation: dai::MR [\ref MoR05] and dai::LC [\ref MoK07]

457 * - Gibbs sampling: dai::Gibbs

458 * - Conditioned Belief Propagation: dai::CBP [\ref EaG09]

459 *

460 * Not all inference tasks are implemented by each method: calculating MAP states

461 * is only possible with dai::JTree and dai::BP, calculating partition sums is

462 * not possible with dai::MR, dai::LC and dai::Gibbs.

463 *

464 * \section terminology-learning Parameter learning

465 *

466 * In addition, libDAI supports parameter learning of conditional probability

467 * tables by Expectation Maximization (or Maximum Likelihood, if there is no

468 * missing data). This is implemented in dai::EMAlg.

469 *

470 * \section terminology-variables-states Variables and states

471 *

472 * Linear states are a concept that is used often in libDAI, for example for storing

473 * and accessing factors, which are functions mapping from states of a set of variables

474 * to the real numbers. Internally, a factor is stored as an array, and the array index

475 * of an entry corresponds with the linear state of the set of variables. Below we will

476 * define variables, states and linear states of (sets of) variables.

477 *

478 * \subsection terminology-variables Variables

479 *

480 * Each (random) \a variable has a unique identifier, its \a label (which has

481 * a non-negative integer value). If two variables have the same

482 * label, they are considered as identical. A variable can take on a finite

483 * number of different values or \a states.

484 *

485 * We use the following notational conventions. The discrete

486 * random variable with label \f$l\f$ is denoted as \f$x_l\f$, and the number

487 * of possible values of this variable as \f$S_{x_l}\f$ or simply \f$S_l\f$.

488 * The set of possible values of variable \f$x_l\f$ is denoted

489 * \f$X_l := \{0,1,\dots,S_l-1\}\f$ and called its \a state \a space.

490 *

491 * \subsection terminology-variable-sets Sets of variables and the canonical ordering

492 *

493 * Let \f$A := \{x_{l_1},x_{l_2},\dots,x_{l_n}\}\f$ be a set of variables.

494 *

495 * The \a canonical \a ordering of the variables in \a A is induced by their labels.

496 * That is: if \f$l_1 < l_2\f$, then \f$x_{l_1}\f$ occurs before \f$x_{l_2}\f$ in the

497 * canonical ordering. Below, we will assume that \f$(l_i)_{i=1}^n\f$ is

498 * ordered according to the canonical ordering, i.e., \f$l_1 < l_2 < \dots < l_n\f$.

499 *

500 * \subsection terminology-variable-states States and linear states of sets of variables

501 *

502 * A \a state of the variables in \a A refers to a joint assignment of the

503 * variables, or in other words, to an element of the Cartesian product

504 * \f$ \prod_{i=1}^n X_{l_i}\f$ of the state spaces of the variables in \a A.

505 * Note that a state can also be interpreted as a mapping from variables (or

506 * variable labels) to the natural numbers, which assigns to a variable (or its

507 * label) the corresponding state of the variable.

508 *

509 * A state of \a n variables can be represented as an n-tuple of

510 * non-negative integers: \f$(s_1,s_2,\dots,s_n)\f$ corresponds to the

511 * joint assignment \f$x_{l_1} = s_1, \dots, x_{l_n} = s_n\f$.

512 * Alternatively, a state can be represented compactly as one non-negative integer;

513 * this representation is called a \a linear \a state. The linear state

514 * \a s corresponding to the state \f$(s_1,s_2,\dots,s_n)\f$ would be:

515 * \f[

516 * s := \sum_{i=1}^n s_i \prod_{j=1}^{i-1} S_{l_j}

517 * = 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}}.

518 * \f]

519 *

520 * Vice versa, given a linear state \a s for the variables \a A, the

521 * corresponding state \f$s_i\f$ of the \a i 'th variable \f$x_{l_i}\f$ (according to

522 * the canonical ordering of the variables in \a A) is given by

523 * \f[

524 * 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.

525 * \f]

526 *

527 * Finally, the \a number \a of \a states of the set of variables \a A is simply the

528 * number of different joint assignments of the variables, that is, \f$\prod_{i=1}^n S_{l_i}\f$.

529 */

532 /** \page fileformats libDAI file formats

533 *

534 * \section fileformats-factorgraph Factor graph (.fg) file format

535 *

536 * This section describes the .fg file format used in libDAI to store factor graphs.

537 * Markov Random Fields are special cases of factor graphs, as are Bayesian

538 * networks. A factor graph can be specified as follows: for each factor, one has

539 * to specify which variables occur in the factor, what their respective

540 * cardinalities (i.e., number of possible values) are, and a table listing all

541 * the values of that factor for all possible configurations of these variables.

542 *

543 * A .fg file is not much more than that. It starts with a line containing the

544 * number of factors in that graph, followed by an empty line. Then all factors

545 * are specified, using one block for each factor, where the blocks are seperated

546 * by empty lines. Each variable occurring in the factor graph has a unique

547 * identifier, its label (which should be a nonnegative integer). Comment lines

548 * which start with # are ignored.

549 *

550 * \subsection fileformats-factorgraph-factor Factor block format

551 *

552 * Each block describing a factor starts with a line containing the number of

553 * variables in that factor. The second line contains the labels of these

554 * variables, seperated by spaces (labels are nonnegative integers and to avoid

555 * confusion, it is suggested to start counting at 0). The third line contains

556 * the number of possible values of each of these variables, also seperated by

557 * spaces. Note that there is some redundancy here, since if a variable appears

558 * in more than one factor, the cardinality of that variable appears several

559 * times in the .fg file; obviously, these cardinalities should be consistent.

560 * The fourth line contains the number of nonzero entries

561 * in the factor table. The rest of the lines contain these nonzero entries;

562 * each line consists of a table index, followed by white-space, followed by the

563 * value corresponding to that table index. The most difficult part is getting

564 * the indexing right. The convention that is used is that the left-most

565 * variables cycle through their values the fastest (similar to MatLab indexing

566 * of multidimensional arrays).

567 *

568 * \subsubsection fileformats-factorgraph-factor-example Example

569 *

570 * An example block describing one factor is:

571 *

572 * <pre>

573 * 3

574 * 4 8 7

575 * 3 2 2

576 * 11

577 * 0 0.1

578 * 1 3.5

579 * 2 2.8

580 * 3 6.3

581 * 4 8.4

582 * 6 7.4

583 * 7 2.4

584 * 8 8.9

585 * 9 1.3

586 * 10 1.6

587 * 12 6.4

588 * 11 2.6

589 * </pre>

590 *

591 * which corresponds to the following factor:

592 *

593 * \f[

594 * \begin{array}{ccc|c}

595 * x_4 & x_8 & x_7 & \mbox{value}\\

596 * \hline

597 * 0 & 0 & 0 & 0.1\\

598 * 1 & 0 & 0 & 3.5\\

599 * 2 & 0 & 0 & 2.8\\

600 * 0 & 1 & 0 & 6.3\\

601 * 1 & 1 & 0 & 8.4\\

602 * 2 & 1 & 0 & 0.0\\

603 * 0 & 0 & 1 & 7.4\\

604 * 1 & 0 & 1 & 2.4\\

605 * 2 & 0 & 1 & 8.9\\

606 * 0 & 1 & 1 & 1.3\\

607 * 1 & 1 & 1 & 1.6\\

608 * 2 & 1 & 1 & 2.6

609 * \end{array}

610 * \f]

611 *

612 * Note that the value of \f$x_4\f$ changes fastest, followed by that of \f$x_8\f$, and \f$x_7\f$

613 * varies the slowest, corresponding to the second line of the block ("4 8 7").

614 * Further, \f$x_4\f$ can take on three values, and \f$x_8\f$ and \f$x_7\f$ each have two possible

615 * values, as described in the third line of the block ("3 2 2"). The table

616 * contains 11 non-zero entries (all except for the fifth entry). Note that the

617 * eleventh and twelveth entries are interchanged.

618 *

619 * A final note: the internal representation in libDAI of the factor above is

620 * different, because the variables are ordered according to their indices

621 * (i.e., the ordering would be \f$x_4 x_7 x_8\f$) and the values of the table are

622 * stored accordingly, with the variable having the smallest index changing

623 * fastest:

624 *

625 * \f[

626 * \begin{array}{ccc|c}

627 * x_4 & x_7 & x_8 & \mbox{value}\\

628 * \hline

629 * 0 & 0 & 0 & 0.1\\

630 * 1 & 0 & 0 & 3.5\\

631 * 2 & 0 & 0 & 2.8\\

632 * 0 & 1 & 0 & 7.4\\

633 * 1 & 1 & 0 & 2.4\\

634 * 2 & 1 & 0 & 8.9\\

635 * 0 & 0 & 1 & 6.3\\

636 * 1 & 0 & 1 & 8.4\\

637 * 2 & 0 & 1 & 0.0\\

638 * 0 & 1 & 1 & 1.3\\

639 * 1 & 1 & 1 & 1.6\\

640 * 2 & 1 & 1 & 2.6

641 * \end{array}

642 * \f]

643 *

644 *

645 * \section fileformats-evidence Evidence (.tab) file format

646 *

647 * This section describes the .tab fileformat used in libDAI to store "evidence",

648 * i.e., a data set consisting of multiple samples, where each sample is the

649 * observed joint state of some variables.

650 *

651 * A .tab file is a tabular data file, consisting of a header line, followed by

652 * an empty line, followed by the data points, with one line for each data point.

653 * Each line (apart from the empty one) should have the same number of columns,

654 * where columns are separated by one tab character. Each column corresponds to

655 * a variable. The header line consists of the variable labels (corresponding to

656 * dai::Var::label()). The other lines are observed joint states of the variables, i.e.,

657 * each line corresponds to a joint observation of the variables, and each column

658 * of a line contains the state of the variable associated with that column.

659 * Missing data is handled simply by having two consecutive tab characters,

660 * without any characters in between.

661 *

662 * \subsection fileformats-evidence-example Example

663 *

664 * <pre>

665 * 1 3 2

666 *

667 * 0 0 1

668 * 1 0 1

669 * 1 1

670 * </pre>

671 *

672 * This would correspond to a data set consisting of three observations concerning

673 * the variables with labels 1, 3 and 2; the first observation being

674 * \f$x_1 = 0, x_3 = 0, x_2 = 1\f$, the second observation being

675 * \f$x_1 = 1, x_3 = 0, x_2 = 1\f$, and the third observation being

676 * \f$x_1 = 1, x_2 = 1\f$ (where the state of \f$x_3\f$ is missing).

677 *

678 * \section fileformats-emalg Expectation Maximization (.em) file format

679 *

680 * This section describes the file format of .em files, which are used

681 * to specify a particular EM algorithm. The .em files are complementary

682 * to .fg files; in other words, an .em file without a corresponding .fg

683 * file is useless. Furthermore, one also needs a corresponding .tab file

684 * containing the data used for parameter learning.

685 *

686 * An .em file starts with a line specifying the number of maximization steps,

687 * followed by an empty line. Then, each maximization step is described in a

688 * block, which should satisfy the format described in the next subsection.

689 *

690 * \subsection fileformats-emalg-maximizationstep Maximization Step block format

691 *

692 * A maximization step block of an .em file starts with a single line

693 * describing the number of shared parameters blocks that will follow.

694 * Then, each shared parameters block follows, in the format described in

695 * the next subsection.

696 *

697 * \subsection fileformats-emalg-sharedparameters Shared parameters block format

698 *

699 * A shared parameters block of an .em file starts with a single line

700 * consisting of the name of a ParameterEstimation subclass

701 * and its parameters in the format of a PropertySet. For example:

702 * <pre> CondProbEstimation [target_dim=2,total_dim=4,pseudo_count=1]</pre>

703 * The next line contains the number of factors that share their parameters.

704 * Then, each of these factors is specified on separate lines (possibly

705 * seperated by empty lines), where each line consists of several fields

706 * seperated by a space or a tab character. The first field contains

707 * the index of the factor in the factor graph. The following fields should

708 * contain the variable labels of the variables on which that factor depends,

709 * in a specific ordering. This ordering can be different from the canonical

710 * ordering of the variables used internally in libDAI (which would be sorted

711 * ascendingly according to the variable labels). The ordering of the variables

712 * specifies the implicit ordering of the shared parameters: when iterating

713 * over all shared parameters, the corresponding index of the first variable

714 * changes fastest (in the inner loop), and the corresponding index of the

715 * last variable changes slowest (in the outer loop). By choosing the right

716 * ordering, it is possible to let different factors (depending on different

717 * variables) share parameters in parameter learning using EM. This convention

718 * is similar to the convention used in factor blocks in a factor graph .fg

719 * file (see \ref fileformats-factorgraph-factor).

720 *

721 * \section fileformats-aliases Aliases file format

722 *

723 * An aliases file is basically a list of "macros" and the strings that they

724 * should be substituted with.

725 *

726 * Each line of the aliases file can be either empty, contain a comment

727 * (if the first character is a '#') or contain an alias. In the latter case,

728 * the line should contain a colon; the part before the colon contains the

729 * name of the alias, the part after the colon the string that it should be

730 * substituted with. Any whitespace before and after the colon is ignored.

731 *

732 * For example, the following line would define the alias \c BP_SEQFIX

733 * as a shorthand for "BP[updates=SEQFIX,tol=1e-9,maxiter=10000,logdomain=0]":

734 * <pre>

735 * BP_SEQFIX: BP[updates=SEQFIX,tol=1e-9,maxiter=10000,logdomain=0]

736 * </pre>

737 *

738 * Aliases files can be used to store default options for algorithms.

739 */

741 /** \page bibliography Bibliography

742 * \anchor EaG09 \ref EaG09

743 * F. Eaton and Z. Ghahramani (2009):

744 * "Choosing a Variable to Clamp",

745 * <em>Proceedings of the Twelfth International Conference on Artificial Intelligence and Statistics (AISTATS 2009)</em> 5:145-152,

746 * http://jmlr.csail.mit.edu/proceedings/papers/v5/eaton09a/eaton09a.pdf

747 *

748 * \anchor EMK06 \ref EMK06

749 * G. Elidan and I. McGraw and D. Koller (2006):

750 * "Residual Belief Propagation: Informed Scheduling for Asynchronous Message Passing",

751 * <em>Proceedings of the 22nd Annual Conference on Uncertainty in Artificial Intelligence (UAI-06)</em>,

752 * http://uai.sis.pitt.edu/papers/06/UAI2006_0091.pdf

753 *

754 * \anchor HAK03 \ref HAK03

755 * T. Heskes and C. A. Albers and H. J. Kappen (2003):

756 * "Approximate Inference and Constrained Optimization",

757 * <em>Proceedings of the 19th Annual Conference on Uncertainty in Artificial Intelligence (UAI-03)</em> pp. 313-320,

758 * http://www.snn.ru.nl/reports/Heskes.uai2003.ps.gz

759 *

760 * \anchor KFL01 \ref KFL01

761 * F. R. Kschischang and B. J. Frey and H.-A. Loeliger (2001):

762 * "Factor Graphs and the Sum-Product Algorithm",

763 * <em>IEEE Transactions on Information Theory</em> 47(2):498-519,

764 * http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=910572

765 *

766 * \anchor KoF09 \ref KoF09

767 * D. Koller and N. Friedman (2009):

768 * <em>Probabilistic Graphical Models - Principles and Techniques</em>,

769 * The MIT Press, Cambridge, Massachusetts, London, England.

771 * \anchor Min05 \ref Min05

772 * T. Minka (2005):

773 * "Divergence measures and message passing",

774 * <em>MicroSoft Research Technical Report</em> MSR-TR-2005-173,

775 * http://research.microsoft.com/en-us/um/people/minka/papers/message-passing/minka-divergence.pdf

776 *

777 * \anchor MiQ04 \ref MiQ04

778 * T. Minka and Y. Qi (2004):

779 * "Tree-structured Approximations by Expectation Propagation",

780 * <em>Advances in Neural Information Processing Systems</em> (NIPS) 16,

781 * http://books.nips.cc/papers/files/nips16/NIPS2003_AA25.pdf

782 *

783 * \anchor MoK07 \ref MoK07

784 * J. M. Mooij and H. J. Kappen (2007):

785 * "Loop Corrections for Approximate Inference on Factor Graphs",

786 * <em>Journal of Machine Learning Research</em> 8:1113-1143,

787 * http://www.jmlr.org/papers/volume8/mooij07a/mooij07a.pdf

788 *

789 * \anchor MoK07b \ref MoK07b

790 * J. M. Mooij and H. J. Kappen (2007):

791 * "Sufficient Conditions for Convergence of the Sum-Product Algorithm",

792 * <em>IEEE Transactions on Information Theory</em> 53(12):4422-4437,

793 * http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=4385778

794 *

795 * \anchor MoR05 \ref MoR05

796 * A. Montanari and T. Rizzo (2005):

797 * "How to Compute Loop Corrections to the Bethe Approximation",

798 * <em>Journal of Statistical Mechanics: Theory and Experiment</em> 2005(10)-P10011,

799 * http://stacks.iop.org/1742-5468/2005/P10011

800 *

801 * \anchor StW99 \ref StW99

802 * A. Steger and N. C. Wormald (1999):

803 * "Generating Random Regular Graphs Quickly",

804 * <em>Combinatorics, Probability and Computing</em> Vol 8, Issue 4, pp. 377-396,

805 * http://www.math.uwaterloo.ca/~nwormald/papers/randgen.pdf

806 *

807 * \anchor WiH03 \ref WiH03

808 * W. Wiegerinck and T. Heskes (2003):

809 * "Fractional Belief Propagation",

810 * <em>Advances in Neural Information Processing Systems</em> (NIPS) 15, pp. 438-445,

811 * http://books.nips.cc/papers/files/nips15/LT16.pdf

812 *

813 * \anchor WJW03 \ref WJW03

814 * M. J. Wainwright, T. S. Jaakkola and A. S. Willsky (2003):

815 * "Tree-reweighted belief propagation algorithms and approximate ML estimation by pseudo-moment matching",

816 * <em>9th Workshop on Artificial Intelligence and Statistics</em>,

817 * http://www.eecs.berkeley.edu/~wainwrig/Papers/WJW_AIStat03.pdf

818 *

819 * \anchor YFW05 \ref YFW05

820 * J. S. Yedidia and W. T. Freeman and Y. Weiss (2005):

821 * "Constructing Free-Energy Approximations and Generalized Belief Propagation Algorithms",

822 * <em>IEEE Transactions on Information Theory</em> 51(7):2282-2312,

823 * http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=1459044

824 */

827 /** \page discussion Ideas not worth exploring

828 * \section discuss_extendedgraphs Extended factorgraphs/regiongraphs

829 *

830 * A FactorGraph and a RegionGraph are often equipped with

831 * additional properties for nodes and edges. The code to initialize those

832 * is often quite similar. Maybe one could abstract this, e.g.:

833 * \code

834 * template <typename Node1Properties, typename Node2Properties, typename EdgeProperties>

835 * class ExtFactorGraph : public FactorGraph {

836 * public:

837 * std::vector<Node1Properties> node1Props;

838 * std::vector<Node2Properties> node2Props;

839 * std::vector<std::vector<EdgeProperties> > edgeProps;

840 * // ...

841 * }

842 * \endcode

843 *

844 * Advantages:

845 * - Less code duplication.

846 * - Easier maintainability.

847 * - Easier to write new inference algorithms.

848 *

849 * Disadvantages:

850 * - Cachability may be worse.

851 * - A problem is the case where there are no properties for either type of nodes or for edges.

852 * Maybe this can be solved using specializations, or using variadac template arguments?

853 * Another possible solution would be to define a "class Empty {}", and add some code

854 * that checks for the typeid, comparing it with Empty, and doing something special in that case

855 * (e.g., not allocating memory).

856 * - The main disadvantage of this approach seems to be that it leads to even more entanglement.

857 * Therefore this is probably a bad idea.

858 *

859 * \section discuss_templates Polymorphism by template parameterization

860 *

861 * Instead of polymorphism by inheritance, use polymorphism by template parameterization.

862 * For example, the real reason for introducing the complicated inheritance scheme of dai::InfAlg

863 * was for functions like dai::calcMarginal. Instead, one could use a template function:

864 * \code

865 * template<typename InfAlg>

866 * Factor calcMarginal( const InfAlg &obj, const VarSet &ns, bool reInit );

867 * \endcode

868 * This would assume that the type InfAlg supports certain methods. Ideally, one would use

869 * concepts to define different classes of inference algorithms with different capabilities,

870 * for example the ability to calculate logZ, the ability to calculate marginals, the ability to

871 * calculate bounds, the ability to calculate MAP states, etc. Then, one would use traits

872 * classes in order to be able to query the capabilities of the model. For example, one would be

873 * able to query whether the inference algorithm supports calculation of logZ. Unfortunately,

874 * this is compile-time polymorphism, whereas tests/testdai needs runtime polymorphism.

875 * Therefore this is probably a bad idea.

876 */