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

Base class for the Gaussian Kernel Mixture Models. More...

#include <STK_KmmBase.h>

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

Public Types

typedef IMixtureDensity< Derived > Base
 
typedef hidden::MixtureTraits< Derived >::Array Array
 
- 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 sigma2 (int k) const
 
Real dim (int k) const
 
Kernel::IKernel const *const p_kernel () const
 
CArrayXX dik () const
 
void setDim (Real const &dim)
 set the dimensions of the kernel mixture model using an unique value
 
template<class Vector >
void setDim (ExprBase< Vector > const &dim)
 set the dimension of the kernel mixture model
 
void setKernel (Kernel::IKernel const *p_kernel)
 set the dimensions of the kernel mixture model using an unique value.
 
template<class Weights >
Real impute (int i, int j, Weights const &pk) const
 
Real rand (int i, int j, int k) const
 
template<class ArrayParam >
void getParameters (ArrayParam &params) const
 This function is used in order to get the current values of the parameters in an array.
 
void writeParameters (CArrayXX const *p_tik, ostream &os) const
 This function can be used to write summary of parameters to the output stream.
 
int nbSample () const
 
int nbCluster () 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

 KmmBase (int nbCluster)
 default constructor
 
 KmmBase (KmmBase const &model)
 copy constructor
 
 ~KmmBase ()
 destructor
 
void initializeModel ()
 Initialize the model before its first use.
 
void compute_dik (CArrayXX const *p_tik, CPointX const *p_tk)
 compute the distance of the ith individual to the kth centroid
 
void initializeModelImpl ()
 default implementation of initializeModelImpl (do nothing)
 
- 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.
 

Protected Attributes

Kernel::IKernel constp_kernel_
 pointer on the kernel
 
CArrayXX dik_
 Array of the intermediate results dik.
 
- Protected Attributes inherited from STK::IMixtureDensity< Derived >
Parameters param_
 parameters of the derived mixture model.
 

Private Member Functions

void setData (Array const &data)
 Set the data set.
 

Detailed Description

template<class Derived>
class STK::KmmBase< Derived >

Base class for the Gaussian Kernel Mixture Models.

Definition at line 53 of file STK_KmmBase.h.

Member Typedef Documentation

◆ Array

template<class Derived >
typedef hidden::MixtureTraits<Derived>::Array STK::KmmBase< Derived >::Array

Definition at line 57 of file STK_KmmBase.h.

◆ Base

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

Definition at line 56 of file STK_KmmBase.h.

Constructor & Destructor Documentation

◆ KmmBase() [1/2]

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

default constructor

Parameters
nbClusternumber of cluster in the model

Definition at line 67 of file STK_KmmBase.h.

67: Base(nbCluster), p_kernel_(0) {}
int nbCluster() const
Kernel::IKernel const * p_kernel_
pointer on the kernel
IMixtureDensity< Derived > Base
Definition STK_KmmBase.h:56

◆ KmmBase() [2/2]

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

copy constructor

Parameters
modelModel to copy

Definition at line 71 of file STK_KmmBase.h.

72 : Base(model)
73 , p_kernel_(model.p_kernel_)
74 , dik_(model.dik_)
75 {}
CArrayXX dik_
Array of the intermediate results dik.

◆ ~KmmBase()

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

destructor

Definition at line 77 of file STK_KmmBase.h.

77{}

Member Function Documentation

◆ compute_dik()

template<class Derived >
void STK::KmmBase< Derived >::compute_dik ( CArrayXX const p_tik,
CPointX const p_tk 
)
protected

compute the distance of the ith individual to the kth centroid

\[ d_{ik} = \|\phi(x_i)-m_k\| \]

using the kernel trick.

Definition at line 172 of file STK_KmmBase.h.

173{
174#ifdef STK_KERNELS_DEBUG
175 stk_cout << _T("Entering KmmBase::compute_dik\n");
176 stk_cout << _T("dik_.cols() =") << dik_.cols() << _T("\n");
177 stk_cout << _T("dik_.rows() =") << dik_.rows() << _T("\n");
178#endif
179 CVectorX wik(dik_.rows());
180 for (int k=dik_.beginCols(); k<dik_.endCols(); ++k)
181 {
182 // Compute wik=\sum_{j=1}^n k(x_i,x_j) t_{jk}/t_{.k}, for k=1,..,K
183 for (int i= wik.begin(); i < wik.end(); ++i)
184 {
185 wik[i] = 0.;
186 for (int j= wik.begin(); j < wik.end(); ++j)
187 { wik[i] += p_kernel_->comp(i, j) * p_tik->elt(j,k);}
188 }
189 wik /= p_tk->elt(k);
190 // compute dik_ = k(i,i) - 2 * wik + \sum_{i=1}^n t_{ik} w_{ik}/t_{.k}
191 Real dk = p_tik->col(k).dot(wik)/p_tk->elt(k);
192 for (int i= wik.begin(); i<wik.end(); ++i)
193 { dik_(i,k) = p_kernel_->diag(i) - 2. * wik[i] + dk ;}
194 }
195#ifdef STK_KERNELS_DEBUG
196 stk_cout << _T("KmmBase::compute_dik done\n");
197#endif
198}
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
virtual Real diag(int i) const
virtual method.
virtual Real comp(int i, int j) const =0
pure virtual method.
double Real
STK fundamental type of Real values.
CArrayVector< Real, UnknownSize, Arrays::by_col_ > CVectorX

References _T, STK::ICArray< Derived >::col(), and stk_cout.

◆ dik()

template<class Derived >
CArrayXX STK::KmmBase< Derived >::dik ( ) const
inline
Returns
distance of the individual to the kth centroid

Definition at line 88 of file STK_KmmBase.h.

88{ return dik_;}

References STK::KmmBase< Derived >::dik_.

◆ dim()

template<class Derived >
Real STK::KmmBase< Derived >::dim ( int  k) const
inline
Returns
the value of dim in the kth

Definition at line 84 of file STK_KmmBase.h.

84{ return param_.dim(k);}
Parameters param_
parameters of the derived mixture model.

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

Referenced by STK::KmmBase< Derived >::setDim(), and STK::KmmBase< Derived >::setDim().

◆ getParameters()

template<class Derived >
template<class ArrayParam >
void STK::KmmBase< Derived >::getParameters ( ArrayParam params) const
inline

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

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

Definition at line 227 of file STK_KmmBase.h.

228{
229 param.resize(this->nbCluster(), 2);
230 for (int k= param.beginRows(); k < param.endRows(); ++k)
231 {
232 param(k, baseIdx ) = param_.sigma2(k);
233 param(k, baseIdx+1) = param_.dim(k);
234 }
235}
Parameters const & param() const
const int baseIdx
base index of the containers created in STK++.

References STK::baseIdx.

◆ impute()

template<class Derived >
template<class Weights >
Real STK::KmmBase< Derived >::impute ( int  i,
int  j,
Weights const pk 
) const
inline
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
Note
this method is not used as there is no missing values to impute

Definition at line 116 of file STK_KmmBase.h.

116{ return 0.;}

◆ initializeModel()

template<class Derived >
void STK::KmmBase< Derived >::initializeModel ( )
protected

Initialize the model before its first use.

This function is triggered when kernel is set. In this interface, the initializeModel() method

Definition at line 211 of file STK_KmmBase.h.

212{
213 // set dimensions
216 // call specific model initialization stuff (not really necessary)
217 this->asDerived().initializeModelImpl();
218 //compute_dik();
219}
Derived & resize(Range const &I, Range const &J)
resize the Array.
void setNbSample(int nbSample)
Set the number of sample of the model (needed by kernel models)
Derived & asDerived()
static cast : return a reference of this with a cast to the derived class.
virtual int nbSample() const =0
pure virtual method.

Referenced by STK::KmmBase< Derived >::setKernel().

◆ initializeModelImpl()

template<class Derived >
void STK::KmmBase< Derived >::initializeModelImpl ( )
inlineprotected

default implementation of initializeModelImpl (do nothing)

Definition at line 157 of file STK_KmmBase.h.

157{/* do nothing*/}

◆ 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_kernel()

template<class Derived >
Kernel::IKernel const *const STK::KmmBase< Derived >::p_kernel ( ) const
inline
Returns
the pointer on the kernel

Definition at line 86 of file STK_KmmBase.h.

86{ return p_kernel_;}

References STK::KmmBase< Derived >::p_kernel_.

Referenced by STK::KmmBase< Derived >::setKernel().

◆ rand()

template<class Derived >
Real STK::KmmBase< Derived >::rand ( int  i,
int  j,
int  k 
) const
inline
Returns
a simulated value for the jth variable of the ith sample
Note
this method is not used as there is no missing values to simulate

Definition at line 120 of file STK_KmmBase.h.

120{ return 0.;}

◆ setData()

template<class Derived >
void STK::KmmBase< Derived >::setData ( Array const data)
inlineprivate

Set the data set.

method is re-implemented for debug purpose

Parameters
datathe data set to set

Definition at line 164 of file STK_KmmBase.h.

164{ this->p_dataij_ = 0;}
Array const * p_dataij_
pointer on the data set

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

◆ setDim() [1/2]

template<class Derived >
template<class Vector >
void STK::KmmBase< Derived >::setDim ( ExprBase< Vector > const dim)
inline

set the dimension of the kernel mixture model

Definition at line 95 of file STK_KmmBase.h.

96 {
98 param_.dim_ = dim.asDerived();
99 }
#define STK_STATIC_ASSERT_ONE_DIMENSION_ONLY(EXPR)
Real dim(int k) const
Definition STK_KmmBase.h:84
Array2DVector< Real > Vector
final class for a Real vertical container.

References STK::KmmBase< Derived >::dim(), STK::KmmBase< Derived >::param_, and STK_STATIC_ASSERT_ONE_DIMENSION_ONLY.

◆ setDim() [2/2]

template<class Derived >
void STK::KmmBase< Derived >::setDim ( Real const dim)
inline

set the dimensions of the kernel mixture model using an unique value

Definition at line 92 of file STK_KmmBase.h.

92{ param_.dim_ = dim;}

References STK::KmmBase< Derived >::dim(), and STK::KmmBase< Derived >::param_.

◆ setKernel()

template<class Derived >
void STK::KmmBase< Derived >::setKernel ( Kernel::IKernel const p_kernel)
inline

set the dimensions of the kernel mixture model using an unique value.

call to this method will triger the call to initializeModelImpl (as setData will not be called)

Definition at line 105 of file STK_KmmBase.h.

106 {
109 }
Kernel::IKernel const *const p_kernel() const
Definition STK_KmmBase.h:86
void initializeModel()
Initialize the model before its first use.

References STK::KmmBase< Derived >::initializeModel(), STK::KmmBase< Derived >::p_kernel(), and STK::KmmBase< Derived >::p_kernel_.

◆ sigma2()

template<class Derived >
Real STK::KmmBase< Derived >::sigma2 ( int  k) const
inline
Returns
the value of sigma2 in the kth cluster

Definition at line 82 of file STK_KmmBase.h.

82{ return param_.sigma2(k);}

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

◆ writeParameters()

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

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 241 of file STK_KmmBase.h.

242{
243 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
244 {
245 os << _T("---> Component ") << k << _T("\n");
246 os << _T("sigma2 = ") << param_.sigma2(k) << _T("\n");
247 os << _T("dim = ") << param_.dim(k) << _T("\n");
248 }
249}

References _T.

Member Data Documentation

◆ dik_

template<class Derived >
CArrayXX STK::KmmBase< Derived >::dik_
protected

Array of the intermediate results dik.

Definition at line 138 of file STK_KmmBase.h.

Referenced by STK::KmmBase< Derived >::dik().

◆ p_kernel_

template<class Derived >
Kernel::IKernel const* STK::KmmBase< Derived >::p_kernel_
protected

pointer on the kernel

Definition at line 136 of file STK_KmmBase.h.

Referenced by STK::KmmBase< Derived >::p_kernel(), and STK::KmmBase< Derived >::setKernel().

◆ 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::KmmBase< Derived >::dim(), STK::KmmBase< Derived >::setDim(), STK::KmmBase< Derived >::setDim(), and STK::KmmBase< Derived >::sigma2().


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