STK++ 0.9.13
STK_MixtureAlgoLearn.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Clustering
27 * created on: 16 oct. 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 * Originally created by Parmeet bhatia <b..._DOT_p..._AT_gmail_Dot_com>
30 **/
31
36#include <Sdk.h>
37
40
41namespace STK
42{
43
45{
46#ifdef STK_MIXTURE_VERY_VERBOSE
47 stk_cout << _T("--------------------------------\n");
48 stk_cout << _T("Entering ImputeAlgo::run() with:\n")
49 << _T("nbIterMax_ = ") << nbIterMax_ << _T("\n")
50 << _T("epsilon_ = ") << epsilon_ << _T("\n");
51#endif
52
53 try
54 {
56 int iter;
57 for (iter = 0; iter < nbIterMax_; iter++)
58 {
61 Real lnLikelihood = p_model_->lnLikelihood();
62 // no abs as the likelihood should increase
63 if ( (lnLikelihood - currentLnLikelihood) < epsilon_)
64 {
65#ifdef STK_MIXTURE_VERY_VERBOSE
66 stk_cout << _T("Terminating ImputeAlgo::run() with:\n")
67 << _T("iter = ") << iter << _T("\n")
68 << _T("delta = ") << lnLikelihood - currentLnLikelihood << _T("\n");
69#endif
70 break;
71 }
72 currentLnLikelihood = lnLikelihood;
73 }
74 // finalize and compute posterior probabilities and predicted values
77#ifdef STK_MIXTURE_VERBOSE
78 stk_cout << _T("In ImputeAlgo::run() iteration ") << iter << _T("terminated.\n")
79 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
80#endif
81 }
82 catch (Clust::exceptions const& error)
83 {
85#ifdef STK_MIXTURE_VERBOSE
86 stk_cout << _T("An error occur in ImputeAlgo::run():\n") << msg_error_ << _T("\n");
87#endif
88 return false;
89 }
90 return true;
91}
92
94{
95#ifdef STK_MIXTURE_VERBOSE
96 stk_cout << _T("-------------------------------\n");
97 stk_cout << _T("Entering SimulAlgo::run() with:\n")
98 << _T("nbIterMax_ = ") << nbIterMax_ << _T("\n")
99 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
100#endif
101#ifdef STK_MIXTURE_VERY_VERBOSE
102 stk_cout << _T("Parameters of the model\n");
104#endif
105 bool result = true;
106 try
107 {
108 int iter;
109 for (iter = 0; iter < nbIterMax_; ++iter)
110 {
111 p_model_->samplingStep(); // simulate missing values
112 p_model_->paramUpdateStep(); // estimate parameters
113 p_model_->storeIntermediateResults(iter+1); // store current parameters
114 }
115#ifdef STK_MIXTURE_VERBOSE
116 stk_cout << _T("In SimulAlgo::run() iterations terminated.\n")
117 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
118#endif
119#ifdef STK_MIXTURE_VERY_VERBOSE
120 stk_cout << _T("Parameters of the model\n");
122#endif
123 }
124 catch (Clust::exceptions const& error)
125 {
128#ifdef STK_MIXTURE_VERBOSE
129 stk_cout << _T("An error occur in SimulAlgo::run(): ") << msg_error_ << _T("\n");
130#endif
131 result = false;
132 }
133 if (result)
134 {
135 // set averaged parameters
137 p_model_->mapStep();
138#ifdef STK_MIXTURE_VERY_VERBOSE
139 stk_cout << _T("\nIn SimulAlgo::run(), setParameters done.\n")
140 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
141#endif
142 }
143 else
145#ifdef STK_MIXTURE_VERY_VERBOSE
146 stk_cout << _T("Terminating SimulAlgo::run()\n");
147 stk_cout << _T("----------------------------\n");
148#endif
149#ifdef STK_MIXTURE_VERY_VERBOSE
150 stk_cout << _T("Parameters of the model\n");
152#endif
153 return result;
154}
155
156} // namespace STK
In this file we define the interface base class for learners.
In this file we define learning mixture algorithms.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
This file include all the other header files of the project Sdk.
Real epsilon_
tolerance of the algorithm.
IMixtureLearner * p_model_
pointer on the mixture model
int nbIterMax_
maximal number of iterations of the algorithm
virtual void paramUpdateStep()=0
Compute the model parameters given the current mixture parameters and imputation/simulation of the mi...
virtual void mapStep()
Compute ziPred using the Map estimate.
virtual void writeParameters(ostream &os) const
write the parameters of the model in the stream os.
virtual void imputationStep()
Impute the missing values.
virtual void finalizeStep()
Finalize the estimation of the model.
virtual void releaseIntermediateResults()
This step can be used to signal to the mixtures that they must release the stored results.
virtual void storeIntermediateResults(int iteration)
This step can be used to signal to the mixtures that they must store results.
virtual void samplingStep()
Simulation of all the latent variables and/or missing data excluding class labels.
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
virtual bool run()
run the algorithm on the model calling the eStep and mStep of the model until the maximal number of i...
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
virtual bool run()
run the algorithm on the model calling sStep, mStep and eStep of the model until the maximal number o...
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.
The namespace STK is the main domain space of the Statistical ToolKit project.