| 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 | <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 | /* |
| 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 | Integer maxIteration) { |
| 34 | |
| 35 | QL_REQUIRE(a>0.0, "non-positive a is not allowed" ); |
| 36 | |
| 37 | QL_REQUIRE(x>=0.0, "negative x non allowed" ); |
| 38 | |
| 39 | if (x < (a+1.0)) { |
| 40 | // Use the series representation |
| 41 | return incompleteGammaFunctionSeriesRepr(a, x, |
| 42 | accuracy, maxIteration); |
| 43 | } else { |
| 44 | // Use the continued fraction representation |
| 45 | return 1.0-incompleteGammaFunctionContinuedFractionRepr(a, x, |
| 46 | accuracy, maxIteration); |
| 47 | } |
| 48 | |
| 49 | } |
| 50 | |
| 51 | |
| 52 | Real incompleteGammaFunctionSeriesRepr(Real a, Real x, Real accuracy, |
| 53 | Integer maxIteration) { |
| 54 | |
| 55 | if (x==0.0) return 0.0; |
| 56 | |
| 57 | Real gln = GammaFunction().logValue(x: a); |
| 58 | Real ap=a; |
| 59 | Real del=1.0/a; |
| 60 | Real sum=del; |
| 61 | for (Integer n=1; n<=maxIteration; n++) { |
| 62 | ++ap; |
| 63 | del *= x/ap; |
| 64 | sum += del; |
| 65 | if (std::fabs(x: del) < std::fabs(x: sum)*accuracy) { |
| 66 | return sum*std::exp(x: -x+a*std::log(x: x)-gln); |
| 67 | } |
| 68 | } |
| 69 | QL_FAIL("accuracy not reached" ); |
| 70 | } |
| 71 | |
| 72 | Real incompleteGammaFunctionContinuedFractionRepr(Real a, Real x, |
| 73 | Real accuracy, |
| 74 | Integer maxIteration) { |
| 75 | |
| 76 | Integer i; |
| 77 | Real an, b, c, d, del, h; |
| 78 | Real gln = GammaFunction().logValue(x: a); |
| 79 | b=x+1.0-a; |
| 80 | c=1.0/QL_EPSILON; |
| 81 | d=1.0/b; |
| 82 | h=d; |
| 83 | for (i=1; i<=maxIteration; i++) { |
| 84 | an = -i*(i-a); |
| 85 | b += 2.0; |
| 86 | d=an*d+b; |
| 87 | if (std::fabs(x: d) < QL_EPSILON) d=QL_EPSILON; |
| 88 | c=b+an/c; |
| 89 | if (std::fabs(x: c) < QL_EPSILON) c=QL_EPSILON; |
| 90 | d=1.0/d; |
| 91 | del=d*c; |
| 92 | h *= del; |
| 93 | if (std::fabs(x: del-1.0) < accuracy) { |
| 94 | return std::exp(x: -x+a*std::log(x: x)-gln)*h; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | QL_FAIL("accuracy not reached" ); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | } |
| 104 | |