Rewrote implementation of response propagation in MR
[libdai.git] / include / dai / mr.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
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) 2007 Bastian Wemmenhove
8 * Copyright (C) 2007-2009 Joris Mooij [joris dot mooij at libdai dot org]
9 * Copyright (C) 2007 Radboud University Nijmegen, The Netherlands
10 */
11
12
13 /// \file
14 /// \brief Defines class MR, which implements loop corrections as proposed by Montanari and Rizzo
15
16
17 #ifndef __defined_libdai_mr_h
18 #define __defined_libdai_mr_h
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 <boost/dynamic_bitset.hpp>
29
30
31 namespace dai {
32
33
34 /// Approximate inference algorithm by Montanari and Rizzo [\ref MoR05]
35 /** \author Bastian Wemmenhove wrote the original implementation before it was merged into libDAI
36 * \todo Clean up code (use a BipartiteGraph-like implementation for the graph structure, and BBP for response propagation).
37 */
38 class MR : public DAIAlgFG {
39 private:
40 /// Is the underlying factor graph supported?
41 bool supported;
42 /// con[i] = connectivity of spin \a i
43 std::vector<size_t> con;
44 /// nb[i] are the neighbours of spin \a i
45 std::vector<std::vector<size_t> > nb;
46 /// tJ[i][_j] is the hyperbolic tangent of the interaction between spin \a i and its neighbour 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 /// M[i][_j] is \f$ M^{(i)}_j \f$
51 std::vector<std::vector<Real> > M;
52 /// The \a _j 'th neighbour of spin \a i has spin \a i as its kindex[i][_j]'th neighbour
53 std::vector<std::vector<size_t> > kindex;
54 /// Cavity correlations
55 std::vector<std::vector<std::vector<Real> > > cors;
56 /// Maximum connectivity
57 static const size_t kmax = 31;
58 /// Type used for managing a subset of neighbors
59 typedef boost::dynamic_bitset<> sub_nb;
60 /// Number of variables (spins)
61 size_t N;
62 /// Magnetizations
63 std::vector<Real> Mag;
64 /// Maximum difference encountered so far
65 Real _maxdiff;
66 /// Number of iterations needed
67 size_t _iters;
68
69 public:
70 /// Parameters for MR
71 struct Properties {
72 /// Enumeration of different types of update equations
73 /** The possible update equations are:
74 * - FULL full updates, slow but accurate
75 * - LINEAR linearized updates, faster but less accurate
76 */
77 DAI_ENUM(UpdateType,FULL,LINEAR);
78
79 /// Enumeration of different ways of initializing the cavity correlations
80 /** The possible cavity initializations are:
81 * - RESPPROP using response propagation ("linear response")
82 * - CLAMPING using clamping and BP
83 * - EXACT using JunctionTree
84 * - RESPPROPOLD using response propagation ("linear response"), old implementation
85 */
86 DAI_ENUM(InitType,RESPPROP,CLAMPING,EXACT,RESPPROPOLD);
87
88 /// Verbosity (amount of output sent to stderr)
89 size_t verbose;
90
91 /// Tolerance for convergence test
92 Real tol;
93
94 /// Update equations
95 UpdateType updates;
96
97 /// How to initialize the cavity correlations
98 InitType inits;
99 } props;
100
101 /// Name of this inference method
102 static const char *Name;
103
104 public:
105 /// Default constructor
106 MR() : DAIAlgFG(), supported(), con(), nb(), tJ(), theta(), M(), kindex(), cors(), N(), Mag(), _maxdiff(), _iters(), props() {}
107
108 /// Construct from FactorGraph \a fg and PropertySet \a opts
109 /** \param opts Parameters @see Properties
110 * \note This implementation only deals with binary variables and pairwise interactions.
111 * \throw NOT_IMPLEMENTED if \a fg has factors depending on three or more variables or has variables with more than two possible states.
112 */
113 MR( const FactorGraph &fg, const PropertySet &opts );
114
115
116 /// \name General InfAlg interface
117 //@{
118 virtual MR* clone() const { return new MR(*this); }
119 virtual std::string identify() const;
120 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
121 virtual Factor belief( const VarSet &/*vs*/ ) const { DAI_THROW(NOT_IMPLEMENTED); return Factor(); }
122 virtual Factor beliefV( size_t i ) const;
123 virtual std::vector<Factor> beliefs() const;
124 virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
125 virtual void init() {}
126 virtual void init( const VarSet &/*ns*/ ) { DAI_THROW(NOT_IMPLEMENTED); }
127 virtual Real run();
128 virtual Real maxDiff() const { return _maxdiff; }
129 virtual size_t Iterations() const { return _iters; }
130 virtual void setProperties( const PropertySet &opts );
131 virtual PropertySet getProperties() const;
132 virtual std::string printProperties() const;
133 //@}
134
135 private:
136 /// Returns the signum of \a a
137 Real sign(Real a) { return (a >= 0) ? 1.0 : -1.0; }
138
139 /// Initialize N, con, nb, tJ, theta
140 void init(size_t Nin, Real *_w, Real *_th);
141
142 /// Initialize kindex
143 void makekindex();
144
145 /// Initialize cors
146 Real init_cor();
147
148 /// Calculate cors using response propagation
149 Real init_cor_resp();
150
151 /// Calculate cors using response propagation (old implementation)
152 /** \deprecated Should be removed soon
153 */
154 Real init_cor_resp_old();
155
156 /// Iterate update equations for cavity fields
157 void solvemcav();
158
159 /// Calculate magnetizations
160 void solveM();
161
162 /// Calculate the product of all tJ[i][_j] for _j in A
163 /** \param i variable index
164 * \param A subset of neighbors of variable \a i
165 */
166 Real _tJ(size_t i, sub_nb A);
167
168 /// Calculate \f$ \Omega^{(i)}_{j,l} \f$ as defined in [\ref MoR05] eqn. (2.15)
169 Real Omega(size_t i, size_t _j, size_t _l);
170
171 /// Calculate \f$ T^{(i)}_A \f$ as defined in [\ref MoR05] eqn. (2.17) with \f$ A = \{l_1,l_2,\dots\} \f$
172 /** \param i variable index
173 * \param A subset of neighbors of variable \a i
174 */
175 Real T(size_t i, sub_nb A);
176
177 /// Calculates \f$ T^{(i)}_j \f$ where \a j is the \a _j 'th neighbor of \a i
178 Real T(size_t i, size_t _j);
179
180 /// Calculates \f$ \Gamma^{(i)}_{j,l_1l_2} \f$ as defined in [\ref MoR05] eqn. (2.16)
181 Real Gamma(size_t i, size_t _j, size_t _l1, size_t _l2);
182
183 /// Calculates \f$ \Gamma^{(i)}_{l_1l_2} \f$ as defined in [\ref MoK07] on page 1141
184 Real Gamma(size_t i, size_t _l1, size_t _l2);
185
186 /// Approximates moments of variables in \a A
187 /** Calculate the moment of variables in \a A from M and cors, neglecting higher order cumulants,
188 * defined as the sum over all partitions of A into subsets of cardinality two at most of the
189 * product of the cumulants (either first order, i.e. M, or second order, i.e. cors) of the
190 * entries of the partitions.
191 *
192 * \param i variable index
193 * \param A subset of neighbors of variable \a i
194 */
195 Real appM(size_t i, sub_nb A);
196
197 /// Calculate sum over all even/odd subsets B of \a A of _tJ(j,B) appM(j,B)
198 /** \param j variable index
199 * \param A subset of neighbors of variable \a j
200 * \param sum_even on return, will contain the sum over all even subsets
201 * \param sum_odd on return, will contain the sum over all odd subsets
202 */
203 void sum_subs(size_t j, sub_nb A, Real *sum_even, Real *sum_odd);
204 };
205
206
207 } // end of namespace dai
208
209
210 #endif