QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
incompletegamma.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) 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
28
29namespace QuantLib {
30
31
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
42 accuracy, maxIteration);
43 } else {
44 // Use the continued fraction representation
46 accuracy, maxIteration);
47 }
48
49 }
50
51
53 Integer maxIteration) {
54
55 if (x==0.0) return 0.0;
56
57 Real gln = GammaFunction().logValue(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(del) < std::fabs(sum)*accuracy) {
66 return sum*std::exp(-x+a*std::log(x)-gln);
67 }
68 }
69 QL_FAIL("accuracy not reached");
70 }
71
73 Real accuracy,
74 Integer maxIteration) {
75
76 Integer i;
77 Real an, b, c, d, del, h;
78 Real gln = GammaFunction().logValue(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(d) < QL_EPSILON) d=QL_EPSILON;
88 c=b+an/c;
89 if (std::fabs(c) < QL_EPSILON) c=QL_EPSILON;
90 d=1.0/d;
91 del=d*c;
92 h *= del;
93 if (std::fabs(del-1.0) < accuracy) {
94 return std::exp(-x+a*std::log(x)-gln)*h;
95 }
96 }
97
98 QL_FAIL("accuracy not reached");
99 }
100
101
102
103}
Gamma function class.
Real logValue(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_EPSILON
Definition: qldefines.hpp:178
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Incomplete Gamma function.
Definition: any.hpp:35
Real incompleteGammaFunctionSeriesRepr(Real a, Real x, Real accuracy, Integer maxIteration)
Real incompleteGammaFunction(Real a, Real x, Real accuracy, Integer maxIteration)
Incomplete Gamma function.
Real incompleteGammaFunctionContinuedFractionRepr(Real a, Real x, Real accuracy, Integer maxIteration)