libDAI
mr.h
Go to the documentation of this file.
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 
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 
36 
38 class MR : public DAIAlgFG {
39  private:
41  bool supported;
42 
45 
47  std::vector<std::vector<Real> > tJ;
49  std::vector<Real> theta;
50 
52  std::vector<std::vector<Real> > M;
54  std::vector<std::vector<std::vector<Real> > > cors;
55 
57  typedef boost::dynamic_bitset<> sub_nb;
58 
60  std::vector<Real> Mag;
61 
64 
66  size_t _iters;
67 
68  public:
70  struct Properties {
72 
76  DAI_ENUM(UpdateType,FULL,LINEAR);
77 
79 
84  DAI_ENUM(InitType,RESPPROP,CLAMPING,EXACT);
85 
87  size_t verbose;
88 
91 
93  UpdateType updates;
94 
96  InitType inits;
97  } props;
98 
99  public:
101  MR() : DAIAlgFG(), supported(), G(), tJ(), theta(), M(), cors(), Mag(), _maxdiff(), _iters(), props() {}
102 
104 
109  MR( const FactorGraph &fg, const PropertySet &opts );
110 
111 
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;
131 
132  private:
135 
137  void propagateCavityFields();
138 
140  void calcMagnetizations();
141 
143 
146  Real _tJ(size_t i, sub_nb A);
147 
149  Real Omega(size_t i, size_t _j, size_t _l);
150 
152 
155  Real T(size_t i, sub_nb A);
156 
158  Real T(size_t i, size_t _j);
159 
161  Real Gamma(size_t i, size_t _j, size_t _l1, size_t _l2);
162 
164  Real Gamma(size_t i, size_t _l1, size_t _l2);
165 
167 
175  Real appM(size_t i, sub_nb A);
176 
178 
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
virtual MR * construct(const FactorGraph &fg, const PropertySet &opts) const
Returns a pointer to a newly constructed inference algorithm.
Definition: mr.h:115
DAI_ENUM(UpdateType, FULL, LINEAR)
Enumeration of different types of update equations.
virtual Real maxDiff() const
Returns maximum difference between single variable beliefs in the last iteration. ...
Definition: mr.h:125
void calcMagnetizations()
Calculate magnetizations.
Definition: mr.cpp:249
std::vector< std::vector< Real > > M
M[i][_j] is .
Definition: mr.h:52
Real calcCavityCorrelations()
Initialize cors.
Definition: mr.cpp:276
virtual MR * clone() const
Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor) ...
Definition: mr.h:114
size_t verbose
Verbosity (amount of output sent to stderr)
Definition: mr.h:87
Represents a factor graph.
Definition: factorgraph.h:65
virtual void init()
Initializes all data structures of the approximate inference algorithm.
Definition: mr.h:122
InitType inits
How to initialize the cavity correlations.
Definition: mr.h:96
FactorGraph & fg()
Returns reference to underlying FactorGraph.
Definition: daialg.h:221
std::vector< std::vector< std::vector< Real > > > cors
Cavity correlations.
Definition: mr.h:54
virtual PropertySet getProperties() const
Returns parameters of this inference algorithm converted into a PropertySet.
Definition: mr.cpp:45
Real tol
Tolerance for convergence test.
Definition: mr.h:90
Real Omega(size_t i, size_t _j, size_t _l)
Calculate as defined in [MoR05] eqn. (2.15)
Definition: mr.cpp:86
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
boost::dynamic_bitset sub_nb
Type used for managing a subset of neighbors.
Definition: mr.h:57
Real Gamma(size_t i, size_t _j, size_t _l1, size_t _l2)
Calculates as defined in [MoR05] eqn. (2.16)
Definition: mr.cpp:95
void sum_subs(size_t j, sub_nb A, Real *sum_even, Real *sum_odd)
Calculate sum over all even/odd subsets B of A of _tJ(j,B) appM(j,B)
Definition: mr.cpp:146
std::vector< Real > theta
theta[i] is the local field on spin i
Definition: mr.h:49
Real _maxdiff
Maximum difference encountered so far.
Definition: mr.h:63
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
virtual std::string printProperties() const
Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1...
Definition: mr.cpp:55
virtual void setProperties(const PropertySet &opts)
Set parameters of this inference algorithm.
Definition: mr.cpp:30
Represents a set of variables.
Definition: varset.h:94
UpdateType updates
Update equations.
Definition: mr.h:93
MR()
Default constructor.
Definition: mr.h:101
virtual size_t Iterations() const
Returns number of iterations done (one iteration passes over the complete factorgraph).
Definition: mr.h:126
Defines the Exception class and macros for throwing exceptions and doing assertions.
virtual Real run()
Runs the approximate inference algorithm.
Definition: mr.cpp:346
virtual Real logZ() const
Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)...
Definition: mr.h:121
virtual void init(const VarSet &)
Initializes all data structures corresponding to some set of variables.
Definition: mr.h:123
bool supported
Is the underlying factor graph supported?
Definition: mr.h:41
Real appM(size_t i, sub_nb A)
Approximates moments of variables in A.
Definition: mr.cpp:127
Defines the Property and PropertySet classes, which are mainly used for managing parameters of infere...
GraphAL G
The interaction graph (Markov graph)
Definition: mr.h:44
Defines the GraphAL class, which represents an undirected graph as an adjacency list.
size_t _iters
Number of iterations needed.
Definition: mr.h:66
virtual std::string name() const
Returns the name of the algorithm.
Definition: mr.h:116
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
Real T(size_t i, sub_nb A)
Calculate as defined in [MoR05] eqn. (2.17) with .
Definition: mr.cpp:66
virtual std::vector< Factor > beliefs() const
Returns all beliefs (approximate marginal probability distributions) calculated by the algorithm...
Definition: mr.cpp:397
void propagateCavityFields()
Iterate update equations for cavity fields.
Definition: mr.cpp:172
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
Parameters for MR.
Definition: mr.h:70
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
std::vector< Real > Mag
Magnetizations.
Definition: mr.h:60
Defines the general interface for inference methods in libDAI (classes InfAlg, DaiAlg<>, DaiAlgFG and DaiAlgRG).
virtual Factor beliefV(size_t i) const
Returns the (approximate) marginal probability distribution of the variable with index i...
Definition: mr.cpp:373
Allows the user to specify which algorithms will be built into libDAI.
virtual Factor belief(const Var &v) const
Returns the (approximate) marginal probability distribution of a variable.
Definition: mr.h:117
Represents the neighborhood structure of nodes in an undirected graph.
Definition: graph.h:114
Combines the abstract base class InfAlg with a graphical model (e.g., a FactorGraph or RegionGraph)...
Definition: daialg.h:207
std::vector< std::vector< Real > > tJ
tJ[i][_j] is the hyperbolic tangent of the interaction between spin i and its neighbour G...
Definition: mr.h:47
Real _tJ(size_t i, sub_nb A)
Calculate the product of all tJ[i][_j] for _j in A.
Definition: mr.cpp:118
Approximate inference algorithm by Montanari and Rizzo [MoR05].
Definition: mr.h:38