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

Base class for the diagonal Gaussian models. More...

#include <STK_DiagGaussianBase.h>

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

Public Types

typedef IMixtureDensity< Derived > Base
 
- Public Types inherited from STK::IMixtureDensity< Derived >
typedef hidden::MixtureTraits< Derived >::Array Array
 
typedef hidden::MixtureTraits< Derived >::Parameters Parameters
 
typedef hidden::Traits< Array >::Type Type
 

Public Member Functions

Real constmean (int k, int j) const
 
Real constsigma (int k, int j) const
 
void initializeModelImpl ()
 Initialize the parameters of the model.
 
Real lnComponentProbability (int i, int k) const
 
template<class Weights >
Real impute (int i, int j, Weights const &pk) const
 
Real rand (int i, int j, int k) const
 
template<class Array >
void getParameters (Array &params) const
 This function is used in order to get the current values of the means and standard deviations.
 
void writeParameters (CArrayXX const *p_tik, ostream &os) const
 This function can be used to write summary of parameters to the output stream.
 
int nbCluster () const
 
int nbSample () const
 
Array const *constp_data () const
 
- Public Member Functions inherited from STK::IMixtureDensity< Derived >
 ~IMixtureDensity ()
 destructor
 
int nbCluster () const
 
int nbSample () const
 
Real lnNbSample () const
 
Array const *constp_data () const
 
Parameters constparam () const
 
void setData (Array const &data)
 Set the data set.
 
void setData (Array const &data, int nbRow, int nbCol, bool byRow=true)
 Set the data set and give dimensions.
 
bool initializeStep ()
 This function will be called at the beginning of the estimation process once the model is created and data is set.
 
void setParametersStep ()
 set the parameters obtained with the intermediate results and release the intermediate results.
 
void finalizeStep ()
 This function will be called once the model is estimated.
 
template<class Weights >
Type sample (int i, int j, Weights const &tk) const
 
- 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.
 

Public Attributes

Parameters param_
 parameters of the derived mixture model.
 

Protected Member Functions

 DiagGaussianBase (int nbCluster)
 default constructor
 
 DiagGaussianBase (DiagGaussianBase const &model)
 copy constructor
 
 ~DiagGaussianBase ()
 destructor
 
void randomMean (CArrayXX const *p_tik)
 sample randomly the mean of each component by sampling randomly a row of the data set.
 
bool updateMean (CArrayXX const *p_tik)
 compute the weighted mean of a Gaussian mixture.
 
- Protected Member Functions inherited from STK::IMixtureDensity< Derived >
 IMixtureDensity (int nbCluster)
 Default constructor.
 
 IMixtureDensity (IMixtureDensity const &model)
 copy constructor.
 
void initializeModel ()
 Initialize the model before its first use.
 
bool initializeStepImpl ()
 default implementation of initializeStepImpl (do nothing and return true)
 
void finalizeStepImpl ()
 default implementation of finalizeStepImpl (do nothing)
 
void setNbSample (int nbSample)
 Set the number of sample of the model (needed by kernel models)
 
- Protected Member Functions inherited from STK::IRecursiveTemplate< Derived >
 IRecursiveTemplate ()
 constructor.
 
 ~IRecursiveTemplate ()
 destructor.
 

Additional Inherited Members

- Protected Attributes inherited from STK::IMixtureDensity< Derived >
Parameters param_
 parameters of the derived mixture model.
 

Detailed Description

template<class Derived>
class STK::DiagGaussianBase< Derived >

Base class for the diagonal Gaussian models.

Definition at line 53 of file STK_DiagGaussianBase.h.

Member Typedef Documentation

◆ Base

template<class Derived >
typedef IMixtureDensity<Derived > STK::DiagGaussianBase< Derived >::Base

Definition at line 56 of file STK_DiagGaussianBase.h.

Constructor & Destructor Documentation

◆ DiagGaussianBase() [1/2]

template<class Derived >
STK::DiagGaussianBase< Derived >::DiagGaussianBase ( int  nbCluster)
inlineprotected

default constructor

Parameters
nbClusternumber of cluster in the model

Definition at line 66 of file STK_DiagGaussianBase.h.

66: Base(nbCluster) {}
IMixtureDensity< Derived > Base

◆ DiagGaussianBase() [2/2]

template<class Derived >
STK::DiagGaussianBase< Derived >::DiagGaussianBase ( DiagGaussianBase< Derived > const model)
inlineprotected

copy constructor

Parameters
modelThe model to copy

Definition at line 70 of file STK_DiagGaussianBase.h.

70: Base(model) {}

◆ ~DiagGaussianBase()

template<class Derived >
STK::DiagGaussianBase< Derived >::~DiagGaussianBase ( )
inlineprotected

destructor

Definition at line 72 of file STK_DiagGaussianBase.h.

72{}

Member Function Documentation

◆ getParameters()

template<class Derived >
template<class Array >
void STK::DiagGaussianBase< Derived >::getParameters ( Array params) const

This function is used in order to get the current values of the means and standard deviations.

Parameters
[out]paramsthe array with the parameters of the mixture.

Definition at line 187 of file STK_DiagGaussianBase.h.

188{
189 int nbClust = nbCluster();
190 params.resize(2*nbClust, p_data()->cols());
191 for (int k= 0; k < nbClust; ++k)
192 {
193 for (int j= params.beginCols(); j< params.endCols(); ++j)
194 {
195 params(baseIdx+2*k , j) = mean(baseIdx + k, j);
196 params(baseIdx+2*k+1, j) = sigma(baseIdx + k, j);
197 }
198 }
199}
Real const & mean(int k, int j) const
Array const *const & p_data() const
Real const & sigma(int k, int j) const
const int baseIdx
base index of the containers created in STK++.

References STK::baseIdx, and STK::mean().

◆ impute()

template<class Derived >
template<class Weights >
Real STK::DiagGaussianBase< Derived >::impute ( int  i,
int  j,
Weights const pk 
) const
Returns
an imputation value for the jth variable of the ith sample
Parameters
i,jindexes of the data to impute
pkthe probabilities of each class for the ith individual

Definition at line 140 of file STK_DiagGaussianBase.h.

141{
142 Real sum = 0.;
143 for (int k= pk.begin(); k < pk.end(); ++k)
144 { sum += pk[k] * mean(k,j);}
145 return sum;
146}
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
double Real
STK fundamental type of Real values.

References STK::mean(), and STK::sum().

◆ initializeModelImpl()

template<class Derived >
void STK::DiagGaussianBase< Derived >::initializeModelImpl ( )
inline

Initialize the parameters of the model.

Definition at line 81 of file STK_DiagGaussianBase.h.

81{ param_.resize(p_data()->cols());}
Parameters param_
parameters of the derived mixture model.

References STK::DiagGaussianBase< Derived >::p_data(), and STK::DiagGaussianBase< Derived >::param_.

◆ lnComponentProbability()

template<class Derived >
Real STK::DiagGaussianBase< Derived >::lnComponentProbability ( int  i,
int  k 
) const
Returns
value of the log-probability of the i-th sample in the k-th cluster.
Parameters
i,kindexes of the sample and of the component

Definition at line 123 of file STK_DiagGaussianBase.h.

124{
125 Real sum =0.;
126 for (int j=p_data()->beginCols(); j<p_data()->endCols(); ++j)
127 {
128 if (sigma(k,j))
129 { sum += Law::Normal::lpdf(p_data()->elt(i,j), mean(k, j), sigma(k,j));}
130 }
131 return sum;
132}
virtual Real lpdf(Real const &x) const

References STK::Law::Normal::lpdf(), STK::mean(), and STK::sum().

◆ mean()

template<class Derived >
Real const & STK::DiagGaussianBase< Derived >::mean ( int  k,
int  j 
) const
inline
Returns
mean of the kth cluster and jth variable

Definition at line 76 of file STK_DiagGaussianBase.h.

76{ return param_.mean(k,j);}

References STK::DiagGaussianBase< Derived >::param_.

Referenced by STK::DiagGaussianBase< Derived >::rand().

◆ nbCluster()

template<class Derived >
int STK::IMixtureDensity< Derived >::nbCluster ( ) const
inline
Returns
the number of cluster

Definition at line 124 of file STK_IMixtureDensity.h.

124{ return nbCluster_;}
int nbCluster_
number of cluster.

◆ nbSample()

template<class Derived >
int STK::IMixtureDensity< Derived >::nbSample ( ) const
inline
Returns
the total available observations

Definition at line 126 of file STK_IMixtureDensity.h.

126{ return nbSample_;}
int nbSample_
total available samples

◆ p_data()

template<class Derived >
Array const *const & STK::IMixtureDensity< Derived >::p_data ( ) const
inline
Returns
a pointer on the current data set

Definition at line 131 of file STK_IMixtureDensity.h.

131{ return p_dataij_;}
Array const * p_dataij_
pointer on the data set

Referenced by STK::DiagGaussianBase< Derived >::initializeModelImpl().

◆ rand()

template<class Derived >
Real STK::DiagGaussianBase< Derived >::rand ( int  i,
int  j,
int  k 
) const
inline
Returns
a simulated value for the jth variable of the ith sample in the kth cluster
Parameters
i,j,kindexes of the data to simulate

Definition at line 95 of file STK_DiagGaussianBase.h.

96 { return Law::Normal::rand(mean(k, j), sigma(k,j));}
Real rand() const
Generate a pseudo Normal random variate.

References STK::DiagGaussianBase< Derived >::mean(), STK::Law::Normal::rand(), and STK::DiagGaussianBase< Derived >::sigma().

◆ randomMean()

template<class Derived >
void STK::DiagGaussianBase< Derived >::randomMean ( CArrayXX const p_tik)
protected

sample randomly the mean of each component by sampling randomly a row of the data set.

Definition at line 149 of file STK_DiagGaussianBase.h.

150{
151 // indexes array
152 VectorXi indexes(p_data()->rows());
153 for(int i=indexes.begin(); i< indexes.end(); ++i) { indexes[i] = i;}
154 Range rind(p_data()->rows());
155 // sample without repetition
156 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
157 {
158 // random number in [0, end-k[
159 int i = Law::UniformDiscrete::rand(rind.begin(), rind.end()-1);
160 // get ith individuals
161 param_.mean_[k] = p_data()->row(indexes[i]);
162 // exchange it with nth
163 indexes.swap(i, rind.lastIdx());
164 // decrease
165 rind.decLast(1);
166 }
167}
virtual int rand() const
Generate a pseudo Uniform random variate.
Array2DVector< int > VectorXi
TRange< UnknownSize > Range
Definition STK_Range.h:59

References STK::Law::UniformDiscrete::rand().

◆ sigma()

template<class Derived >
Real const & STK::DiagGaussianBase< Derived >::sigma ( int  k,
int  j 
) const
inline
Returns
sigma of the kth cluster and jth variable

Definition at line 78 of file STK_DiagGaussianBase.h.

78{ return param_.sigma(k,j);}

References STK::DiagGaussianBase< Derived >::param_.

Referenced by STK::DiagGaussianBase< Derived >::rand().

◆ updateMean()

template<class Derived >
bool STK::DiagGaussianBase< Derived >::updateMean ( CArrayXX const p_tik)
protected

compute the weighted mean of a Gaussian mixture.

Definition at line 171 of file STK_DiagGaussianBase.h.

172{
173 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
174 {
175 for (int j=p_data()->beginCols(); j< p_data()->endCols(); ++j)
176 { param_.mean_[k][j] = p_data()->col(j).wmean(p_tik->col(k));}
177 }
178 return true;
179}

References STK::ICArray< Derived >::col().

◆ writeParameters()

template<class Derived >
void STK::DiagGaussianBase< Derived >::writeParameters ( CArrayXX const p_tik,
ostream os 
) const

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

Parameters
p_tika constant pointer on the posterior probabilities
osStream where you want to write the summary of parameters.

Definition at line 207 of file STK_DiagGaussianBase.h.

208{
209 CPointX m(p_data()->cols()), s(p_data()->cols());
210 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
211 {
212 // store sigma values in an array for a nice output
213 for (int j= s.begin(); j < s.end(); ++j)
214 { m[j] = mean(k,j); s[j] = sigma(k,j);}
215 os << _T("---> Component ") << k << _T("\n");
216 os << _T("mean = ") << m;
217 os << _T("sigma = ")<< s;
218 }
219}
#define _T(x)
Let x unmodified.
CArrayPoint< Real, UnknownSize, Arrays::by_col_ > CPointX

References _T, and STK::mean().

Member Data Documentation

◆ param_

template<class Derived >
Parameters STK::IMixtureDensity< Derived >::param_

parameters of the derived mixture model.

Should be an instance of the STK::ModelParameters struct.

See also
STK::ModelParameters

Definition at line 180 of file STK_IMixtureDensity.h.

Referenced by STK::DiagGaussianBase< Derived >::initializeModelImpl(), STK::DiagGaussianBase< Derived >::mean(), and STK::DiagGaussianBase< Derived >::sigma().


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