STK++ 0.9.13
STK_MixtureLearner.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
37
39
40namespace STK
41{
42
43/* Constructor.
44 * @param nbCluster,nbSample, number of clusters and samples.
45 */
46MixtureLearner::MixtureLearner( int nbSample, int nbCluster)
47 : IMixtureLearner( nbSample, nbCluster)
48 , meanlnLikelihood_(0.)
50
51/* copy constructor.
52 * @param composer the composer to copy
53 */
56 , meanlnLikelihood_(learner.meanlnLikelihood_)
57{}
58
60
61/* clone pattern */
63{ return new MixtureLearner(*this);}
64
66{
67 // set dimensions
68 MixtureLearner* p_composer = new MixtureLearner(nbSample(), nbCluster());
69 p_composer->createLearner( 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 for (MixtIterator it = v_mixtures_.begin() ; it != v_mixtures_.end(); ++it)
84 { (*it)->paramUpdateStep();}
86}
87
88void MixtureLearner::writeParameters(std::ostream& os) const
89{
90 os << _T("Learner nbSample = ") << nbSample() << std::endl;
91 os << _T("Learner nbCluster = ") << nbCluster() << std::endl;
92 os << _T("Learner nbFreeParameter = ") << nbFreeParameter() << std::endl;
93 os << _T("Learner lnLikelihood = ") << lnLikelihood() << std::endl;
94 os << _T("Learner proportions = ") << pk();
95
96 for (ConstMixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
97 {
98 os << _T("\nParameters of the mixture: ") << (*it)->idData() << _T("\n");
99 (*it)->writeParameters(os);
100 }
101}
102
104{
105#ifdef STK_MIXTURE_VERBOSE
106 stk_cout << _T("Entering MixtureLearner::RandomInit()\n");
107#endif
108 if (state() < 2) { initializeStep();}
109 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
110 { (*it)->randomInit();}
112#ifdef STK_MIXTURE_VERBOSE
113 stk_cout << _T("MixtureLearner::RandomInit() done\n");
114#endif
115}
116
117
119{
120 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
121 { (*it)->imputationStep();}
122}
123
124/* @brief Simulation of all the latent variables and/or missing data
125 * excluding class labels.
126 */
128{
129 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
130 { (*it)->samplingStep();}
131}
132
133/* store the intermediate results */
135{
136 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
137 { (*it)->storeIntermediateResults(iteration);}
139}
140
142{
143 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
144 { (*it)->releaseIntermediateResults();}
146}
147
148/* Utility method allowing to signal to a mixture to set its parameters */
150{
151 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
152 { (*it)->setParametersStep();}
155}
156
157/* finalize */
159{
160 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
161 { (*it)->finalizeStep();}
162}
163
164/* @brief Create the composer using existing data handler and ingredients.
165 * This method is essentially used by the create() method and can be
166 * reused in derived classes. */
167void MixtureLearner::createLearner( std::vector<IMixture*> const& v_mixtures)
168{
169 initialize(nbSample(), 0);
170 v_mixtures_.resize( v_mixtures.size());
171 for (size_t l = 0; l < v_mixtures_.size(); ++l)
172 {
173 v_mixtures_[l] = v_mixtures[l]->create();
174 v_mixtures_[l]->setMixtureModel(this);
176 }
177 // compute number of free parameters
179}
180
181} /* namespace STK */
This file define methods for displaying Arrays and Expressions.
In this file we define the class MixtureLearner.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Base class for Learner of a Mixture mixed model.
void setState(Clust::modelState state)
set the state of the model : should be used by any strategy
Clust::modelState state() const
CPointX const & pk() const
virtual void initializeStep()
Initialize the model before at its first use.
std::vector< IMixture * > const & v_mixtures() const
int computeNbFreeParameters() const
compute the number of free parameters of the model.
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.
virtual bool initializeStep()
perform any computation needed before 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
void initialize(int nbSample, int nbVariable)
set the dimensions of the parameters of the model
Main class for learning mixture models.
void createLearner(std::vector< IMixture * > const &v_mixtures_)
Create the composer using existing data handler and mixtures.
virtual MixtureLearner * create() const
Create a composer, but reinitialize the mixtures parameters.
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 void paramUpdateStep()
Compute the model parameters given the current imputed/simulated missing values.
virtual void samplingStep()
Simulation of all the data missing values.
std::vector< IMixture * >::const_iterator ConstMixtIterator
virtual void setParametersStep()
Utility method allowing to signal to a mixture to set its parameters.
MixtureLearner(int nbSample, int nbCluster)
Constructor.
Real meanlnLikelihood_
averaged lnLikelihood values.
virtual ~MixtureLearner()
The registered mixtures will be deleted there.
virtual void releaseIntermediateResults()
This step can be used to signal to the mixtures that they must release the stored results.
virtual void imputationStep()
Impute the data missing values.
virtual MixtureLearner * clone() const
Create a clone of the current model, with mixtures parameters preserved.
virtual Real lnComponentProbability(int i, int k) const
virtual void finalizeStep()
This step can be used by developer to finalize any thing.
std::vector< IMixture * >::iterator MixtIterator
virtual void storeIntermediateResults(int iteration)
This step can be used to signal to the mixtures that they must store results.
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
@ 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.