STK++ 0.9.13
STK_GammaBase.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._DOT_I..._AT_stkpp.org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Clustering
27 * created on: Dec 4, 2013
28 * Authors: Serge Iovleff
29 **/
30
35#ifndef STK_GAMMABASE_H
36#define STK_GAMMABASE_H
37
38#include "../STK_IMixtureDensity.h"
39
42
46#include "../GammaModels/STK_GammaParameters.h"
47
48namespace STK
49{
50
51namespace hidden
52{
55class invPsiMLog: public IFunction<invPsiMLog >
56{
57 public:
58 inline invPsiMLog( Real const& y): y_(y) {}
62 inline Real fImpl(Real const& a) const
63 { return (y_ + std::log(a) - Funct::psi_raw(a));}
65 inline Real xminImpl() const { return 0;}
66
67 private:
69};
70
74class invPsi: public IFunction<invPsi >
75{
76 public:
78 inline invPsi( Real const& y): y_(y) {}
82 inline Real fImpl(Real const& x) const { return (y_ - Funct::psi_raw(x));}
84 inline Real xminImpl() const { return 0;}
85 private:
87};
88
89} // namespace hidden
90
94template<class Derived>
95class GammaBase: public IMixtureDensity<Derived >
96{
97 public:
99 using Base::param_;
100 using Base::p_data;
101
102 protected:
113
114 public:
116 inline Real shape(int k, int j) const { return param_.shape(k,j);}
118 inline Real scale(int k, int j) const { return param_.scale(k,j);}
119
121 void initializeModelImpl() { param_.resize(p_data()->cols());}
122
126 Real lnComponentProbability(int i, int k) const;
131 template<class Weights>
132 Real impute(int i, int j, Weights const& pk) const;
137 inline Real rand(int i, int j, int k) const
138 { return Law::Gamma::rand(shape(k,j), scale(k,j));}
143 template<class Array>
149 void writeParameters(CArrayXX const* p_tik, ostream& os) const;
150
151 protected:
153 Real qValue(CArrayXX const* p_tik, CPointX const* p_tk) const;
155 bool moments(CArrayXX const* p_tik);
157 inline Real meanjk( int j, int k) { return param_.mean_[k][j];}
159 inline Real variancejk( int j, int k) { return param_.variance_[k][j];}
161 inline Real meank( int k) { return param_.mean_[k].mean();}
163 inline Real variancek( int k) { return param_.variance_[k].mean();}
164};
165
169template<class Derived>
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}
180/* @return an imputation value for the jth variable of the ith sample
181 * @param i,j indexes of the data to impute
182 * @param pk the probabilities of each class for the ith individual
183 **/
184template<class Derived>
185template<class Weights>
186Real GammaBase<Derived>::impute(int i, int j, Weights const& pk) const
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}
193
194/* compute safely the weighted moments of a gamma law. */
195template<class Derived>
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}
219
220/* compute the intermediate value needed by Newton algorithm*/
221template<class Derived>
222Real GammaBase<Derived>::qValue(CArrayXX const* p_tik, CPointX const* p_tk) const
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}
238
239template<class Derived>
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}
256
257template<class Derived>
258template<class Array>
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}
272
273
274
275} // namespace STK
276
277#endif /* STK_GammaBASE_H */
In this file we declare functions around the gamma function.
In this file we declare raw the functions.
In this file we define the Gamma probability distribution.
This file contain the functors computings statistics.
#define _T(x)
Let x unmodified.
Base class for the gamma models.
Real meank(int k)
get the mean of the weighted means of the kth cluster.
Real lnComponentProbability(int i, int k) const
Array const *const & p_data() const
Real variancek(int k)
get the mean of the weighted variances of the kth cluster.
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 rand(int i, int j, int k) const
void writeParameters(CArrayXX const *p_tik, ostream &os) const
This function can be used to write summary of parameters to the output stream.
Real impute(int i, int j, Weights const &pk) const
void initializeModelImpl()
Initialize the parameters of the model.
GammaBase(int nbCluster)
default constructor
Real shape(int k, int j) const
Real scale(int k, int j) const
~GammaBase()
destructor
IMixtureDensity< Derived > Base
Real variancejk(int j, int k)
get the weighted variance of the jth variable of the kth cluster.
Real qValue(CArrayXX const *p_tik, CPointX const *p_tk) const
compute the Q(theta) value.
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*nbC...
GammaBase(GammaBase const &model)
copy constructor
hidden::CSlice< Derived, sizeRows_, 1 >::Result col(int j) const
implement the col operator using a reference on the column of the allocator
Interface base class for functions.
Base class for all Mixture densities.
Array const *const & p_data() const
Parameters param_
parameters of the derived mixture model.
hidden::MixtureTraits< Derived >::Array Array
virtual Real lpdf(Real const &x) const
Compute.
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...
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Functor computing the derivative of the lnLikelihood of a gamma_ajk_bjk model.
invPsiMLog(Real const &y)
Real fImpl(Real const &a) const
Functor computing the difference between the psi function and a fixed value.
invPsi(Real const &y)
initialize y_
Real fImpl(Real const &x) const
Real xminImpl() const
Real psi_raw(Real x)
Compute the psi function.
Real lgamma(Real const &)
This function computes the 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
const int baseIdx
base index of the containers created in STK++.
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...
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
The namespace STK is the main domain space of the Statistical ToolKit project.