1 /* This file is part of libDAI - http://www.libdai.org/
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
10 /// \brief Defines class MF which implements the Mean Field algorithm
13 #ifndef __defined_libdai_mf_h
14 #define __defined_libdai_mf_h
17 #include <dai/dai_config.h>
23 #include <dai/daialg.h>
24 #include <dai/factorgraph.h>
25 #include <dai/properties.h>
31 /// Approximate inference algorithm "Mean Field"
32 /** The Mean Field algorithm iteratively calculates approximations of
33 * single variable marginals (beliefs). The update equation for
34 * a single belief \f$b_i\f$ is given by:
35 * \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]
36 * for naive mean field and by
37 * \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]
38 * for hard-spin mean field.
39 * These update equations are performed for all variables until convergence.
41 class MF
: public DAIAlgFG
{
43 /// Current approximations of single variable marginals
44 std::vector
<Factor
> _beliefs
;
45 /// Maximum difference encountered so far
47 /// Number of iterations needed
53 /// Enumeration of possible message initializations
54 DAI_ENUM(InitType
,UNIFORM
,RANDOM
);
56 /// Enumeration of possible update types
57 DAI_ENUM(UpdateType
,NAIVE
,HARDSPIN
);
59 /// Verbosity (amount of output sent to stderr)
62 /// Maximum number of iterations
65 /// Tolerance for convergence test
68 /// Damping constant (0.0 means no damping, 1.0 is maximum damping)
71 /// How to initialize the messages/beliefs
74 /// How to update the messages/beliefs
79 /// \name Constructors/destructors
81 /// Default constructor
82 MF() : DAIAlgFG(), _beliefs(), _maxdiff(0.0), _iters(0U), props() {}
84 /// Construct from FactorGraph \a fg and PropertySet \a opts
85 /** \param fg Factor graph.
86 * \param opts Parameters @see Properties
88 MF( const FactorGraph
&fg
, const PropertySet
&opts
) : DAIAlgFG(fg
), _beliefs(), _maxdiff(0.0), _iters(0U), props() {
89 setProperties( opts
);
94 /// \name General InfAlg interface
96 virtual MF
* clone() const { return new MF(*this); }
97 virtual MF
* construct( const FactorGraph
&fg
, const PropertySet
&opts
) const { return new MF( fg
, opts
); }
98 virtual std::string
name() const { return "MF"; }
99 virtual Factor
belief( const Var
&v
) const { return beliefV( findVar( v
) ); }
100 virtual Factor
belief( const VarSet
&vs
) const;
101 virtual Factor
beliefV( size_t i
) const;
102 virtual std::vector
<Factor
> beliefs() const;
103 virtual Real
logZ() const;
105 virtual void init( const VarSet
&ns
);
107 virtual Real
maxDiff() const { return _maxdiff
; }
108 virtual size_t Iterations() const { return _iters
; }
109 virtual void setMaxIter( size_t maxiter
) { props
.maxiter
= maxiter
; }
110 virtual void setProperties( const PropertySet
&opts
);
111 virtual PropertySet
getProperties() const;
112 virtual std::string
printProperties() const;
116 /// Helper function for constructors
119 /// Calculates an updated belief of variable \a i
120 Factor
calcNewBelief( size_t i
);
124 } // end of namespace dai