STK++ 0.9.13
STK_IMixtureStatModel.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: 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
37
38namespace STK
39{
40
41
42/* Constructor.
43 * @param nbCluster,nbSample number of clusters and samples
44 **/
45IMixtureStatModel::IMixtureStatModel( int nbSample, int nbCluster)
46 : IStatModelBase(nbSample)
47 , nbCluster_(nbCluster)
48 , pk_(nbCluster, 1./nbCluster)
49 , tik_(nbSample, nbCluster, 1./nbCluster)
50 , tk_(nbCluster, Real(nbSample)/nbCluster)
51 , zi_(nbSample, baseIdx)
52 , v_mixtures_()
53{}
54
55/* copy constructor */
58 , nbCluster_(model.nbCluster_)
59 , pk_(model.pk_), tik_(model.tik_)
60 , tk_(model.tk_), zi_(model.zi_)
61 , v_mixtures_(model.v_mixtures_.size())
62{
63 // clone mixtures
64 for (size_t l = 0; l < v_mixtures_.size(); ++l)
65 {
66 v_mixtures_[l] = model.v_mixtures_[l]->clone();
67 v_mixtures_[l]->setMixtureModel(this);
68 }
69}
70/* destructor */
72{
73 for (MixtIterator it = v_mixtures_.begin() ; it != v_mixtures_.end(); ++it)
74 { delete (*it);}
75}
76
79
80/* @return the computed likelihood of the i-th sample.
81 * @param i index of the sample
82 **/
84{
85 // get maximal value
86 CPointX lnComp(pk_.size());
87 for (int k = pk_.begin(); k< pk_.end(); ++k)
88 { lnComp[k] = std::log(pk_[k]) + lnComponentProbability(i, k);}
89 // compute result
90 Real lnCompMax = lnComp.maxElt();
91 return std::log((lnComp-lnCompMax).exp().sum())+lnCompMax;
92}
93
94/* @return the computed log-likelihood. */
96{
97 Real res = 0.0;
98 for (int i = tik().beginRows(); i< tik().endRows(); ++i)
100 return res;
101}
102
103/* @return the computed ICL criteria. */
105{
106 Real res = 0.0;
107 for (int j = tik().beginCols(); j< tik().endCols(); ++j)
108 { res += (tik_.col(j) * (tik_.col(j)+1e-15).log()).sum();}
109 // compute result
110 return (- 2. * lnLikelihood() + nbFreeParameter() * lnNbSample() - 2. * res);
111}
112
113/* Utility lookup function allowing to find a Mixture from its idData
114 * @param idData the id name of the mixture we want to get
115 * @return a pointer on the mixture
116 **/
118{
119 for (ConstMixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
120 { if ((*it)->idData() == idData) return (*it);}
121 return 0;
122}
123/* register the mixture in the composer*/
125{
126#ifdef STK_MIXTURE_VERBOSE
127 stk_cout << _T("In IMixtureStatModel::registerMixture, registering mixture: ")
128 << p_mixture->idData() << _T("\n");
129#endif
130 p_mixture->setMixtureModel(this);
131 v_mixtures_.push_back(p_mixture);
132 // update NbFreeParameters
134}
135
136/* Utility lookup function allowing to find a Mixture from its idData
137 * @param idData the id name of the mixture we want to get
138 * @return a pointer on the mixture
139 **/
141{
142 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
143 {
144 if ((*it)->idData() == idData)
145 {
146 setNbFreeParameter(nbFreeParameter()-(*it)->nbFreeParameter());
147 // remove mixture
148 delete (*it);
149 v_mixtures_.erase(it);
150 // update log-likelihood
151 if (v_mixtures_.size() == 0)
153 else
155 // and break
156 break;
157 }
158 }
159}
160
161// implement computeNbFreeParameters
163{
164 int sum = nbCluster_-1; // proportions
165 for (ConstMixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
166 { sum+= (*it)->nbFreeParameter();}
167 return sum;
168}
169
175{
176 int sum = nbCluster_-1; // proportions
177 for (ConstMixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
178 { sum+= (*it)->nbMissingValues();}
179 return sum;
180}
181
182/* @brief Initialize the model before its first use.
183 * This function can be overloaded in derived class for initialization of
184 * the specific model parameters. It should be called prior to any used of
185 * the class.
186 * @sa IMixture,MixtureBridge,MixtureLearner
187 **/
189{
190#ifdef STK_MIXTURE_VERBOSE
191 stk_cout << _T("Entering IMixtureStatModel::initializeStep\n");
192#endif
193 if (v_mixtures_.size() == 0)
196 // initialize registered mixtures
197 for (MixtIterator it = v_mixtures_.begin(); it != v_mixtures_.end(); ++it)
198 { (*it)->initializeStep();}
199}
200
201} // namespace STK
202
In this file we define the abstract base class for mixture statistical models.
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
hidden::CSlice< Derived, sizeRows_, 1 >::Result col(int j) const
implement the col operator using a reference on the column of the allocator
Interface base class for Mixture (composed) model.
CArrayXX tik_
The tik probabilities.
int computeNbMissingValues() const
compute the missing values of the model.
virtual ~IMixtureStatModel()
destructor
void registerMixture(IMixture *p_mixture)
register a mixture to the composer.
Real computeLikelihood(int i) const
virtual Real lnComponentProbability(int i, int k) const =0
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.
void releaseMixture(String const &idData)
release a mixture from the composer.
CArrayXX const & tik() const
int computeNbFreeParameters() const
compute the number of free parameters of the model.
std::vector< IMixture * >::const_iterator ConstMixtIterator
std::vector< IMixture * > v_mixtures_
vector of pointers to the mixtures components
IMixtureStatModel(int nbSample, int nbCluster)
Constructor.
CPointX pk_
The proportions of each mixtures.
std::vector< IMixture * >::iterator MixtIterator
int nbCluster_
number of cluster.
Interface base class for all the mixture models that will be processed by the composer.
int nbFreeParameter() const
Interface base class for all Statistical Models.
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
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
std::basic_string< Char > String
STK fundamental type of a String.
const int baseIdx
base index of the containers created in STK++.
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.