STK++ 0.9.13
STK_JointGammaModel.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2015 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_IMultiStatModel.h"
45
49
50namespace STK
51{
98
99
109template <class Array, class WColVector>
110class JointGammaModel: public IMultiStatModel<Array, WColVector, JointGammaParameters >
111{
112
113 public:
115 typedef typename Array::Type Type;
122 using Base::p_data;
123 using Base::p_param;
127 JointGammaModel(Array const& data): Base(data){}
135 JointGammaModel* clone() const { return new JointGammaModel(*this);}
136
138 inline Array2DPoint<Real> const& shape() const {return p_param()->shape_;}
140 inline Array2DPoint<Real> const& scale() const {return p_param()->scale_;}
142 inline Array2DPoint<Real> const& mean() const {return p_param()->mean_;}
144 inline Array2DPoint<Real> const& meanLog() const {return p_param()->meanLog_;}
146 inline Array2DPoint<Real> const& variance() const {return p_param()->variance_;}
147
149 virtual int computeNbFreeParameters() const
150 { return 2*p_data()->sizeCols();}
153 {
154 Real sum =0.;
155 for (Integer j= rowData.begin(); j < rowData.end(); ++j)
156 { sum += Law::Gamma::lpdf(rowData[j], shape()[j], scale()[j]);}
157 return sum;
158 }
159 protected:
160 class dloglikelihood: public IFunction<dloglikelihood >
161 {
162 public:
164 : delta_(meanLog - Real(std::log(mean))) {}
168 inline Real fImpl(Real a) const
169 { return (delta_ + std::log(a) - Funct::psi_raw(a));}
171 inline Real xminImpl() const { return 0;}
172 private:
174 };
176 virtual void computeParameters()
177 {
178 for (int j=p_data()->beginCols(); j < p_data()->endCols(); ++j)
179 {
180 mean()[j] = p_data()->col(j).safe().mean();
181 meanLog()[j] = p_data()->col(j).safe(1.).log().mean();
182 variance()[j] = p_data()->col(j).safe().variance();
183 Real x0 = (mean()[j]*mean()[j]) / variance()[j];
184 Real x1 = 0.9*x0 + 0.05/(mean()[j] - meanLog()[j]);
187 // replace with moment estimate if needed
188 if (!Arithmetic<Real>::isFinite(a)) { a = mean()[j]*mean()[j]/variance()[j];}
189 shape()[j] = a;
190 scale()[j] = mean()[j]/a;
191 }
192 }
195 {
196 for (int j=p_data()->beginCols(); j < p_data()->endCols(); ++j)
197 {
198 mean()[j] = p_data()->col(j).safe().wmean(weights);
199 meanLog()[j] = p_data()->col(j).safe(1).log().wmean(weights);
200 variance()[j] = p_data()->col(j).safe().wvariance(weights);
201 Real x0 = (mean()[j]*mean()[j]) / variance()[j];
202 Real x1 = 0.9*x0 + 0.05/(mean()[j] - meanLog()[j]);
205 // replace with moment estimate if needed
206 if (!Arithmetic<Real>::isFinite(a)) { a = mean()[j]*mean()[j]/variance()[j];}
207 shape()[j] = a;
208 scale()[j] = mean()[j]/a;
209 }
210 }
211
212 private:
214 inline Array2DPoint<Real>& shape() {return p_param()->shape_;}
216 inline Array2DPoint<Real>& scale() {return p_param()->scale_;}
218 inline Array2DPoint<Real>& mean() {return p_param()->mean_;}
220 inline Array2DPoint<Real>& meanLog() {return p_param()->meanLog_;}
222 inline Array2DPoint<Real>& variance() {return p_param()->variance_;}
223};
224
225} // namespace STK
226
227#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 interface IMultiParameters.
In this file we define the class IMultiStatModel.
In this file we define the Gamma probability distribution.
Derived & resize(Range const &I, Range const &J)
resize the array.
Interface base class for functions.
Interface base class for the parameters of a multivariate model.
Interface base class for all Multivariate Statistical Models.
hidden::StatModelTraits< Array >::WColVector WColVector
Type of the vector with the weights.
dloglikelihood(Real const &mean, Real const &meanLog)
A joint Gamma model is a statistical model of the following form.
virtual Real computeLnLikelihood(RowVector const &rowData) const
compute the log Likelihood of an observation.
IMultiStatModel< Array, WColVector, JointGammaParameters > Base
Base class.
hidden::Traits< Array >::Row RowVector
Type of the row vector of the container.
Array2DPoint< Real > const & meanLog() const
vector of the mean log of the observations
virtual void computeParameters()
compute the parameters
JointGammaModel()
default constructor.
Array2DPoint< Real > & shape()
hidden::Traits< Array >::Col ColVector
Type of the column vector of the container.
Array2DPoint< Real > const & variance() const
vector of the variance of the observations
Array2DPoint< Real > const & shape() const
JointGammaModel(Array const &data)
Constructor with data set.
virtual void computeParameters(WColVector const &weights)
compute the weighted parameters
Array2DPoint< Real > & variance()
vector of the variance of the observations
JointGammaModel(JointGammaModel const &model)
Copy constructor.
Array2DPoint< Real > const & scale() const
Array2DPoint< Real > & mean()
JointGammaModel(Array const *p_data)
Constructor with a ptr on the data set.
virtual int computeNbFreeParameters() const
compute the number of free parameters
Array::Type Type
Type of the data contained in the container.
Array2DPoint< Real > const & mean() const
virtual ~JointGammaModel()
destructor
Array2DPoint< Real > & scale()
JointGammaModel * clone() const
clone pattern.
Array2DPoint< Real > & meanLog()
vector of the mean log of the observations
virtual Real lpdf(Real const &x) const
Compute.
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.
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.
JointGammaParameters(JointGammaParameters const &param)
copy constructor.
Array2DPoint< Real > shape_
vector of the shape
JointGammaParameters()
default constructor
Array2DPoint< Real > meanLog_
vector of the mean log of the observations
void resizeImpl(Range const &range)
resize the set of parameter
Array2DPoint< Real > scale_
vector of the scale
Array2DPoint< Real > mean_
vector of the mean of the observations
Array2DPoint< Real > variance_
vector of the variance of the observations
JointGammaParameters(Range const &range)
default constructor