Fixed bug (found by cax): when building MatLab MEX files, GMP libraries were not...
[libdai.git] / include / dai / fbp.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 /// \file
10 /// \brief Defines class FBP, which implements Fractional Belief Propagation
11
12
13 #ifndef __defined_libdai_fbp_h
14 #define __defined_libdai_fbp_h
15
16
17 #include <string>
18 #include <dai/daialg.h>
19 #include <dai/factorgraph.h>
20 #include <dai/properties.h>
21 #include <dai/enum.h>
22 #include <dai/bp.h>
23
24
25 namespace dai {
26
27
28 /// Approximate inference algorithm "Fractional Belief Propagation" [\ref WiH03]
29 /** The Fractional Belief Propagation algorithm is like Belief
30 * Propagation, but associates each factor with a weight (scale parameter)
31 * which controls the divergence measure being minimized. Standard
32 * Belief Propagation corresponds to the case of FBP where each weight
33 * is 1. When cast as an EP algorithm, BP (and EP) minimize
34 * the inclusive KL-divergence, i.e. \f$\min_q KL(p||q)\f$ (note that the
35 * Bethe free energy is typically derived from \f$ KL(q||p) \f$). If each
36 * factor \a I has weight \f$ c_I \f$, then FBP minimizes the
37 * alpha-divergence with \f$ \alpha=1/c_I \f$ for that factor, which also
38 * corresponds to Power EP [\ref Min05].
39 *
40 * The messages \f$m_{I\to i}(x_i)\f$ are passed from factors \f$I\f$ to variables \f$i\f$.
41 * The update equation is given by:
42 * \f[ m_{I\to i}(x_i) \propto \left( \sum_{x_{N_I\setminus\{i\}}} f_I(x_I)^{1/c_I} \prod_{j\in N_I\setminus\{i\}} m_{I\to j}^{1-1/c_I} \prod_{J\in N_j\setminus\{I\}} m_{J\to j} \right)^{c_I} \f]
43 * After convergence, the variable beliefs are calculated by:
44 * \f[ b_i(x_i) \propto \prod_{I\in N_i} m_{I\to i} \f]
45 * and the factor beliefs are calculated by:
46 * \f[ b_I(x_I) \propto f_I(x_I)^{1/c_I} \prod_{j \in N_I} m_{I\to j}^{1-1/c_I} \prod_{J\in N_j\setminus\{I\}} m_{J\to j} \f]
47 * The logarithm of the partition sum is approximated by:
48 * \f[ \log Z = \sum_{I} \sum_{x_I} b_I(x_I) \big( \log f_I(x_I) - c_I \log b_I(x_I) \big) + \sum_{i} (c_i - 1) \sum_{x_i} b_i(x_i) \log b_i(x_i) \f]
49 * where the variable weights are defined as
50 * \f[ c_i := \sum_{I \in N_i} c_I \f]
51 *
52 * \todo Add nice way to set weights
53 * \author Frederik Eaton
54 */
55 class FBP : public BP {
56 protected:
57 /// Factor weights (indexed by factor ID)
58 std::vector<Real> _weight;
59
60 public:
61 /// \name Constructors/destructors
62 //@{
63 /// Default constructor
64 FBP() : BP(), _weight() {}
65
66 /// Construct from FactorGraph \a fg and PropertySet \a opts
67 /** \param fg Factor graph.
68 * \param opts Parameters @see BP::Properties
69 */
70 FBP( const FactorGraph &fg, const PropertySet &opts ) : BP(fg, opts), _weight() {
71 setProperties( opts );
72 construct();
73 }
74 //@}
75
76 /// \name General InfAlg interface
77 //@{
78 virtual FBP* clone() const { return new FBP(*this); }
79 virtual FBP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new FBP( fg, opts ); }
80 virtual std::string name() const { return "FBP"; }
81 virtual Real logZ() const;
82 //@}
83
84 /// \name FBP accessors/mutators for weights
85 //@{
86 /// Returns weight of the \a I 'th factor
87 Real Weight( size_t I ) const { return _weight[I]; }
88
89 /// Returns constant reference to vector of all factor weights
90 const std::vector<Real>& Weights() const { return _weight; }
91
92 /// Sets the weight of the \a I 'th factor to \a c
93 void setWeight( size_t I, Real c ) { _weight[I] = c; }
94
95 /// Sets the weights of all factors simultaenously
96 /** \note Faster than calling setWeight(size_t,Real) for each factor
97 */
98 void setWeights( const std::vector<Real> &c ) { _weight = c; }
99
100 protected:
101 /// Calculate the product of factor \a I and the incoming messages
102 /** If \a without_i == \c true, the message coming from variable \a i is omitted from the product
103 * \note This function is used by calcNewMessage() and calcBeliefF()
104 */
105 virtual Prob calcIncomingMessageProduct( size_t I, bool without_i, size_t i ) const;
106
107 // Calculate the updated message from the \a _I 'th neighbor of variable \a i to variable \a i
108 virtual void calcNewMessage( size_t i, size_t _I );
109
110 // Calculates unnormalized belief of factor \a I
111 virtual void calcBeliefF( size_t I, Prob &p ) const {
112 p = calcIncomingMessageProduct( I, false, 0 );
113 }
114
115 // Helper function for constructors
116 virtual void construct();
117 };
118
119
120 } // end of namespace dai
121
122
123 #endif