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

Base class for the Categorical models. More...

#include <STK_CategoricalBase.h>

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

Public Member Functions

PointXi constnbModalities () const
 
Range constmodalities () const
 
Real proba (int k, int j, int l) const
 
CVectorX proba (int k, int j) const
 
void initializeModelImpl ()
 Initialize the model.
 
Real lnComponentProbability (int i, int k) const
 
template<class Weights >
int impute (int i, int j, Weights const &tk) const
 
int 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 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.
 
- 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.
 

Protected Types

typedef IMixtureDensity< Derived > Base
 

Protected Member Functions

 CategoricalBase (int nbCluster)
 default constructor
 
 CategoricalBase (CategoricalBase const &model)
 copy constructor
 
 ~CategoricalBase ()
 destructor
 
Array const *constp_data () const
 
- 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

PointXi nbModalities_
 Array with the number of modalities of each columns of the data set.
 
Range modalities_
 range of the modalities
 
Parameters param_
 parameters of the derived mixture model.
 
- Protected Attributes inherited from STK::IMixtureDensity< Derived >
Parameters param_
 parameters of the derived mixture model.
 

Additional Inherited Members

- 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
 

Detailed Description

template<class Derived>
class STK::CategoricalBase< Derived >

Base class for the Categorical models.

Definition at line 50 of file STK_CategoricalBase.h.

Member Typedef Documentation

◆ Base

template<class Derived >
typedef IMixtureDensity<Derived> STK::CategoricalBase< Derived >::Base
protected

Definition at line 53 of file STK_CategoricalBase.h.

Constructor & Destructor Documentation

◆ CategoricalBase() [1/2]

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

default constructor

Parameters
nbClusternumber of cluster in the model

Definition at line 60 of file STK_CategoricalBase.h.

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

◆ CategoricalBase() [2/2]

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

copy constructor

Parameters
modelThe model to copy

Definition at line 64 of file STK_CategoricalBase.h.

65 : Base(model), modalities_(model.modalities_)
66 {}
Range modalities_
range of the modalities

◆ ~CategoricalBase()

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

destructor

Definition at line 68 of file STK_CategoricalBase.h.

68{}

Member Function Documentation

◆ getParameters()

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

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 195 of file STK_CategoricalBase.h.

196{
197 int nbCluster = this->nbCluster();
198 int nbModalities = modalities().size();
199
200 params.resize(nbModalities * nbCluster, p_data()->cols());
201 for (int k = 0; k < nbCluster; ++k)
202 {
203 for (int j = p_data()->beginCols(); j < p_data()->endCols(); ++j)
204 {
205 for (int l = 0; l < nbModalities; ++l)
206 { params(baseIdx+k * nbModalities + l, j) = proba(baseIdx+k, j, modalities().begin() + l);}
207 }
208 }
209}
Array const *const & p_data() const
Real proba(int k, int j, int l) const
Range const & modalities() const
PointXi const & nbModalities() const
int size() const
get the size of the TRange (the number of elements).
Definition STK_Range.h:303
const int baseIdx
base index of the containers created in STK++.

References STK::baseIdx.

◆ impute()

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

Definition at line 155 of file STK_CategoricalBase.h.

156{
157 int lmax = modalities_.begin();
158 Real pmax = -Arithmetic<Real>::max();
159 // compute for each modality the pondered probability of occurrence
160 for (int l=modalities_.begin(); l< modalities_.end(); ++l)
161 {
162 Real p = 0.;
163 for (int k= tk.begin(); k < tk.end(); ++k)
164 { p += tk[k] * proba(k, j, l);}
165
166 if (pmax < p) { pmax = p; lmax = l;}
167 }
168 return lmax;
169}
int begin() const
get the first index of the TRange.
Definition STK_Range.h:93
int end() const
get the ending index of the TRange.
Definition STK_Range.h:299
double Real
STK fundamental type of Real values.

◆ initializeModelImpl()

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

Initialize the model.

Resize the probability arrays of each component and compute the number and range of modalities of each variables.

Definition at line 82 of file STK_CategoricalBase.h.

83 {
84 // compute the maximal number of modalities
85 nbModalities_.resize(p_data()->cols());
86 int amin = Arithmetic<int>::max(), amax = Arithmetic<int>::min();
87 for (int j= p_data()->beginCols(); j < p_data()->endCols(); ++j)
88 {
89 int min = p_data()->col(j).minElt(), max = p_data()->col(j).maxElt();
90 amin = std::min(amin, min); amax = std::max(amax, max);
91 nbModalities_[j] = max-min+1;
92 }
93 // set range of the modalities
94 modalities_ = _R(amin, amax);
95 // resize vectors of probabilities
96 param_.resize(modalities_,p_data()->cols());
97#ifdef STK_MIXTURE_VERBOSE
98 stk_cout << _T("In CatagoricalBase::initializeModelImpl. modalities_ = ")
99 << modalities_ << _T("\n");
100#endif
101 }
#define _R(first, last)
Utility macro that can be used in a similar way that first:last.
Definition STK_Range.h:53
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Parameters param_
parameters of the derived mixture model.
PointXi nbModalities_
Array with the number of modalities of each columns of the data set.
Derived & resize(Range const &I, Range const &J)
resize the array.
hidden::SliceVisitorSelector< Derived, hidden::MinVisitor, Arrays::by_col_ >::type_result min(Derived const &A)
If A is a row-vector or a column-vector then the function will return the usual minimal value of the ...
hidden::SliceVisitorSelector< Derived, hidden::MaxVisitor, Arrays::by_col_ >::type_result max(Derived const &A)
If A is a row-vector or a column-vector then the function will return the usual maximal value of the ...

References _R, _T, STK::max(), STK::min(), STK::CategoricalBase< Derived >::modalities_, STK::CategoricalBase< Derived >::nbModalities_, STK::CategoricalBase< Derived >::p_data(), STK::CategoricalBase< Derived >::param_, STK::IArray2D< Derived >::resize(), and stk_cout.

◆ lnComponentProbability()

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

Definition at line 140 of file STK_CategoricalBase.h.

141{
142 Real sum =0.;
143 for (int j=p_data()->beginCols(); j<p_data()->endCols(); ++j)
144 { // what to do if the probability is zero but a sample get this modality
145 // for now, just ignore it (it's possible if tik_(i,k) == 0)
146 Real prob= proba(k, j, p_data()->elt(i,j));
147 if (prob) { sum += std::log(prob);}
148 }
149 return sum;
150}
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays

References STK::sum().

◆ modalities()

template<class Derived >
Range const & STK::CategoricalBase< Derived >::modalities ( ) const
inline
Returns
the range of the modalities

Definition at line 74 of file STK_CategoricalBase.h.

74{ return modalities_;}

References STK::CategoricalBase< Derived >::modalities_.

◆ nbModalities()

template<class Derived >
PointXi const & STK::CategoricalBase< Derived >::nbModalities ( ) const
inline
Returns
the array with the number of modalities of each columns in data set

Definition at line 72 of file STK_CategoricalBase.h.

72{ return nbModalities_;}

References STK::CategoricalBase< Derived >::nbModalities_.

◆ p_data()

template<class Derived >
Array const *const & STK::IMixtureDensity< Derived >::p_data ( ) const
inlineprotected
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::CategoricalBase< Derived >::initializeModelImpl().

◆ proba() [1/2]

template<class Derived >
CVectorX STK::CategoricalBase< Derived >::proba ( int  k,
int  j 
) const
inline
Returns
the probabilities of the kth cluster for the jth variable

Definition at line 78 of file STK_CategoricalBase.h.

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

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

◆ proba() [2/2]

template<class Derived >
Real STK::CategoricalBase< Derived >::proba ( int  k,
int  j,
int  l 
) const
inline
Returns
the probability of the kth cluster, jth variable, lth modality

Definition at line 76 of file STK_CategoricalBase.h.

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

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

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

◆ rand()

template<class Derived >
int STK::CategoricalBase< 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 115 of file STK_CategoricalBase.h.

116 { return Law::Categorical::rand(proba(k,j));}
virtual int rand() const

References STK::CategoricalBase< Derived >::proba(), and STK::Law::Categorical::rand().

◆ writeParameters()

template<class Derived >
void STK::CategoricalBase< 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 176 of file STK_CategoricalBase.h.

177{
178 ArrayXX p(modalities(), p_data()->cols());
179 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
180 {
181 // store proba values in an array for a nice output
182 for (int j= p.beginCols(); j < p.endCols(); ++j)
183 {
184 for (int l= modalities().begin(); l < modalities().end(); ++l)
185 { p(l, j) = proba(k,j,l);}
186 }
187 os << _T("---> Component ") << k << _T("\n");
188 os << _T("probabilities =\n") << p << _T("\n");
189 }
190
191}
Array2D< Real > ArrayXX
Specialization of the Array2D class for Real values.
Definition STK_Array2D.h:53

References _T.

Member Data Documentation

◆ modalities_

template<class Derived >
Range STK::CategoricalBase< Derived >::modalities_
protected

◆ nbModalities_

template<class Derived >
PointXi STK::CategoricalBase< Derived >::nbModalities_
protected

Array with the number of modalities of each columns of the data set.

Definition at line 131 of file STK_CategoricalBase.h.

Referenced by STK::CategoricalBase< Derived >::initializeModelImpl(), and STK::CategoricalBase< Derived >::nbModalities().

◆ param_

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

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::CategoricalBase< Derived >::initializeModelImpl(), STK::CategoricalBase< Derived >::proba(), and STK::CategoricalBase< Derived >::proba().


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