QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdmblackscholesfwdop.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) 2012, 2013 Klaus Spanderen
5 Copyright (C) 2014 Johannes Göttker-Schnetmann
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
26
27namespace QuantLib {
28
30 const ext::shared_ptr<FdmMesher>& mesher,
31 const ext::shared_ptr<GeneralizedBlackScholesProcess> & bsProcess,
32 Real strike,
33 bool localVol,
34 Real illegalLocalVolOverwrite,
35 Size direction)
36 : mesher_(mesher),
37 rTS_ (bsProcess->riskFreeRate().currentLink()),
38 qTS_ (bsProcess->dividendYield().currentLink()),
39 volTS_ (bsProcess->blackVolatility().currentLink()),
40 localVol_((localVol) ? bsProcess->localVolatility().currentLink()
41 : ext::shared_ptr<LocalVolTermStructure>()),
42 x_ ((localVol) ? Array(Exp(mesher->locations(direction))) : Array()),
43 dxMap_ (FirstDerivativeOp(direction, mesher)),
44 dxxMap_(SecondDerivativeOp(direction, mesher)),
45 mapT_ (direction, mesher),
46 strike_(strike),
47 illegalLocalVolOverwrite_(illegalLocalVolOverwrite),
48 direction_(direction) {
49 }
50
52 const Rate r = rTS_->forwardRate(t1, t2, Continuous).rate();
53 const Rate q = qTS_->forwardRate(t1, t2, Continuous).rate();
54
55 if (localVol_ != nullptr) {
56 Array v(mesher_->layout()->size());
57 for (const auto& iter : *mesher_->layout()) {
58 const Size i = iter.index();
59
60 if (illegalLocalVolOverwrite_ < 0.0) {
61 v[i] = squared(localVol_->localVol(0.5*(t1+t2), x_[i], true));
62 }
63 else {
64 try {
65 v[i] = squared(localVol_->localVol(0.5*(t1+t2), x_[i], true));
66 } catch (Error&) {
68 }
69 }
70 }
71 mapT_.axpyb(Array(1, 1.0), dxMap_.multR(- r + q + 0.5*v),
72 dxxMap_.multR(0.5*v), Array(1, 0.0));
73 } else {
74 const Real v
75 = volTS_->blackForwardVariance(t1, t2, strike_)/(t2-t1);
76 mapT_.axpyb(Array(1, - r + q + 0.5*v), dxMap_,
77 dxxMap_.mult(0.5*Array(mesher_->layout()->size(), v)),
78 Array(1, 0.0));
79 }
80 }
81
82 Size FdmBlackScholesFwdOp::size() const { return 1U; }
83
85 return mapT_.apply(u);
86 }
87
89 Size direction, const Array& r) const {
90 if (direction == direction_)
91 return mapT_.apply(r);
92 else {
93 return Array(r.size(), 0.0);
94 }
95 }
96
98 return Array(r.size(), 0.0);
99 }
100
102 Size direction, const Array& r, Real dt) const {
103 if (direction == direction_)
104 return mapT_.solve_splitting(r, dt, 1.0);
105 else {
106 return r;
107 }
108 }
109
111 const Array& r, Real dt) const {
112 return solve_splitting(direction_, r, dt);
113 }
114
115 std::vector<SparseMatrix> FdmBlackScholesFwdOp::toMatrixDecomp() const {
116 return std::vector<SparseMatrix>(1, mapT_.toMatrix());
117 }
118
119}
const Handle< BlackVolTermStructure > volTS_
1-D array used in linear algebra.
Definition: array.hpp:52
Base error class.
Definition: errors.hpp:39
FdmBlackScholesFwdOp(const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< GeneralizedBlackScholesProcess > &process, Real strike, bool localVol=false, Real illegalLocalVolOverwrite=-Null< Real >(), Size direction=0)
Array apply_direction(Size direction, const Array &r) const override
Array preconditioner(const Array &r, Real s) const override
std::vector< SparseMatrix > toMatrixDecomp() const override
const TripleBandLinearOp dxxMap_
void setTime(Time t1, Time t2) override
Time is required.
Array apply_mixed(const Array &r) const override
const ext::shared_ptr< YieldTermStructure > qTS_
const ext::shared_ptr< FdmMesher > mesher_
Array solve_splitting(Size direction, const Array &r, Real s) const override
const ext::shared_ptr< YieldTermStructure > rTS_
const ext::shared_ptr< BlackVolTermStructure > volTS_
Array apply(const Array &r) const override
const ext::shared_ptr< LocalVolTermStructure > localVol_
SparseMatrix toMatrix() const override
TripleBandLinearOp multR(const Array &u) const
Array solve_splitting(const Array &r, Real a, Real b=1.0) const
void axpyb(const Array &a, const TripleBandLinearOp &x, const TripleBandLinearOp &y, const Array &b)
TripleBandLinearOp mult(const Array &u) const
Array apply(const Array &r) const override
Black Scholes linear operator for the Fokker-Planck forward equation.
const ext::shared_ptr< YieldTermStructure > rTS_
memory layout of a fdm linear operator
mesher for a fdm grid
const ext::shared_ptr< FdmMesher > mesher_
const Size direction_
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
functionals and combinators not included in the STL
Definition: any.hpp:35
T squared(T x)
Definition: functional.hpp:37
Array Exp(const Array &v)
Definition: array.hpp:875
ext::shared_ptr< YieldTermStructure > q
ext::shared_ptr< YieldTermStructure > r
ext::shared_ptr< BlackVolTermStructure > v
second derivative operator
std::uint64_t x_