/src/quantlib/ql/math/distributions/gammadistribution.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | Real CumulativeGammaDistribution::operator()(Real x) const { |
25 | 0 | if (x <= 0.0) return 0.0; |
26 | | |
27 | 0 | Real gln = GammaFunction().logValue(a_); |
28 | |
|
29 | 0 | if (x<(a_+1.0)) { |
30 | 0 | Real ap = a_; |
31 | 0 | Real del = 1.0/a_; |
32 | 0 | Real sum = del; |
33 | 0 | for (Size n=1; n<=100; n++) { |
34 | 0 | ap += 1.0; |
35 | 0 | del *= x/ap; |
36 | 0 | sum += del; |
37 | 0 | if (std::fabs(del) < std::fabs(sum)*3.0e-7) |
38 | 0 | return sum*std::exp(-x + a_*std::log(x) - gln); |
39 | 0 | } |
40 | 0 | } else { |
41 | 0 | Real b = x + 1.0 - a_; |
42 | 0 | Real c = QL_MAX_REAL; |
43 | 0 | Real d = 1.0/b; |
44 | 0 | Real h = d; |
45 | 0 | for (Size n=1; n<=100; n++) { |
46 | 0 | Real an = -1.0*n*(n-a_); |
47 | 0 | b += 2.0; |
48 | 0 | d = an*d + b; |
49 | 0 | if (std::fabs(d) < QL_EPSILON) d = QL_EPSILON; |
50 | 0 | c = b + an/c; |
51 | 0 | if (std::fabs(c) < QL_EPSILON) c = QL_EPSILON; |
52 | 0 | d = 1.0/d; |
53 | 0 | Real del = d*c; |
54 | 0 | h *= del; |
55 | 0 | if (std::fabs(del - 1.0)<QL_EPSILON) |
56 | 0 | return 1.0-h*std::exp(-x + a_*std::log(x) - gln); |
57 | 0 | } |
58 | 0 | } |
59 | 0 | QL_FAIL("too few iterations"); |
60 | 0 | } |
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 | 0 | Real GammaFunction::logValue(Real x) const { |
70 | 0 | QL_REQUIRE(x>0.0, "positive argument required"); |
71 | 0 | Real temp = x + 5.5; |
72 | 0 | temp -= (x + 0.5)*std::log(temp); |
73 | 0 | Real ser=1.000000000190015; |
74 | 0 | ser += c1_/(x + 1.0); |
75 | 0 | ser += c2_/(x + 2.0); |
76 | 0 | ser += c3_/(x + 3.0); |
77 | 0 | ser += c4_/(x + 4.0); |
78 | 0 | ser += c5_/(x + 5.0); |
79 | 0 | ser += c6_/(x + 6.0); |
80 | |
|
81 | 0 | return -temp+std::log(2.5066282746310005*ser/x); |
82 | 0 | } |
83 | | |
84 | 0 | Real GammaFunction::value(Real x) const { |
85 | 0 | if (x >= 1.0) { |
86 | 0 | return std::exp(logValue(x)); |
87 | 0 | } |
88 | 0 | else { |
89 | 0 | if (x > -20.0) { |
90 | | // \Gamma(x) = \frac{\Gamma(x+1)}{x} |
91 | 0 | return value(x+1.0)/x; |
92 | 0 | } |
93 | 0 | else { |
94 | | // \Gamma(-x) = -\frac{\pi}{\Gamma(x)\sin(\pi x) x} |
95 | 0 | return -M_PI/(value(-x)*x*std::sin(M_PI*x)); |
96 | 0 | } |
97 | 0 | } |
98 | 0 | } |
99 | | } |