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