STK++ 0.9.13

Gamma distribution law. More...

#include <STK_Law_Gamma.h>

Inheritance diagram for STK::Law::Gamma:
Inheritance graph

Public Types

typedef IUnivLaw< RealBase
 

Public Member Functions

 Gamma (Real const &shape=1., Real const &scale=1.)
 Default constructor.
 
virtual ~Gamma ()
 destructor
 
Real constshape () const
 
Real constscale () const
 
void setShape (Real const &shape)
 
void setScale (Real const &scale)
 
virtual Real rand () const
 generate a gamma random variate using the G.S algorithm of Ahrens and Dieter (1974) for 0<a_<1 and Marsaglia fast Gamma generator with enhanced squeeze step for a>1.
 
virtual Real pdf (Real const &x) const
 compute
 
virtual Real lpdf (Real const &x) const
 Compute.
 
virtual Real cdf (Real const &t) const
 
virtual Real icdf (Real const &p) const
 
- Public Member Functions inherited from STK::Law::IUnivLaw< Real >
virtual ~IUnivLaw ()
 Virtual destructor.
 
virtual Real lcdf (Real const &t) const
 compute the lower tail log-cumulative distribution function Give the log-probability that a random variate is less or equal to t.
 
virtual Real cdfc (Real const &t) const
 calculate the complement of cumulative distribution function, called in statistics the survival function.
 
virtual Real lcdfc (Real const &t) const
 calculate the log-complement of cumulative distribution function Give the log-probability that a random variate is greater than t.
 
- Public Member Functions inherited from STK::Law::ILawBase
String constname () const
 

Static Public Member Functions

static Real rand (Real const &shape, Real const &scale)
 
static Real pdf (Real const &x, Real const &shape, Real const &scale)
 
static Real lpdf (Real const &x, Real const &shape, Real const &scale)
 
static Real cdf (Real const &t, Real const &shape, Real const &scale)
 
static Real icdf (Real const &p, Real const &shape, Real const &scale)
 

Protected Attributes

Real a_
 The shape parameter.
 
Real b_
 The scale parameter.
 
- Protected Attributes inherited from STK::Law::ILawBase
String name_
 Name of the Law.
 

Private Member Functions

void computeCtes () const
 compute c_ and d_
 

Private Attributes

Real c_
 First and second constants for rand.
 
Real d_
 

Additional Inherited Members

- Protected Member Functions inherited from STK::Law::IUnivLaw< Real >
 IUnivLaw (String const &name)
 Constructor.
 
 IUnivLaw (IUnivLaw const &law)
 copy Constructor.
 
- Protected Member Functions inherited from STK::Law::ILawBase
 ILawBase (String const &name)
 Constructor.
 
 ~ILawBase ()
 destructor.
 

Detailed Description

Gamma distribution law.

In probability theory and statistics, the gamma distribution is a two-parameter family of continuous probability distributions. The common exponential distribution and chi-squared distribution are special cases of the gamma distribution.

The probability pdf function of the gamma distribution can be expressed in terms of the Funct::gamma function:

\[
 f(x;a,b) = \left(\frac{x}{b}\right)^{a-1}
                 \frac{e^{-x/b}}{b \, \Gamma(a)}
 \ \mathrm{for}\ x>0,\ a>0,\ b>0
\]

where a is the shape parameter and b is the scale parameter.

Definition at line 62 of file STK_Law_Gamma.h.

Member Typedef Documentation

◆ Base

Constructor & Destructor Documentation

◆ Gamma()

STK::Law::Gamma::Gamma ( Real const shape = 1.,
Real const scale = 1. 
)
inline

Default constructor.

Parameters
shapeshape (position) parameter
scalescale (dispersion) parameter

Definition at line 70 of file STK_Law_Gamma.h.

71 : Base(_T("Gamma")), a_(shape), b_(scale)
72#ifndef IS_RTKPP_LIB
73 , c_(), d_()
74#endif
75 {
76 // check parameters
78 || a_ <= 0 || b_ <= 0
79 )
80 { STKDOMAIN_ERROR_2ARG(Gamma::Gamma,a_, b_,arguments not valid);}
81#ifndef IS_RTKPP_LIB
83#endif
84 }
#define STKDOMAIN_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:147
#define _T(x)
Let x unmodified.
Real a_
The shape parameter.
Real b_
The scale parameter.
Real const & shape() const
Real const & scale() const
IUnivLaw< Real > Base
Gamma(Real const &shape=1., Real const &scale=1.)
Default constructor.
Real c_
First and second constants for rand.
void computeCtes() const
compute c_ and d_
static bool isFinite(Type const &x)

References a_, b_, computeCtes(), Gamma(), and STKDOMAIN_ERROR_2ARG.

Referenced by Gamma().

◆ ~Gamma()

virtual STK::Law::Gamma::~Gamma ( )
inlinevirtual

destructor

Definition at line 86 of file STK_Law_Gamma.h.

86{}

Member Function Documentation

◆ cdf() [1/2]

Real STK::Law::Gamma::cdf ( Real const t) const
virtual
Returns
the cumulative distribution function
Parameters
ta positive real value

Implements STK::Law::IUnivLaw< Real >.

Definition at line 149 of file STK_Law_Gamma.cpp.

150{
151 // check NA value
152 if (isNA(t)) return t;
153 // trivial cases
154 if (t <= 0.) return 0.;
155 if (Arithmetic<Real>::isInfinite(t)) return 1.;
156 // general case
157 return Funct::gammaRatioP(a_, t/b_);
158}
Real gammaRatioP(Real const &a, Real const &x)
Compute the incomplete gamma function ratio P(a,x).
bool isNA(Type const &x)
utility method allowing to know if a value is a NA (Not Available) value
static bool isInfinite(Type const &x)

References a_, b_, STK::Funct::gammaRatioP(), and STK::isNA().

◆ cdf() [2/2]

Real STK::Law::Gamma::cdf ( Real const t,
Real const shape,
Real const scale 
)
static
Returns
the cumulative distribution function
Parameters
ta positive real value
shape,scaleshape and scale parameters

Definition at line 270 of file STK_Law_Gamma.cpp.

271{
272 // check NA value
273 if (isNA(t)) return t;
274 // trivial cases
275 if (t <= 0.) return 0.;
276 if (Arithmetic<Real>::isInfinite(t)) return 1.;
277 // general case
278 return Funct::gammaRatioP(a, t/b);
279}

References STK::Funct::gammaRatioP(), and STK::isNA().

◆ computeCtes()

void STK::Law::Gamma::computeCtes ( ) const
private

compute c_ and d_

Definition at line 290 of file STK_Law_Gamma.cpp.

291{
292 if (a_ < 1.)
293 {
294 c_ = 1. + a_/Const::_E_;
295 d_ = c_ * generator.randUnif();
296 }
297 else
298 {
299 d_ = a_-1./3.;
300 c_ = 1./std::sqrt(9.*d_);
301 }
302}

References a_, c_, and d_.

Referenced by Gamma(), and setShape().

◆ icdf() [1/2]

Real STK::Law::Gamma::icdf ( Real const p) const
virtual
Returns
the inverse cumulative distribution function
Parameters
pa probability number

Implements STK::Law::IUnivLaw< Real >.

Definition at line 164 of file STK_Law_Gamma.cpp.

165{
166 STKRUNTIME_ERROR_1ARG(Gamma::icdf,p,not implemented);
167 return 0.0;
168}
#define STKRUNTIME_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:129
virtual Real icdf(Real const &p) const

References icdf(), and STKRUNTIME_ERROR_1ARG.

Referenced by icdf().

◆ icdf() [2/2]

Real STK::Law::Gamma::icdf ( Real const p,
Real const shape,
Real const scale 
)
static
Returns
the inverse cumulative distribution function
Parameters
pa probability number
shape,scaleshape and scale parameters

Definition at line 285 of file STK_Law_Gamma.cpp.

286{
287 return 0;
288}

◆ lpdf() [1/2]

Real STK::Law::Gamma::lpdf ( Real const x) const
virtual

Compute.

\[
 \ln(f(x;\alpha,\beta)) = - x/\beta + (\alpha-1) \ln(x)
                          - \alpha \ln(\beta) + \ln(\Gamma(\alpha))
\]

Returns
the value of the log-pdf
Parameters
xa positive real value

Reimplemented from STK::Law::IUnivLaw< Real >.

Definition at line 127 of file STK_Law_Gamma.cpp.

128{
129 // check NA value
130 if (isNA(x)) return x;
131 // trivial cases
132 if (x < 0.) return -Arithmetic<Real>::infinity();
133 if (Arithmetic<Real>::isInfinite(x)) return -Arithmetic<Real>::infinity();
134 if (x == 0.) return (a_<1) ? Arithmetic<Real>::infinity()
135 : (a_>1.) ? -Arithmetic<Real>::infinity() : -std::log(b_);
136 // general case
137 return (a_ < 1) ? Funct::poisson_lpdf_raw(a_, x/b_) +std::log(a_/x)
138 : Funct::poisson_lpdf_raw(a_-1, x/b_) - std::log(b_);
139}
Real poisson_lpdf_raw(Real const &x, Real const &lambda)
Compute the log-poisson density.
double Real
STK fundamental type of Real values.

References a_, b_, STK::isNA(), and STK::Funct::poisson_lpdf_raw().

Referenced by STK::JointGammaModel< Array, WColVector >::computeLnLikelihood(), STK::ModelGamma_aj_bj< Data_, WColVector_ >::computeLnLikelihood(), and STK::GammaBase< Derived >::lnComponentProbability().

◆ lpdf() [2/2]

Real STK::Law::Gamma::lpdf ( Real const x,
Real const shape,
Real const scale 
)
static
Returns
the value of the log-pdf
Parameters
xa positive real value
shape,scaleshape and scale parameters

Definition at line 251 of file STK_Law_Gamma.cpp.

252{
253 // check NA value
254 if (isNA(x)) return x;
255 // trivial cases
256 if (x < 0.) return -Arithmetic<Real>::infinity();
257 if (Arithmetic<Real>::isInfinite(x)) return -Arithmetic<Real>::infinity();
258 if (x == 0.) return (a<1) ? Arithmetic<Real>::infinity()
259 : (a>1.) ? -Arithmetic<Real>::infinity() : -std::log(b);
260 // general case
261 return (a < 1) ? Funct::poisson_lpdf_raw(a, x/b) + std::log(a/x)
262 : Funct::poisson_lpdf_raw(a-1, x/b) - std::log(b);
263}

References STK::isNA(), and STK::Funct::poisson_lpdf_raw().

◆ pdf() [1/2]

Real STK::Law::Gamma::pdf ( Real const x) const
virtual

compute

\[
 f(x;\alpha,\beta) = \left(\frac{x}{\beta}\right)^{\alpha-1}
                 \frac{e^{-x/\beta}}{\beta \, \Gamma(\alpha)}
 \ \mathrm{for}\ x > 0
\]

where $ \alpha >, \mbox{ et } \beta > 0$ are the shape and scale parameters.

Returns
the value of the pdf
Parameters
xa positive real value

Implements STK::Law::IUnivLaw< Real >.

Definition at line 106 of file STK_Law_Gamma.cpp.

107{
108 // check NA value
109 if (isNA(x)) return x;
110 // trivial cases
111 if (x < 0.) return 0.;
112 if (Arithmetic<Real>::isInfinite(x)) return 0.;
113 if (x == 0.) return (a_<1) ? Arithmetic<Real>::infinity()
114 : (a_>1.) ? 0. : 1./b_;
115 // general case
116 return (a_ < 1) ? Funct::poisson_pdf_raw(a_, x/b_) * a_/x
118}
Real poisson_pdf_raw(Real const &x, Real const &lambda)
Compute the Poisson density.

References a_, b_, STK::isNA(), and STK::Funct::poisson_pdf_raw().

◆ pdf() [2/2]

Real STK::Law::Gamma::pdf ( Real const x,
Real const shape,
Real const scale 
)
static
Returns
the value of the pdf
Parameters
xa positive real value
shape,scaleshape and scale parameters

Definition at line 230 of file STK_Law_Gamma.cpp.

231{
232 // check NA value
233 if (isNA(x)) return x;
234 // trivial cases
235 if (x < 0.) return 0.;
236 if (Arithmetic<Real>::isInfinite(x)) return 0.;
237 if (x == 0.) return (a<1) ? Arithmetic<Real>::infinity()
238 : (a>1.) ? 0. : 1./b;
239 // general case
240 return (a < 1) ? Funct::poisson_pdf_raw(a, x/b) * a/x
241 : Funct::poisson_pdf_raw(a-1, x/b)/b;
242}

References STK::isNA(), and STK::Funct::poisson_pdf_raw().

◆ rand() [1/2]

Real STK::Law::Gamma::rand ( ) const
virtual

generate a gamma random variate using the G.S algorithm of Ahrens and Dieter (1974) for 0<a_<1 and Marsaglia fast Gamma generator with enhanced squeeze step for a>1.

Returns
a pseudo Gamma random variate.

Implements STK::Law::IUnivLaw< Real >.

Definition at line 52 of file STK_Law_Gamma.cpp.

53{
54 // GS algorithm for parameter a_ < 1.
55 if (a_ < 1.)
56 {
57 if(a_ == 0.) return 0.;
58 Real x;
59 do
60 {
61 Real p = c_ * generator.randUnif();
62 if (p >= 1.)
63 {
64 x = -std::log((c_ - p) / a_);
65 if (generator.randExp() >= (1. - a_) * std::log(x)) break;
66 }
67 else
68 {
69 x = std::exp(std::log(p) / a_);
70 if (generator.randExp() >= x) break;
71 }
72 }
73 while(1);
74 return(x*b_);
75 }
76 // Exponential law for parameter a_ = 1
77 if (a_ == 1.0) return b_*generator.randExp();
78 // Marsaglia fast Gamma generator with enhanced squeeze step for a>1
79 // initialize constants
80 Real v;
81 while(1)
82 {
83 Real x,u;
84 // generate gaussian rv in the range (-1/c, infty)
85 while( (v = 1. + c_* ( x = generator.randGauss() ) ) <= 0. );
86 // cubic normal variate
87 v = v*v*v;
88 // squeeze step
89 if ( (u = generator.randUnif()) < 1.-(x*x)*(x*x)/(108. * d_) ) break;
90 // rejection step
91 if ( std::log(u) < 0.5*x*x+d_*(1.-v+std::log(v)) ) break;
92 }
93 return b_*d_*v;
94}

References a_, b_, c_, and d_.

Referenced by STK::Law::Beta::rand(), STK::GammaBase< Derived >::rand(), STK::Law::Beta::rand(), and rand().

◆ rand() [2/2]

Real STK::Law::Gamma::rand ( Real const shape,
Real const scale 
)
static
Returns
a pseudo Gamma random variate with the specified parameters.
Parameters
shape,scaleshape and scale parameters

Definition at line 170 of file STK_Law_Gamma.cpp.

171{
172 // check parameters
174 || a <= 0 || b <= 0
175 )
176 STKDOMAIN_ERROR_2ARG(Gamma::rand,a, b,arguments not valid);
177 // GS algorithm for parameter a_ < 1.
178 if (a < 1.)
179 {
180 Real c = 1. + a/Const::_E_;
181 Real x;
182 if(a == 0.) return 0.;
183 do
184 {
185 Real p = c * generator.randUnif();
186 if (p >= 1.)
187 {
188 x = -std::log((c - p) / a);
189 if (generator.randExp() >= (1. - a) * std::log(x)) break;
190 }
191 else
192 {
193 x = std::exp(std::log(p) / a);
194 if (generator.randExp() >= x) break;
195 }
196 } while(1);
197 return b *x;
198 }
199 // special case
200 if (a == 1.0) return Exponential::rand(b);
201 // Marsaglia fast Gamma generator with enhanced squeeze step for alpha>1
202 // initialize constants
203 const Real d = a-1./3.;
204 const Real c = 1./sqrt(9.*d);
205 // rejection method : main loop
206 Real v;
207 while(1)
208 {
209 // auxiliary variables
210 Real x,u;
211 // generate gaussian rv in the range (-1/c, infty)
212 while( (v = 1. + c * ( x = generator.randGauss() ) ) <= 0. ) {}
213 // cubic normal variate
214 v = v*v*v;
215 // squeeze step
216 if ( (u = generator.randUnif()) < 1.-(x*x)*(x*x)/(108. * d) ) break;
217 // rejection step
218 if ( std::log(double(u)) < 0.5*x*x+d*(1.-v+log(v)) ) break;
219 }
220 return (b*d*v);
221}
virtual Real rand() const
Generate a pseudo Exponential random variate.
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...

References STK::Law::Exponential::rand(), rand(), and STKDOMAIN_ERROR_2ARG.

◆ scale()

Real const & STK::Law::Gamma::scale ( ) const
inline
Returns
scale

Definition at line 90 of file STK_Law_Gamma.h.

90{ return b_;}

References b_.

Referenced by setScale().

◆ setScale()

void STK::Law::Gamma::setScale ( Real const scale)
inline
Parameters
scalethe scale parameter

Definition at line 101 of file STK_Law_Gamma.h.

102 {
104 b_ = scale;
105 }
#define STKDOMAIN_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:165
void setScale(Real const &scale)

References b_, scale(), setScale(), and STKDOMAIN_ERROR_1ARG.

Referenced by setScale().

◆ setShape()

void STK::Law::Gamma::setShape ( Real const shape)
inline
Parameters
shapethe shape parameter

Definition at line 92 of file STK_Law_Gamma.h.

93 {
95 a_ = shape;
96#ifndef IS_RTKPP_LIB
98#endif
99 }
void setShape(Real const &shape)

References a_, computeCtes(), setShape(), shape(), and STKDOMAIN_ERROR_1ARG.

Referenced by setShape().

◆ shape()

Real const & STK::Law::Gamma::shape ( ) const
inline
Returns
shape

Definition at line 88 of file STK_Law_Gamma.h.

88{ return a_;}

References a_.

Referenced by setShape().

Member Data Documentation

◆ a_

Real STK::Law::Gamma::a_
protected

The shape parameter.

Definition at line 178 of file STK_Law_Gamma.h.

Referenced by cdf(), computeCtes(), Gamma(), lpdf(), pdf(), rand(), setShape(), and shape().

◆ b_

Real STK::Law::Gamma::b_
protected

The scale parameter.

Definition at line 180 of file STK_Law_Gamma.h.

Referenced by cdf(), Gamma(), lpdf(), pdf(), rand(), scale(), and setScale().

◆ c_

Real STK::Law::Gamma::c_
mutableprivate

First and second constants for rand.

Definition at line 185 of file STK_Law_Gamma.h.

Referenced by computeCtes(), and rand().

◆ d_

Real STK::Law::Gamma::d_
private

Definition at line 185 of file STK_Law_Gamma.h.

Referenced by computeCtes(), and rand().


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