QuantLib
: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
ql
math
distributions
gammadistribution.cpp
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
Copyright (C) 2002, 2003 Sadruddin Rejeb
5
6
This file is part of QuantLib, a free-software/open-source library
7
for financial quantitative analysts and developers - http://quantlib.org/
8
9
QuantLib is free software: you can redistribute it and/or modify it
10
under the terms of the QuantLib license. You should have received a
11
copy of the license along with this program; if not, please email
12
<quantlib-dev@lists.sf.net>. The license is also available online at
13
<http://quantlib.org/license.shtml>.
14
15
This program is distributed in the hope that it will be useful, but WITHOUT
16
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
FOR A PARTICULAR PURPOSE. See the license for more details.
18
*/
19
20
#include <ql/math/distributions/gammadistribution.hpp>
21
22
namespace
QuantLib
{
23
24
Real
CumulativeGammaDistribution::operator()
(
Real
x)
const
{
25
if
(x <= 0.0)
return
0.0;
26
27
Real
gln =
GammaFunction
().
logValue
(
a_
);
28
29
if
(x<(
a_
+1.0)) {
30
Real
ap =
a_
;
31
Real
del = 1.0/
a_
;
32
Real
sum = del;
33
for
(
Size
n=1; n<=100; n++) {
34
ap += 1.0;
35
del *= x/ap;
36
sum += del;
37
if
(std::fabs(del) < std::fabs(sum)*3.0e-7)
38
return
sum*std::exp(-x +
a_
*std::log(x) - gln);
39
}
40
}
else
{
41
Real
b = x + 1.0 -
a_
;
42
Real
c =
QL_MAX_REAL
;
43
Real
d = 1.0/b;
44
Real
h = d;
45
for
(
Size
n=1; n<=100; n++) {
46
Real
an = -1.0*n*(n-
a_
);
47
b += 2.0;
48
d = an*d + b;
49
if
(std::fabs(d) <
QL_EPSILON
) d =
QL_EPSILON
;
50
c = b + an/c;
51
if
(std::fabs(c) <
QL_EPSILON
) c =
QL_EPSILON
;
52
d = 1.0/d;
53
Real
del = d*c;
54
h *= del;
55
if
(std::fabs(del - 1.0)<
QL_EPSILON
)
56
return
1.0-h*std::exp(-x +
a_
*std::log(x) - gln);
57
}
58
}
59
QL_FAIL(
"too few iterations"
);
60
}
61
62
const
Real
GammaFunction::c1_
= 76.18009172947146;
63
const
Real
GammaFunction::c2_
= -86.50532032941677;
64
const
Real
GammaFunction::c3_
= 24.01409824083091;
65
const
Real
GammaFunction::c4_
= -1.231739572450155;
66
const
Real
GammaFunction::c5_
= 0.1208650973866179e-2;
67
const
Real
GammaFunction::c6_
= -0.5395239384953e-5;
68
69
Real
GammaFunction::logValue
(
Real
x)
const
{
70
QL_REQUIRE(x>0.0,
"positive argument required"
);
71
Real
temp = x + 5.5;
72
temp -= (x + 0.5)*std::log(temp);
73
Real
ser=1.000000000190015;
74
ser +=
c1_
/(x + 1.0);
75
ser +=
c2_
/(x + 2.0);
76
ser +=
c3_
/(x + 3.0);
77
ser +=
c4_
/(x + 4.0);
78
ser +=
c5_
/(x + 5.0);
79
ser +=
c6_
/(x + 6.0);
80
81
return
-temp+std::log(2.5066282746310005*ser/x);
82
}
83
84
Real
GammaFunction::value
(
Real
x)
const
{
85
if
(x >= 1.0) {
86
return
std::exp(
logValue
(x));
87
}
88
else
{
89
if
(x > -20.0) {
90
// \Gamma(x) = \frac{\Gamma(x+1)}{x}
91
return
value
(x+1.0)/x;
92
}
93
else
{
94
// \Gamma(-x) = -\frac{\pi}{\Gamma(x)\sin(\pi x) x}
95
return
-M_PI/(
value
(-x)*x*std::sin(M_PI*x));
96
}
97
}
98
}
99
}
QuantLib::CumulativeGammaDistribution::a_
Real a_
Definition:
gammadistribution.hpp:52
QuantLib::CumulativeGammaDistribution::operator()
Real operator()(Real x) const
Definition:
gammadistribution.cpp:24
QuantLib::GammaFunction
Gamma function class.
Definition:
gammadistribution.hpp:68
QuantLib::GammaFunction::c1_
static const Real c1_
Definition:
gammadistribution.hpp:85
QuantLib::GammaFunction::c3_
static const Real c3_
Definition:
gammadistribution.hpp:85
QuantLib::GammaFunction::logValue
Real logValue(Real x) const
Definition:
gammadistribution.cpp:69
QuantLib::GammaFunction::c6_
static const Real c6_
Definition:
gammadistribution.hpp:85
QuantLib::GammaFunction::c5_
static const Real c5_
Definition:
gammadistribution.hpp:85
QuantLib::GammaFunction::c2_
static const Real c2_
Definition:
gammadistribution.hpp:85
QuantLib::GammaFunction::value
Real value(Real x) const
Definition:
gammadistribution.cpp:84
QuantLib::GammaFunction::c4_
static const Real c4_
Definition:
gammadistribution.hpp:85
QL_MAX_REAL
#define QL_MAX_REAL
Definition:
qldefines.hpp:176
QL_EPSILON
#define QL_EPSILON
Definition:
qldefines.hpp:178
QuantLib::Real
QL_REAL Real
real number
Definition:
types.hpp:50
QuantLib::Size
std::size_t Size
size of a container
Definition:
types.hpp:58
QuantLib
Definition:
any.hpp:35
Generated by
Doxygen
1.9.5