STK++ 0.9.13
STK::IMixtureBridge< Derived > Class Template Reference

Interface base class for the bridges of the STK++ mixture. More...

#include <STK_IMixtureBridge.h>

Inheritance diagram for STK::IMixtureBridge< Derived >:
Inheritance graph

Public Types

enum  { idMixtureClass_ = hidden::MixtureBridgeTraits<Derived>::idMixtureClass_ }
 
typedef hidden::MixtureBridgeTraits< Derived >::Mixture Mixture
 
typedef hidden::MixtureBridgeTraits< Derived >::Data Data
 
typedef hidden::MixtureBridgeTraits< Derived >::Type Type
 
typedef hidden::MixtureBridgeTraits< Derived >::Parameters Parameters
 
typedef hidden::MixtureBridgeTraits< Derived >::MissingIndexes MissingIndexes
 
typedef hidden::MixtureBridgeTraits< Derived >::MissingValues MissingValues
 
typedef MissingIndexes::const_iterator ConstIterator
 

Public Member Functions

Mixture constmixture () const
 
MissingIndexes constv_missing () const
 
Data *const p_dataij () const
 
int nbMissing () const
 
void getMissingValues (MissingValues &data) const
 get the (imputed) missing values of a data set.
 
void getParameters (Parameters &param) const
 get the parameters of the model
 
void setParameters (Parameters const &param)
 set the parameters of the model
 
template<class Array >
void getParameters (Array &param) const
 This function is used in order to get the current values of the parameters in an array.
 
template<class Array >
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 array.
 
virtual void initializeStep ()
 Initialize the mixture model before its use by the composer.
 
virtual Real lnComponentProbability (int i, int k)
 This function must be defined to return the component probability distribution function (PDF) for corresponding sample i and cluster k.
 
virtual void paramUpdateStep ()
 This function is equivalent to Mstep and must be defined to update parameters.
 
virtual void randomInit ()
 This function should be used in order to initialize randomly the parameters of the mixture.
 
virtual int nbFreeParameter () const
 This function must return the number of free parameters.
 
virtual int nbMissingValues () const
 This function must return the number of missing value in data set identified by idData_.
 
virtual void storeIntermediateResults (int iteration)
 This function should be used to store any intermediate results during various iterations after the burn-in period.
 
virtual void releaseIntermediateResults ()
 This step can be used to signal to the mixtures that they must release the stored results.
 
virtual void setParametersStep ()
 set the parameters of the model.
 
virtual void finalizeStep ()
 This step can be used by developer to finalize any thing.
 
virtual void imputationStep ()
 This function should be used for imputation of data.
 
virtual void samplingStep ()
 This function must be defined for simulation of all the latent variables and/or missing data excluding class labels.
 
virtual void writeParameters (ostream &os) const
 This function can be used to write summary of parameters to the output stream.
 
- Public Member Functions inherited from STK::IMixture
 IMixture (String const &idData=String())
 Constructor with identification character.
 
 IMixture (IMixture const &mixture)
 copy constructor.
 
virtual ~IMixture ()
 Virtual destructor.
 
String constidData () const
 
IMixtureStatModel const *const p_composer () const
 
void setMixtureModel (IMixtureStatModel const *p_composer)
 set the mixture composer to the mixture
 
virtual IMixtureclone () const =0
 This is a standard clone function in usual sense.
 
virtual IMixturecreate () const =0
 This is a standard create function in usual sense.
 
virtual void writeParameters (std::ostream &out) const
 This function can be used to write summary of parameters on to the output stream.
 
- Public Member Functions inherited from STK::IRecursiveTemplate< Derived >
Derived & asDerived ()
 static cast : return a reference of this with a cast to the derived class.
 
Derived constasDerived () const
 static cast : return a const reference of this with a cast to the derived class.
 
Derived * asPtrDerived ()
 static cast : return a ptr on a Derived of this with a cast to the derived class.
 
Derived constasPtrDerived () const
 static cast : return a ptr on a constant Derived of this with a cast to the derived class.
 
Derived * clone () const
 create a leaf using the copy constructor of the Derived class.
 
Derived * clone (bool isRef) const
 create a leaf using the copy constructor of the Derived class and a flag determining if the clone is a reference or not.
 

Protected Member Functions

 IMixtureBridge (Data *p_data, String const &idData, int nbCluster)
 default constructor.
 
 IMixtureBridge (IMixtureBridge const &bridge)
 copy constructor
 
 IMixtureBridge (Mixture const &mixture, String const &idData, int nbCluster)
 protected constructor to use in order to create a bridge.
 
virtual ~IMixtureBridge ()
 destructor
 
virtual std::vector< std::pair< int, int > >::size_type findMissing ()
 utility function for lookup the data set and find missing values coordinates.
 
virtual void removeMissing ()
 This function will be used once for imputation of missing data at the initialization step (.
 
- Protected Member Functions inherited from STK::IMixture
int nbSample () const
 This function can be used in derived classes to get number of samples.
 
int nbCluster () const
 This function can be used in derived classes to get number of classes.
 
CPointX constp_pk () const
 This function can be used in derived classes to get estimated number of individuals from the framework.
 
CPointX constp_tk () const
 This function can be used in derived classes to get proportions from the framework.
 
CArrayXX constp_tik () const
 This function can be used in derived classes to get posterior probabilities from the framework.
 
CVectorXi constp_zi () const
 This function can be used in derived classes to get class labels from the framework.
 
- Protected Member Functions inherited from STK::IRecursiveTemplate< Derived >
 IRecursiveTemplate ()
 constructor.
 
 ~IRecursiveTemplate ()
 destructor.
 

Protected Attributes

Mixture mixture_
 The Mixture to bridge with the composer.
 
MissingIndexes v_missing_
 vector with the coordinates of the missing values
 
Datap_dataij_
 pointer on the data set
 

Detailed Description

template<class Derived>
class STK::IMixtureBridge< Derived >

Interface base class for the bridges of the STK++ mixture.

This interface produce a default implementation to all the virtual functions required by the STK::IMixture interface by delegating to the derived bridged Mixture the computations.

The virtual methods to implement in derived class are

virtual Derived* create() const;
virtual Derived* clone() const;
virtual IMixture * create() const =0
This is a standard create function in usual sense.
virtual IMixture * clone() const =0
This is a standard clone function in usual sense.

Pseudo virtual method to implement in derived class are

safeValue(j); // return a safe value for column j
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...

The members of this interface are the following

Mixture mixture_; // concrete class deriving from STK::IMixtureDensity
MissingIndexes v_missing_; // array with the indexes (i,j) of the missing values
MissingIndexes v_missing_
vector with the coordinates of the missing values
Data * p_dataij_
pointer on the data set
hidden::MixtureBridgeTraits< Derived >::MissingIndexes MissingIndexes
hidden::MixtureBridgeTraits< Derived >::Data Data
hidden::MixtureBridgeTraits< Derived >::Mixture Mixture
Mixture mixture_
The Mixture to bridge with the composer.

Definition at line 72 of file STK_IMixtureBridge.h.

Member Typedef Documentation

◆ ConstIterator

template<class Derived >
typedef MissingIndexes::const_iterator STK::IMixtureBridge< Derived >::ConstIterator

Definition at line 81 of file STK_IMixtureBridge.h.

◆ Data

template<class Derived >
typedef hidden::MixtureBridgeTraits<Derived>::Data STK::IMixtureBridge< Derived >::Data

Definition at line 76 of file STK_IMixtureBridge.h.

◆ MissingIndexes

template<class Derived >
typedef hidden::MixtureBridgeTraits<Derived>::MissingIndexes STK::IMixtureBridge< Derived >::MissingIndexes

Definition at line 79 of file STK_IMixtureBridge.h.

◆ MissingValues

template<class Derived >
typedef hidden::MixtureBridgeTraits<Derived>::MissingValues STK::IMixtureBridge< Derived >::MissingValues

Definition at line 80 of file STK_IMixtureBridge.h.

◆ Mixture

template<class Derived >
typedef hidden::MixtureBridgeTraits<Derived>::Mixture STK::IMixtureBridge< Derived >::Mixture

Definition at line 75 of file STK_IMixtureBridge.h.

◆ Parameters

template<class Derived >
typedef hidden::MixtureBridgeTraits<Derived>::Parameters STK::IMixtureBridge< Derived >::Parameters

Definition at line 78 of file STK_IMixtureBridge.h.

◆ Type

template<class Derived >
typedef hidden::MixtureBridgeTraits<Derived>::Type STK::IMixtureBridge< Derived >::Type

Definition at line 77 of file STK_IMixtureBridge.h.

Member Enumeration Documentation

◆ anonymous enum

template<class Derived >
anonymous enum
Enumerator
idMixtureClass_ 

Definition at line 83 of file STK_IMixtureBridge.h.

84 {
85 // class of mixture
86 idMixtureClass_ = hidden::MixtureBridgeTraits<Derived>::idMixtureClass_
87 };

Constructor & Destructor Documentation

◆ IMixtureBridge() [1/3]

template<class Derived >
STK::IMixtureBridge< Derived >::IMixtureBridge ( Data p_data,
String const idData,
int  nbCluster 
)
protected

default constructor.

Parameters
p_datapointer on the DataBridge wrapping the data set
idDataid name of the data
nbClusternumber of cluster

Definition at line 256 of file STK_IMixtureBridge.h.

259 , v_missing_()
261{// find coordinates of the missing values
262 findMissing();
263}
virtual std::vector< std::pair< int, int > >::size_type findMissing()
utility function for lookup the data set and find missing values coordinates.
Data *const p_dataij() const
IMixture(String const &idData=String())
Constructor with identification character.
String const & idData() const
int nbCluster() const
This function can be used in derived classes to get number of classes.

References STK::IMixtureBridge< Derived >::findMissing().

◆ IMixtureBridge() [2/3]

template<class Derived >
STK::IMixtureBridge< Derived >::IMixtureBridge ( IMixtureBridge< Derived > const bridge)
protected

copy constructor

Parameters
bridgethe IMixtureBridge to copy

Definition at line 266 of file STK_IMixtureBridge.h.

267 : IMixture(bridge)
268 , mixture_(bridge.mixture_)
269 , v_missing_(bridge.v_missing_)
270 , p_dataij_(bridge.p_dataij_)
271{}

◆ IMixtureBridge() [3/3]

template<class Derived >
STK::IMixtureBridge< Derived >::IMixtureBridge ( Mixture const mixture,
String const idData,
int  nbCluster 
)
protected

protected constructor to use in order to create a bridge.

Parameters
mixturethe mixture to copy
idDataid name of the mixture
nbClusternumber of cluster

Definition at line 294 of file STK_IMixtureBridge.h.

297 , v_missing_()
298 , p_dataij_(0)
299{}
Mixture const & mixture() const

◆ ~IMixtureBridge()

template<class Derived >
virtual STK::IMixtureBridge< Derived >::~IMixtureBridge ( )
inlineprotectedvirtual

destructor

Definition at line 108 of file STK_IMixtureBridge.h.

108{}

Member Function Documentation

◆ finalizeStep()

template<class Derived >
virtual void STK::IMixtureBridge< Derived >::finalizeStep ( )
inlinevirtual

This step can be used by developer to finalize any thing.

It will be called only once after we finish running the estimation algorithm.

Reimplemented from STK::IMixture.

Definition at line 208 of file STK_IMixtureBridge.h.

208{ mixture_.finalizeStep();}

References STK::IRegression< YArray, XArray, Weights >::finalizeStep(), and STK::IMixtureBridge< Derived >::mixture_.

◆ findMissing()

template<class Derived >
std::vector< std::pair< int, int > >::size_type STK::IMixtureBridge< Derived >::findMissing ( )
protectedvirtual

utility function for lookup the data set and find missing values coordinates.

Returns
the number of missing values

Definition at line 302 of file STK_IMixtureBridge.h.

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}
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
static bool isNA(Type const &x)

References _T, and stk_cout.

Referenced by STK::IMixtureBridge< Derived >::IMixtureBridge().

◆ getMissingValues()

template<class Derived >
void STK::IMixtureBridge< Derived >::getMissingValues ( MissingValues data) const

get the (imputed) missing values of a data set.

Note
In C++11, it will be possible to use a tuple rather that this pair of pair...
Parameters
datathe array to return with the missing values

Definition at line 357 of file STK_IMixtureBridge.h.

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}

◆ getParameters() [1/2]

template<class Derived >
template<class Array >
void STK::IMixtureBridge< Derived >::getParameters ( Array &  param) const
inline

This function is used in order to get the current values of the parameters in an array.

Parameters
paramarray that will store the parameters of the mixture.

Definition at line 138 of file STK_IMixtureBridge.h.

138{ mixture_.getParameters(param);}

References STK::IMixtureBridge< Derived >::mixture_.

◆ getParameters() [2/2]

template<class Derived >
void STK::IMixtureBridge< Derived >::getParameters ( Parameters param) const
inline

get the parameters of the model

Definition at line 128 of file STK_IMixtureBridge.h.

128{ param = mixture_.param_;}

References STK::IMixtureBridge< Derived >::mixture_.

◆ imputationStep()

template<class Derived >
void STK::IMixtureBridge< Derived >::imputationStep ( )
inlinevirtual

This function should be used for imputation of data.

The default implementation (in base class IMixture) is to do nothing.

Reimplemented from STK::IMixture.

Reimplemented in STK::KmmBridge< Id, Data >.

Definition at line 274 of file STK_IMixtureBridge.h.

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}
hidden::CSlice< Derived, 1, sizeCols_ >::Result row(int i) const
implement the row operator using a reference on the row of the allocator
MissingIndexes::const_iterator ConstIterator
MissingIndexes const & v_missing() const
CArrayXX const * p_tik() const
This function can be used in derived classes to get posterior probabilities from the framework.

◆ initializeStep()

template<class Derived >
virtual void STK::IMixtureBridge< Derived >::initializeStep ( )
inlinevirtual

Initialize the mixture model before its use by the composer.

The parameters values are set to their default values if the mixture_ is newly created. if IMixtureBridge::initializeStep is used during a cloning, mixture class have to take care of the existing values of the parameters.

Implements STK::IMixture.

Definition at line 156 of file STK_IMixtureBridge.h.

157 {
158 if (!p_composer())
160 if (!mixture_.initializeStep()) throw Clust::initializeStepFail_;
161 }
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
virtual void initializeStep()
Initialize the mixture model before its use by the composer.
IMixtureStatModel const *const p_composer() const

References STK::IMixtureBridge< Derived >::initializeStep(), STK::Clust::initializeStepFail_, STK::IMixtureBridge< Derived >::mixture_, STK::IMixture::p_composer(), and STKRUNTIME_ERROR_NO_ARG.

Referenced by STK::IMixtureBridge< Derived >::initializeStep().

◆ lnComponentProbability()

template<class Derived >
virtual Real STK::IMixtureBridge< Derived >::lnComponentProbability ( int  i,
int  k 
)
inlinevirtual

This function must be defined to return the component probability distribution function (PDF) for corresponding sample i and cluster k.

Parameters
i,ksample and cluster numbers
Returns
the log-component probability

Implements STK::IMixture.

Definition at line 167 of file STK_IMixtureBridge.h.

168 { return mixture_.lnComponentProbability(i, k);}

References STK::IMixtureBridge< Derived >::mixture_.

◆ mixture()

template<class Derived >
Mixture const & STK::IMixtureBridge< Derived >::mixture ( ) const
inline
Returns
the mixture

Definition at line 113 of file STK_IMixtureBridge.h.

113{ return mixture_;}

References STK::IMixtureBridge< Derived >::mixture_.

◆ nbFreeParameter()

template<class Derived >
virtual int STK::IMixtureBridge< Derived >::nbFreeParameter ( ) const
inlinevirtual

This function must return the number of free parameters.

Returns
Number of free parameters

Implements STK::IMixture.

Definition at line 181 of file STK_IMixtureBridge.h.

181{ return mixture_.computeNbFreeParameters();}

References STK::IMixtureBridge< Derived >::mixture_.

◆ nbMissing()

template<class Derived >
int STK::IMixtureBridge< Derived >::nbMissing ( ) const
inline
Returns
number of the missing values in data set

Definition at line 119 of file STK_IMixtureBridge.h.

119{ return v_missing_.size();}

References STK::IMixtureBridge< Derived >::v_missing_.

◆ nbMissingValues()

template<class Derived >
virtual int STK::IMixtureBridge< Derived >::nbMissingValues ( ) const
inlinevirtual

This function must return the number of missing value in data set identified by idData_.

Returns
Number of missing values

Implements STK::IMixture.

Definition at line 185 of file STK_IMixtureBridge.h.

185{ return v_missing_.size();}

References STK::IMixtureBridge< Derived >::v_missing_.

◆ p_dataij()

template<class Derived >
Data *const STK::IMixtureBridge< Derived >::p_dataij ( ) const
inline
Returns
the pointer on the data set

Definition at line 117 of file STK_IMixtureBridge.h.

117{ return p_dataij_;}

References STK::IMixtureBridge< Derived >::p_dataij_.

◆ paramUpdateStep()

template<class Derived >
virtual void STK::IMixtureBridge< Derived >::paramUpdateStep ( )
inlinevirtual

This function is equivalent to Mstep and must be defined to update parameters.

Implements STK::IMixture.

Definition at line 172 of file STK_IMixtureBridge.h.

173 { if (!mixture_.run( p_tik(), p_tk())) throw Clust::mStepFail_;}
CPointX const * p_tk() const
This function can be used in derived classes to get proportions from the framework.

References STK::IMixtureBridge< Derived >::mixture_, STK::Clust::mStepFail_, STK::IMixture::p_tik(), and STK::IMixture::p_tk().

◆ randomInit()

template<class Derived >
virtual void STK::IMixtureBridge< Derived >::randomInit ( )
inlinevirtual

This function should be used in order to initialize randomly the parameters of the mixture.

Implements STK::IMixture.

Definition at line 177 of file STK_IMixtureBridge.h.

177{ mixture_.randomInit( p_tik(), p_tk());};

References STK::IMixtureBridge< Derived >::mixture_, STK::IMixture::p_tik(), and STK::IMixture::p_tk().

◆ releaseIntermediateResults()

template<class Derived >
virtual void STK::IMixtureBridge< Derived >::releaseIntermediateResults ( )
inlinevirtual

This step can be used to signal to the mixtures that they must release the stored results.

This is usually called if the estimation process failed.

Reimplemented from STK::IMixture.

Definition at line 197 of file STK_IMixtureBridge.h.

198 { mixture_.param_.releaseStatistics();}

References STK::IMixtureBridge< Derived >::mixture_.

◆ removeMissing()

template<class Derived >
void STK::IMixtureBridge< Derived >::removeMissing ( )
protectedvirtual

This function will be used once for imputation of missing data at the initialization step (.

See also
initializeStep).

By default missing values in a column will be replaced by the mean or map (maximum a posteriori) value of the column. Derived class as to implement the safeValue(i,j) method.

Note
This method is virtual as derived class may want to implement a more efficient algorithm.

Definition at line 333 of file STK_IMixtureBridge.h.

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}
hidden::MixtureBridgeTraits< Derived >::Type Type
Derived & asDerived()
static cast : return a reference of this with a cast to the derived class.
static Type NA()
Adding a Non Available (NA) special number.

References STK::Arithmetic< Type >::NA().

◆ samplingStep()

template<class Derived >
void STK::IMixtureBridge< Derived >::samplingStep ( )
inlinevirtual

This function must be defined for simulation of all the latent variables and/or missing data excluding class labels.

The class labels will be simulated by the framework itself because to do so we have to take into account all the mixture laws.

Reimplemented from STK::IMixture.

Reimplemented in STK::KmmBridge< Id, Data >.

Definition at line 281 of file STK_IMixtureBridge.h.

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}

◆ setParameters() [1/2]

template<class Derived >
template<class Array >
void STK::IMixtureBridge< Derived >::setParameters ( ExprBase< Array > const param)
inline

This function is used in order to set the current values of the parameters to the parameters using an array.

Parameters
paramthe array/expression with the parameters of the mixture to store in the Parameters.

Definition at line 145 of file STK_IMixtureBridge.h.

146 { mixture_.param_.setParameters(param.asDerived());}

References STK::IMixtureBridge< Derived >::mixture_.

◆ setParameters() [2/2]

template<class Derived >
void STK::IMixtureBridge< Derived >::setParameters ( Parameters const param)
inline

set the parameters of the model

Definition at line 130 of file STK_IMixtureBridge.h.

130{ mixture_.param_ = param;}

References STK::IMixtureBridge< Derived >::mixture_.

◆ setParametersStep()

template<class Derived >
virtual void STK::IMixtureBridge< Derived >::setParametersStep ( )
inlinevirtual

set the parameters of the model.

This step should be used to set the initialize the statistics to the current value of the parameters.

Reimplemented from STK::IMixture.

Definition at line 203 of file STK_IMixtureBridge.h.

204 { mixture_.param_.setStatistics();}

References STK::IMixtureBridge< Derived >::mixture_.

◆ storeIntermediateResults()

template<class Derived >
virtual void STK::IMixtureBridge< Derived >::storeIntermediateResults ( int  iteration)
inlinevirtual

This function should be used to store any intermediate results during various iterations after the burn-in period.

Parameters
iterationProvides the iteration number beginning after the burn-in period.

Reimplemented from STK::IMixture.

Definition at line 191 of file STK_IMixtureBridge.h.

192 { mixture_.param_.updateStatistics();}

References STK::IMixtureBridge< Derived >::mixture_.

◆ v_missing()

template<class Derived >
MissingIndexes const & STK::IMixtureBridge< Derived >::v_missing ( ) const
inline
Returns
coordinates of the missing values in the data set

Definition at line 115 of file STK_IMixtureBridge.h.

115{ return v_missing_;}

References STK::IMixtureBridge< Derived >::v_missing_.

◆ writeParameters()

template<class Derived >
virtual void STK::IMixtureBridge< Derived >::writeParameters ( ostream os) const
inlinevirtual

This function can be used to write summary of parameters to the output stream.

Parameters
osStream where you want to write the summary of parameters.

Definition at line 222 of file STK_IMixtureBridge.h.

223 { mixture_.writeParameters(p_tik(), os);}

References STK::IMixtureBridge< Derived >::mixture_, and STK::IMixture::p_tik().

Member Data Documentation

◆ mixture_

◆ p_dataij_

◆ v_missing_

template<class Derived >
MissingIndexes STK::IMixtureBridge< Derived >::v_missing_
protected

The documentation for this class was generated from the following file: