STK++ 0.9.13
STK_MixtureComposer.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff
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: 14 nov. 2013
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
38
39namespace STK
40{
41
42/* Constructor.
43 * @param nbCluster,nbSample, number of clusters and samples.
44 */
45MixtureComposer::MixtureComposer( int nbSample, int nbCluster)
46 : IMixtureComposer( nbSample, nbCluster)
47 , meanlnLikelihood_(0.)
49
50/* copy constructor.
51 * @param composer the composer to copy
52 */
55 , meanlnLikelihood_(composer.meanlnLikelihood_)
56{}
57
58/* destructor */
60
61/* clone pattern */
64
66{
67 // set dimensions
68 MixtureComposer* p_composer = new MixtureComposer(nbSample(), nbCluster());
69 p_composer->createComposer( v_mixtures_);
70 return p_composer;
71}
72
74{
75 Real sum=0.0;
76 for (ConstMixtIterator it = v_mixtures_.begin() ; it != v_mixtures_.end(); ++it)
77 { sum += (*it)->lnComponentProbability(i,k);}
78 return sum;
79}
80
82{
83#ifdef STK_MIXTURE_VERY_VERBOSE
84 stk_cout << _T("Entering MixtureComposer::paramUpdateStep()\n");
85#endif
86 for (MixtIterator it = v_mixtures_.begin() ; it != v_mixtures_.end(); ++it)
87 { (*it)->paramUpdateStep();}
88#ifdef STK_MIXTURE_VERY_VERBOSE
89 stk_cout << _T("MixtureComposer::paramUpdateStep() done\n");
90#endif
91}
92
93void MixtureComposer::writeParameters(std::ostream& os) const
94{
95 os << _T("nbSample = ") << nbSample() << std::endl;
96 os << _T("nbCluster = ") << nbCluster() << std::endl;
97 os << _T("nbFreeParameter = ") << nbFreeParameter() << std::endl;
98 os << _T("nbMissingValues = ") << computeNbMissingValues() << std::endl;
99 os << _T("lnLikelihood = ") << lnLikelihood() << std::endl;
100 os << _T("v_mixtures_.size() = ") << v_mixtures_.size() << std::endl;
101 os << _T("proportions = ") << pk();
102
103 for (ConstMixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
104 {
105 os << _T("\nParameters of the mixture: ") << (*it)->idData() << _T("\n");
106 (*it)->writeParameters(os);
107 }
108}
109
111{
112#ifdef STK_MIXTURE_VERBOSE
113 stk_cout << _T("Entering MixtureComposer::RandomInit()\n");
114#endif
115 // in case the mixture is not initialized
117 // create random tik
119 // compute zi
120 mapStep();
121 // set random values to the parameters of the model
122 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
123 { (*it)->randomInit();}
124 // perform eStep
125 eStep();
126 // update state of the model
128#ifdef STK_MIXTURE_VERBOSE
129 stk_cout << _T("MixtureComposer::RandomInit() done\n");
130#endif
131}
132
133
135{
136 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
137 { (*it)->imputationStep();}
138}
139
140/* @brief Simulation of all the latent variables and/or missing data
141 * excluding class labels.
142 */
144{
145#ifdef STK_MIXTURE_VERY_VERBOSE
146 stk_cout << _T("Entering MixtureComposer::samplingStep()\n");
147#endif
148
149 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
150 { (*it)->samplingStep();}
151#ifdef STK_MIXTURE_VERY_VERBOSE
152 stk_cout << _T("MixtureComposer::samplingStep() done\n");
153#endif
154}
155
156/* store the intermediate results */
158{
159 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
160 { (*it)->storeIntermediateResults(iteration);}
162}
163
165{
166 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
167 { (*it)->releaseIntermediateResults();}
169}
170
171/* Utility method allowing to signal to a mixture to set its parameters */
173{
174 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
175 { (*it)->setParametersStep();}
178}
179
180/* finalize */
182{
183 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
184 { (*it)->finalizeStep();}
185}
186
187/* @brief Create the composer using existing data handler and ingredients.
188 * This method is essentially used by the create() method and can be
189 * reused in derived classes. */
190void MixtureComposer::createComposer( std::vector<IMixture*> const& v_mixtures)
191{
192 v_mixtures_.resize( v_mixtures.size());
193 for (size_t l = 0; l < v_mixtures_.size(); ++l)
194 {
195 v_mixtures_[l] = v_mixtures[l]->create();
196 v_mixtures_[l]->setMixtureModel(this);
197 }
199}
200
201/* Constructor.
202 * @param nbCluster,nbSample number of clusters and samples
203 */
205 : MixtureComposer( nbSample, nbCluster)
206{ setNbFreeParameter(0); /* remove the count of the pk parameters */}
207
208/* copy constructor.
209 * @param model the model to copy
210 */
213
214/* Create a composer, but reinitialize the ingredients parameters. */
216{
218 p_composer->createComposer(v_mixtures_);
219 p_composer->setNbFreeParameter(p_composer->nbFreeParameter()-(nbCluster()-1));
220 return p_composer;
221}
222/* Create a clone of the current model, with ingredients parameters preserved. */
225
226/* overloading of the computeProportions() method.
227 * Let them initialized to 1/K. */
229
230} /* namespace mixt */
This file define methods for displaying Arrays and Expressions.
In this file we define the class MixtureComposer.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Base class for Mixture (composed) model.
virtual void initializeStep()
Initialize the model before its first use.
void setState(Clust::modelState state)
set the state of the model : should be used by any strategy
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.
Clust::modelState state() const
virtual int randomTik()
generate random tik_
int computeNbMissingValues() const
compute the missing values of the model.
CPointX const & pk() const
std::vector< IMixture * > const & v_mixtures() const
std::vector< IMixture * > v_mixtures_
vector of pointers to the mixtures components
virtual bool finalizeStep()
perform any computation needed after the call of the regression method.
void setNbFreeParameter(int const &nbFreeParameter)
set the number of free parameters of the model
void setLnLikelihood(Real const &lnLikelihood)
set the log-likelihood of the model
specialization of the composer for the fixed proportion case.
MixtureComposerFixedProp(int nbSample, int nbCluster)
Constructor.
virtual MixtureComposerFixedProp * clone() const
Create a clone of the current model, with mixtures parameters preserved.
virtual void pStep()
overloading of the computePropotions() method.
virtual MixtureComposerFixedProp * create() const
Create a composer, but reinitialize the mixtures parameters.
Main class for handling composed mixture models.
Real meanlnLikelihood_
averaged lnLikelihood values.
virtual MixtureComposer * create() const
Create a composer, but reinitialize the mixtures parameters.
void createComposer(std::vector< IMixture * > const &v_mixtures_)
Create the composer using existing data handler and mixtures.
virtual MixtureComposer * clone() const
Create a clone of the current model, with mixtures parameters preserved.
virtual ~MixtureComposer()
The registered mixtures will be deleted there.
virtual void paramUpdateStep()
Compute the proportions and the model parameters given the current tik mixture parameters.
std::vector< IMixture * >::iterator MixtIterator
virtual void releaseIntermediateResults()
This step can be used to signal to the mixtures that they must release the stored results.
MixtureComposer(int nbSample, int nbCluster)
Constructor.
virtual void samplingStep()
Simulation of all the latent variables and/or missing data excluding class labels.
virtual void storeIntermediateResults(int iteration)
This step can be used to signal to the mixtures that they must store results.
virtual void finalizeStep()
This step can be used by developer to finalize any thing.
virtual void randomInit()
initialize randomly the parameters of the components of the model
virtual void writeParameters(ostream &os) const
write the parameters of the model in the stream os.
virtual Real lnComponentProbability(int i, int k) const
virtual void setParametersStep()
Utility method allowing to signal to a mixture to set its parameters.
std::vector< IMixture * >::const_iterator ConstMixtIterator
virtual void imputationStep()
Impute the missing values.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
@ modelInitialized_
the model is initialized and its parameters are initialized to default values
@ modelParamInitialized_
The parameters of the model have been initialized.
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.