STK++ 0.9.13
STK_MixtureAlgoPredict.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{
47#ifdef STK_MIXTURE_VERBOSE
48 stk_cout << _T("-------------------------------\n");
49 stk_cout << _T("Entering EMPredict::run() with:\n")
50 << _T("nbIterBurn_ = ") << nbIterBurn_ << _T("\n")
51 << _T("nbIterLong_ = ") << nbIterLong_ << _T("\n");
52#endif
53 try
54 {
56 if (!burnStep())
57 {
60 return false;
61 }
63 int iter;
64 for (iter = 0; iter < nbIterLong_; iter++)
65 {
67 p_model_->eStep();
68 Real lnLikelihood = p_model_->lnLikelihood();
69 if ( (lnLikelihood - currentLnLikelihood) < epsilon_) // no abs as the likelihood should increase
70 {
71#ifdef STK_MIXTURE_VERY_VERBOSE
72 stk_cout << _T("Terminating EMPredict::run() with:\n")
73 << _T("iter = ") << iter << _T("\n")
74 << _T("delta = ") << lnLikelihood - currentLnLikelihood << _T("\n");
75#endif
76 break;
77 }
78 currentLnLikelihood = lnLikelihood;
79 }
80#ifdef STK_MIXTURE_VERBOSE
81 stk_cout << _T("In EMPredict::run() iteration ") << iter << _T("terminated.\n")
82 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
83#endif
84 }
85 catch (Clust::exceptions const& error)
86 {
88#ifdef STK_MIXTURE_VERBOSE
89 stk_cout << _T("An error occur in EMPredict::run():\n") << msg_error_ << _T("\n");
90#endif
93 return false;
94 }
95 catch (Exception const& error)
96 {
98#ifdef STK_MIXTURE_VERBOSE
99 stk_cout << _T("An error occur in EMPredict::run():\n") << msg_error_ << _T("\n");
100#endif
101 p_model_->mapStep();
103 return false;
104 }
105 p_model_->mapStep();
107#ifdef STK_MIXTURE_VERY_VERBOSE
108 stk_cout << _T("Terminating EMPredict::run()\n");
109 stk_cout << _T("----------------------------\n");
110#endif
111 return true;
112}
113
115{
117 { return predictBayesClassifier();}
118
119#ifdef STK_MIXTURE_VERY_VERBOSE
120 stk_cout << _T("------------------------------------\n");
121 stk_cout << _T("Entering SemiSEMPredict::run() with:\n");
122 stk_cout << _T("nbIterBurn_ = ") << nbIterBurn_ << _T("\n")
123 << _T("nbIterLong_ = ") << nbIterLong_
124 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
125#endif
126 try
127 {
129 if (!burnStep())
130 {
131 p_model_->mapStep();
133 return false;
134 }
135 int iter;
136 for (iter = 0; iter < nbIterLong_; ++iter)
137 {
139 p_model_->eStep();
140 p_model_->storeIntermediateResults(iter+1); // store current parameters
141 }
142 }
143 catch (Clust::exceptions const& error)
144 {
146#ifdef STK_MIXTURE_VERBOSE
147 stk_cout << _T("An exception occur in SemiSEMPredict::run(): ") << msg_error_ << _T("\n");
148#endif
150 p_model_->mapStep();
152 return false;
153 }
154 catch (Exception const& error)
155 {
157#ifdef STK_MIXTURE_VERBOSE
158 stk_cout << _T("An error occur in SemiSEMPredict::run():\n") << msg_error_ << _T("\n");
159#endif
161 p_model_->mapStep();
163 return false;
164 }
165#ifdef STK_MIXTURE_VERBOSE
166 stk_cout << _T("In SemiSEMPredict::run() iterations terminated.\n")
167 << _T("p_model_->lnLikelihood = ") << p_model_->lnLikelihood() << _T("\n");
168#endif
169 // set averaged parameters
171 p_model_->mapStep();
173#ifdef STK_MIXTURE_VERY_VERBOSE
174 stk_cout << _T("Terminating SemiSEMPredict::run()\n");
175 stk_cout << _T("---------------------------------\n");
176#endif
177 return true;
178}
179
180
181} // namespace STK
In this file we define the abstract base class for mixture models.
In this file we define algorithms for predicting in a mixture model.
#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 until the maximal number of iteration or the threshold is reached.
Sdk class for all library Exceptions.
bool predictBayesClassifier()
predict class labels when there is no missing values.
int nbIterLong_
maximal number of iterations of the algorithm
bool burnStep()
Perform burn step using SEM algorithm.
int nbIterBurn_
Number of burning iterations of the algorithm.
Real epsilon_
tolerance of the algorithm.
IMixtureComposer * p_model_
pointer on the mixture model
virtual void initializeStep()
Initialize the model before its first use.
virtual Real eStep()
compute the zi, the lnLikelihood of the current estimates and the next value of the tik.
virtual void mapStep()
Compute zi using the Map estimate.
virtual void finalizeStep()
Finalize the estimation of the model.
int computeNbMissingValues() const
compute the missing values of the model.
virtual void imputationStep()
Impute the missing values.
virtual void setParametersStep()
Utility method allowing to signal to a mixture to set its parameters.
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 until the maximal number of iteration is reached.
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.