254a0b8b8dedfe35ec07312131e6f70c8eac3c96

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 * \todo Document utils/createfg (and clean up the code a bit)

15 *

16 * \idea Adapt (part of the) guidelines in http://www.boost.org/development/requirements.html#Design_and_Programming

17 *

18 * \idea Use "gcc -MM" to generate dependencies for targets: http://make.paulandlesley.org/autodep.html

19 *

20 * \idea Disentangle structures. In particular, ensure that graphical properties are not

21 * entangled with probabilistic properties. For example, a FactorGraph contains several components:

22 * - a BipartiteGraph

23 * - an array of variable labels

24 * - an array of variable state space sizes

25 * - an array of pointers to factor value vectors

26 * In this way, each factor could be implemented differently, e.g., we could have

27 * some sparse factors, some noisy-OR factors, some dense factors, some arbitrary

28 * precision factors, etcetera.

29 *

30 * \idea Use boost::uBLAS framework to deal with matrices, especially, with 2D sparse matrices.

31 * See http://www.boost.org/libs/numeric/ublas/doc/matrix_sparse.htm

32 * However: I read somewhere that boost::uBLAS concentrates more on correct implementation than on performance.

33 *

34 * \idea Introduce naming scheme:

35 * - all Vars should be named v_..., e.g. v_i instead of i

36 * - all VarSets should be named vs_..., e.g. v_i instead of i

37 * - all Factors should be named f_..., e.g. f_I instead of I

38 * - all indices should be named _..., e.g. _k instead of k

39 * - all iterators should be named i_, e.g. i_i is an iterator to i

40 * - all const_iterators should be named ci_, e.g. ci_i is an iterator to i

41 **/

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

45 * \author Joris Mooij

46 * \version git HEAD

47 * \date February 4, 2010 - or later

48 *

49 * <hr size="1">

50 * \section about About libDAI

51 * libDAI is a free/open source C++ library (licensed under GPL 2+) that provides

52 * implementations of various (approximate) inference methods for discrete

53 * graphical models. libDAI supports arbitrary factor graphs with discrete

54 * variables; this includes discrete Markov Random Fields and Bayesian

55 * Networks.

56 *

57 * The library is targeted at researchers. To be able to use the library, a

58 * good understanding of graphical models is needed.

59 *

60 * The best way to use libDAI is by writing C++ code that invokes the library;

61 * in addition, part of the functionality is accessibly by using the

62 * - command line interface

63 * - (limited) MatLab interface

64 * - (experimental) python interface

65 * - (experimental) octave interface.

66 *

67 * libDAI can be used to implement novel (approximate) inference algorithms

68 * and to easily compare the accuracy and performance with existing algorithms

69 * that have been implemented already.

70 *

71 * \section features Features

72 * Currently, libDAI supports the following (approximate) inference methods:

73 * - Exact inference by brute force enumeration;

74 * - Exact inference by junction-tree methods;

75 * - Mean Field;

76 * - Loopy Belief Propagation [\ref KFL01];

77 * - Fractional Belief Propagation [\ref WiH03];

78 * - Tree-Reweighted Belief Propagation [\ref WJW03];

79 * - Tree Expectation Propagation [\ref MiQ04];

80 * - Generalized Belief Propagation [\ref YFW05];

81 * - Double-loop GBP [\ref HAK03];

82 * - Various variants of Loop Corrected Belief Propagation

83 * [\ref MoK07, \ref MoR05];

84 * - Gibbs sampler;

85 * - Clamped Belief Propagation [\ref EaG09].

86 *

87 * These inference methods can be used to calculate partition sums, marginals

88 * over subsets of variables, and MAP states (the joint state of variables that

89 * has maximum probability).

90 *

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

92 * tables by Expectation Maximization.

93 *

94 * \section limitations Limitations

95 * libDAI is not intended to be a complete package for approximate inference.

96 * Instead, it should be considered as an "inference engine", providing

97 * various inference methods. In particular, it contains no GUI, currently

98 * only supports its own file format for input and output (although support

99 * for standard file formats may be added later), and provides very limited

100 * visualization functionalities. The only learning method supported currently

101 * is Expectation Maximization (or Maximum Likelihood if no data is missing)

102 * for learning factor parameters.

103 *

104 * \section rationale Rationale

105 *

106 * In my opinion, the lack of open source "reference" implementations hampers

107 * progress in research on approximate inference. Methods differ widely in terms

108 * of quality and performance characteristics, which also depend in different

109 * ways on various properties of the graphical models. Finding the best

110 * approximate inference method for a particular application therefore often

111 * requires empirical comparisons. However, implementing and debugging these

112 * methods takes a lot of time which could otherwise be spent on research. I hope

113 * that this code will aid researchers to be able to easily compare various

114 * (existing as well as new) approximate inference methods, in this way

115 * accelerating research and stimulating real-world applications of approximate

116 * inference.

117 *

118 * \section language Language

119 * Because libDAI is implemented in C++, it is very fast compared with

120 * implementations in MatLab (a factor 1000 faster is not uncommon).

121 * libDAI does provide a (limited) MatLab interface for easy integration with MatLab.

122 * It also provides a command line interface and experimental python and octave

123 * interfaces (thanks to Patrick Pletscher).

124 *

125 * \section compatibility Compatibility

126 *

127 * The code has been developed under Debian GNU/Linux with the GCC compiler suite.

128 * libDAI compiles successfully with g++ versions 3.4, 4.1, 4.2 and 4.3.

129 *

130 * libDAI has also been successfully compiled with MS Visual Studio 2008 under Windows

131 * (but not all build targets are supported yet) and with Cygwin under Windows.

132 *

133 * Finally, libDAI has been compiled successfully on MacOS X.

134 *

135 * \section download Downloading libDAI

136 * The libDAI sources and documentation can be downloaded from the libDAI website:

137 * http://www.libdai.org.

138 *

139 * \section support Mailing list

140 * The Google group "libDAI" (http://groups.google.com/group/libdai)

141 * can be used for getting support and discussing development issues.

142 */

145 /** \page license License

146 * <hr size="1">

147 * \section license-license License

148 *

149 * libDAI is free software; you can redistribute it and/or modify

150 * it under the terms of the GNU General Public License as published by

151 * the Free Software Foundation; either version 2 of the License, or

152 * (at your option) any later version.

153 *

154 * libDAI is distributed in the hope that it will be useful,

155 * but WITHOUT ANY WARRANTY; without even the implied warranty of

156 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

157 * GNU General Public License for more details.

158 *

159 * <hr size="1">

160 * \section license-gpl GNU General Public License version 2

161 *

162 * \verbinclude COPYING

163 */

166 /** \page citations Citing libDAI

167 * <hr size="1">

168 * \section citations-citations Citing libDAI

169 *

170 * If you write a scientific paper describing research that made substantive use

171 * of this program, please:

172 * - mention the fashion in which this software was

173 * used, including the version number, with a citation to the literature,

174 * to allow replication;

175 * - mention this software in the Acknowledgements section.

176 *

177 * An appropriate citation would be:\n

178 * J. M. Mooij (2009) "libDAI 0.2.3: A free/open source C++ library for Discrete

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

180 *

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

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

183 */

186 /** \page authors Authors

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

188 *

189 * \verbinclude AUTHORS

190 */

193 /** \page build Building libDAI

194 * <hr size="1">

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

196 *

197 * You need:

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

199 * - GNU make

200 * - doxygen

201 * - graphviz

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

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

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

205 *

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

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

208 * (root permissions needed).

209 *

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

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

212 * Then, a simple

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

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

215 *

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

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

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

219 *

220 * <pre> ./configure

221 * make

222 * make install

223 * </pre>

224 *

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

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

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

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

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

230 * <pre> make</pre>

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

232 *

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

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

235 * or the more elaborate test program:

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

237 *

238 *

239 * <hr size="1">

240 * \section build-windows Building libDAI under Windows

241 *

242 * You need:

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

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

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

246 *

247 * For the regression test, you need:

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

249 *

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

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

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

253 * <pre> make</pre>

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

255 *

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

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

258 * or the more elaborate test program:

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

260 *

261 *

262 * <hr size="1">

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

264 *

265 * You need:

266 * - MatLab

267 * - The platform-dependent requirements described above

268 *

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

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

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

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

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

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

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

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

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

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

279 * The required MEX files are built by issuing

280 * <pre> make</pre>

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

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

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

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

285 * something that MatLab understands.

286 *

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

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

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

290 * <pre> cd path_to_libdai/matlab

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

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

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

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

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

296 * tests/aliases.conf.

297 *

298 * <hr size="1">

299 * \section build-doxygen Building the documentation

300 *

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

302 * <pre> make doc</pre>

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

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

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

306 */

309 /** \page changelog Change Log

310 * \verbinclude ChangeLog

311 */

314 /** \page terminology Terminology and conventions

315 *

316 * \section terminology-graphicalmodels Graphical models

317 *

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

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

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

321 *

322 * An example of a Bayesian network is:

323 * \dot

324 * digraph bayesnet {

325 * size="1,1";

326 * x0 [label="0"];

327 * x1 [label="1"];

328 * x2 [label="2"];

329 * x3 [label="3"];

330 * x4 [label="4"];

331 * x0 -> x1;

332 * x0 -> x2;

333 * x1 -> x3;

334 * x1 -> x4;

335 * x2 -> x4;

336 * }

337 * \enddot

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

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

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

341 *

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

343 * \dot

344 * graph mrf {

345 * size="1.5,1.5";

346 * x0 [label="0"];

347 * x1 [label="1"];

348 * x2 [label="2"];

349 * x3 [label="3"];

350 * x4 [label="4"];

351 * x0 -- x1;

352 * x0 -- x2;

353 * x1 -- x2;

354 * x1 -- x3;

355 * x1 -- x4;

356 * x2 -- x4;

357 * }

358 * \enddot

359 *

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

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

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

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

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

365 * distribution.

366 *

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

368 * \dot

369 * graph factorgraph {

370 * size="1.8,1";

371 * x0 [label="0"];

372 * x1 [label="1"];

373 * x2 [label="2"];

374 * x3 [label="3"];

375 * x4 [label="4"];

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

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

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

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

380 * x0 -- f01;

381 * x1 -- f01;

382 * x0 -- f02;

383 * x2 -- f02;

384 * x1 -- f13;

385 * x3 -- f13;

386 * x1 -- f124;

387 * x2 -- f124;

388 * x4 -- f124;

389 * }

390 * \enddot

391 *

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

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

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

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

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

397 * which properly normalizes the probability distribution.

398 *

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

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

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

402 * naturally express the factorization structure of a probability

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

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

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

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

407 *

408 * \section terminology-inference Inference tasks

409 *

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

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

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

413 *

414 * - Calculating the partition sum:

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

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

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

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

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

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

421 *

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

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

424 * algorithms are implemented:

425 *

426 * Exact inference:

427 * - Brute force enumeration: dai::ExactInf

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

429 *

430 * Approximate inference:

431 * - Mean Field: dai::MF

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

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

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

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

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

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

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

439 * - Gibbs sampling: dai::Gibbs

440 * - Clamped Belief Propagation: dai::CBP [\ref EaG09]

441 *

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

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

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

445 *

446 * \section terminology-learning Parameter learning

447 *

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

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

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

451 *

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

453 *

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

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

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

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

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

459 *

460 * \subsection terminology-variables Variables

461 *

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

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

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

465 * number of different values or \a states.

466 *

467 * We use the following notational conventions. The discrete

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

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

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

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

472 *

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

474 *

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

476 *

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

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

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

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

481 *

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

483 *

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

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

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

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

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

489 * label) the corresponding state of the variable.

490 *

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

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

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

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

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

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

497 * \f[

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

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

500 * \f]

501 *

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

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

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

505 * \f[

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

507 * \f]

508 *

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

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

511 */

514 /** \page fileformats libDAI file formats

515 *

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

517 *

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

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

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

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

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

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

524 *

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

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

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

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

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

530 * which start with # are ignored.

531 *

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

533 *

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

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

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

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

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

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

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

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

542 * The fourth line contains the number of nonzero entries

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

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

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

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

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

548 * of multidimensional arrays).

549 *

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

551 *

552 * An example block describing one factor is:

553 *

554 * <pre>

555 * 3

556 * 4 8 7

557 * 3 2 2

558 * 11

559 * 0 0.1

560 * 1 3.5

561 * 2 2.8

562 * 3 6.3

563 * 4 8.4

564 * 6 7.4

565 * 7 2.4

566 * 8 8.9

567 * 9 1.3

568 * 10 1.6

569 * 12 6.4

570 * 11 2.6

571 * </pre>

572 *

573 * which corresponds to the following factor:

574 *

575 * \f[

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

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

578 * \hline

579 * 0 & 0 & 0 & 0.1\\

580 * 1 & 0 & 0 & 3.5\\

581 * 2 & 0 & 0 & 2.8\\

582 * 0 & 1 & 0 & 6.3\\

583 * 1 & 1 & 0 & 8.4\\

584 * 2 & 1 & 0 & 0.0\\

585 * 0 & 0 & 1 & 7.4\\

586 * 1 & 0 & 1 & 2.4\\

587 * 2 & 0 & 1 & 8.9\\

588 * 0 & 1 & 1 & 1.3\\

589 * 1 & 1 & 1 & 1.6\\

590 * 2 & 1 & 1 & 2.6

591 * \end{array}

592 * \f]

593 *

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

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

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

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

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

599 * eleventh and twelveth entries are interchanged.

600 *

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

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

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

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

605 * fastest:

606 *

607 * \f[

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

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

610 * \hline

611 * 0 & 0 & 0 & 0.1\\

612 * 1 & 0 & 0 & 3.5\\

613 * 2 & 0 & 0 & 2.8\\

614 * 0 & 1 & 0 & 7.4\\

615 * 1 & 1 & 0 & 2.4\\

616 * 2 & 1 & 0 & 8.9\\

617 * 0 & 0 & 1 & 6.3\\

618 * 1 & 0 & 1 & 8.4\\

619 * 2 & 0 & 1 & 0.0\\

620 * 0 & 1 & 1 & 1.3\\

621 * 1 & 1 & 1 & 1.6\\

622 * 2 & 1 & 1 & 2.6

623 * \end{array}

624 * \f]

625 *

626 *

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

628 *

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

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

631 * observed joint state of some variables.

632 *

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

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

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

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

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

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

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

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

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

642 * without any characters in between.

643 *

644 * \subsection fileformats-evidence-example Example

645 *

646 * <pre>

647 * 1 3 2

648 *

649 * 0 0 1

650 * 1 0 1

651 * 1 1

652 * </pre>

653 *

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

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

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

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

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

659 *

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

661 *

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

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

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

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

666 * containing the data used for parameter learning.

667 *

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

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

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

671 *

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

673 *

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

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

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

677 * the next subsection.

678 *

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

680 *

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

682 * consisting of the name of a ParameterEstimation subclass

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

702 *

703 * \section fileformats-aliases Aliases file format

704 *

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

706 * should be substituted with.

707 *

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

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

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

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

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

713 *

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

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

716 * <pre>

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

718 * </pre>

719 *

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

721 */

723 /** \page bibliography Bibliography

724 * \anchor EaG09 \ref EaG09

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

726 * "Choosing a Variable to Clamp",

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

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

729 *

730 * \anchor EMK06 \ref EMK06

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

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

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

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

735 *

736 * \anchor HAK03 \ref HAK03

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

738 * "Approximate Inference and Constrained Optimization",

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

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

741 *

742 * \anchor KFL01 \ref KFL01

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

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

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

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

747 *

748 * \anchor KoF09 \ref KoF09

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

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

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

753 * \anchor Min05 \ref Min05

754 * T. Minka (2005):

755 * "Divergence measures and message passing",

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

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

758 *

759 * \anchor MiQ04 \ref MiQ04

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

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

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

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

764 *

765 * \anchor MoK07 \ref MoK07

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

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

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

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

770 *

771 * \anchor MoK07b \ref MoK07b

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

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

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

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

776 *

777 * \anchor MoR05 \ref MoR05

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

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

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

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

782 *

783 * \anchor StW99 \ref StW99

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

785 * "Generating Random Regular Graphs Quickly",

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

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

788 *

789 * \anchor WiH03 \ref WiH03

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

791 * "Fractional Belief Propagation",

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

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

794 *

795 * \anchor WJW03 \ref WJW03

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

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

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

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

800 *

801 * \anchor YFW05 \ref YFW05

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

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

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

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

806 */

809 /** \page discussion Ideas not worth exploring

810 * \section discuss_extendedgraphs Extended factorgraphs/regiongraphs

811 *

812 * A FactorGraph and a RegionGraph are often equipped with

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

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

815 * \code

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

817 * class ExtFactorGraph : public FactorGraph {

818 * public:

819 * std::vector<Node1Properties> node1Props;

820 * std::vector<Node2Properties> node2Props;

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

822 * // ...

823 * }

824 * \endcode

825 *

826 * Advantages:

827 * - Less code duplication.

828 * - Easier maintainability.

829 * - Easier to write new inference algorithms.

830 *

831 * Disadvantages:

832 * - Cachability may be worse.

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

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

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

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

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

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

839 * Therefore this is probably a bad idea.

840 *

841 * \section discuss_templates Polymorphism by template parameterization

842 *

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

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

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

846 * \code

847 * template<typename InfAlg>

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

849 * \endcode

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

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

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

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

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

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

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

857 * Therefore this is probably a bad idea.

858 */