STK++ 0.9.13
STK::Gamma_a_bk< Array > Class Template Reference

Gamma_a_bk is a mixture model of the following form. More...

#include <STK_Gamma_a_bk.h>

Inheritance diagram for STK::Gamma_a_bk< Array >:
Inheritance graph

Public Types

typedef GammaBase< Gamma_a_bk< Array > > Base
 
- Public Types inherited from STK::GammaBase< Gamma_a_bk< Array > >
typedef IMixtureDensity< Gamma_a_bk< Array > > 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

 Gamma_a_bk (int nbCluster)
 default constructor
 
 Gamma_a_bk (Gamma_a_bk const &model)
 copy constructor
 
 ~Gamma_a_bk ()
 destructor
 
void randomInit (CArrayXX const *const &p_tik, CPointX const *const &p_tk)
 Initialize randomly the parameters of the Gamma mixture.
 
bool run (CArrayXX const *const &p_tik, CPointX const *const &p_tk)
 Compute the run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) .
 
int computeNbFreeParameters () const
 
- Public Member Functions inherited from STK::GammaBase< Gamma_a_bk< Array > >
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
 
Real impute (int i, int j, Weights const &pk) const
 
Real rand (int i, int j, int k) const
 
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.
 

Additional Inherited Members

- Public Attributes inherited from STK::GammaBase< Gamma_a_bk< Array > >
Parameters param_
 parameters of the derived mixture model.
 
- Protected Member Functions inherited from STK::GammaBase< Gamma_a_bk< Array > >
 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.
 
- Protected Attributes inherited from STK::IMixtureDensity< Derived >
Parameters param_
 parameters of the derived mixture model.
 

Detailed Description

template<class Array>
class STK::Gamma_a_bk< Array >

Gamma_a_bk is a mixture model of the following form.

\[
    f(\mathbf{x}_i|\theta) = \sum_{k=1}^K p_k
    \prod_{j=1}^p\left(\frac{x_i^j}{b_{k}}\right)^{a-1}
                  \frac{e^{-x_i^j/b_{k}}}{b_{k} \, \Gamma(a)},
     \quad x_i^j>0, \quad i=1,\ldots,n.
\]

Definition at line 70 of file STK_Gamma_a_bk.h.

Member Typedef Documentation

◆ Base

Definition at line 73 of file STK_Gamma_a_bk.h.

Constructor & Destructor Documentation

◆ Gamma_a_bk() [1/2]

template<class Array >
STK::Gamma_a_bk< Array >::Gamma_a_bk ( int  nbCluster)
inline

default constructor

Parameters
nbClusternumber of cluster in the model

Definition at line 86 of file STK_Gamma_a_bk.h.

86: Base(nbCluster) {}
GammaBase< Gamma_a_bk< Array > > Base

◆ Gamma_a_bk() [2/2]

template<class Array >
STK::Gamma_a_bk< Array >::Gamma_a_bk ( Gamma_a_bk< Array > const model)
inline

copy constructor

Parameters
modelThe model to copy

Definition at line 90 of file STK_Gamma_a_bk.h.

90: Base(model) {}

◆ ~Gamma_a_bk()

template<class Array >
STK::Gamma_a_bk< Array >::~Gamma_a_bk ( )
inline

destructor

Definition at line 92 of file STK_Gamma_a_bk.h.

92{}

Member Function Documentation

◆ computeNbFreeParameters()

template<class Array >
int STK::Gamma_a_bk< Array >::computeNbFreeParameters ( ) const
inline
Returns
the number of free parameters of the model

Definition at line 102 of file STK_Gamma_a_bk.h.

102{ return this->nbCluster()+1;}

References STK::IMixtureDensity< Derived >::nbCluster().

◆ randomInit()

template<class Array >
void STK::Gamma_a_bk< Array >::randomInit ( CArrayXX const *const p_tik,
CPointX const *const p_tk 
)

Initialize randomly the parameters of the Gamma mixture.

The shape will be selected randomly using an exponential of parameter mean^2/variance and the scale will be selected randomly using an exponential of parameter variance/mean.

Definition at line 106 of file STK_Gamma_a_bk.h.

107{
108 // compute moments
109 this->moments(p_tik);
110 Real value = 0.0;
111 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
112 {
113 Real mean = meank(k), variance = variancek(k);
114 // generate scales
115 param_.scale_[k] = Law::Exponential::rand(variance/mean);
116 value += p_tk->elt(k) * (mean*mean/variance);
117 }
118 param_.shape_ = STK::Law::Exponential::rand(value/this->nbSample());
119#ifdef STK_MIXTURE_VERY_VERBOSE
120 stk_cout << _T(" Gamma_a_bk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) done\n");
121#endif
122}
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
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.
Parameters param_
parameters of the derived mixture model.
bool moments(CArrayXX const *p_tik)
compute the weighted moments of a gamma mixture.
virtual Real rand() const
Generate a pseudo Exponential random variate.
double Real
STK fundamental type of Real values.
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.

References _T, STK::mean(), STK::Law::Exponential::rand(), and stk_cout.

◆ run()

template<class Array >
bool STK::Gamma_a_bk< Array >::run ( CArrayXX const *const p_tik,
CPointX const *const p_tk 
)

Compute the run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) .

Definition at line 127 of file STK_Gamma_a_bk.h.

128{
129 bool flag = true;
130 if (!moments(p_tik)) { flag = false;}
131 // estimate a
132 Real y =0.0, x0 = 0.0, x1 = param_.shape_;
133 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
134 {
135 Real mean = meank(k);
136 y += p_tk->elt(k) * (param_.meanLog_[k] - std::log(mean)).sum();
137 x0 += p_tk->elt(k) * (mean*mean/variancek(k));
138 }
139 y /= (this->nbSample()*p_data()->sizeCols());
140 x0 /= this->nbSample();
141 // moment estimate and oldest value
142 if ((x0 <=0.) || (isNA(x0)))
143 { x0 = 1; flag = false;}
144
145 // get shape
146 hidden::invPsiMLog f(y);
147 Real a = 1.;
148 try
149 {
150 a = Algo::findZero(f, x0, x1, 1e-08);
151 }
152 catch (...)
153 {
154#ifdef STK_MIXTURE_DEBUG
155 stk_cout << "ML estimation failed in Gamma_a_bjk::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) \n";
156 stk_cout << "x0 =" << x0 << _T("\n";);
157 stk_cout << "f(x0) =" << f(x0) << _T("\n";);
158 stk_cout << "x1 =" << x1 << _T("\n";);
159 stk_cout << "f(x1) =" << f(x1) << _T("\n";);
160#endif
161 a = x0; // use moment estimate
162 flag = false;
163 }
164// Real a = Algo::findZero(f, x0, x1, 1e-08);
166 {
167#ifdef STK_MIXTURE_DEBUG
168 stk_cout << "ML estimation failed in Gamma_a_bjk::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) \n";
169 stk_cout << "x0 =" << x0 << _T("\n";);
170 stk_cout << "f(x0) =" << f(x0) << _T("\n";);
171 stk_cout << "x1 =" << x1 << _T("\n";);
172 stk_cout << "f(x1) =" << f(x1) << _T("\n";);
173#endif
174 a = 1.; // use default value
175 flag = false;
176 }
177 // set values
178 param_.shape_ = a;
179 // estimate b
180 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
181 { param_.scale_[k] = meank(k)/a;}
182 return flag;
183}
Real findZero(IFunction< Function > const &f, Real const &x0, Real const &x1, Real tol)
find the zero of a function.
bool isNA(Type const &x)
utility method allowing to know if a value is a NA (Not Available) value
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
static bool isFinite(Type const &x)

References _T, STK::Algo::findZero(), STK::isNA(), STK::mean(), stk_cout, and STK::sum().


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