STK++ 0.9.13
STK_IMixtureStatModel.h
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: 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#ifndef STK_IMIXTURESTATMODEL_H
37#define STK_IMIXTURESTATMODEL_H
38
40
41#include "STK_Clust_Util.h"
42#include "STK_IMixtureManager.h"
44
45
46namespace STK
47{
48
124{
125 protected:
134
135 public:
136 typedef std::vector<IMixture*>::const_iterator ConstMixtIterator;
137 typedef std::vector<IMixture*>::iterator MixtIterator;
138
140 virtual ~IMixtureStatModel();
141
143 inline int nbCluster() const { return nbCluster_;}
145 inline CPointX const& pk() const { return pk_;};
147 inline CArrayXX const& tik() const { return tik_;};
149 inline CPointX const& tk() const { return tk_;};
151 inline CVectorXi const& zi() const { return zi_;};
153 inline std::vector<IMixture*> const& v_mixtures() const { return v_mixtures_;}
154
158 Real computeLnLikelihood(int i) const;
162 Real computeLikelihood(int i) const;
166 Real computeICL() const;
167
172 IMixture* getMixture( String const& idData) const;
187 void releaseMixture(String const& idData);
194 int computeNbFreeParameters() const;
199 int computeNbMissingValues() const;
200
201 // pure virtual
203 virtual IMixtureStatModel* create() const = 0;
205 virtual IMixtureStatModel* clone() const = 0;
207 virtual void randomInit() = 0;
211 virtual Real lnComponentProbability(int i, int k) const = 0;
212
213 // virtual with default implementation
220 virtual void initializeStep();
221
225 inline virtual void imputationStep() {}
229 inline virtual void samplingStep() {}
233 inline virtual void setParametersStep() {}
238 inline virtual void storeIntermediateResults(int iteration) {}
243 inline virtual void releaseIntermediateResults() {}
249 virtual void writeParameters(ostream& os) const {};
250
251 // initialization of the arrays pk_, tik_, tk_ and zi_
257 template<class Array>
258 void setMixtureParameters( Array const& tik);
266 template<class Array, class RowVector>
267 void setMixtureParameters( Array const& tik, RowVector const& pk);
271 template<class RowVector>
272 void setProportions( RowVector const& pk);
273
274 // allow to update specific mixture parameters values
280 template<class Manager, class Parameters>
281 void setParameters(IMixtureManager<Manager> const& manager, String const& idData, Parameters const& param);
282
283 // utilities methods
288 template<class Manager>
296 template<class Manager>
303 template<class Manager>
305
311 template<class Manager, class Parameters>
312 void getParameters(IMixtureManager<Manager> const& manager, String const& idData, Parameters& param) const;
313
319 template<class Manager, class MissingValues>
320 void getMissingValues(IMixtureManager<Manager> const& manager, String const& idData, MissingValues& missing) const;
321
322 protected:
327
338
340 std::vector<IMixture*> v_mixtures_;
341};
342
343
344/* Utility method allowing to create all the mixtures handled by a mixture manager.
345 * @param manager the manager with the responsibility of the creation.
346 **/
347template<class DataHandler>
349{
350 typedef typename DataHandlerBase<DataHandler>::InfoMap InfoMap;
351 typedef typename InfoMap::const_iterator const_iterator;
352 for ( const_iterator it=manager.p_handler()->info().begin(); it!=manager.p_handler()->info().end(); ++it)
353 {
354 IMixture* p_mixture = manager.createMixture(it->first, nbCluster());
355#ifdef STK_MIXTURE_DEBUG
356 if (!p_mixture)
357 { stk_cout << _T("In IMixtureStatModel::createMixture(manager) failed.\n");}
358#endif
360 }
361}
362
363/* Utility method allowing to create a mixture with a given data set
364 * and register it. The Mixture Manager will find the associated model
365 * to use with this data set.
366 * @param manager the manager with the responsibility of the creation.
367 * @param idData the id name of the data to modelize.
368 **/
369template<class Manager>
371{
372 IMixture* p_mixture = manager.createMixture( idData, nbCluster());
373#ifdef STK_MIXTURE_DEBUG
374 if (!p_mixture)
375 { stk_cout << _T("In IMixtureStatModel::createMixture(manager,")<< idData << _T(") failed.\n");}
376#endif
378 return p_mixture;
379}
380
381/* Utility method allowing to release completely a mixture with its data set.
382 * The MixtureManager will find and release the associated data set.
383 * @param manager the manager with the responsibility of the release.
384 * @param idData the id name of the data to modelize.
385 **/
386template<class Manager>
388{
389 IMixture* p_mixture = getMixture(idData);
390#ifdef STK_MIXTURE_DEBUG
391 if (!p_mixture)
392 { stk_cout << _T("In IMixtureStatModel::removeMixture(manager,")<< idData << _T(") failed.\n");}
393#endif
394 if (p_mixture)
395 {
396 releaseMixture(idData);
397 manager.releaseDataBridge( idData);
398 }
399}
400
401/* Utility method allowing to set the parameters to a specific mixture.
402 * @param manager the manager with the responsibility of the parameters
403 * @param idData Id of the data we want to set the parameters
404 * @param param structure which contains the parameters
405 **/
406template<class Manager, class Parameters>
407void IMixtureStatModel::setParameters(IMixtureManager<Manager> const& manager, String const& idData, Parameters const& param)
408{ IMixture* p_mixture= getMixture(idData);
409 if (p_mixture) manager.setParameters(p_mixture, param);
410}
411/* set the mixture parameters using the posterior probabilities.
412 **/
413template<class Array>
415{
416 tik_ = tik;
418 pk_ = tk_ / nbSample();
419 for (int i = tik_.beginCols(); i< tik_.endCols(); ++i)
420 {
421 int k;
422 tik_.row(i).maxElt(k);
423 zi_[i] = k;
424 }
425}
426
427/* set the mixture parameters using the posterior probabilities.
428 **/
429template<class Array, class RowVector>
430void IMixtureStatModel::setMixtureParameters(Array const& tik, RowVector const& pk)
431{
432#ifdef STK_MIXTURE_DEBUG
433 if (pk_.size() != tik_.sizeCols())
435#endif
436 tik_ = tik;
437 pk_ = pk;
439 for (int i = tik_.beginCols(); i< tik_.endCols(); ++i)
440 {
441 int k;
442 tik_.row(i).maxElt(k);
443 zi_[i] = k;
444 }
445}
446
447/* set proportions */
448template<class RowVector>
449void IMixtureStatModel::setProportions( RowVector const& pk)
450{
451#ifdef STK_MIXTURE_DEBUG
452 if (pk_.size() != nbCluster())
454#endif
455 pk_ = pk;
456}
457
458/* Utility method allowing to get the parameters of a specific mixture.
459 * @param manager the manager with the responsibility of the parameters
460 * @param idData the Id of the data we want the parameters
461 * @param param the structure which will receive the parameters
462 **/
463template<class Manager, class Parameters>
464void IMixtureStatModel::getParameters(IMixtureManager<Manager> const& manager, String const& idData, Parameters& param) const
465{ IMixture* p_mixture= getMixture(idData);
466 if (p_mixture) manager.getParameters(p_mixture, param);
467}
468/* Utility method allowing to get the missing values of a specific mixture.
469 * @param manager the manager with the responsibility of the parameters
470 * @param idData the Id of the data we want the parameters
471 * @param missing the structure which will receive the missing values
472 **/
473template<class Manager, class MissingValues>
475{
476 IMixture* p_mixture= getMixture(idData);
477 if (p_mixture) manager.getMissingValues(p_mixture, missing);
478}
479
480
481
482} // namespace STK
483
484#endif /* STK_IMIXTURESTATMODEL_H */
485
486
In this file we define the enum, constants and utilities functions of the Clustering project.
In this file we define the Interface class IMixtureManager.
In this file we define the interface base class IStatModelBase.
#define STKRUNTIME_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:120
This file contain the functors computings statistics.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
std::map< std::string, std::string > InfoMap
hidden::CSlice< Derived, 1, sizeCols_ >::Result row(int i) const
implement the row operator using a reference on the row of the allocator
Interface base class for Mixture (composed) model.
CArrayXX tik_
The tik probabilities.
void setParameters(IMixtureManager< Manager > const &manager, String const &idData, Parameters const &param)
Utility method allowing to set the parameters to a specific mixture.
int computeNbMissingValues() const
compute the missing values of the model.
void setMixtureParameters(Array const &tik)
set the mixture parameters using an array of posterior probabilities.
CVectorXi const & zi() const
virtual void writeParameters(ostream &os) const
write the parameters of the model in the stream os.
void setNbCluster(int nbCluster)
set the number of cluster of the model
virtual ~IMixtureStatModel()
destructor
virtual IMixtureStatModel * create() const =0
create pattern
CPointX const & tk() const
void registerMixture(IMixture *p_mixture)
register a mixture to the composer.
CPointX const & pk() const
Real computeLikelihood(int i) const
void setProportions(RowVector const &pk)
Set proportions of each classes.
virtual Real lnComponentProbability(int i, int k) const =0
CVectorXi zi_
The zi class label.
void createMixture(IMixtureManager< Manager > &manager)
Utility method allowing to create all the mixtures registered in the data handler of a mixture manage...
virtual IMixtureStatModel * clone() const =0
clone pattern
virtual void initializeStep()
Initialize the model before at its first use.
IMixture * getMixture(String const &idData) const
Utility lookup function allowing to find a Mixture from its idData.
virtual void imputationStep()
Impute the missing values.
virtual void randomInit()=0
initialize randomly the parameters of the components of the model
virtual void setParametersStep()
Utility method allowing to signal to a mixture to set its parameters.
virtual void finalizeStep()
Finalize the estimation of the model.
void releaseMixture(String const &idData)
release a mixture from the composer.
std::vector< IMixture * > const & v_mixtures() const
CArrayXX const & tik() const
void getMissingValues(IMixtureManager< Manager > const &manager, String const &idData, MissingValues &missing) const
Utility method allowing to get the missing values of a specific mixture.
int computeNbFreeParameters() const
compute the number of free parameters of the model.
virtual void releaseIntermediateResults()
This step can be used to signal to the mixtures that they must release the stored results.
std::vector< IMixture * >::const_iterator ConstMixtIterator
std::vector< IMixture * > v_mixtures_
vector of pointers to the mixtures components
virtual void storeIntermediateResults(int iteration)
This step can be used to signal to the mixtures that they must store results.
void removeMixture(IMixtureManager< Manager > &manager, String const &idData)
Utility method allowing to release completely a mixture with its data set.
void getParameters(IMixtureManager< Manager > const &manager, String const &idData, Parameters &param) const
Utility method allowing to get the parameters of a specific mixture.
virtual void samplingStep()
Simulation of all the latent variables and/or missing data excluding class labels.
CPointX pk_
The proportions of each mixtures.
std::vector< IMixture * >::iterator MixtIterator
CPointX tk_
The sum of the columns of tik_.
int nbCluster_
number of cluster.
Interface base class for all the mixture models that will be processed by the composer.
Interface base class for all Statistical Models.
void setLnLikelihood(Real const &lnLikelihood)
set the log-likelihood of the model
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
std::basic_string< Char > String
STK fundamental type of a String.
double Real
STK fundamental type of Real values.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
hidden::FunctorTraits< Derived, SumOp >::Row sumByCol(Derived const &A)
The namespace STK is the main domain space of the Statistical ToolKit project.