STK++ 0.9.13
STK_MixtureAlgo.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/* run the CEM algorithm */
45{
46#ifdef STK_MIXTURE_VERY_VERBOSE
47 stk_cout << _T("Entering CEMAlgo::run() with:\n")
48 << _T("nbIterMax_ = ") << nbIterMax_
49 << _T(", epsilon_ = ") << epsilon_ << _T("\n");
50#endif
51 try
52 {
54 int iter;
55 for (iter = 0; iter < nbIterMax_; iter++)
56 {
58 {
60#ifdef STK_MIXTURE_VERBOSE
61 stk_cout << _T("An error occur in CEMAlgo::run():\n") << msg_error_ << _T("\n");
62#endif
63 return false;
64 }
66 p_model_->pStep();
68 Real nb = p_model_->eStep();
69 if (nb<threshold_)
70 {
72#ifdef STK_MIXTURE_VERBOSE
73 stk_cout << _T("An error occur in CEMAlgo::run():\n") << msg_error_ << _T("\n");
74#endif
75 return false;
76 }
77 Real lnLikelihood = p_model_->lnLikelihood();
78 if (std::abs(lnLikelihood - currentLnLikelihood) < epsilon_)
79 {
80#ifdef STK_MIXTURE_VERY_VERBOSE
81 stk_cout << _T("Terminating CEMAlgo::run() with:\n")
82 << _T("iter = ") << iter << _T("\n")
83 << _T("delta = ") << lnLikelihood - currentLnLikelihood << _T("\n");
84#endif
85 break;
86 }
87 currentLnLikelihood = lnLikelihood;
88 }
89#ifdef STK_MIXTURE_VERBOSE
90 stk_cout << _T("In EMAlgo::run() iteration ") << iter << _T("terminated.\n")
91 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
92#endif
93 }
94 catch (Clust::exceptions const& error)
95 {
97#ifdef STK_MIXTURE_VERBOSE
98 stk_cout << _T("An error occur in CEMAlgo::run():\n") << msg_error_ << _T("\n");
99#endif
100 return false;
101 }
102 return true;
103}
104
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 {
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 {
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}
160
162{
163#ifdef STK_MIXTURE_VERY_VERBOSE
164 stk_cout << _T("-----------------------------\n");
165 stk_cout << _T("Entering SEMAlgo::run() with:\n")
166 << _T("nbIterMax_ = ") << nbIterMax_ << _T("\n")
167 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
168#endif
169 bool result = true;
170 try
171 {
172 int iter;
173 for (iter = 0; iter < nbIterMax_; ++iter)
174 {
175 Real nb = p_model_->sStep(); // simulate labels
176 if (nb<threshold_)
177 {
179#ifdef STK_MIXTURE_VERBOSE
180 stk_cout << _T("An error occur in SEMAlgo::run():\n") << msg_error_ << _T("\n");
181#endif
182 result = false;
183 break;
184 }
185 p_model_->samplingStep(); // simulate missing values
186 p_model_->pStep(); // estimate proportions
187 p_model_->paramUpdateStep(); // estimate parameters
188 nb = p_model_->eStep(); // update tik and lnLikelihood
189 if (nb<threshold_)
190 {
192#ifdef STK_MIXTURE_VERBOSE
193 stk_cout << _T("An error occur in SEMAlgo::run():\n") << msg_error_ << _T("\n");
194#endif
195 return false;
196 break;
197 }
198 p_model_->storeIntermediateResults(iter+1); // store current parameters
199 }
200#ifdef STK_MIXTURE_VERBOSE
201 stk_cout << _T("In SEMAlgo::run() iterations terminated.\n")
202 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
203#endif
204 }
205 catch (Clust::exceptions const& error)
206 {
208#ifdef STK_MIXTURE_VERBOSE
209 stk_cout << _T("An error occur in SEMAlgo::run(): ") << msg_error_ << _T("\n");
210#endif
211 result = false;
212 }
213 if (result)
214 {
215 // set averaged parameters
217#ifdef STK_MIXTURE_VERY_VERBOSE
218 stk_cout << _T("\nIn SEMAlgo::run(), setParameters done.\n")
219 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
220#endif
221 }
222 else
224#ifdef STK_MIXTURE_VERY_VERBOSE
225 stk_cout << _T("Terminating SEMAlgo::run()\n");
226 stk_cout << _T("--------------------------\n");
227#endif
228 return result;
229}
230
232{
233#ifdef STK_MIXTURE_VERY_VERBOSE
234 stk_cout << _T("---------------------------------\n");
235 stk_cout << _T("Entering SemiSEMAlgo::run() with:\n")
236 << _T("nbIterMax_ = ") << nbIterMax_ << _T("\n")
237 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
238#endif
239 bool result = true;
240 try
241 {
242 int iter;
243 for (iter = 0; iter < nbIterMax_; ++iter)
244 {
246 p_model_->pStep();
248 Real nb = p_model_->eStep();
249 if (nb<threshold_)
250 {
252#ifdef STK_MIXTURE_VERBOSE
253 stk_cout << _T("An exception occur in SemiSEMAlgo::run(): ") << msg_error_ << _T("\n");
254#endif
255 result = false;
256 break;
257 }
258 p_model_->storeIntermediateResults(iter+1); // store current parameters
259 }
260#ifdef STK_MIXTURE_VERBOSE
261 stk_cout << _T("In SemiSEMAlgo::run() iteration ") << iter << _T("terminated.\n")
262 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
263#endif
264 }
265 catch (Clust::exceptions const& error)
266 {
268#ifdef STK_MIXTURE_VERBOSE
269 stk_cout << _T("An exception occur in SemiSEMAlgo::run(): ") << msg_error_ << _T("\n");
270#endif
271 result = false;
272 }
273 // set averaged parameters
274 if (result)
275 {
277#ifdef STK_MIXTURE_VERY_VERBOSE
278 stk_cout << _T("\nIn SemiSEMAlgo::run(), setParameters done.\n")
279 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
280#endif
281 }
282 else // no result
284#ifdef STK_MIXTURE_VERY_VERBOSE
285 stk_cout << _T("Terminating SemiSEMAlgo::run()\n");
286 stk_cout << _T("------------------------------\n");
287#endif
288 return result;
289}
290
291
292} // namespace STK
In this file we define the abstract base class for mixture models.
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
#define STKERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:61
In this file we define 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.
virtual bool run()
run the algorithm on the model calling cStep, mStep and eStep of the model until the maximal number o...
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 int cStep()
Replace tik by zik.
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 int sStep()
Simulate zi accordingly to tik and replace tik by zik by calling cStep().
virtual void paramUpdateStep()=0
Compute the proportions and the model parameters given the current tik mixture parameters.
virtual void imputationStep()
Impute the missing values.
virtual void setParametersStep()
Utility method allowing to signal to a mixture to set its parameters.
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
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...
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.