QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
blackscholesprocess.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 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
6 Copyright (C) 2004, 2005, 2006, 2007 StatPro Italia srl
7 Copyright (C) 2015 Peter Caspers
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
30#include <utility>
31
32
33namespace QuantLib {
34
41 : StochasticProcess1D(ext::make_shared<EulerDiscretization>()), x0_(std::move(x0)),
42 riskFreeRate_(std::move(riskFreeTS)), dividendYield_(std::move(dividendTS)),
43 blackVolatility_(std::move(blackVolTS)), externalLocalVolTS_(std::move(localVolTS)),
44 forceDiscretization_(false), hasExternalLocalVol_(true), updated_(false),
45 isStrikeIndependent_(false) {
51 }
52
58 const ext::shared_ptr<discretization>& disc,
59 bool forceDiscretization)
60 : StochasticProcess1D(disc), x0_(std::move(x0)), riskFreeRate_(std::move(riskFreeTS)),
61 dividendYield_(std::move(dividendTS)), blackVolatility_(std::move(blackVolTS)),
62 forceDiscretization_(forceDiscretization), hasExternalLocalVol_(false), updated_(false),
63 isStrikeIndependent_(false) {
68 }
69
71 return x0_->value();
72 }
73
75 Real sigma = diffusion(t,x);
76 // we could be more anticipatory if we know the right dt
77 // for which the drift will be used
78 Time t1 = t + 0.0001;
79 return riskFreeRate_->forwardRate(t,t1,Continuous,NoFrequency,true).rate()
80 - dividendYield_->forwardRate(t,t1,Continuous,NoFrequency,true).rate()
81 - 0.5 * sigma * sigma;
82 }
83
85 return localVolatility()->localVol(t, x, true);
86 }
87
89 return x0 * std::exp(dx);
90 }
91
93 Real x0,
94 Time dt) const {
95 localVolatility(); // trigger update
97 // exact value for curves
98 return x0 *
99 std::exp(dt * (riskFreeRate_->forwardRate(t0, t0 + dt, Continuous,
100 NoFrequency, true).rate() -
101 dividendYield_->forwardRate(
102 t0, t0 + dt, Continuous, NoFrequency, true).rate()));
103 } else {
104 QL_FAIL("not implemented");
105 }
106 }
107
109 localVolatility(); // trigger update
111 // exact value for curves
112 return std::sqrt(variance(t0,x0,dt));
113 }
114 else{
115 return discretization_->diffusion(*this,t0,x0,dt);
116 }
117 }
118
120 localVolatility(); // trigger update
122 // exact value for curves
123 return blackVolatility_->blackVariance(t0 + dt, 0.01) -
124 blackVolatility_->blackVariance(t0, 0.01);
125 }
126 else{
127 return discretization_->variance(*this,t0,x0,dt);
128 }
129 }
130
132 Time dt, Real dw) const {
133 localVolatility(); // trigger update
135 // exact value for curves
136 Real var = variance(t0, x0, dt);
137 Real drift = (riskFreeRate_->forwardRate(t0, t0 + dt, Continuous,
138 NoFrequency, true).rate() -
139 dividendYield_->forwardRate(t0, t0 + dt, Continuous,
140 NoFrequency, true).rate()) *
141 dt -
142 0.5 * var;
143 return apply(x0, std::sqrt(var) * dw + drift);
144 } else
145 return apply(x0, discretization_->drift(*this, t0, x0, dt) +
146 stdDeviation(t0, x0, dt) * dw);
147 }
148
150 return riskFreeRate_->dayCounter().yearFraction(
151 riskFreeRate_->referenceDate(), d);
152 }
153
155 updated_ = false;
157 }
158
159 const Handle<Quote>&
161 return x0_;
162 }
163
166 return dividendYield_;
167 }
168
171 return riskFreeRate_;
172 }
173
176 return blackVolatility_;
177 }
178
182 return externalLocalVolTS_;
183
184 if (!updated_) {
186
187 // constant Black vol?
188 ext::shared_ptr<BlackConstantVol> constVol =
189 ext::dynamic_pointer_cast<BlackConstantVol>(
190 *blackVolatility());
191 if (constVol != nullptr) {
192 // ok, the local vol is constant too.
193 localVolatility_.linkTo(ext::make_shared<LocalConstantVol>(
194 constVol->referenceDate(),
195 constVol->blackVol(0.0, x0_->value()),
196 constVol->dayCounter()));
197 updated_ = true;
198 return localVolatility_;
199 }
200
201 // ok, so it's not constant. Maybe it's strike-independent?
202 ext::shared_ptr<BlackVarianceCurve> volCurve =
203 ext::dynamic_pointer_cast<BlackVarianceCurve>(
204 *blackVolatility());
205 if (volCurve != nullptr) {
206 // ok, we can use the optimized algorithm
207 localVolatility_.linkTo(ext::make_shared<LocalVolCurve>(
208 Handle<BlackVarianceCurve>(volCurve)));
209 updated_ = true;
210 return localVolatility_;
211 }
212
213 // ok, so it's strike-dependent. Never mind.
214 localVolatility_.linkTo(
215 ext::make_shared<LocalVolSurface>(blackVolatility_, riskFreeRate_,
216 dividendYield_, x0_->value()));
217 updated_ = true;
218 isStrikeIndependent_ = false;
219 return localVolatility_;
220
221 } else {
222 return localVolatility_;
223 }
224 }
225
226
227 // specific models
228
230 const Handle<Quote>& x0,
231 const Handle<YieldTermStructure>& riskFreeTS,
232 const Handle<BlackVolTermStructure>& blackVolTS,
233 const ext::shared_ptr<discretization>& d,
234 bool forceDiscretization)
236 x0,
237 // no dividend yield
239 new FlatForward(0, NullCalendar(), 0.0, Actual365Fixed()))),
240 riskFreeTS,
241 blackVolTS,
242 d,forceDiscretization) {}
243
244
246 const Handle<Quote>& x0,
247 const Handle<YieldTermStructure>& dividendTS,
248 const Handle<YieldTermStructure>& riskFreeTS,
249 const Handle<BlackVolTermStructure>& blackVolTS,
250 const ext::shared_ptr<discretization>& d,
251 bool forceDiscretization)
252 : GeneralizedBlackScholesProcess(x0,dividendTS,riskFreeTS,blackVolTS,d,
253 forceDiscretization) {}
254
255
257 const Handle<YieldTermStructure>& riskFreeTS,
258 const Handle<BlackVolTermStructure>& blackVolTS,
259 const ext::shared_ptr<discretization>& d,
260 bool forceDiscretization)
261 : GeneralizedBlackScholesProcess(x0,riskFreeTS,riskFreeTS,blackVolTS,d,
262 forceDiscretization) {}
263
264
266 const Handle<Quote>& x0,
267 const Handle<YieldTermStructure>& foreignRiskFreeTS,
268 const Handle<YieldTermStructure>& domesticRiskFreeTS,
269 const Handle<BlackVolTermStructure>& blackVolTS,
270 const ext::shared_ptr<discretization>& d,
271 bool forceDiscretization)
272 : GeneralizedBlackScholesProcess(x0,foreignRiskFreeTS,domesticRiskFreeTS,
273 blackVolTS,d,forceDiscretization) {}
274
275}
Actual/365 (Fixed) day counter.
Black-Scholes processes.
Actual/365 (Fixed) day count convention.
BlackProcess(const Handle< Quote > &x0, const Handle< YieldTermStructure > &riskFreeTS, const Handle< BlackVolTermStructure > &blackVolTS, const ext::shared_ptr< discretization > &d=ext::shared_ptr< discretization >(new EulerDiscretization), bool forceDiscretization=false)
BlackScholesMertonProcess(const Handle< Quote > &x0, const Handle< YieldTermStructure > &dividendTS, const Handle< YieldTermStructure > &riskFreeTS, const Handle< BlackVolTermStructure > &blackVolTS, const ext::shared_ptr< discretization > &d=ext::shared_ptr< discretization >(new EulerDiscretization), bool forceDiscretization=false)
BlackScholesProcess(const Handle< Quote > &x0, const Handle< YieldTermStructure > &riskFreeTS, const Handle< BlackVolTermStructure > &blackVolTS, const ext::shared_ptr< discretization > &d=ext::shared_ptr< discretization >(new EulerDiscretization), bool forceDiscretization=false)
Concrete date class.
Definition: date.hpp:125
Euler discretization for stochastic processes.
Flat interest-rate curve.
Definition: flatforward.hpp:37
GarmanKohlagenProcess(const Handle< Quote > &x0, const Handle< YieldTermStructure > &foreignRiskFreeTS, const Handle< YieldTermStructure > &domesticRiskFreeTS, const Handle< BlackVolTermStructure > &blackVolTS, const ext::shared_ptr< discretization > &d=ext::shared_ptr< discretization >(new EulerDiscretization), bool forceDiscretization=false)
Generalized Black-Scholes stochastic process.
Real apply(Real x0, Real dx) const override
const Handle< Quote > & stateVariable() const
GeneralizedBlackScholesProcess(Handle< Quote > x0, Handle< YieldTermStructure > dividendTS, Handle< YieldTermStructure > riskFreeTS, Handle< BlackVolTermStructure > blackVolTS, const ext::shared_ptr< discretization > &d=ext::shared_ptr< discretization >(new EulerDiscretization), bool forceDiscretization=false)
const Handle< LocalVolTermStructure > & localVolatility() const
RelinkableHandle< LocalVolTermStructure > localVolatility_
Handle< LocalVolTermStructure > externalLocalVolTS_
Handle< BlackVolTermStructure > blackVolatility_
const Handle< YieldTermStructure > & dividendYield() const
Time time(const Date &) const override
Real diffusion(Time t, Real x) const override
Real stdDeviation(Time t0, Real x0, Time dt) const override
Real evolve(Time t0, Real x0, Time dt, Real dw) const override
Real drift(Time t, Real x) const override
Real expectation(Time t0, Real x0, Time dt) const override
const Handle< BlackVolTermStructure > & blackVolatility() const
Real x0() const override
returns the initial value of the state variable
Real variance(Time t0, Real x0, Time dt) const override
const Handle< YieldTermStructure > & riskFreeRate() const
Shared handle to an observable.
Definition: handle.hpp:41
Calendar for reproducing theoretical calculations.
virtual void update()=0
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
1-dimensional stochastic process
ext::shared_ptr< discretization > discretization_
Interest-rate term structure.
const DefaultType & t
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Date d
LinearInterpolation variance
flat forward rate term structure
@ NoFrequency
null frequency
Definition: frequency.hpp:37
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real sigma
Local constant volatility, no time dependence, no asset dependence.
Local volatility curve derived from a Black curve.
Local volatility surface derived from a Black vol surface.
Definition: any.hpp:35
STL namespace.
Calendar for reproducing theoretical calculations.