| 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 | #include <ql/math/beta.hpp> |
| 21 | |
| 22 | namespace QuantLib { |
| 23 | |
| 24 | /* |
| 25 | The implementation of the algorithm was inspired by |
| 26 | "Numerical Recipes in C", 2nd edition, |
| 27 | Press, Teukolsky, Vetterling, Flannery, chapter 6 |
| 28 | */ |
| 29 | Real betaContinuedFraction(Real a, Real b, Real x, |
| 30 | Real accuracy, Integer maxIteration) { |
| 31 | |
| 32 | Real aa, del; |
| 33 | Real qab = a+b; |
| 34 | Real qap = a+1.0; |
| 35 | Real qam = a-1.0; |
| 36 | Real c = 1.0; |
| 37 | Real d = 1.0-qab*x/qap; |
| 38 | if (std::fabs(x: d) < QL_EPSILON) |
| 39 | d = QL_EPSILON; |
| 40 | d = 1.0/d; |
| 41 | Real result = d; |
| 42 | |
| 43 | Integer m, m2; |
| 44 | for (m=1; m<=maxIteration; m++) { |
| 45 | m2=2*m; |
| 46 | aa=m*(b-m)*x/((qam+m2)*(a+m2)); |
| 47 | d=1.0+aa*d; |
| 48 | if (std::fabs(x: d) < QL_EPSILON) d=QL_EPSILON; |
| 49 | c=1.0+aa/c; |
| 50 | if (std::fabs(x: c) < QL_EPSILON) c=QL_EPSILON; |
| 51 | d=1.0/d; |
| 52 | result *= d*c; |
| 53 | aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2)); |
| 54 | d=1.0+aa*d; |
| 55 | if (std::fabs(x: d) < QL_EPSILON) d=QL_EPSILON; |
| 56 | c=1.0+aa/c; |
| 57 | if (std::fabs(x: c) < QL_EPSILON) c=QL_EPSILON; |
| 58 | d=1.0/d; |
| 59 | del=d*c; |
| 60 | result *= del; |
| 61 | if (std::fabs(x: del-1.0) < accuracy) |
| 62 | return result; |
| 63 | } |
| 64 | QL_FAIL("a or b too big, or maxIteration too small in betacf" ); |
| 65 | } |
| 66 | |
| 67 | Real incompleteBetaFunction(Real a, Real b, |
| 68 | Real x, Real accuracy, |
| 69 | Integer maxIteration) { |
| 70 | |
| 71 | QL_REQUIRE(a > 0.0, "a must be greater than zero" ); |
| 72 | QL_REQUIRE(b > 0.0, "b must be greater than zero" ); |
| 73 | |
| 74 | |
| 75 | if (x == 0.0) |
| 76 | return 0.0; |
| 77 | else if (x == 1.0) |
| 78 | return 1.0; |
| 79 | else |
| 80 | QL_REQUIRE(x>0.0 && x<1.0, "x must be in [0,1]" ); |
| 81 | |
| 82 | Real result = std::exp(x: GammaFunction().logValue(x: a+b) - |
| 83 | GammaFunction().logValue(x: a) - GammaFunction().logValue(x: b) + |
| 84 | a*std::log(x: x) + b*std::log(x: 1.0-x)); |
| 85 | |
| 86 | if (x < (a+1.0)/(a+b+2.0)) |
| 87 | return result * |
| 88 | betaContinuedFraction(a, b, x, accuracy, maxIteration)/a; |
| 89 | else |
| 90 | return 1.0 - result * |
| 91 | betaContinuedFraction(a: b, b: a, x: 1.0-x, accuracy, maxIteration)/b; |
| 92 | } |
| 93 | |
| 94 | } |
| 95 | |