STK++ 0.9.13
STK::EMAlgo Class Reference

Implementation of the EM algorithm. More...

#include <STK_MixtureAlgo.h>

Inheritance diagram for STK::EMAlgo:
Inheritance graph

Public Member Functions

 EMAlgo ()
 default constructor
 
 EMAlgo (EMAlgo const &algo)
 Copy constructor.
 
virtual ~EMAlgo ()
 destructor
 
virtual EMAlgoclone () const
 clone pattern
 
virtual bool run ()
 run the algorithm on the model calling the eStep and mStep of the model until the maximal number of iteration is reached or the variation of the lnLikelihood is less than epsilon.
 
- Public Member Functions inherited from STK::IMixtureAlgo
virtual ~IMixtureAlgo ()
 destructor
 
int nbIterMax () const
 
int epsilon () const
 
Real threshold () const
 
void setModel (IMixtureComposer *p_model)
 set model
 
void setNbIterMax (int nbIterMax)
 set maximal number of iterations
 
void setEpsilon (Real epsilon)
 set tolerance value
 
void setThreshold (Real threshold)
 set threshold value
 
- Public Member Functions inherited from STK::IRunnerBase
String consterror () const
 get the last error message.
 

Additional Inherited Members

- Protected Member Functions inherited from STK::IMixtureAlgo
 IMixtureAlgo ()
 default constructor
 
 IMixtureAlgo (IMixtureAlgo const &algo)
 Copy constructor.
 
- Protected Member Functions inherited from STK::IRunnerBase
 IRunnerBase ()
 default constructor
 
 IRunnerBase (IRunnerBase const &runner)
 copy constructor
 
virtual ~IRunnerBase ()
 destructor
 
virtual void update ()
 update the runner.
 
- Protected Attributes inherited from STK::IMixtureAlgo
IMixtureComposerp_model_
 pointer on the mixture model
 
int nbIterMax_
 number of iterations of the algorithm
 
Real epsilon_
 tolerance of the algorithm.
 
Real threshold_
 Minimal number of individuals.
 
- Protected Attributes inherited from STK::IRunnerBase
String msg_error_
 String with the last error message.
 
bool hasRun_
 true if run has been used, false otherwise
 

Detailed Description

Implementation of the EM algorithm.

The EM algorithm call alternatively the steps:

  • paramUpdateStep()
  • eStep() until the maximum number of iterations is reached or the variation of the ln-likelihood is less than the tolerance.

Definition at line 51 of file STK_MixtureAlgo.h.

Constructor & Destructor Documentation

◆ EMAlgo() [1/2]

STK::EMAlgo::EMAlgo ( )
inline

default constructor

Definition at line 55 of file STK_MixtureAlgo.h.

55: IMixtureAlgo() {}
IMixtureAlgo()
default constructor

Referenced by clone().

◆ EMAlgo() [2/2]

STK::EMAlgo::EMAlgo ( EMAlgo const algo)
inline

Copy constructor.

Parameters
algothe algorithm to copy

Definition at line 58 of file STK_MixtureAlgo.h.

58: IMixtureAlgo(algo) {}

◆ ~EMAlgo()

virtual STK::EMAlgo::~EMAlgo ( )
inlinevirtual

destructor

Definition at line 60 of file STK_MixtureAlgo.h.

60{}

Member Function Documentation

◆ clone()

virtual EMAlgo * STK::EMAlgo::clone ( ) const
inlinevirtual

clone pattern

Definition at line 62 of file STK_MixtureAlgo.h.

62{ return new EMAlgo(*this);}
EMAlgo()
default constructor

References EMAlgo().

◆ run()

bool STK::EMAlgo::run ( )
virtual

run the algorithm on the model calling the eStep and mStep of the model until the maximal number of iteration is reached or the variation of the lnLikelihood is less than epsilon.

Returns
true if no error occur, false otherwise

Implements STK::IRunnerBase.

Definition at line 105 of file STK_MixtureAlgo.cpp.

106{
107#ifdef STK_MIXTURE_VERY_VERBOSE
108 stk_cout << _T("----------------------------\n");
109 stk_cout << _T("Entering EMAlgo::run() with:\n")
110 << _T("nbIterMax_ = ") << nbIterMax_ << _T("\n")
111 << _T("epsilon_ = ") << epsilon_ << _T("\n");
112#endif
113
114 try
115 {
116 Real currentLnLikelihood = p_model_->lnLikelihood();
117 int iter;
118 for (iter = 0; iter < nbIterMax_; iter++)
119 {
121 p_model_->pStep();
123 Real nb = p_model_->eStep();
124 if (nb<threshold_)
125 {
126 msg_error_ = STKERROR_1ARG(EMAlgo::run,nb,Not enough individuals after eStep\n);
127#ifdef STK_MIXTURE_VERBOSE
128 stk_cout << _T("An error occur in EMAlgo::run():\n") << msg_error_ << _T("\n");
129#endif
130 return false;
131 }
132 Real lnLikelihood = p_model_->lnLikelihood();
133 // no abs as the likelihood should increase
134 if ( (lnLikelihood - currentLnLikelihood) < epsilon_)
135 {
136#ifdef STK_MIXTURE_VERY_VERBOSE
137 stk_cout << _T("Terminating EMAlgo::run() with:\n")
138 << _T("iter = ") << iter << _T("\n")
139 << _T("delta = ") << lnLikelihood - currentLnLikelihood << _T("\n");
140#endif
141 break;
142 }
143 currentLnLikelihood = lnLikelihood;
144 }
145#ifdef STK_MIXTURE_VERBOSE
146 stk_cout << _T("In EMAlgo::run() iteration ") << iter << _T("terminated.\n")
147 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
148#endif
149 }
150 catch (Clust::exceptions const& error)
151 {
153#ifdef STK_MIXTURE_VERBOSE
154 stk_cout << _T("An error occur in EMAlgo::run():\n") << msg_error_ << _T("\n");
155#endif
156 return false;
157 }
158 return true;
159}
#define STKERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:61
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
virtual bool run()
run the algorithm on the model calling the eStep and mStep of the model until the maximal number of i...
int nbIterMax_
number of iterations of the algorithm
IMixtureComposer * p_model_
pointer on the mixture model
Real epsilon_
tolerance of the algorithm.
Real threshold_
Minimal number of individuals.
virtual void pStep()
Compute proportions using the ML estimates, default implementation.
virtual Real eStep()
compute the zi, the lnLikelihood of the current estimates and the next value of the tik.
virtual void paramUpdateStep()=0
Compute the proportions and the model parameters given the current tik mixture parameters.
virtual void imputationStep()
Impute the missing values.
String msg_error_
String with the last error message.
Definition STK_IRunner.h:96
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
String exceptionToString(exceptions const &type)
convert a Clust::exceptions to a String.
exceptions
Specific exceptions allowing to handle the erroros that can occur in the estimation process.
double Real
STK fundamental type of Real values.

References _T, STK::IMixtureAlgo::epsilon_, STK::IRunnerBase::error(), STK::IMixtureComposer::eStep(), STK::Clust::exceptionToString(), STK::IMixtureStatModel::imputationStep(), STK::IStatModelBase::lnLikelihood(), STK::IRunnerBase::msg_error_, STK::IMixtureAlgo::nbIterMax_, STK::IMixtureAlgo::p_model_, STK::IMixtureComposer::paramUpdateStep(), STK::IMixtureComposer::pStep(), run(), stk_cout, STKERROR_1ARG, and STK::IMixtureAlgo::threshold_.

Referenced by run().


The documentation for this class was generated from the following files: