Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/math/beta.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) 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
0
                               Real accuracy, Integer maxIteration) {
31
32
0
        Real aa, del;
33
0
        Real qab = a+b;
34
0
        Real qap = a+1.0;
35
0
        Real qam = a-1.0;
36
0
        Real c = 1.0;
37
0
        Real d = 1.0-qab*x/qap;
38
0
        if (std::fabs(d) < QL_EPSILON)
39
0
            d = QL_EPSILON;
40
0
        d = 1.0/d;
41
0
        Real result = d;
42
43
0
        Integer m, m2;
44
0
        for (m=1; m<=maxIteration; m++) {
45
0
            m2=2*m;
46
0
            aa=m*(b-m)*x/((qam+m2)*(a+m2));
47
0
            d=1.0+aa*d;
48
0
            if (std::fabs(d) < QL_EPSILON) d=QL_EPSILON;
49
0
            c=1.0+aa/c;
50
0
            if (std::fabs(c) < QL_EPSILON) c=QL_EPSILON;
51
0
            d=1.0/d;
52
0
            result *= d*c;
53
0
            aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
54
0
            d=1.0+aa*d;
55
0
            if (std::fabs(d) < QL_EPSILON) d=QL_EPSILON;
56
0
            c=1.0+aa/c;
57
0
            if (std::fabs(c) < QL_EPSILON) c=QL_EPSILON;
58
0
            d=1.0/d;
59
0
            del=d*c;
60
0
            result *= del;
61
0
            if (std::fabs(del-1.0) < accuracy)
62
0
                return result;
63
0
        }
64
0
        QL_FAIL("a or b too big, or maxIteration too small in betacf");
65
0
    }
66
67
    Real incompleteBetaFunction(Real a, Real b,
68
                                Real x, Real accuracy,
69
0
                                Integer maxIteration) {
70
71
0
    QL_REQUIRE(a > 0.0, "a must be greater than zero");
72
0
    QL_REQUIRE(b > 0.0, "b must be greater than zero");
73
74
75
0
    if (x == 0.0)
76
0
        return 0.0;
77
0
    else if (x == 1.0)
78
0
        return 1.0;
79
0
    else
80
0
        QL_REQUIRE(x>0.0 && x<1.0, "x must be in [0,1]");
81
82
0
    Real result = std::exp(GammaFunction().logValue(a+b) -
83
0
        GammaFunction().logValue(a) - GammaFunction().logValue(b) +
84
0
        a*std::log(x) + b*std::log(1.0-x));
85
86
0
    if (x < (a+1.0)/(a+b+2.0))
87
0
        return result *
88
0
            betaContinuedFraction(a, b, x, accuracy, maxIteration)/a;
89
0
    else
90
0
        return 1.0 - result *
91
0
            betaContinuedFraction(b, a, 1.0-x, accuracy, maxIteration)/b;
92
0
    }
93
94
}