STK++ 0.9.13
STK_Law_Gamma.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::STatistiK::Law
27 * Purpose: Gamma probability distribution.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef STK_LAW_GAMMA_H
36#define STK_LAW_GAMMA_H
37
38#include "STK_Law_IUnivLaw.h"
39
40namespace STK
41{
42
43namespace Law
44{
62class Gamma: public IUnivLaw<Real>
63{
64 public:
70 Gamma( Real const& shape = 1., Real const& scale = 1.)
71 : Base(_T("Gamma")), a_(shape), b_(scale)
73 , c_(), d_()
74#endif
75 {
76 // check parameters
78 || a_ <= 0 || b_ <= 0
79 )
81#ifndef IS_RTKPP_LIB
83#endif
84 }
86 inline virtual ~Gamma() {}
88 inline Real const& shape() const { return a_;}
90 inline Real const& scale() const { return b_;}
92 inline void setShape( Real const& shape)
93 {
95 a_ = shape;
96#ifndef IS_RTKPP_LIB
98#endif
99 }
101 inline void setScale( Real const& scale)
102 {
104 b_ = scale;
105 }
111 virtual Real rand() const;
123 virtual Real pdf(Real const& x) const;
132 virtual Real lpdf(Real const& x) const;
136 virtual Real cdf(Real const& t) const;
140 virtual Real icdf(Real const& p) const;
141
145 static Real rand( Real const& shape, Real const& scale);
150 static Real pdf(Real const& x, Real const& shape, Real const& scale);
155 static Real lpdf(Real const& x, Real const& shape, Real const& scale);
160 static Real cdf(Real const& t, Real const& shape, Real const& scale);
165 static Real icdf(Real const& p, Real const& shape, Real const& scale);
166
167#ifdef IS_RTKPP_LIB
169 virtual Real lcdf( Real const& t) const;
171 virtual Real cdfc( Real const& t) const;
173 virtual Real lcdfc( Real const& t) const;
174#endif
175
176 protected:
181
182#ifndef IS_RTKPP_LIB
183 private:
185 mutable Real c_, d_;
187 void computeCtes() const;
188#endif
189};
190
191#ifdef IS_RTKPP_LIB
192
193inline Real Gamma::rand() const
194{
195#ifdef _OPENMP
196//#pragma omp critical
197#endif
198GetRNGstate(); Real s = Rf_rgamma(a_, b_); PutRNGstate(); return s;
199}
200
201inline Real Gamma::pdf( Real const& x) const { return Rf_dgamma(x, a_, b_, false);}
202inline Real Gamma::lpdf( Real const& x) const { return Rf_dgamma(x, a_, b_, true);}
203inline Real Gamma::cdf( Real const& t) const { return Rf_pgamma(t, a_, b_, true, false);}
204inline Real Gamma::lcdf( Real const& t) const { return Rf_pgamma(t, a_, b_, true, true);}
205inline Real Gamma::cdfc( Real const& t) const { return Rf_pgamma(t, a_, b_, false, false);}
206inline Real Gamma::lcdfc( Real const& t) const { return Rf_pgamma(t, a_, b_, false, true);}
207inline Real Gamma::icdf( Real const& p) const { return Rf_qgamma(p, a_, b_, true, false);}
208
209inline Real Gamma::rand( Real const& a, Real const& b)
210{
211#ifdef _OPENMP
212//#pragma omp critical
213#endif
214GetRNGstate(); Real s = Rf_rgamma(a, b); PutRNGstate(); return s;
215}
216
217inline Real Gamma::pdf( Real const& x, Real const& a, Real const& b)
218{ return Rf_dgamma(x, a, b, false);}
219inline Real Gamma::lpdf( Real const& x, Real const& a, Real const& b)
220{ return Rf_dgamma(x, a, b, true);}
221inline Real Gamma::cdf(Real const& t, Real const& a, Real const& b)
222{ return Rf_pgamma(t, a, b, true, false);}
223inline Real Gamma::icdf(Real const& p, Real const& a, Real const& b)
224{ return Rf_qgamma(p, a, b, true, false);}
225
226#endif
227
228} // namespace Law
229
230} // namespace STK
231
232#endif /*STK_LAWGAMMA_H*/
In this file we define the interface base class IUnivLaw for all probabilities laws.
#define STKDOMAIN_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:165
#define STKDOMAIN_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:147
#define _T(x)
Let x unmodified.
Gamma distribution law.
Real a_
The shape parameter.
Real b_
The scale parameter.
Real const & shape() const
virtual Real pdf(Real const &x) const
compute
virtual Real lpdf(Real const &x) const
Compute.
Real const & scale() const
IUnivLaw< Real > Base
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...
void setScale(Real const &scale)
void setShape(Real const &shape)
Gamma(Real const &shape=1., Real const &scale=1.)
Default constructor.
Real c_
First and second constants for rand.
virtual Real icdf(Real const &p) const
virtual Real cdf(Real const &t) const
void computeCtes() const
compute c_ and d_
virtual ~Gamma()
destructor
Interface base class for all the univariate distributions.
virtual Real cdfc(Real const &t) const
calculate the complement of cumulative distribution function, called in statistics the survival funct...
virtual Real lcdfc(Real const &t) const
calculate the log-complement of cumulative distribution function Give the log-probability that a rand...
virtual Real lcdf(Real const &t) const
compute the lower tail log-cumulative distribution function Give the log-probability that a random va...
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
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.