QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmblackscholesmesher.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 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
24#include <ql/processes/blackscholesprocess.hpp>
25#include <ql/termstructures/yieldtermstructure.hpp>
26#include <ql/termstructures/yield/quantotermstructure.hpp>
27#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
28#include <ql/math/distributions/normaldistribution.hpp>
29#include <ql/methods/finitedifferences/utilities/fdmquantohelper.hpp>
30#include <ql/methods/finitedifferences/meshers/uniform1dmesher.hpp>
31#include <ql/methods/finitedifferences/meshers/concentrating1dmesher.hpp>
32#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
33
34namespace QuantLib {
35
37 Size size,
38 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
39 Time maturity, Real strike,
40 Real xMinConstraint, Real xMaxConstraint,
41 Real eps, Real scaleFactor,
42 const std::pair<Real, Real>& cPoint,
43 const DividendSchedule& dividendSchedule,
44 const ext::shared_ptr<FdmQuantoHelper>& fdmQuantoHelper,
45 Real spotAdjustment)
46 : Fdm1dMesher(size) {
47
48 const Real S = process->x0();
49 QL_REQUIRE(S > 0.0, "negative or null underlying given");
50
51 std::vector<std::pair<Time, Real> > intermediateSteps;
52 for (const auto& i : dividendSchedule) {
53 const Time t = process->time(i->date());
54 if (t <= maturity && t >= 0.0)
55 intermediateSteps.emplace_back(process->time(i->date()), i->amount());
56 }
57
58 const Size intermediateTimeSteps = std::max<Size>(2, Size(24.0*maturity));
59 for (Size i=0; i < intermediateTimeSteps; ++i)
60 intermediateSteps.emplace_back((i + 1) * (maturity / intermediateTimeSteps), 0.0);
61
62 std::sort(intermediateSteps.begin(), intermediateSteps.end());
63
64 const Handle<YieldTermStructure> rTS = process->riskFreeRate();
65
67 (fdmQuantoHelper) != nullptr ?
68 Handle<YieldTermStructure>(ext::make_shared<QuantoTermStructure>(
69 process->dividendYield(), process->riskFreeRate(),
70 Handle<YieldTermStructure>(fdmQuantoHelper->fTS_), process->blackVolatility(),
71 strike, Handle<BlackVolTermStructure>(fdmQuantoHelper->fxVolTS_),
72 fdmQuantoHelper->exchRateATMlevel_, fdmQuantoHelper->equityFxCorrelation_)) :
73 process->dividendYield();
74
75 Time lastDivTime = 0.0;
76 Real fwd = S + spotAdjustment;
77 Real mi = fwd, ma = fwd;
78
79 for (auto& intermediateStep : intermediateSteps) {
80 const Time divTime = intermediateStep.first;
81 const Real divAmount = intermediateStep.second;
82
83 fwd = fwd / rTS->discount(divTime) * rTS->discount(lastDivTime)
84 * qTS->discount(divTime) / qTS->discount(lastDivTime);
85
86 mi = std::min(mi, fwd); ma = std::max(ma, fwd);
87
88 fwd-= divAmount;
89
90 mi = std::min(mi, fwd); ma = std::max(ma, fwd);
91
92 lastDivTime = divTime;
93 }
94
95 // Set the grid boundaries
96 const Real normInvEps = InverseCumulativeNormal()(1-eps);
97 const Real sigmaSqrtT
98 = process->blackVolatility()->blackVol(maturity, strike)
99 *std::sqrt(maturity);
100
101 Real xMin = std::log(mi) - sigmaSqrtT*normInvEps*scaleFactor;
102 Real xMax = std::log(ma) + sigmaSqrtT*normInvEps*scaleFactor;
103
104 if (xMinConstraint != Null<Real>()) {
105 xMin = xMinConstraint;
106 }
107 if (xMaxConstraint != Null<Real>()) {
108 xMax = xMaxConstraint;
109 }
110
111 ext::shared_ptr<Fdm1dMesher> helper;
112 if ( cPoint.first != Null<Real>()
113 && std::log(cPoint.first) >=xMin && std::log(cPoint.first) <=xMax) {
114
115 helper = ext::shared_ptr<Fdm1dMesher>(
116 new Concentrating1dMesher(xMin, xMax, size,
117 std::pair<Real,Real>(std::log(cPoint.first),
118 cPoint.second)));
119 }
120 else {
121 helper = ext::shared_ptr<Fdm1dMesher>(
122 new Uniform1dMesher(xMin, xMax, size));
123
124 }
125
126 locations_ = helper->locations();
127 for (Size i=0; i < locations_.size(); ++i) {
128 dplus_[i] = helper->dplus(i);
129 dminus_[i] = helper->dminus(i);
130 }
131 }
132
133 ext::shared_ptr<GeneralizedBlackScholesProcess>
137 Volatility vol) {
138
139 return ext::make_shared<GeneralizedBlackScholesProcess>(
140
141 s0, qTS, rTS,
143 ext::shared_ptr<BlackVolTermStructure>(
144 new BlackConstantVol(rTS->referenceDate(),
145 Calendar(),
146 vol,
147 rTS->dayCounter()))));
148 }
149}
150
Constant Black volatility, no time-strike dependence.
calendar class
Definition: calendar.hpp:61
std::vector< Real > locations_
Definition: fdm1dmesher.hpp:47
std::vector< Real > dplus_
Definition: fdm1dmesher.hpp:48
std::vector< Real > dminus_
Definition: fdm1dmesher.hpp:48
FdmBlackScholesMesher(Size size, const ext::shared_ptr< GeneralizedBlackScholesProcess > &process, Time maturity, Real strike, Real xMinConstraint=Null< Real >(), Real xMaxConstraint=Null< Real >(), Real eps=0.0001, Real scaleFactor=1.5, const std::pair< Real, Real > &cPoint={ Null< Real >(), Null< Real >() }, const DividendSchedule &dividendSchedule={}, const ext::shared_ptr< FdmQuantoHelper > &fdmQuantoHelper={}, Real spotAdjustment=0.0)
static ext::shared_ptr< GeneralizedBlackScholesProcess > processHelper(const Handle< Quote > &s0, const Handle< YieldTermStructure > &rTS, const Handle< YieldTermStructure > &qTS, Volatility vol)
Shared handle to an observable.
Definition: handle.hpp:41
Inverse cumulative normal distribution function.
template class providing a null value for a given type.
Definition: null.hpp:76
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule