1 /* This file is part of libDAI - http://www.libdai.org/
2 *
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) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
9 */
12 /// \file
13 /// \brief Defines class MF which implements the Mean Field algorithm
16 #ifndef __defined_libdai_mf_h
17 #define __defined_libdai_mf_h
20 #include <string>
21 #include <dai/enum.h>
22 #include <dai/daialg.h>
23 #include <dai/factorgraph.h>
24 #include <dai/properties.h>
27 namespace dai {
30 /// Approximate inference algorithm "Mean Field"
31 /** The Mean Field algorithm iteratively calculates approximations of
32 * single variable marginals (beliefs). The update equation for
33 * a single belief \f$b_i\f$ is given by:
34 * \f[ b_i^{\mathrm{new}}(x_i) \propto \prod_{I\in N_i} \exp \left( \sum_{x_{N_I \setminus \{i\}}} \log f_I(x_I) \prod_{j \in N_I \setminus \{i\}} b_j(x_j) \right) \f]
35 * for naive mean field and by
36 * \f[ b_i^{\mathrm{new}}(x_i) \propto \prod_{I\in N_i} \left( \sum_{x_{N_I \setminus \{i\}}} f_I(x_I) \prod_{j \in N_I \setminus \{i\}} b_j(x_j) \right) \f]
37 * for hard-spin mean field.
38 * These update equations are performed for all variables until convergence.
39 */
40 class MF : public DAIAlgFG {
41 private:
42 /// Current approximations of single variable marginals
43 std::vector<Factor> _beliefs;
44 /// Maximum difference encountered so far
45 Real _maxdiff;
46 /// Number of iterations needed
47 size_t _iters;
49 public:
50 /// Parameters for MF
51 struct Properties {
52 /// Enumeration of possible message initializations
53 DAI_ENUM(InitType,UNIFORM,RANDOM);
55 /// Enumeration of possible update types
56 DAI_ENUM(UpdateType,NAIVE,HARDSPIN);
58 /// Verbosity (amount of output sent to stderr)
59 size_t verbose;
61 /// Maximum number of iterations
62 size_t maxiter;
64 /// Tolerance for convergence test
65 Real tol;
67 /// Damping constant (0.0 means no damping, 1.0 is maximum damping)
68 Real damping;
70 /// How to initialize the messages/beliefs
71 InitType init;
73 /// How to update the messages/beliefs
75 } props;
77 /// Name of this inference algorithm
78 static const char *Name;
80 public:
81 /// \name Constructors/destructors
82 //@{
83 /// Default constructor
84 MF() : DAIAlgFG(), _beliefs(), _maxdiff(0.0), _iters(0U), props() {}
86 /// Construct from FactorGraph \a fg and PropertySet \a opts
87 /** \param fg Factor graph.
88 * \param opts Parameters @see Properties
89 */
90 MF( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _beliefs(), _maxdiff(0.0), _iters(0U), props() {
91 setProperties( opts );
92 construct();
93 }
94 //@}
96 /// \name General InfAlg interface
97 //@{
98 virtual MF* clone() const { return new MF(*this); }
99 virtual std::string identify() const;
100 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
101 virtual Factor belief( const VarSet &vs ) const;
102 virtual Factor beliefV( size_t i ) const;
103 virtual std::vector<Factor> beliefs() const;
104 virtual Real logZ() const;
105 virtual void init();
106 virtual void init( const VarSet &ns );
107 virtual Real run();
108 virtual Real maxDiff() const { return _maxdiff; }
109 virtual size_t Iterations() const { return _iters; }
110 virtual void setProperties( const PropertySet &opts );
111 virtual PropertySet getProperties() const;
112 virtual std::string printProperties() const;
113 //@}
115 private:
116 /// Helper function for constructors
117 void construct();
119 /// Calculates an updated belief of variable \a i
120 Factor calcNewBelief( size_t i );
121 };
124 } // end of namespace dai
127 #endif