QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
exponentialjump1dmesher.cpp
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
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
31namespace QuantLib {
33 Size steps, Real beta, Real jumpIntensity, Real eta, Real eps)
34 : Fdm1dMesher(steps),
35 beta_(beta), jumpIntensity_(jumpIntensity), eta_(eta)
36 {
37 QL_REQUIRE(eps > 0.0 && eps < 1.0, "eps > 0.0 and eps < 1.0");
38 QL_REQUIRE(steps > 1, "minimum number of steps is two");
39
40 const Real start = 0.0;
41 const Real end = 1.0-eps;
42 const Real dx = (end-start)/(steps-1);
43 const Real scale = 1/(1-std::exp(-beta/jumpIntensity));
44
45 for (Size i=0; i < steps; ++i) {
46 const Real p = start + i*dx;
47 locations_[i] = scale*(-1.0/eta*std::log(1.0-p));
48 }
49
50 for (Size i=0; i < steps-1; ++i) {
51 dminus_[i+1] = dplus_[i] = locations_[i+1]-locations_[i];
52 }
53 dplus_.back() = dminus_.front() = Null<Real>();
54 }
55
56
57 Real ExponentialJump1dMesher::jumpSizeDensity(Real x, Time t) const {
58 const Real a = 1.0-jumpIntensity_/beta_;
59 const Real norm = 1.0-std::exp(-jumpIntensity_*t);
60 const Real gammaValue
61 = std::exp(GammaFunction().logValue(1.0-jumpIntensity_/beta_));
62 return jumpIntensity_*gammaValue/norm
63 *( incompleteGammaFunction(a, x*std::exp(beta_*t)*eta_)
64 -incompleteGammaFunction(a, x*eta_))
65 *std::pow(eta_, jumpIntensity_/beta_)
66 /(beta_*std::pow(x, a));
67 }
68
69 Real ExponentialJump1dMesher::jumpSizeDensity(Real x) const {
70 const Real a = 1.0-jumpIntensity_/beta_;
71 const Real gammaValue
72 = std::exp(GammaFunction().logValue(jumpIntensity_/beta_));
73 return std::exp(-x*eta_)*std::pow(x, -a) * std::pow(eta_, 1.0-a)
74 / gammaValue;
75 }
76
77 Real ExponentialJump1dMesher::jumpSizeDistribution(Real x, Time t) const {
78 const Real xmin = std::min(x, 1.0e-100);
79
80 return GaussLobattoIntegral(1000000, 1e-12)(
81 [&](Real _x){ return jumpSizeDensity(_x, t); },
82 xmin, std::max(x, xmin));
83 }
84
85 Real ExponentialJump1dMesher::jumpSizeDistribution(Real x) const {
86 const Real a = jumpIntensity_/beta_;
87 const Real xmin = std::min(x, QL_EPSILON);
88 const Real gammaValue
89 = std::exp(GammaFunction().logValue(jumpIntensity_/beta_));
90
91 const Real lowerEps =
92 (std::pow(xmin, a)/a - std::pow(xmin, a+1)/(a+1))/gammaValue;
93
94 return lowerEps + GaussLobattoIntegral(10000, 1e-12)(
95 [&](Real _x){ return jumpSizeDensity(_x); },
96 xmin/eta_, std::max(x, xmin/eta_));
97 }
98}
ExponentialJump1dMesher(Size steps, Real beta, Real jumpIntensity, Real eta, Real eps=1e-3)
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35