STK++ 0.9.13
STK_ModelGamma_aj_bj.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
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_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Model
27 * created on: 22 juil. 2011
28 * Purpose: define the class IUnivStatModel.
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 *
31 **/
32
37#ifndef STK_JOINTGAMMAMODEL_H
38#define STK_JOINTGAMMAMODEL_H
39
40#include <cmath>
41
42#include "STK_Model_Util.h"
43#include "STK_IMultiStatModel.h"
45
49
50namespace STK
51{
52// forward declaration
53template <class Array, class WColVector = CVectorX> class ModelGamma_aj_bj;
54struct ModelGamma_aj_bjParameters;
55
56namespace hidden
57{
62template<class Data_, class WColVector_>
74
75} // hidden
76
82{
87 : shape_(range, 1.), scale_(range, 1.)
88 , mean_(range, 1.), meanLog_(range, 0.), variance_(range, 1.)
89 {}
92 : shape_(param.shape_), scale_(param.scale_)
93 , mean_(param.mean_), meanLog_(param.meanLog_), variance_(param.variance_)
94 {}
98 inline CPointX const& shape() const {return shape_;}
100 inline CPointX const& scale() const {return scale_;}
101
106 inline void resize(Range const& range)
107 {
108 if (range != range_)
109 {
110 shape_.resize(range); shape_ = 1.;
111 scale_.resize(range); scale_ = 1.;
112 mean_.resize(range); mean_ = 1.;
113 meanLog_.resize(range); meanLog_ = 0.;
114 variance_.resize(range); variance_ = 1.;
115 range_ = range;
116 }
117 }
129};
130
131
141template <class Data_, class WColVector_>
142class ModelGamma_aj_bj: public IMultiStatModel< ModelGamma_aj_bj<Data_, WColVector_> >
143{
144 protected:
145 class dloglikelihood: public IFunction<dloglikelihood >
146 {
147 public:
149 : delta_(meanLog - Real(std::log(mean))) {}
153 inline Real fImpl(Real a) const
154 { return (delta_ + std::log(a) - Funct::psi_raw(a));}
156 inline Real xminImpl() const { return 0;}
157 private:
159 };
160
161 public:
172 using Base::p_data;
173 using Base::param;
174
178 ModelGamma_aj_bj(Data const& data): Base(data){}
185
187 inline CPointX const& shape() const {return param().shape_;}
189 inline CPointX const& scale() const {return param().scale_;}
191 inline CPointX const& mean() const {return param().mean_;}
193 inline CPointX const& meanLog() const {return param().meanLog_;}
195 inline CPointX const& variance() const {return param().variance_;}
196
198 int computeNbFreeParameters() const { return 2*p_data()->dataij().sizeCols();}
202 void computeParameters();
206 void writeParametersImpl(ostream& os) const;
207
208 private:
210 inline CPointX& shape() { return param().shape_;}
212 inline CPointX& scale() { return param().scale_;}
214 inline CPointX& mean() { return param().mean_;}
216 inline CPointX& meanLog() { return param().meanLog_;}
218 inline CPointX& variance() { return param().variance_;}
219};
220
221/* compute the log Likelihood of an observation. */
222template<class Data_, class WColVector_>
224{
225 Real sum =0.;
226 for (Integer j= rowData.begin(); j < rowData.end(); ++j)
227 { sum += Law::Gamma::lpdf(rowData[j], shape()[j], scale()[j]);}
228 return sum;
229}
230
231/* compute the parameters */
232template<class Data_, class WColVector_>
234{
235 for (int j=p_data()->dataij().beginCols(); j < p_data()->dataij().endCols(); ++j)
236 {
237 mean()[j] = p_data()->dataij().col(j).meanSafe();
238 meanLog()[j] = p_data()->dataij().col(j).safe(1.).log().mean();
239 variance()[j] = p_data()->dataij().col(j).safe().variance();
240 Real x0 = (mean()[j]*mean()[j]) / variance()[j];
241 Real x1 = 0.9*x0 + 0.05/(mean()[j] - meanLog()[j]);
242 dloglikelihood funct(mean()[j], meanLog()[j]);
243 Real a = Algo::findZero(funct, x0, x1, 1e-08);
244 // replace with moment estimate if needed
245 if (!Arithmetic<Real>::isFinite(a)) { a = mean()[j]*mean()[j]/variance()[j];}
246 shape()[j] = a;
247 scale()[j] = mean()[j]/a;
248 }
249}
250/* compute the weighted parameters */
251template<class Data_, class WColVector_>
253{
254 for (int j=p_data()->dataij().beginCols(); j < p_data()->dataij().endCols(); ++j)
255 {
256 mean()[j] = p_data()->dataij().col(j).safe().wmean(weights);
257 meanLog()[j] = p_data()->dataij().col(j).safe(1).log().wmean(weights);
258 variance()[j] = p_data()->dataij().col(j).safe().wvariance(weights);
259 Real x0 = (mean()[j]*mean()[j]) / variance()[j];
260 Real x1 = 0.9*x0 + 0.05/(mean()[j] - meanLog()[j]);
261 dloglikelihood funct(mean()[j], meanLog()[j]);
262 Real a = Algo::findZero(funct, x0, x1, 1e-08);
263 // replace with moment estimate if needed
264 if (!Arithmetic<Real>::isFinite(a)) { a = mean()[j]*mean()[j]/variance()[j];}
265 shape()[j] = a;
266 scale()[j] = mean()[j]/a;
267 }
268}
269
270/* Write the parameters on the output stream os */
271template<class Data_, class WColVector_>
273{
274 os << _T("shape = ") << shape();
275 os << _T("scale = ") << scale();
276}
277
278
279} // namespace STK
280
281#endif /* STK_JOINTGAMMAMODEL_H */
A Array2DPoint is a one dimensional horizontal container.
In this file we declare raw the functions.
In this file we define the class IMultiStatModel.
In this file we define the Gamma probability distribution.
In this file we define the constant and utilities methods used in the project Model.
#define _T(x)
Let x unmodified.
Derived & resize(Range const &I, Range const &J)
resize the Array.
Interface base class for functions.
Interface base class for all Multivariate Statistical Models.
Real computeLnLikelihood() const
compute the log Likelihood of the statistical model.
virtual Real lpdf(Real const &x) const
Compute.
dloglikelihood(Real const &mean, Real const &meanLog)
A joint Gamma model is a statistical model of the following form.
void computeParameters()
compute the parameters
ModelGamma_aj_bj()
default constructor.
DataBridge< Data_ > Data
Type of the container storing the data.
void writeParametersImpl(ostream &os) const
Write the parameters on the output stream os.
WColVector_ WColVector
Type of the array storing the weights of the data.
CPointX const & variance() const
vector of the variance of the observations
CPointX const & scale() const
ModelGamma_aj_bj(Data const &data)
Constructor with data set.
hidden::Traits< Data_ >::Row RowVector
CPointX const & meanLog() const
vector of the mean log of the observations
CPointX const & mean() const
ModelGamma_aj_bj(ModelGamma_aj_bj const &model)
Copy constructor.
ModelGamma_aj_bj(Data const *p_data)
Constructor with a ptr on the data set.
CPointX & meanLog()
vector of the mean log of the observations
CPointX & variance()
vector of the variance of the observations
int computeNbFreeParameters() const
compute the number of free parameters
CPointX const & shape() const
IMultiStatModel< ModelGamma_aj_bj< Data_, WColVector_ > > Base
Type of the data in the container.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
Real psi_raw(Real x)
Compute the psi function.
Real findZero(IFunction< Function > const &f, Real const &x0, Real const &x1, Real tol)
find the zero of a function.
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.
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.
Arithmetic properties of STK fundamental types.
Structure encapsulating the parameters of a Joint Gamma model.
CPointX shape_
vector of the shape
ModelGamma_aj_bjParameters(ModelGamma_aj_bjParameters const &param)
copy constructor.
ModelGamma_aj_bjParameters()
default constructor
CPointX scale_
vector of the scale
CPointX meanLog_
vector of the mean log of the observations
CPointX mean_
vector of the mean of the observations
void resize(Range const &range)
resize the parameters only if the range is modified, otherwise, stay with the current values.
ModelGamma_aj_bjParameters(Range const &range)
default constructor
CPointX variance_
vector of the variance of the observations
CPointX const & scale() const
vector of the mean log of the observations
ModelGamma_aj_bjParameters Parameters
Type of the parameters of the ModelGamma_aj_bj.
Traits< Data_ >::Type Type
Type of the data in the container.
WColVector_ WColVector
Type of the array storing the weights of the data.
DataBridge< Data_ > Data
Type of the container storing the data.
Policy trait class for (Stat) Model classes.