/src/quantlib/ql/math/incompletegamma.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2003 Ferdinando Ametrano |
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 | | <https://www.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 | | /* |
21 | | The implementation of the algorithm was inspired by |
22 | | "Numerical Recipes in C", 2nd edition, |
23 | | Press, Teukolsky, Vetterling, Flannery, chapter 6 |
24 | | */ |
25 | | |
26 | | #include <ql/math/incompletegamma.hpp> |
27 | | #include <ql/math/distributions/gammadistribution.hpp> |
28 | | |
29 | | namespace QuantLib { |
30 | | |
31 | | |
32 | | Real incompleteGammaFunction(Real a, Real x, Real accuracy, |
33 | 0 | Integer maxIteration) { |
34 | |
|
35 | 0 | QL_REQUIRE(a>0.0, "non-positive a is not allowed"); |
36 | | |
37 | 0 | QL_REQUIRE(x>=0.0, "negative x non allowed"); |
38 | | |
39 | 0 | if (x < (a+1.0)) { |
40 | | // Use the series representation |
41 | 0 | return incompleteGammaFunctionSeriesRepr(a, x, |
42 | 0 | accuracy, maxIteration); |
43 | 0 | } else { |
44 | | // Use the continued fraction representation |
45 | 0 | return 1.0-incompleteGammaFunctionContinuedFractionRepr(a, x, |
46 | 0 | accuracy, maxIteration); |
47 | 0 | } |
48 | |
|
49 | 0 | } |
50 | | |
51 | | |
52 | | Real incompleteGammaFunctionSeriesRepr(Real a, Real x, Real accuracy, |
53 | 0 | Integer maxIteration) { |
54 | |
|
55 | 0 | if (x==0.0) return 0.0; |
56 | | |
57 | 0 | Real gln = GammaFunction().logValue(a); |
58 | 0 | Real ap=a; |
59 | 0 | Real del=1.0/a; |
60 | 0 | Real sum=del; |
61 | 0 | for (Integer n=1; n<=maxIteration; n++) { |
62 | 0 | ++ap; |
63 | 0 | del *= x/ap; |
64 | 0 | sum += del; |
65 | 0 | if (std::fabs(del) < std::fabs(sum)*accuracy) { |
66 | 0 | return sum*std::exp(-x+a*std::log(x)-gln); |
67 | 0 | } |
68 | 0 | } |
69 | 0 | QL_FAIL("accuracy not reached"); |
70 | 0 | } |
71 | | |
72 | | Real incompleteGammaFunctionContinuedFractionRepr(Real a, Real x, |
73 | | Real accuracy, |
74 | 0 | Integer maxIteration) { |
75 | |
|
76 | 0 | Integer i; |
77 | 0 | Real an, b, c, d, del, h; |
78 | 0 | Real gln = GammaFunction().logValue(a); |
79 | 0 | b=x+1.0-a; |
80 | 0 | c=1.0/QL_EPSILON; |
81 | 0 | d=1.0/b; |
82 | 0 | h=d; |
83 | 0 | for (i=1; i<=maxIteration; i++) { |
84 | 0 | an = -i*(i-a); |
85 | 0 | b += 2.0; |
86 | 0 | d=an*d+b; |
87 | 0 | if (std::fabs(d) < QL_EPSILON) d=QL_EPSILON; |
88 | 0 | c=b+an/c; |
89 | 0 | if (std::fabs(c) < QL_EPSILON) c=QL_EPSILON; |
90 | 0 | d=1.0/d; |
91 | 0 | del=d*c; |
92 | 0 | h *= del; |
93 | 0 | if (std::fabs(del-1.0) < accuracy) { |
94 | 0 | return std::exp(-x+a*std::log(x)-gln)*h; |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | 0 | QL_FAIL("accuracy not reached"); |
99 | 0 | } |
100 | | |
101 | | |
102 | | |
103 | | } |