QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
gammadistribution.cpp
Go to the documentation of this file.
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
21
22namespace QuantLib {
23
25 if (x <= 0.0) return 0.0;
26
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
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
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}
Gamma function class.
Real logValue(Real x) const
Real value(Real x) const
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Date d
ext::function< Real(Real)> b
Gamma distribution.
#define QL_MAX_REAL
Definition: qldefines.hpp:176
#define QL_EPSILON
Definition: qldefines.hpp:178
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
#define M_PI
Definition: any.hpp:35