/src/quantlib/ql/methods/finitedifferences/meshers/exponentialjump1dmesher.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) 2011 Klaus Spanderen |
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 | | /*! \file exponentialjump1dmesher.cpp |
21 | | \brief mesher for a exponential jump mesher with high |
22 | | mean reversion rate and low jump intensity |
23 | | */ |
24 | | |
25 | | #include <ql/math/incompletegamma.hpp> |
26 | | #include <ql/math/integrals/gausslobattointegral.hpp> |
27 | | #include <ql/math/distributions/gammadistribution.hpp> |
28 | | #include <ql/methods/finitedifferences/meshers/exponentialjump1dmesher.hpp> |
29 | | #include <algorithm> |
30 | | |
31 | | namespace QuantLib { |
32 | | ExponentialJump1dMesher::ExponentialJump1dMesher( |
33 | | Size steps, Real beta, Real jumpIntensity, Real eta, Real eps) |
34 | 0 | : Fdm1dMesher(steps), |
35 | 0 | beta_(beta), jumpIntensity_(jumpIntensity), eta_(eta) |
36 | 0 | { |
37 | 0 | QL_REQUIRE(eps > 0.0 && eps < 1.0, "eps > 0.0 and eps < 1.0"); |
38 | 0 | QL_REQUIRE(steps > 1, "minimum number of steps is two"); |
39 | | |
40 | 0 | const Real start = 0.0; |
41 | 0 | const Real end = 1.0-eps; |
42 | 0 | const Real dx = (end-start)/(steps-1); |
43 | 0 | const Real scale = 1/(1-std::exp(-beta/jumpIntensity)); |
44 | |
|
45 | 0 | for (Size i=0; i < steps; ++i) { |
46 | 0 | const Real p = start + i*dx; |
47 | 0 | locations_[i] = scale*(-1.0/eta*std::log(1.0-p)); |
48 | 0 | } |
49 | |
|
50 | 0 | for (Size i=0; i < steps-1; ++i) { |
51 | 0 | dminus_[i+1] = dplus_[i] = locations_[i+1]-locations_[i]; |
52 | 0 | } |
53 | 0 | dplus_.back() = dminus_.front() = Null<Real>(); |
54 | 0 | } |
55 | | |
56 | | |
57 | 0 | Real ExponentialJump1dMesher::jumpSizeDensity(Real x, Time t) const { |
58 | 0 | const Real a = 1.0-jumpIntensity_/beta_; |
59 | 0 | const Real norm = 1.0-std::exp(-jumpIntensity_*t); |
60 | 0 | const Real gammaValue |
61 | 0 | = std::exp(GammaFunction().logValue(1.0-jumpIntensity_/beta_)); |
62 | 0 | return jumpIntensity_*gammaValue/norm |
63 | 0 | *( incompleteGammaFunction(a, x*std::exp(beta_*t)*eta_) |
64 | 0 | -incompleteGammaFunction(a, x*eta_)) |
65 | 0 | *std::pow(eta_, jumpIntensity_/beta_) |
66 | 0 | /(beta_*std::pow(x, a)); |
67 | 0 | } |
68 | | |
69 | 0 | Real ExponentialJump1dMesher::jumpSizeDensity(Real x) const { |
70 | 0 | const Real a = 1.0-jumpIntensity_/beta_; |
71 | 0 | const Real gammaValue |
72 | 0 | = std::exp(GammaFunction().logValue(jumpIntensity_/beta_)); |
73 | 0 | return std::exp(-x*eta_)*std::pow(x, -a) * std::pow(eta_, 1.0-a) |
74 | 0 | / gammaValue; |
75 | 0 | } |
76 | | |
77 | 0 | Real ExponentialJump1dMesher::jumpSizeDistribution(Real x, Time t) const { |
78 | 0 | const Real xmin = std::min(x, 1.0e-100); |
79 | |
|
80 | 0 | return GaussLobattoIntegral(1000000, 1e-12)( |
81 | 0 | [&](Real _x){ return jumpSizeDensity(_x, t); }, |
82 | 0 | xmin, std::max(x, xmin)); |
83 | 0 | } |
84 | | |
85 | 0 | Real ExponentialJump1dMesher::jumpSizeDistribution(Real x) const { |
86 | 0 | const Real a = jumpIntensity_/beta_; |
87 | 0 | const Real xmin = std::min(x, QL_EPSILON); |
88 | 0 | const Real gammaValue |
89 | 0 | = std::exp(GammaFunction().logValue(jumpIntensity_/beta_)); |
90 | | |
91 | 0 | const Real lowerEps = |
92 | 0 | (std::pow(xmin, a)/a - std::pow(xmin, a+1)/(a+1))/gammaValue; |
93 | | |
94 | 0 | return lowerEps + GaussLobattoIntegral(10000, 1e-12)( |
95 | 0 | [&](Real _x){ return jumpSizeDensity(_x); }, |
96 | 0 | xmin/eta_, std::max(x, xmin/eta_)); |
97 | 0 | } |
98 | | } |