libDAI
emalg.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 
9 #ifndef __defined_libdai_emalg_h
10 #define __defined_libdai_emalg_h
11 
12 
13 #include <vector>
14 #include <map>
15 
16 #include <dai/factor.h>
17 #include <dai/daialg.h>
18 #include <dai/evidence.h>
19 #include <dai/index.h>
20 #include <dai/properties.h>
21 
22 
26 
27 
28 namespace dai {
29 
30 
32 
51  public:
53  typedef ParameterEstimation* (*ParamEstFactory)( const PropertySet& );
54 
56  virtual ~ParameterEstimation() {}
57 
59  virtual ParameterEstimation* clone() const = 0;
60 
62 
67  static ParameterEstimation* construct( const std::string &method, const PropertySet &p );
68 
70  static void registerMethod( const std::string &method, const ParamEstFactory &f ) {
71  if( _registry == NULL )
73  (*_registry)[method] = f;
74  }
75 
77  virtual Prob estimate() = 0;
78 
80  virtual void addSufficientStatistics( const Prob &p ) = 0;
81 
83  virtual size_t probSize() const = 0;
84 
85  private:
87  static std::map<std::string, ParamEstFactory> *_registry;
88 
90  static void loadDefaultRegistry();
91 };
92 
93 
95 
98  private:
100  size_t _target_dim;
105 
106  public:
108 
112  CondProbEstimation( size_t target_dimension, const Prob &pseudocounts );
113 
115 
123  static ParameterEstimation* factory( const PropertySet &p );
124 
127 
129  virtual ~CondProbEstimation() {}
130 
132 
135  virtual Prob estimate();
136 
138  virtual void addSufficientStatistics( const Prob &p );
139 
141  virtual size_t probSize() const { return _stats.size(); }
142 };
143 
144 
146 
157  public:
159  typedef size_t FactorIndex;
161  typedef std::map<FactorIndex, std::vector<Var> > FactorOrientations;
162 
163  private:
165  std::map<FactorIndex, VarSet> _varsets;
167  std::map<FactorIndex, Permute> _perms;
174 
176 
180  static Permute calculatePermutation( const std::vector<Var> &varOrder, VarSet &outVS );
181 
184 
185  public:
187 
191  SharedParameters( const FactorOrientations &varorders, ParameterEstimation *estimation, bool ownPE=false );
192 
194 
197  SharedParameters( std::istream &is, const FactorGraph &fg );
198 
201  // If sp owns its _estimation object, we should clone it instead of copying the pointer
202  if( _ownEstimation )
204  }
205 
208  // If we own the _estimation object, we should delete it now
209  if( _ownEstimation )
210  delete _estimation;
211  }
212 
214 
220  void collectSufficientStatistics( InfAlg &alg );
221 
223 
228  void setParameters( FactorGraph &fg );
229 };
230 
231 
233 
236  private:
238  std::vector<SharedParameters> _params;
239 
240  public:
243 
245  MaximizationStep( std::vector<SharedParameters> &maximizations ) : _params(maximizations) {}
246 
248 
250  MaximizationStep( std::istream &is, const FactorGraph &fg_varlookup );
251 
253  void addExpectations( InfAlg &alg );
254 
256  void maximize( FactorGraph &fg );
257 
259 
260 
261  typedef std::vector<SharedParameters>::iterator iterator;
263  typedef std::vector<SharedParameters>::const_iterator const_iterator;
264 
266  iterator begin() { return _params.begin(); }
268  const_iterator begin() const { return _params.begin(); }
270  iterator end() { return _params.end(); }
272  const_iterator end() const { return _params.end(); }
274 };
275 
276 
278 
295 class EMAlg {
296  private:
299 
302 
304  std::vector<MaximizationStep> _msteps;
305 
307  size_t _iters;
308 
310  std::vector<Real> _lastLogZ;
311 
313  size_t _max_iters;
314 
317 
318  public:
320  static const std::string MAX_ITERS_KEY;
322  static const size_t MAX_ITERS_DEFAULT;
324  static const std::string LOG_Z_TOL_KEY;
326  static const Real LOG_Z_TOL_DEFAULT;
327 
329 
334  EMAlg( const Evidence &evidence, InfAlg &estep, std::vector<MaximizationStep> &msteps, const PropertySet &termconditions )
336  {
337  setTermConditions( termconditions );
338  }
339 
341 
343  EMAlg( const Evidence &evidence, InfAlg &estep, std::istream &mstep_file );
344 
346 
352  void setTermConditions( const PropertySet &p );
353 
355 
361  bool hasSatisfiedTermConditions() const;
362 
364  Real logZ() const { return _lastLogZ.back(); }
365 
367  size_t Iterations() const { return _iters; }
368 
370  const InfAlg& eStep() const { return _estep; }
371 
373 
375  Real iterate();
376 
378  Real iterate( MaximizationStep &mstep );
379 
381  void run();
382 
384 
385 
386  typedef std::vector<MaximizationStep>::iterator s_iterator;
388  typedef std::vector<MaximizationStep>::const_iterator const_s_iterator;
389 
391  s_iterator s_begin() { return _msteps.begin(); }
393  const_s_iterator s_begin() const { return _msteps.begin(); }
395  s_iterator s_end() { return _msteps.end(); }
397  const_s_iterator s_end() const { return _msteps.end(); }
399 };
400 
401 
402 } // end of namespace dai
403 
404 
410 #endif