libDAI version 0.3.2
[libdai.git] / include / dai / mr.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 MR, which implements loop corrections as proposed by Montanari and Rizzo
11
12
13 #ifndef __defined_libdai_mr_h
14 #define __defined_libdai_mr_h
15
16
17 #include <dai/dai_config.h>
18 #ifdef DAI_WITH_MR
19
20
21 #include <vector>
22 #include <string>
23 #include <dai/factorgraph.h>
24 #include <dai/daialg.h>
25 #include <dai/enum.h>
26 #include <dai/properties.h>
27 #include <dai/exceptions.h>
28 #include <dai/graph.h>
29 #include <boost/dynamic_bitset.hpp>
30
31
32 namespace dai {
33
34
35 /// Approximate inference algorithm by Montanari and Rizzo [\ref MoR05]
36 /** \author Bastian Wemmenhove wrote the original implementation before it was merged into libDAI
37 */
38 class MR : public DAIAlgFG {
39 private:
40 /// Is the underlying factor graph supported?
41 bool supported;
42
43 /// The interaction graph (Markov graph)
44 GraphAL G;
45
46 /// tJ[i][_j] is the hyperbolic tangent of the interaction between spin \a i and its neighbour G.nb(i,_j)
47 std::vector<std::vector<Real> > tJ;
48 /// theta[i] is the local field on spin \a i
49 std::vector<Real> theta;
50
51 /// M[i][_j] is \f$ M^{(i)}_j \f$
52 std::vector<std::vector<Real> > M;
53 /// Cavity correlations
54 std::vector<std::vector<std::vector<Real> > > cors;
55
56 /// Type used for managing a subset of neighbors
57 typedef boost::dynamic_bitset<> sub_nb;
58
59 /// Magnetizations
60 std::vector<Real> Mag;
61
62 /// Maximum difference encountered so far
63 Real _maxdiff;
64
65 /// Number of iterations needed
66 size_t _iters;
67
68 public:
69 /// Parameters for MR
70 struct Properties {
71 /// Enumeration of different types of update equations
72 /** The possible update equations are:
73 * - FULL full updates, slow but accurate
74 * - LINEAR linearized updates, faster but less accurate
75 */
76 DAI_ENUM(UpdateType,FULL,LINEAR);
77
78 /// Enumeration of different ways of initializing the cavity correlations
79 /** The possible cavity initializations are:
80 * - RESPPROP using response propagation ("linear response")
81 * - CLAMPING using clamping and BP
82 * - EXACT using JunctionTree
83 */
84 DAI_ENUM(InitType,RESPPROP,CLAMPING,EXACT);
85
86 /// Verbosity (amount of output sent to stderr)
87 size_t verbose;
88
89 /// Tolerance for convergence test
90 Real tol;
91
92 /// Update equations
93 UpdateType updates;
94
95 /// How to initialize the cavity correlations
96 InitType inits;
97 } props;
98
99 public:
100 /// Default constructor
101 MR() : DAIAlgFG(), supported(), G(), tJ(), theta(), M(), cors(), Mag(), _maxdiff(), _iters(), props() {}
102
103 /// Construct from FactorGraph \a fg and PropertySet \a opts
104 /** \param fg Factor graph.
105 * \param opts Parameters @see Properties
106 * \note This implementation only deals with binary variables and pairwise interactions.
107 * \throw NOT_IMPLEMENTED if \a fg has factors depending on three or more variables or has variables with more than two possible states.
108 */
109 MR( const FactorGraph &fg, const PropertySet &opts );
110
111
112 /// \name General InfAlg interface
113 //@{
114 virtual MR* clone() const { return new MR(*this); }
115 virtual MR* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new MR( fg, opts ); }
116 virtual std::string name() const { return "MR"; }
117 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
118 virtual Factor belief( const VarSet &/*vs*/ ) const;
119 virtual Factor beliefV( size_t i ) const;
120 virtual std::vector<Factor> beliefs() const;
121 virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
122 virtual void init() {}
123 virtual void init( const VarSet &/*ns*/ ) { DAI_THROW(NOT_IMPLEMENTED); }
124 virtual Real run();
125 virtual Real maxDiff() const { return _maxdiff; }
126 virtual size_t Iterations() const { return _iters; }
127 virtual void setProperties( const PropertySet &opts );
128 virtual PropertySet getProperties() const;
129 virtual std::string printProperties() const;
130 //@}
131
132 private:
133 /// Initialize cors
134 Real calcCavityCorrelations();
135
136 /// Iterate update equations for cavity fields
137 void propagateCavityFields();
138
139 /// Calculate magnetizations
140 void calcMagnetizations();
141
142 /// Calculate the product of all tJ[i][_j] for _j in A
143 /** \param i variable index
144 * \param A subset of neighbors of variable \a i
145 */
146 Real _tJ(size_t i, sub_nb A);
147
148 /// Calculate \f$ \Omega^{(i)}_{j,l} \f$ as defined in [\ref MoR05] eqn. (2.15)
149 Real Omega(size_t i, size_t _j, size_t _l);
150
151 /// Calculate \f$ T^{(i)}_A \f$ as defined in [\ref MoR05] eqn. (2.17) with \f$ A = \{l_1,l_2,\dots\} \f$
152 /** \param i variable index
153 * \param A subset of neighbors of variable \a i
154 */
155 Real T(size_t i, sub_nb A);
156
157 /// Calculates \f$ T^{(i)}_j \f$ where \a j is the \a _j 'th neighbor of \a i
158 Real T(size_t i, size_t _j);
159
160 /// Calculates \f$ \Gamma^{(i)}_{j,l_1l_2} \f$ as defined in [\ref MoR05] eqn. (2.16)
161 Real Gamma(size_t i, size_t _j, size_t _l1, size_t _l2);
162
163 /// Calculates \f$ \Gamma^{(i)}_{l_1l_2} \f$ as defined in [\ref MoK07] on page 1141
164 Real Gamma(size_t i, size_t _l1, size_t _l2);
165
166 /// Approximates moments of variables in \a A
167 /** Calculate the moment of variables in \a A from M and cors, neglecting higher order cumulants,
168 * defined as the sum over all partitions of A into subsets of cardinality two at most of the
169 * product of the cumulants (either first order, i.e. M, or second order, i.e. cors) of the
170 * entries of the partitions.
171 *
172 * \param i variable index
173 * \param A subset of neighbors of variable \a i
174 */
175 Real appM(size_t i, sub_nb A);
176
177 /// Calculate sum over all even/odd subsets B of \a A of _tJ(j,B) appM(j,B)
178 /** \param j variable index
179 * \param A subset of neighbors of variable \a j
180 * \param sum_even on return, will contain the sum over all even subsets
181 * \param sum_odd on return, will contain the sum over all odd subsets
182 */
183 void sum_subs(size_t j, sub_nb A, Real *sum_even, Real *sum_odd);
184 };
185
186
187 } // end of namespace dai
188
189
190 #endif
191
192
193 #endif