QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdmcirop.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) 2020 Lew Wei Hao
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
26
27namespace QuantLib {
28
30 const ext::shared_ptr<FdmMesher>& mesher,
31 const ext::shared_ptr<GeneralizedBlackScholesProcess>& bsProcess,
32 Real strike)
33 : dxMap_ (FirstDerivativeOp(0, mesher)),
34 dxxMap_(SecondDerivativeOp(0, mesher)),
35 mapT_ (0, mesher),
36 mesher_(mesher),
37 qTS_(bsProcess->dividendYield().currentLink()),
38 strike_(strike),
39 sigma1_(bsProcess->blackVolatility().currentLink()){
40 }
41
43 const Rate q = qTS_->forwardRate(t1, t2, Continuous).rate();
44
45 const Real v = sigma1_->blackForwardVariance(t1, t2, strike_)/(t2-t1);
46
47 mapT_.axpyb(mesher_->locations(1) - q - 0.5*v, dxMap_,
48 dxxMap_.mult(Array(mesher_->layout()->size(), v/2)), -0.5*mesher_->locations(1));
49 }
50
52 return mapT_;
53 }
54
56 const ext::shared_ptr<FdmMesher>& mesher,
58 : dyMap_(SecondDerivativeOp(1, mesher)
59 .mult(sigma*sigma*mesher->locations(1))
60 .add(FirstDerivativeOp(1, mesher)
61 .mult(kappa*(theta - mesher->locations(1))))),
62 mapT_(1, mesher),
63 mesher_(mesher){
64 }
65
67 mapT_.axpyb(Array(), dyMap_, dyMap_, -0.5*mesher_->locations(1));
68 }
69
71 return mapT_;
72 }
73
75 const ext::shared_ptr<FdmMesher>& mesher,
76 const ext::shared_ptr<CoxIngersollRossProcess> & cirProcess,
77 const ext::shared_ptr<GeneralizedBlackScholesProcess> & bsProcess,
78 const Real rho,
79 const Real strike)
80 : dyMap_(SecondOrderMixedDerivativeOp(0, 1, mesher)
81 .mult(Array(mesher->layout()->size(), 2*rho*cirProcess->volatility()))),
82 mapT_(0, 1, mesher),
83 mesher_(mesher),
84 sigma1_(bsProcess->blackVolatility().currentLink()),
85 strike_(strike){
86 }
87
89 const Real v = std::sqrt(sigma1_->blackForwardVariance(t1, t2, strike_)/(t2-t1));
90 NinePointLinearOp op(dyMap_.mult(Array(mesher_->layout()->size(), v)));
91 mapT_.swap(op);
92 }
93
95 return mapT_;
96 }
97
99 const ext::shared_ptr<FdmMesher>& mesher,
100 const ext::shared_ptr<CoxIngersollRossProcess> & cirProcess,
101 const ext::shared_ptr<GeneralizedBlackScholesProcess> & bsProcess,
102 const Real rho,
103 const Real strike)
104 : dxMap_(mesher,
105 bsProcess,
106 strike),
107 dyMap_(mesher,
108 cirProcess->volatility(),
109 cirProcess->speed(),
110 cirProcess->level()),
111 dzMap_(mesher,
112 cirProcess,
113 bsProcess,
114 rho,
115 strike){
116 }
117
118
120 dxMap_.setTime(t1, t2);
121 dyMap_.setTime(t1, t2);
122 dzMap_.setTime(t1, t2);
123 }
124
126 return 2;
127 }
128
129 Array FdmCIROp::apply(const Array& u) const {
130 Array dx = dxMap_.getMap().apply(u);
131 Array dy = dyMap_.getMap().apply(u);
132 Array dz = dzMap_.getMap().apply(u);
133
134 return (dy + dx + dz);
135 }
136
138 const Array& r) const {
139 if (direction == 0)
140 return dxMap_.getMap().apply(r);
141 else if (direction == 1)
142 return dyMap_.getMap().apply(r);
143 else
144 QL_FAIL("direction too large");
145 }
146
148 return dzMap_.getMap().apply(r);
149 }
150
152 const Array& r, Real a) const {
153 if (direction == 0) {
154 return dxMap_.getMap().solve_splitting(r, a, 1.0);
155 }
156 else if (direction == 1) {
157 return dyMap_.getMap().solve_splitting(r, a, 1.0);
158 }
159 else
160 QL_FAIL("direction too large");
161 }
162
164 return solve_splitting(1, solve_splitting(0, r, dt), dt) ;
165 }
166
167 std::vector<SparseMatrix> FdmCIROp::toMatrixDecomp() const {
168 return {
172 };
173 }
174
175}
Black-Scholes processes.
1-D array used in linear algebra.
Definition: array.hpp:52
void setTime(Time t1, Time t2)
Definition: fdmcirop.cpp:42
const TripleBandLinearOp & getMap() const
Definition: fdmcirop.cpp:51
const TripleBandLinearOp dxxMap_
Definition: fdmcirop.hpp:52
TripleBandLinearOp mapT_
Definition: fdmcirop.hpp:53
const ext::shared_ptr< YieldTermStructure > qTS_
Definition: fdmcirop.hpp:56
const FirstDerivativeOp dxMap_
Definition: fdmcirop.hpp:51
const ext::shared_ptr< FdmMesher > mesher_
Definition: fdmcirop.hpp:55
const ext::shared_ptr< BlackVolTermStructure > sigma1_
Definition: fdmcirop.hpp:58
FdmCIREquityPart(const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< GeneralizedBlackScholesProcess > &bsProcess, Real strike)
Definition: fdmcirop.cpp:29
void setTime(Time t1, Time t2)
Definition: fdmcirop.cpp:88
const NinePointLinearOp & getMap() const
Definition: fdmcirop.cpp:94
FdmCIRMixedPart(const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< CoxIngersollRossProcess > &cirProcess, const ext::shared_ptr< GeneralizedBlackScholesProcess > &bsProcess, Real rho, Real strike)
Definition: fdmcirop.cpp:74
const ext::shared_ptr< FdmMesher > mesher_
Definition: fdmcirop.hpp:90
const NinePointLinearOp dyMap_
Definition: fdmcirop.hpp:88
const ext::shared_ptr< BlackVolTermStructure > sigma1_
Definition: fdmcirop.hpp:91
NinePointLinearOp mapT_
Definition: fdmcirop.hpp:89
Size size() const override
Definition: fdmcirop.cpp:125
Array apply_direction(Size direction, const Array &r) const override
Definition: fdmcirop.cpp:137
Array preconditioner(const Array &r, Real s) const override
Definition: fdmcirop.cpp:163
std::vector< SparseMatrix > toMatrixDecomp() const override
Definition: fdmcirop.cpp:167
FdmCIREquityPart dxMap_
Definition: fdmcirop.hpp:117
void setTime(Time t1, Time t2) override
Time is required.
Definition: fdmcirop.cpp:119
Array apply_mixed(const Array &r) const override
Definition: fdmcirop.cpp:147
FdmCIRMixedPart dzMap_
Definition: fdmcirop.hpp:119
Array solve_splitting(Size direction, const Array &r, Real s) const override
Definition: fdmcirop.cpp:151
Array apply(const Array &r) const override
Definition: fdmcirop.cpp:129
FdmCIROp(const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< CoxIngersollRossProcess > &cirProcess, const ext::shared_ptr< GeneralizedBlackScholesProcess > &bsProcess, Real rho, Real strike)
Definition: fdmcirop.cpp:98
FdmCIRRatesPart dyMap_
Definition: fdmcirop.hpp:118
void setTime(Time t1, Time t2)
Definition: fdmcirop.cpp:66
const TripleBandLinearOp & getMap() const
Definition: fdmcirop.cpp:70
const TripleBandLinearOp dyMap_
Definition: fdmcirop.hpp:71
TripleBandLinearOp mapT_
Definition: fdmcirop.hpp:72
const ext::shared_ptr< FdmMesher > mesher_
Definition: fdmcirop.hpp:73
FdmCIRRatesPart(const ext::shared_ptr< FdmMesher > &mesher, Real sigma, Real kappa, Real theta)
Definition: fdmcirop.cpp:55
SparseMatrix toMatrix() const override
Array apply(const Array &r) const override
NinePointLinearOp mult(const Array &u) const
void swap(NinePointLinearOp &m) noexcept
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
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
CIR linear operator.
memory layout of a fdm linear operator
mesher for a fdm grid
const ext::shared_ptr< FdmMesher > mesher_
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
Real kappa
Real theta
Real rho
Real sigma
Definition: any.hpp:35
ext::shared_ptr< YieldTermStructure > q
ext::shared_ptr< YieldTermStructure > r
ext::shared_ptr< BlackVolTermStructure > v
second derivative operator
second order mixed derivative linear operator