QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmblackscholesop.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Andreas Gaida
5 Copyright (C) 2008, 2009 Ralph Schreyer
6 Copyright (C) 2008, 2009 Klaus Spanderen
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/instruments/payoffs.hpp>
23#include <ql/math/functional.hpp>
24#include <ql/methods/finitedifferences/meshers/fdmmesher.hpp>
25#include <ql/methods/finitedifferences/operators/fdmblackscholesop.hpp>
26#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
27#include <ql/methods/finitedifferences/operators/secondderivativeop.hpp>
28#include <utility>
29
30namespace QuantLib {
31
33 const ext::shared_ptr<FdmMesher>& mesher,
34 const ext::shared_ptr<GeneralizedBlackScholesProcess>& bsProcess,
35 Real strike,
36 bool localVol,
37 Real illegalLocalVolOverwrite,
38 Size direction,
39 ext::shared_ptr<FdmQuantoHelper> quantoHelper)
40 : mesher_(mesher), rTS_(bsProcess->riskFreeRate().currentLink()),
41 qTS_(bsProcess->dividendYield().currentLink()),
42 volTS_(bsProcess->blackVolatility().currentLink()),
43 localVol_((localVol) ? bsProcess->localVolatility().currentLink() :
44 ext::shared_ptr<LocalVolTermStructure>()),
45 x_((localVol) ? Array(Exp(mesher->locations(direction))) : Array()),
46 dxMap_(FirstDerivativeOp(direction, mesher)), dxxMap_(SecondDerivativeOp(direction, mesher)),
47 mapT_(direction, mesher), strike_(strike),
48 illegalLocalVolOverwrite_(illegalLocalVolOverwrite), direction_(direction),
49 quantoHelper_(std::move(quantoHelper)) {}
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
72 if (quantoHelper_ != nullptr) {
73 mapT_.axpyb(r - q - 0.5*v
74 - quantoHelper_->quantoAdjustment(Sqrt(v), t1, t2),
75 dxMap_, dxxMap_.mult(0.5*v), Array(1, -r));
76 } else {
77 mapT_.axpyb(r - q - 0.5*v, dxMap_,
78 dxxMap_.mult(0.5*v), Array(1, -r));
79 }
80 } else {
81 const Real v
82 = volTS_->blackForwardVariance(t1, t2, strike_)/(t2-t1);
83
84 if (quantoHelper_ != nullptr) {
86 Array(1, r - q - 0.5*v)
87 - quantoHelper_->quantoAdjustment(
88 Array(1, std::sqrt(v)), t1, t2),
89 dxMap_,
90 dxxMap_.mult(0.5*Array(mesher_->layout()->size(), v)),
91 Array(1, -r));
92 } else {
93 mapT_.axpyb(Array(1, r - q - 0.5*v), dxMap_,
94 dxxMap_.mult(0.5*Array(mesher_->layout()->size(), v)),
95 Array(1, -r));
96 }
97 }
98 }
99
100 Size FdmBlackScholesOp::size() const { return 1U; }
101
103 return mapT_.apply(u);
104 }
105
107 const Array& r) const {
108 if (direction == direction_)
109 return mapT_.apply(r);
110 else {
111 return Array(r.size(), 0.0);
112 }
113 }
114
116 return Array(r.size(), 0.0);
117 }
118
120 const Array& r, Real dt) const {
121 if (direction == direction_)
122 return mapT_.solve_splitting(r, dt, 1.0);
123 else {
124 return r;
125 }
126 }
127
129 Real dt) const {
130 return solve_splitting(direction_, r, dt);
131 }
132
133 std::vector<SparseMatrix> FdmBlackScholesOp::toMatrixDecomp() const {
134 return std::vector<SparseMatrix>(1, mapT_.toMatrix());
135 }
136
137}
1-D array used in linear algebra.
Definition: array.hpp:52
Base error class.
Definition: errors.hpp:39
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
FdmBlackScholesOp(const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< GeneralizedBlackScholesProcess > &process, Real strike, bool localVol=false, Real illegalLocalVolOverwrite=-Null< Real >(), Size direction=0, ext::shared_ptr< FdmQuantoHelper > quantoHelper=ext::shared_ptr< FdmQuantoHelper >())
const TripleBandLinearOp dxxMap_
const ext::shared_ptr< FdmQuantoHelper > quantoHelper_
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 FirstDerivativeOp dxMap_
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
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
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
Definition: any.hpp:35
T squared(T x)
Definition: functional.hpp:37
Array Exp(const Array &v)
Definition: array.hpp:875
Array Sqrt(const Array &v)
Definition: array.hpp:847
STL namespace.