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

Base class for the gamma models. More...

#include <STK_GammaBase.h>

Inheritance diagram for STK::GammaBase< 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 shape (int k, int j) const
 
Real scale (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 parameters in an array of size (2*nbCluster, nbVariable).
 
void writeParameters (CArrayXX const *p_tik, ostream &os) const
 This function can be used to write summary of parameters to the output stream.
 
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

 GammaBase (int nbCluster)
 default constructor
 
 GammaBase (GammaBase const &model)
 copy constructor
 
 ~GammaBase ()
 destructor
 
Real qValue (CArrayXX const *p_tik, CPointX const *p_tk) const
 compute the Q(theta) value.
 
bool moments (CArrayXX const *p_tik)
 compute the weighted moments of a gamma mixture.
 
Real meanjk (int j, int k)
 get the weighted mean of the jth variable of the kth cluster.
 
Real variancejk (int j, int k)
 get the weighted variance of the jth variable of the kth cluster.
 
Real meank (int k)
 get the mean of the weighted means of the kth cluster.
 
Real variancek (int k)
 get the mean of the weighted variances of the kth cluster.
 
- 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::GammaBase< Derived >

Base class for the gamma models.

Definition at line 95 of file STK_GammaBase.h.

Member Typedef Documentation

◆ Base

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

Definition at line 98 of file STK_GammaBase.h.

Constructor & Destructor Documentation

◆ GammaBase() [1/2]

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

default constructor

Parameters
nbClusternumber of cluster in the model

Definition at line 106 of file STK_GammaBase.h.

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

◆ GammaBase() [2/2]

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

copy constructor

Parameters
modelThe model to copy

Definition at line 110 of file STK_GammaBase.h.

110: Base(model) {}

◆ ~GammaBase()

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

destructor

Definition at line 112 of file STK_GammaBase.h.

112{}

Member Function Documentation

◆ getParameters()

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

This function is used in order to get the current values of the parameters in an array of size (2*nbCluster, nbVariable).

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

Definition at line 259 of file STK_GammaBase.h.

260{
261 int nbClust = this->nbCluster();
262 params.resize(2*nbClust, p_data()->cols());
263 for (int k= 0; k < nbClust; ++k)
264 {
265 for (int j= p_data()->beginCols(); j< p_data()->endCols(); ++j)
266 {
267 params(baseIdx+2*k , j) = shape(baseIdx+k,j);
268 params(baseIdx+2*k+1, j) = scale(baseIdx+k,j);
269 }
270 }
271}
Array const *const & p_data() const
Real shape(int k, int j) const
Real scale(int k, int j) const
const int baseIdx
base index of the containers created in STK++.

References STK::baseIdx.

◆ impute()

template<class Derived >
template<class Weights >
Real STK::GammaBase< 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 186 of file STK_GammaBase.h.

187{
188 Real sum = 0.;
189 for (int k= pk.begin(); k < pk.end(); ++k)
190 { sum += pk[k] * shape(k,j) * scale(k,j);}
191 return sum;
192}
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::sum().

◆ initializeModelImpl()

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

Initialize the parameters of the model.

Definition at line 121 of file STK_GammaBase.h.

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

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

◆ lnComponentProbability()

template<class Derived >
Real STK::GammaBase< Derived >::lnComponentProbability ( int  i,
int  k 
) const
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 170 of file STK_GammaBase.h.

171{
172 Real sum =0.;
173 for (int j=p_data()->beginCols(); j<p_data()->endCols(); ++j)
174 {
175 if (param_.shape(k,j) && param_.scale(k,j))
176 { sum += Law::Gamma::lpdf(p_data()->elt(i,j), param_.shape(k,j), param_.scale(k,j));}
177 }
178 return sum;
179}
virtual Real lpdf(Real const &x) const
Compute.

References STK::Law::Gamma::lpdf(), and STK::sum().

◆ meanjk()

template<class Derived >
Real STK::GammaBase< Derived >::meanjk ( int  j,
int  k 
)
inlineprotected

get the weighted mean of the jth variable of the kth cluster.

Definition at line 157 of file STK_GammaBase.h.

157{ return param_.mean_[k][j];}

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

◆ meank()

template<class Derived >
Real STK::GammaBase< Derived >::meank ( int  k)
inlineprotected

get the mean of the weighted means of the kth cluster.

Definition at line 161 of file STK_GammaBase.h.

161{ return param_.mean_[k].mean();}

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

◆ moments()

template<class Derived >
bool STK::GammaBase< Derived >::moments ( CArrayXX const p_tik)
protected

compute the weighted moments of a gamma mixture.

Definition at line 196 of file STK_GammaBase.h.

197{
198 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
199 {
200 CVectorX tikColk(p_tik->col(k), true); // create a reference
201 for (int j=p_data()->beginCols(); j<p_data()->endCols(); ++j)
202 {
203 // mean
204 Real mean = p_data()->col(j).wmean(tikColk);
205 if ( (mean<=0) || isNA(mean) ) { return false;}
206 param_.mean_[k][j] = mean;
207 // mean log
208 Real meanLog = p_data()->col(j).log().wmean(tikColk);
209 if (isNA(meanLog)) { return false;}
210 param_.meanLog_[k][j] = meanLog;
211 // variance
212 Real variance = p_data()->col(j).wvariance(mean, tikColk);
213 if ((variance<=0)||isNA(variance)){ return false;}
214 param_.variance_[k][j] = variance;
215 }
216 }
217 return true;
218}
bool isNA(Type const &x)
utility method allowing to know if a value is a NA (Not Available) value
hidden::SliceVisitorSelector< Derived, hidden::MeanVisitor, Arrays::by_col_ >::type_result mean(Derived const &A)
If A is a row-vector or a column-vector then the function will return the usual mean value of the vec...
hidden::FunctorTraits< Derived, VarianceOp >::Row variance(Derived const &A, bool unbiased=false)
Compute the variance(s) value(s) of A.
CArrayVector< Real, UnknownSize, Arrays::by_col_ > CVectorX

References STK::ICArray< Derived >::col(), STK::isNA(), and STK::mean().

◆ 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::GammaBase< Derived >::initializeModelImpl().

◆ qValue()

template<class Derived >
Real STK::GammaBase< Derived >::qValue ( CArrayXX const p_tik,
CPointX const p_tk 
) const
protected

compute the Q(theta) value.

Definition at line 222 of file STK_GammaBase.h.

223{
224 Real value = 0.;
225 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
226 {
227 Real sumjk=0.0;
228 for (int j=p_data()->beginCols(); j<p_data()->endCols(); ++j)
229 {
230 Real a = shape(k,j), b = scale(k,j);
231 sumjk += a * (param_.meanLog_[k][j]-std::log(b))
232 - param_.mean_[k][j]/b - STK::Funct::lgamma(a);
233 }
234 value += p_tk->elt(k) * sumjk;
235 }
236 return value;
237}
Real lgamma(Real const &)
This function computes the function .

References STK::Funct::lgamma().

◆ rand()

template<class Derived >
Real STK::GammaBase< 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 137 of file STK_GammaBase.h.

138 { return Law::Gamma::rand(shape(k,j), scale(k,j));}
virtual Real rand() const
generate a gamma random variate using the G.S algorithm of Ahrens and Dieter (1974) for 0<a_<1 and Ma...

References STK::Law::Gamma::rand(), STK::GammaBase< Derived >::scale(), and STK::GammaBase< Derived >::shape().

◆ scale()

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

Definition at line 118 of file STK_GammaBase.h.

118{ return param_.scale(k,j);}

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

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

◆ shape()

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

Definition at line 116 of file STK_GammaBase.h.

116{ return param_.shape(k,j);}

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

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

◆ variancejk()

template<class Derived >
Real STK::GammaBase< Derived >::variancejk ( int  j,
int  k 
)
inlineprotected

get the weighted variance of the jth variable of the kth cluster.

Definition at line 159 of file STK_GammaBase.h.

159{ return param_.variance_[k][j];}

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

◆ variancek()

template<class Derived >
Real STK::GammaBase< Derived >::variancek ( int  k)
inlineprotected

get the mean of the weighted variances of the kth cluster.

Definition at line 163 of file STK_GammaBase.h.

163{ return param_.variance_[k].mean();}

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

◆ writeParameters()

template<class Derived >
void STK::GammaBase< 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 240 of file STK_GammaBase.h.

241{
242 CPointX a(p_data()->cols()), b(p_data()->cols());
243 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
244 {
245 // store shape and scale values in an array for a nice output
246 for (int j=p_data()->beginCols(); j < p_data()->endCols(); ++j)
247 {
248 a[j] = shape(k,j);
249 b[j] = scale(k,j);
250 }
251 os << _T("---> Component ") << k << _T("\n");
252 os << _T("shape = ") << a;
253 os << _T("scale = ") << b;
254 }
255}
#define _T(x)
Let x unmodified.
CArrayPoint< Real, UnknownSize, Arrays::by_col_ > CPointX

References _T.

Member Data Documentation

◆ param_


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