STK++ 0.9.13
STK_IMixtureBridge.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.org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Clustering
27 * created on: 15 nov. 2013
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_IMIXTUREBRIDGE_H
37#define STK_IMIXTUREBRIDGE_H
38
39#include "STK_IMixture.h"
40
42
43namespace STK
44{
45
71template<class Derived>
72class IMixtureBridge: public IMixture, public IRecursiveTemplate<Derived>
73{
74 public:
81 typedef typename MissingIndexes::const_iterator ConstIterator;
82
83 enum
84 {
85 // class of mixture
87 };
88
89
90 protected:
96 IMixtureBridge( Data* p_data, String const& idData, int nbCluster);
108 virtual ~IMixtureBridge() {}
109
110 public:
111 // getters
113 inline Mixture const& mixture() const { return mixture_;}
115 inline MissingIndexes const& v_missing() const { return v_missing_;}
117 inline Data* const p_dataij() const { return p_dataij_;}
119 inline int nbMissing() const { return v_missing_.size();}
124 void getMissingValues( MissingValues& data) const;
125
126 // getter and setter of the parameters
128 inline void getParameters( Parameters& param) const { param = mixture_.param_;}
130 inline void setParameters( Parameters const& param) { mixture_.param_ = param;}
131
132 // getter and setter of the parameters using an array
137 template<class Array>
138 inline void getParameters(Array& param) const { mixture_.getParameters(param);}
144 template<class Array>
145 inline void setParameters(ExprBase<Array> const& param)
146 { mixture_.param_.setParameters(param.asDerived());}
147
148 // start default implementation of virtual method inherited from IMixture
156 inline virtual void initializeStep()
157 {
158 if (!p_composer())
160 if (!mixture_.initializeStep()) throw Clust::initializeStepFail_;
161 }
167 inline virtual Real lnComponentProbability(int i, int k)
168 { return mixture_.lnComponentProbability(i, k);}
172 inline virtual void paramUpdateStep()
173 { if (!mixture_.run( p_tik(), p_tk())) throw Clust::mStepFail_;}
177 inline virtual void randomInit() { mixture_.randomInit( p_tik(), p_tk());};
181 inline virtual int nbFreeParameter() const { return mixture_.computeNbFreeParameters();}
185 virtual int nbMissingValues() const { return v_missing_.size();}
191 inline virtual void storeIntermediateResults(int iteration)
192 { mixture_.param_.updateStatistics();}
197 inline virtual void releaseIntermediateResults()
198 { mixture_.param_.releaseStatistics();}
203 inline virtual void setParametersStep()
204 { mixture_.param_.setStatistics();}
208 inline virtual void finalizeStep() { mixture_.finalizeStep();}
212 inline virtual void imputationStep();
218 inline virtual void samplingStep();
222 inline virtual void writeParameters(ostream& os) const
223 { mixture_.writeParameters(p_tik(), os);}
224
225 protected:
229 virtual std::vector< std::pair<int,int> >::size_type findMissing();
230
240 virtual void removeMissing();
241
248};
249
250/* default constructor.
251 * @param p_data pointer on the DataBridge that will be used by the bridge.
252 * @param idData id name of the mixture model
253 * @param nbCluster number of cluster
254 **/
255template< class Derived>
256IMixtureBridge<Derived>::IMixtureBridge( Data* p_dataij, String const& idData, int nbCluster)
257 : IMixture(idData)
258 , mixture_(nbCluster)
259 , v_missing_()
260 , p_dataij_(p_dataij)
261{// find coordinates of the missing values
262 findMissing();
263}
264/* copy constructor */
265template< class Derived>
268 , mixture_(bridge.mixture_)
269 , v_missing_(bridge.v_missing_)
270 , p_dataij_(bridge.p_dataij_)
271{}
272// implementation
273template< class Derived>
275{
276 for(ConstIterator it = v_missing().begin(); it!= v_missing().end(); ++it)
277 { p_dataij_->elt(it->first, it->second) = mixture_.impute(it->first, it->second, p_tik()->row(it->first) );}
278}
279// implementation
280template< class Derived>
282{
283 for(ConstIterator it = v_missing().begin(); it!= v_missing().end(); ++it)
284 { p_dataij_->elt(it->first, it->second) = mixture_.sample(it->first, it->second, p_tik()->row(it->first));}
285}
286
287// implementation
288/* protected constructor to use in order to create a bridge.
289 * @param mixture the mixture to copy
290 * @param idData id name of the mixture
291 * @param nbCluster number of cluster
292 **/
293template< class Derived>
294IMixtureBridge<Derived>::IMixtureBridge( Mixture const& mixture, String const& idData, int nbCluster)
295 : IMixture(idData)
296 , mixture_(mixture)
297 , v_missing_()
298 , p_dataij_(0)
299{}
300
301template< class Derived>
302std::vector< std::pair<int,int> >::size_type IMixtureBridge<Derived>::findMissing()
303{
304#ifdef STK_MIXTURE_VERBOSE
305 stk_cout << _T("Entering IMixtureBridge<Derived>::findMissing()\n");
306#endif
307 if (p_dataij_)
308 {
309 for (int j=p_dataij_->beginCols(); j< p_dataij_->endCols(); ++j)
310 {
311 for (int i=p_dataij_->beginRows(); i< p_dataij_->endRows(); ++i)
312 {
313 if (Arithmetic<Type>::isNA(p_dataij_->elt(i,j)))
314 {
315 v_missing_.push_back(std::pair<int,int>(i,j));
316 }
317 }
318 }
319 }
320 return v_missing_.size();
321#ifdef STK_MIXTURE_VERBOSE
322 stk_cout << _T("IMixtureBridge<Derived>::findMissing() terminated, nbMiss= ") << v_missing_.size() << _T("\n");
323#endif
324}
325
326
327/* This function will be used for imputation of the missing data
328 * at the initialization (initializationStep). By default missing values
329 * in a column will be replaced by the mean or map (maximum a
330 * posteriori) value of the column.
331 **/
332template< class Derived>
334{
335 if (p_dataij_)
336 {
337 Type value = Type();
338 int j, old_j = Arithmetic<int>::NA();
339 for(ConstIterator it = v_missing().begin(); it!= v_missing().end(); ++it)
340 {
341 j = it->second; // get column
342 if (j != old_j)
343 {
344 old_j = j;
345 value = this->asDerived().safeValue(j);
346 }
347 p_dataij_->elt(it->first, j) = value;
348 }
349 }
350}
351
352/* get the (imputed) missing values of a data set.
353 * @note In C++11, it will be possible to use a tuple rather that this pair of pair...
354 * @param data the array to return with the missing values
355 **/
356template< class Derived>
358{
359 data.resize(v_missing_.size());
360 for(size_t i = 0; i< v_missing_.size(); ++i)
361 {
362 data[i].first = v_missing_[i];
363 data[i].second = p_dataij_->elt(v_missing_[i].first, v_missing_[i].second);
364 }
365}
366
367} // namespace STK
368
369#endif /* IMIXTUREBRIDGE_H */
In this file we define the data wrapper class.
define the main interface for linking specific mixture model to the composer.
#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.
Interface base class for the bridges of the STK++ mixture.
void getParameters(Parameters &param) const
get the parameters of the model
MissingIndexes::const_iterator ConstIterator
hidden::MixtureBridgeTraits< Derived >::Type Type
virtual int nbMissingValues() const
This function must return the number of missing value in data set identified by idData_.
virtual void samplingStep()
This function must be defined for simulation of all the latent variables and/or missing data excludin...
MissingIndexes v_missing_
vector with the coordinates of the missing values
hidden::MixtureBridgeTraits< Derived >::MissingValues MissingValues
virtual void initializeStep()
Initialize the mixture model before its use by the composer.
IMixtureBridge(Data *p_data, String const &idData, int nbCluster)
default constructor.
virtual void paramUpdateStep()
This function is equivalent to Mstep and must be defined to update parameters.
virtual int nbFreeParameter() const
This function must return the number of free parameters.
Data * p_dataij_
pointer on the data set
hidden::MixtureBridgeTraits< Derived >::MissingIndexes MissingIndexes
hidden::MixtureBridgeTraits< Derived >::Data Data
virtual void removeMissing()
This function will be used once for imputation of missing data at the initialization step (.
virtual void finalizeStep()
This step can be used by developer to finalize any thing.
virtual ~IMixtureBridge()
destructor
void setParameters(Parameters const &param)
set the parameters of the model
virtual void releaseIntermediateResults()
This step can be used to signal to the mixtures that they must release the stored results.
virtual void storeIntermediateResults(int iteration)
This function should be used to store any intermediate results during various iterations after the bu...
IMixtureBridge(Mixture const &mixture, String const &idData, int nbCluster)
protected constructor to use in order to create a bridge.
hidden::MixtureBridgeTraits< Derived >::Parameters Parameters
virtual void imputationStep()
This function should be used for imputation of data.
void getParameters(Array &param) const
This function is used in order to get the current values of the parameters in an array.
hidden::MixtureBridgeTraits< Derived >::Mixture Mixture
Mixture const & mixture() const
virtual std::vector< std::pair< int, int > >::size_type findMissing()
utility function for lookup the data set and find missing values coordinates.
virtual void writeParameters(ostream &os) const
This function can be used to write summary of parameters to the output stream.
IMixtureBridge(IMixtureBridge const &bridge)
copy constructor
MissingIndexes const & v_missing() const
void setParameters(ExprBase< Array > const &param)
This function is used in order to set the current values of the parameters to the parameters using an...
virtual void randomInit()
This function should be used in order to initialize randomly the parameters of the mixture.
void getMissingValues(MissingValues &data) const
get the (imputed) missing values of a data set.
virtual void setParametersStep()
set the parameters of the model.
Mixture mixture_
The Mixture to bridge with the composer.
Data *const p_dataij() const
virtual Real lnComponentProbability(int i, int k)
This function must be defined to return the component probability distribution function (PDF) for cor...
Interface base class for all the mixture models that will be processed by the composer.
CPointX const * p_tk() const
This function can be used in derived classes to get proportions from the framework.
IMixtureStatModel const *const p_composer() const
CArrayXX const * p_tik() const
This function can be used in derived classes to get posterior probabilities from the framework.
String const & idData() const
int nbCluster() const
This function can be used in derived classes to get number of classes.
Interface base class for all classes implementing the curious recursive template paradigm.
virtual bool finalizeStep()
perform any computation needed after the call of the regression method.
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
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
static Type NA()
Adding a Non Available (NA) special number.
MixtureBridgeTraits struct for bridged mixtures The traits struct MixtureBridgeTraits must be special...