Improved documentation of include/dai/emalg.h
[libdai.git] / include / dai / emalg.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) 2009 Charles Vaske [cvaske at soe dot ucsc dot edu]
8 * Copyright (C) 2009 University of California, Santa Cruz
9 */
10
11
12 #ifndef __defined_libdai_emalg_h
13 #define __defined_libdai_emalg_h
14
15
16 #include <vector>
17 #include <map>
18
19 #include <dai/factor.h>
20 #include <dai/daialg.h>
21 #include <dai/evidence.h>
22 #include <dai/index.h>
23 #include <dai/properties.h>
24
25
26 /// \file
27 /// \brief Defines classes related to Expectation Maximization: EMAlg, ParameterEstimation, CondProbEstimation and SharedParameters
28
29
30 namespace dai {
31
32
33 /// Base class for parameter estimation methods.
34 /** This class defines the general interface of parameter estimation methods.
35 *
36 * Implementations of this interface (see e.g. CondProbEstimation) should
37 * register a factory function (virtual constructor) via the static
38 * registerMethod() function.
39 * This factory function should return a pointer to a newly constructed
40 * object, whose type is a subclass of ParameterEstimation, and gets as
41 * input a PropertySet of parameters. After a subclass has been registered,
42 * instances of it can be constructed using the construct() method.
43 *
44 * Implementations are responsible for collecting data from a probability
45 * vector passed to it from a SharedParameters container object.
46 *
47 * The default registry only contains CondProbEstimation, named
48 * "ConditionalProbEstimation".
49 *
50 * \author Charles Vaske
51 */
52 class ParameterEstimation {
53 public:
54 /// Type of pointer to factory function.
55 typedef ParameterEstimation* (*ParamEstFactory)( const PropertySet& );
56
57 /// Virtual destructor for deleting pointers to derived classes.
58 virtual ~ParameterEstimation() {}
59
60 /// Virtual copy constructor.
61 virtual ParameterEstimation* clone() const = 0;
62
63 /// General factory method that constructs the desired ParameterEstimation subclass
64 /** \param method Name of the subclass that should be constructed;
65 * \param p Parameters passed to constructor of subclass.
66 * \note \a method should either be in the default registry or should be registered first using registerMethod().
67 */
68 static ParameterEstimation* construct( const std::string &method, const PropertySet &p );
69
70 /// Register a subclass so that it can be used with construct().
71 static void registerMethod( const std::string &method, const ParamEstFactory &f ) {
72 if( _registry == NULL )
73 loadDefaultRegistry();
74 (*_registry)[method] = f;
75 }
76
77 /// Estimate the factor using the accumulated sufficient statistics and reset.
78 virtual Prob estimate() = 0;
79
80 /// Accumulate the sufficient statistics for \a p.
81 virtual void addSufficientStatistics( const Prob &p ) = 0;
82
83 /// Returns the size of the Prob that should be passed to addSufficientStatistics.
84 virtual size_t probSize() const = 0;
85
86 private:
87 /// A static registry containing all methods registered so far.
88 static std::map<std::string, ParamEstFactory> *_registry;
89
90 /// Registers default ParameterEstimation subclasses (currently, only CondProbEstimation).
91 static void loadDefaultRegistry();
92 };
93
94
95 /// Estimates the parameters of a conditional probability table, using pseudocounts.
96 /** \author Charles Vaske
97 */
98 class CondProbEstimation : private ParameterEstimation {
99 private:
100 /// Number of states of the variable of interest
101 size_t _target_dim;
102 /// Current pseudocounts
103 Prob _stats;
104 /// Initial pseudocounts
105 Prob _initial_stats;
106
107 public:
108 /// Constructor
109 /** For a conditional probability \f$ P( X | Y ) \f$,
110 * \param target_dimension should equal \f$ | X | \f$
111 * \param pseudocounts are the initial pseudocounts, of length \f$ |X| \cdot |Y| \f$
112 */
113 CondProbEstimation( size_t target_dimension, const Prob &pseudocounts );
114
115 /// Virtual constructor, using a PropertySet.
116 /** Some keys in the PropertySet are required.
117 * For a conditional probability \f$ P( X | Y ) \f$,
118 * - \a target_dimension should be equal to \f$ | X | \f$
119 * - \a total_dimension should be equal to \f$ |X| \cdot |Y| \f$
120 *
121 * An optional key is:
122 * - \a pseudo_count which specifies the initial counts (defaults to 1)
123 */
124 static ParameterEstimation* factory( const PropertySet &p );
125
126 /// Virtual copy constructor
127 virtual ParameterEstimation* clone() const { return new CondProbEstimation( _target_dim, _initial_stats ); }
128
129 /// Virtual destructor
130 virtual ~CondProbEstimation() {}
131
132 /// Returns an estimate of the conditional probability distribution.
133 /** The format of the resulting Prob keeps all the values for
134 * \f$ P(X | Y=y) \f$ in sequential order in the array.
135 */
136 virtual Prob estimate();
137
138 /// Accumulate sufficient statistics from the expectations in \a p
139 virtual void addSufficientStatistics( const Prob &p );
140
141 /// Returns the required size for arguments to addSufficientStatistics().
142 virtual size_t probSize() const { return _stats.size(); }
143 };
144
145
146 /// Represents a single factor or set of factors whose parameters should be estimated.
147 /** To ensure that parameters can be shared between different factors during
148 * EM learning, each factor's values are reordered to match a desired variable
149 * ordering. The ordering of the variables in a factor may therefore differ
150 * from the canonical ordering used in libDAI. The SharedParameters
151 * class combines one or more factors (together with the specified orderings
152 * of the variables) with a ParameterEstimation object, taking care of the
153 * necessary permutations of the factor entries / parameters.
154 *
155 * \author Charles Vaske
156 */
157 class SharedParameters {
158 public:
159 /// Convenience label for an index of a factor in a FactorGraph.
160 typedef size_t FactorIndex;
161 /// Convenience label for a grouping of factor orientations.
162 typedef std::map<FactorIndex, std::vector<Var> > FactorOrientations;
163
164 private:
165 /// Maps factor indices to the corresponding VarSets
166 std::map<FactorIndex, VarSet> _varsets;
167 /// Maps factor indices to the corresponding Permute objects that permute the desired ordering into the canonical ordering
168 std::map<FactorIndex, Permute> _perms;
169 /// Maps factor indices to the corresponding desired variable orderings
170 FactorOrientations _varorders;
171 /// Parameter estimation method to be used
172 ParameterEstimation *_estimation;
173 /// Indicates whether \c *this gets ownership of _estimation
174 bool _ownEstimation;
175
176 /// Calculates the permutation that permutes the variables in varorder into the canonical ordering
177 /** \param varorder Given ordering of variables
178 * \param outVS Contains variables in \varorder represented as a VarSet
179 * \return Permute object for permuting variables in varorder into the canonical libDAI ordering
180 */
181 static Permute calculatePermutation( const std::vector<Var> &varorder, VarSet &outVS );
182
183 /// Initializes _varsets and _perms from _varorders and checks whether their state spaces correspond with _estimation.probSize()
184 void setPermsAndVarSetsFromVarOrders();
185
186 public:
187 /// Constructor
188 /** \param varorders all the factor orientations for this parameter
189 * \param estimation a pointer to the parameter estimation method
190 * \param ownPE whether the constructed object gets ownership of \a estimation
191 */
192 SharedParameters( const FactorOrientations &varorders, ParameterEstimation *estimation, bool ownPE=false );
193
194 /// Construct a SharedParameters object from an input stream \a is and a factor graph \a fg
195 /** \see \ref fileformats-emalg-sharedparameters
196 */
197 SharedParameters( std::istream &is, const FactorGraph &fg );
198
199 /// Copy constructor
200 SharedParameters( const SharedParameters &sp ) : _varsets(sp._varsets), _perms(sp._perms), _varorders(sp._varorders), _estimation(sp._estimation), _ownEstimation(sp._ownEstimation) {
201 // If sp owns its _estimation object, we should clone it instead of copying the pointer
202 if( _ownEstimation )
203 _estimation = _estimation->clone();
204 }
205
206 /// Destructor
207 ~SharedParameters() {
208 // If we own the _estimation object, we should delete it now
209 if( _ownEstimation )
210 delete _estimation;
211 }
212
213 /// Collect the sufficient statistics from expected values (beliefs) according to \a alg
214 /** For each of the relevant factors (that shares the parameters we are interested in),
215 * the corresponding belief according to \a alg is obtained and its entries are permuted
216 * such that their ordering corresponds with the shared parameters that we are estimating.
217 * Then, the parameter estimation subclass method addSufficientStatistics() is called with
218 * this vector of expected values of the parameters as input.
219 */
220 void collectSufficientStatistics( InfAlg &alg );
221
222 /// Estimate and set the shared parameters
223 /** Based on the sufficient statistics collected so far, the shared parameters are estimated
224 * using the parameter estimation subclass method estimate(). Then, each of the relevant
225 * factors in \a fg (that shares the parameters we are interested in) is set according
226 * to those parameters (permuting the parameters accordingly).
227 */
228 void setParameters( FactorGraph &fg );
229 };
230
231
232 /// A MaximizationStep groups together several parameter estimation tasks (SharedParameters objects) into a single unit.
233 /** \author Charles Vaske
234 */
235 class MaximizationStep {
236 private:
237 std::vector<SharedParameters> _params;
238
239 public:
240 /// Default constructor
241 MaximizationStep() : _params() {}
242
243 /// Construct MaximizationStep from a vector of parameter estimation tasks
244 MaximizationStep( std::vector<SharedParameters> &maximizations ) : _params(maximizations) {}
245
246 /// Constructor from an input stream and a corresponding factor graph
247 /** \see \ref fileformats-emalg-maximizationstep
248 */
249 MaximizationStep( std::istream &is, const FactorGraph &fg_varlookup );
250
251 /// Collect the beliefs from this InfAlg as expectations for the next Maximization step
252 void addExpectations( InfAlg &alg );
253
254 /// Using all of the currently added expectations, make new factors with maximized parameters and set them in the FactorGraph.
255 void maximize( FactorGraph &fg );
256
257 /// \name Iterator interface
258 //@{
259 /// Iterator over the parameter estimation tasks
260 typedef std::vector<SharedParameters>::iterator iterator;
261 /// Constant iterator over the parameter estimation tasks
262 typedef std::vector<SharedParameters>::const_iterator const_iterator;
263
264 /// Returns iterator that points to the first parameter estimation task
265 iterator begin() { return _params.begin(); }
266 /// Returns constant iterator that points to the first parameter estimation task
267 const_iterator begin() const { return _params.begin(); }
268 /// Returns iterator that points beyond the last parameter estimation task
269 iterator end() { return _params.end(); }
270 /// Returns constant iterator that points beyond the last parameter estimation task
271 const_iterator end() const { return _params.end(); }
272 //@}
273 };
274
275
276 /// EMAlg performs Expectation Maximization to learn factor parameters.
277 /** This requires specifying:
278 * - Evidence (instances of observations from the graphical model),
279 * - InfAlg for performing the E-step (which includes the factor graph),
280 * - a vector of MaximizationStep 's steps to be performed.
281 *
282 * This implementation can perform incremental EM by using multiple
283 * MaximizationSteps. An expectation step is performed between execution
284 * of each MaximizationStep. A call to iterate() will cycle through all
285 * MaximizationStep 's. A call to run() will call iterate() until the
286 * termination criteria have been met.
287 *
288 * Having multiple and separate maximization steps allows for maximizing some
289 * parameters, performing another E-step, and then maximizing separate
290 * parameters, which may result in faster convergence in some cases.
291 *
292 * \author Charles Vaske
293 */
294 class EMAlg {
295 private:
296 /// All the data samples used during learning
297 const Evidence &_evidence;
298
299 /// How to do the expectation step
300 InfAlg &_estep;
301
302 /// The maximization steps to take
303 std::vector<MaximizationStep> _msteps;
304
305 /// Number of iterations done
306 size_t _iters;
307
308 /// History of likelihoods
309 std::vector<Real> _lastLogZ;
310
311 /// Maximum number of iterations
312 size_t _max_iters;
313
314 /// Convergence tolerance
315 Real _log_z_tol;
316
317 public:
318 /// Key for setting maximum iterations
319 static const std::string MAX_ITERS_KEY;
320 /// Default maximum iterations
321 static const size_t MAX_ITERS_DEFAULT;
322 /// Key for setting likelihood termination condition
323 static const std::string LOG_Z_TOL_KEY;
324 /// Default likelihood tolerance
325 static const Real LOG_Z_TOL_DEFAULT;
326
327 /// Construct an EMAlg from several objects
328 /** \param evidence Specifies the observed evidence
329 * \param estep Inference algorithm to be used for the E-step
330 * \param msteps Vector of maximization steps, each of which is a group of parameter estimation tasks
331 * \param termconditions Termination conditions @see setTermConditions()
332 */
333 EMAlg( const Evidence &evidence, InfAlg &estep, std::vector<MaximizationStep> &msteps, const PropertySet &termconditions )
334 : _evidence(evidence), _estep(estep), _msteps(msteps), _iters(0), _lastLogZ(), _max_iters(MAX_ITERS_DEFAULT), _log_z_tol(LOG_Z_TOL_DEFAULT)
335 {
336 setTermConditions( termconditions );
337 }
338
339 /// Construct an EMAlg from Evidence \a evidence, an InfAlg \a estep, and an input stream \a mstep_file
340 /** \see \ref fileformats-emalg
341 */
342 EMAlg( const Evidence &evidence, InfAlg &estep, std::istream &mstep_file );
343
344 /// Change the conditions for termination
345 /** There are two possible parameters in the PropertySet \a p:
346 * - \a max_iters maximum number of iterations
347 * - \a log_z_tol critical proportion of increase in logZ
348 *
349 * \see hasSatisifiedTermConditions()
350 */
351 void setTermConditions( const PropertySet &p );
352
353 /// Determine if the termination conditions have been met.
354 /** There are two sufficient termination conditions:
355 * -# the maximum number of iterations has been performed
356 * -# the ratio of logZ increase over previous logZ is less than the
357 * tolerance, i.e.,
358 * \f$ \frac{\log(Z_t) - \log(Z_{t-1})}{| \log(Z_{t-1}) | } < \mathrm{tol} \f$.
359 */
360 bool hasSatisfiedTermConditions() const;
361
362 /// Return the last calculated log likelihood
363 Real logZ() const { return _lastLogZ.back(); }
364
365 /// Returns number of iterations done so far
366 size_t Iterations() const { return _iters; }
367
368 /// Get the E-step method used
369 const InfAlg& eStep() const { return _estep; }
370
371 /// Iterate once over all maximization steps
372 /** \return Log-likelihood after iteration
373 */
374 Real iterate();
375
376 /// Iterate over a single MaximizationStep
377 Real iterate( MaximizationStep &mstep );
378
379 /// Iterate until termination conditions are satisfied
380 void run();
381
382 /// \name Iterator interface
383 //@{
384 /// Iterator over the maximization steps
385 typedef std::vector<MaximizationStep>::iterator s_iterator;
386 /// Constant iterator over the maximization steps
387 typedef std::vector<MaximizationStep>::const_iterator const_s_iterator;
388
389 /// Returns iterator that points to the first maximization step
390 s_iterator s_begin() { return _msteps.begin(); }
391 /// Returns constant iterator that points to the first maximization step
392 const_s_iterator s_begin() const { return _msteps.begin(); }
393 /// Returns iterator that points beyond the last maximization step
394 s_iterator s_end() { return _msteps.end(); }
395 /// Returns constant iterator that points beyond the last maximization step
396 const_s_iterator s_end() const { return _msteps.end(); }
397 //@}
398 };
399
400
401 } // end of namespace dai
402
403
404 #endif