1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]

2 Radboud University Nijmegen, The Netherlands /

3 Max Planck Institute for Biological Cybernetics, Germany

5 This file is part of libDAI.

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

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

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

10 (at your option) any later version.

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

13 but WITHOUT ANY WARRANTY; without even the implied warranty of

14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

15 GNU General Public License for more details.

17 You should have received a copy of the GNU General Public License

18 along with libDAI; if not, write to the Free Software

19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

20 */

23 /** \file

24 * \brief Contains additional doxygen documentation

25 *

26 * \todo Merge COPYING into doxygen documentation

27 * \todo Merge README into doxygen documentation

28 * \todo Document examples, tests and utils

29 *

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

31 *

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

33 * \todo Investigate whether switching to cmake as cross-platform build system would be a good idea.

34 * \todo Switch from nmake to GNU make under Windows http://gnuwin32.sourceforge.net/

35 *

36 * \todo Replace VarSets by SmallSet<size_t> where appropriate, in order to minimize the use of FactorGraph::findVar().

37 *

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

39 * entangled with probabilistic properties. For example, a FactorGraph contains several

40 * components:

41 * - a BipartiteGraph

42 * - an array of variable labels

43 * - an array of variable state space sizes

44 * - an array of pointers to factor value vectors

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

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

47 * precision factors, etc.

48 *

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

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

51 *

52 * \idea Introduce naming scheme:

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

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

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

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

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

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

59 **/

62 /** \page discussion Discussion of possible improvements

63 * \section discuss_extendedgraphs Extended factorgraphs/regiongraphs

64 *

65 * A FactorGraph and a RegionGraph are often equipped with

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

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

68 * \code

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

70 * class ExtFactorGraph : public FactorGraph {

71 * public:

72 * std::vector<Node1Properties> node1Props;

73 * std::vector<Node2Properties> node2Props;

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

75 * // ...

76 * }

77 * \endcode

78 *

79 * Advantages:

80 * - Less code duplication.

81 * - Easier maintainability.

82 * - Easier to write new inference algorithms.

83 *

84 * Disadvantages:

85 * - Cachability may be worse.

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

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

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

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

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

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

92 * Therefore this is probably a bad idea.

93 *

94 * \section discuss_templates Polymorphism by template parameterization

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

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

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

98 * \code

99 * template<typename InfAlg>

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

101 * \endcode

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

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

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

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

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

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

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

109 * Therefore this is probably a bad idea.

110 */

113 /** \mainpage libDAI reference manual

114 * \author Joris Mooij

115 * \version git HEAD

116 * \date October 10, 2008

117 *

118 * \section about About libDAI

119 * libDAI is a free/open source C++ library (licensed under GPLv2+) that provides

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

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

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

123 * Networks.

124 *

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

126 * good understanding of graphical models is needed.

127 *

128 * \section limitations Limitations

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

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

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

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

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

134 * visualization functionalities. The only learning method supported currently

135 * is EM for learning factor parameters.

136 *

137 * \section features Features

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

139 * - Exact inference by brute force enumeration;

140 * - Exact inference by junction-tree methods;

141 * - Mean Field;

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

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

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

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

146 * - Various variants of Loop Corrected Belief Propagation

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

148 * - Gibbs sampler;

149 * - Conditioned BP [\ref EaG09];

150 *

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

152 * tables by Expectation Maximization.

153 *

154 * \section language Why C++?

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

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

157 * libDAI does provide a MatLab interface for easy integration with MatLab.

158 */

161 /** \example example.cpp

162 */

165 /** \page quickstart Quick start

166 * An example program illustrating basic usage of libDAI is given in examples/example.cpp.

167 */

170 /** \page bibliography Bibliography

171 * \section Bibliograpy

172 * \anchor KFL01 \ref KFL01

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

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

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

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

177 *

178 * \anchor MiQ04 \ref MiQ04

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

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

181 * <em>Advances in Neural Information Processing Systems</em> (NIPS) 16.

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

183 *

184 * \anchor MoR05 \ref MoR05

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

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

187 * <em>Journal of Statistical Mechanics: Theory and Experiment</em>

188 * 2005(10)-P10011.

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

190 *

191 * \anchor YFW05 \ref YFW05

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

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

194 * <em>IEEE Transactions on Information Theory</em>

195 * 51(7):2282-2312.

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

197 *

198 * \anchor HAK03 \ref HAK03

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

200 * "Approximate Inference and Constrained Optimization",

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

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

203 *

204 * \anchor MoK07 \ref MoK07

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

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

207 * <em>Journal of Machine Learning Research</em> 8:1113-1143.

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

209 *

210 * \anchor MoK07b \ref MoK07b

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

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

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

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

215 *

216 * \anchor EaG09 \ref EaG09

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

218 * "Choosing a Variable to Clamp",

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

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

221 */

224 /** \page fileformat libDAI factorgraph file format

225 *

226 * This page describes the .fg fileformat used in libDAI to store factor graphs.

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

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

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

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

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

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

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

234 * are specified, one block for each factor, where the blocks are seperated by

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

236 * identifier, its index (which should be a nonnegative integer). Comment lines

237 * start with #.

238 *

239 * Each block starts with a line containing the number of variables in that

240 * factor. The second line contains the indices of these variables, seperated by

241 * spaces (indices are nonnegative integers and to avoid confusion, it is

242 * suggested to start counting at 0). The third line contains the number of

243 * possible values of each of these variables, also seperated by spaces. Note that

244 * there is some redundancy here, since if a variable appears in more than one

245 * factor, the cardinality of that variable appears several times in the .fg file.

246 * The fourth line contains the number of nonzero entries in the factor table.

247 * The rest of the lines contain these nonzero entries; each entry consists of a

248 * table index, followed by white-space, followed by the value corresponding to

249 * that table index. The most difficult part is getting the indexing right. The

250 * convention that is used is that the left-most variables cycle through their

251 * values the fastest (similar to MATLAB indexing of multidimensional arrays). An

252 * example block describing one factor is:

253 *

254 * 3\n

255 * 4 8 7\n

256 * 3 2 2\n

257 * 11\n

258 * 0 0.1\n

259 * 1 3.5\n

260 * 2 2.8\n

261 * 3 6.3\n

262 * 4 8.4\n

263 * 6 7.4\n

264 * 7 2.4\n

265 * 8 8.9\n

266 * 9 1.3\n

267 * 10 1.6\n

268 * 12 6.4\n

269 * 11 2.6\n

270 *

271 * which corresponds to the following factor:

272 *

273 * \f[

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

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

276 * \hline

277 * 0 & 0 & 0 & 0.1\\

278 * 1 & 0 & 0 & 3.5\\

279 * 2 & 0 & 0 & 2.8\\

280 * 0 & 1 & 0 & 6.3\\

281 * 1 & 1 & 0 & 8.4\\

282 * 2 & 1 & 0 & 0.0\\

283 * 0 & 0 & 1 & 7.4\\

284 * 1 & 0 & 1 & 2.4\\

285 * 2 & 0 & 1 & 8.9\\

286 * 0 & 1 & 1 & 1.3\\

287 * 1 & 1 & 1 & 1.6\\

288 * 2 & 1 & 1 & 2.6

289 * \end{array}

290 * \f]

291 *

292 * Note that the value of x_4 changes fastest, followed by that of x_8, and x_7

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

294 * Further, x_4 can take on three values, and x_8 and x_7 each have two possible

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

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

297 * eleventh and twelveth entries are interchanged.

298 *

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

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

301 * (i.e., the ordering would be x_4 x_7 x_8) and the values of the table are

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

303 * fastest:

304 *

305 * \f[

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

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

308 * \hline

309 * 0 & 0 & 0 & 0.1\\

310 * 1 & 0 & 0 & 3.5\\

311 * 2 & 0 & 0 & 2.8\\

312 * 0 & 1 & 0 & 7.4\\

313 * 1 & 1 & 0 & 2.4\\

314 * 2 & 1 & 0 & 8.9\\

315 * 0 & 0 & 1 & 6.3\\

316 * 1 & 0 & 1 & 8.4\\

317 * 2 & 0 & 1 & 0.0\\

318 * 0 & 1 & 1 & 1.3\\

319 * 1 & 1 & 1 & 1.6\\

320 * 2 & 1 & 1 & 2.6

321 * \end{array}

322 * \f]

323 */

326 /** \page license License

327 * \verbinclude COPYING

328 */