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

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

#include <STK_Gamma_ak_bj.h>

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

Public Types

typedef GammaBase< Gamma_ak_bj< Array > > Base
 
- Public Types inherited from STK::GammaBase< Gamma_ak_bj< Array > >
typedef IMixtureDensity< Gamma_ak_bj< 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_ak_bj (int nbCluster)
 default constructor
 
 Gamma_ak_bj (Gamma_ak_bj const &model)
 copy constructor
 
 ~Gamma_ak_bj ()
 destructor
 
void randomInit (CArrayXX const *const &p_tik, CPointX const *const &p_tk)
 Initialize randomly the parameters of the Gaussian mixture.
 
bool run (CArrayXX const *const &p_tik, CPointX const *const &p_tk)
 Compute the weighted mean and the common variance.
 
int computeNbFreeParameters () const
 
- Public Member Functions inherited from STK::GammaBase< Gamma_ak_bj< 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_ak_bj< Array > >
Parameters param_
 parameters of the derived mixture model.
 
- Protected Member Functions inherited from STK::GammaBase< Gamma_ak_bj< 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_ak_bj< Array >

Gamma_ak_bj 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_{j}}\right)^{a_{k}-1}
                  \frac{e^{-x_i^j/b_{j}}} {b_{j} \, \Gamma(a_{k})},
     \quad x_i^j>0, \quad i=1,\ldots,n.
\]

Definition at line 73 of file STK_Gamma_ak_bj.h.

Member Typedef Documentation

◆ Base

Definition at line 76 of file STK_Gamma_ak_bj.h.

Constructor & Destructor Documentation

◆ Gamma_ak_bj() [1/2]

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

default constructor

Parameters
nbClusternumber of cluster in the model

Definition at line 85 of file STK_Gamma_ak_bj.h.

85: Base(nbCluster) {}
GammaBase< Gamma_ak_bj< Array > > Base

◆ Gamma_ak_bj() [2/2]

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

copy constructor

Parameters
modelThe model to copy

Definition at line 89 of file STK_Gamma_ak_bj.h.

89: Base(model) {}

◆ ~Gamma_ak_bj()

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

destructor

Definition at line 91 of file STK_Gamma_ak_bj.h.

91{}

Member Function Documentation

◆ computeNbFreeParameters()

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

Definition at line 100 of file STK_Gamma_ak_bj.h.

101 { return this->nbCluster()+ p_data()->sizeCols();}

References STK::IMixtureDensity< Derived >::nbCluster(), and STK::GammaBase< Gamma_ak_bj< Array > >::p_data().

◆ randomInit()

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

Initialize randomly the parameters of the Gaussian mixture.

The centers will be selected randomly among the data set and the standard-deviation will be set to 1.

Definition at line 109 of file STK_Gamma_ak_bj.h.

110{
111 // compute moments
112 this->moments(p_tik);
113 // simulates ak
114 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
115 {
116 Real value= 0.;
117 for (int j=p_data()->beginCols(); j < p_data()->endCols(); ++j)
118 {
119 Real mean = meanjk(j,k), variance = variancejk(j,k);
120 value += mean*mean/variance;
121 }
122 param_.shape_[k]= Law::Exponential::rand(value/(p_data()->sizeCols()));
123 }
124 // simulate bj
125 for (int j=p_data()->beginCols(); j < p_data()->endCols(); ++j)
126 {
127 Real value= 0.;
128 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
129 {
130 Real mean = meanjk(j,k), variance = variancejk(j,k);
131 value += p_tk->elt(k) * variance/mean;
132 }
133 param_.scale_[j] = Law::Exponential::rand(value/(this->nbSample()));
134 }
135#ifdef STK_MIXTURE_VERY_VERBOSE
136 stk_cout << _T(" Gamma_ak_bj<Array>::randomInit done\n");
137#endif
138}
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Parameters param_
parameters of the derived mixture model.
Real meanjk(int j, int k)
get the weighted mean of the jth variable of the kth cluster.
bool moments(CArrayXX const *p_tik)
compute the weighted moments of a gamma mixture.
Real variancejk(int j, int k)
get the weighted variance of the jth variable of the kth cluster.
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_ak_bj< Array >::run ( CArrayXX const *const p_tik,
CPointX const *const p_tk 
)

Compute the weighted mean and the common variance.

Definition at line 142 of file STK_Gamma_ak_bj.h.

143{
144 if (!this->moments(p_tik)) { return false;}
145 // start estimations of the ajk and bj
146 Real qvalue = this->qValue(p_tik, p_tk);
147 int iter;
148 for(iter=0; iter<MAXITER; ++iter)
149 {
150 // compute ak
151 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
152 {
153 // moment estimate and oldest value
154 Real x0 = (param_.mean_[k].square()/param_.variance_[k]).mean();
155 Real x1 = param_.shape_[k];
156 if ((x0 <=0.) || !Arithmetic<Real>::isFinite(x0)) return false;
157
158 // compute shape
159 hidden::invPsi f((param_.meanLog_[k] - param_.scale_.log()).mean());
160 Real a = Algo::findZero(f, x0, x1, TOL);
161
163 {
164 param_.shape_[k]= x0; // use moment estimate
165#ifdef STK_MIXTURE_DEBUG
166 stk_cout << _T("ML estimation failed in Gamma_ak_bj::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) \n");
167 stk_cout << "x0 =" << x0 << _T("\n";);
168 stk_cout << "f(x0) =" << f(x0) << _T("\n";);
169 stk_cout << "x1 =" << x1 << _T("\n";);
170 stk_cout << "f(x1) =" << f(x1) << _T("\n";);
171#endif
172 }
173 else { param_.shape_[k]= a;}
174 }
175 // update all the b^j
176 for (int j=p_data()->beginCols(); j<p_data()->endCols(); ++j)
177 {
178 Real num = 0., den = 0.;
179 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
180 {
181 num += param_.mean_[k][j] * p_tk->elt(k);
182 den += param_.shape_[k] * p_tk->elt(k);
183 }
184 // compute b_j
185 Real b = num/den;
186 // divergence
187 if (!Arithmetic<Real>::isFinite(b)) { return false;}
188 param_.scale_[j] = b;
189 }
190 // check convergence
191 Real value = this->qValue(p_tik, p_tk);
192#ifdef STK_MIXTURE_DEBUG
193 if (value < qvalue)
194 {
195 stk_cout << _T("In Gamma_ak_bj::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) : run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) diverge\n");
196 stk_cout << _T("New value =") << value << _T(", qvalue =") << qvalue << _T("\n");
197 }
198#endif
199 if ((value - qvalue) < TOL) break;
200 qvalue = value;
201 }
202#ifdef STK_MIXTURE_DEBUG
203 if (iter == MAXITER)
204 {
205 stk_cout << _T("In Gamma_ak_bj::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) : run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) did not converge\n");
206 stk_cout << _T("qvalue =") << qvalue << _T("\n");
207 }
208#endif
209 return true;
210}
#define TOL
#define MAXITER
Real qValue(CArrayXX const *p_tik, CPointX const *p_tk) const
compute the Q(theta) value.
Real findZero(IFunction< Function > const &f, Real const &x0, Real const &x1, Real tol)
find the zero of a function.
static bool isFinite(Type const &x)

References _T, STK::Algo::findZero(), STK::Arithmetic< Type >::isFinite(), MAXITER, STK::mean(), stk_cout, and TOL.


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