QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmhestonhullwhiteop.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 Ralph Schreyer
6 Copyright (C) 2008, 2011 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/methods/finitedifferences/meshers/fdmmesher.hpp>
23#include <ql/methods/finitedifferences/operators/fdmhestonhullwhiteop.hpp>
24#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
25#include <ql/methods/finitedifferences/operators/secondderivativeop.hpp>
26#include <ql/methods/finitedifferences/operators/secondordermixedderivativeop.hpp>
27#include <ql/models/shortrate/onefactormodels/hullwhite.hpp>
28#include <utility>
29
30namespace QuantLib {
31
33 const ext::shared_ptr<FdmMesher>& mesher,
34 ext::shared_ptr<HullWhite> hwModel,
35 ext::shared_ptr<YieldTermStructure> qTS)
36 : x_(mesher->locations(2)), varianceValues_(0.5 * mesher->locations(1)),
37 dxMap_(FirstDerivativeOp(0, mesher)),
38 dxxMap_(SecondDerivativeOp(0, mesher).mult(0.5 * mesher->locations(1))), mapT_(0, mesher),
39 hwModel_(std::move(hwModel)), mesher_(mesher), qTS_(std::move(qTS)) {
40
41 // on the boundary s_min and s_max the second derivative
42 // d²V/dS² is zero and due to Ito's Lemma the variance term
43 // in the drift should vanish.
44 for (const auto& iter : *mesher_->layout()) {
45 if ( iter.coordinates()[0] == 0
46 || iter.coordinates()[0] == mesher_->layout()->dim()[0]-1) {
47 varianceValues_[iter.index()] = 0.0;
48 }
49 }
51 }
52
54 const ext::shared_ptr<OneFactorModel::ShortRateDynamics> dynamics =
55 hwModel_->dynamics();
56
57 const Real phi = 0.5*( dynamics->shortRate(t1, 0.0)
58 + dynamics->shortRate(t2, 0.0));
59
60 const Rate q = qTS_->forwardRate(t1, t2, Continuous).rate();
61
63 }
64
66 return mapT_;
67 }
68
69 FdmHestonHullWhiteOp::FdmHestonHullWhiteOp(const ext::shared_ptr<FdmMesher>& mesher,
70 const ext::shared_ptr<HestonProcess>& hestonProcess,
71 const ext::shared_ptr<HullWhiteProcess>& hwProcess,
72 Real equityShortRateCorrelation)
73 : v0_(hestonProcess->v0()), kappa_(hestonProcess->kappa()), theta_(hestonProcess->theta()),
74 sigma_(hestonProcess->sigma()), rho_(hestonProcess->rho()),
75 hwModel_(ext::make_shared<HullWhite>(
76 hestonProcess->riskFreeRate(), hwProcess->a(), hwProcess->sigma())),
77 hestonCorrMap_(
78 SecondOrderMixedDerivativeOp(0, 1, mesher).mult(rho_ * sigma_ * mesher->locations(1))),
79 equityIrCorrMap_(
81 .mult(Sqrt(mesher->locations(1)) * hwProcess->sigma() * equityShortRateCorrelation)),
82 dyMap_(SecondDerivativeOp(1U, mesher)
83 .mult(0.5 * sigma_ * sigma_ * mesher->locations(1))
84 .add(FirstDerivativeOp(1, mesher).mult(kappa_ * (theta_ - mesher->locations(1))))),
85 dxMap_(mesher, hwModel_, hestonProcess->dividendYield().currentLink()),
86 hullWhiteOp_(mesher, hwModel_, 2) {
87
88 QL_REQUIRE( equityShortRateCorrelation*equityShortRateCorrelation
89 + hestonProcess->rho()*hestonProcess->rho() <= 1.0,
90 "correlation matrix has negative eigenvalues");
91 }
92
94 dxMap_.setTime(t1, t2);
95 hullWhiteOp_.setTime(t1, t2);
96 }
97
99 return 3;
100 }
101
103 return dyMap_.apply(u) + dxMap_.getMap().apply(u)
106 }
107
109 const Array& r) const {
110 if (direction == 0)
111 return dxMap_.getMap().apply(r);
112 else if (direction == 1)
113 return dyMap_.apply(r);
114 else if (direction == 2)
115 return hullWhiteOp_.apply(r);
116 else
117 QL_FAIL("direction too large");
118 }
119
122 }
123
125 Real a) const {
126 if (direction == 0) {
127 return dxMap_.getMap().solve_splitting(r, a, 1.0);
128 }
129 else if (direction == 1) {
130 return dyMap_.solve_splitting(r, a, 1.0);
131 }
132 else if (direction == 2) {
133 return hullWhiteOp_.solve_splitting(2, r, a);
134 }
135 else
136 QL_FAIL("direction too large");
137 }
138
140 Real dt) const {
141 return solve_splitting(0, r, dt);
142 }
143
144 std::vector<SparseMatrix> FdmHestonHullWhiteOp::toMatrixDecomp() const {
145 return {
150 };
151 }
152
153}
1-D array used in linear algebra.
Definition: array.hpp:52
const ext::shared_ptr< HullWhite > hwModel_
const TripleBandLinearOp & getMap() const
const ext::shared_ptr< YieldTermStructure > qTS_
const ext::shared_ptr< FdmMesher > mesher_
FdmHestonHullWhiteEquityPart(const ext::shared_ptr< FdmMesher > &mesher, ext::shared_ptr< HullWhite > hwModel, ext::shared_ptr< YieldTermStructure > qTS)
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
void setTime(Time t1, Time t2) override
Time is required.
Array apply_mixed(const Array &r) const override
Array solve_splitting(Size direction, const Array &r, Real s) const override
FdmHestonHullWhiteEquityPart dxMap_
FdmHestonHullWhiteOp(const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< HestonProcess > &hestonProcess, const ext::shared_ptr< HullWhiteProcess > &hwProcess, Real equityShortRateCorrelation)
Array apply(const Array &r) const override
std::vector< SparseMatrix > toMatrixDecomp() const override
void setTime(Time t1, Time t2) override
Time is required.
Array solve_splitting(Size direction, const Array &r, Real s) const override
Array apply(const Array &r) const override
Single-factor Hull-White (extended Vasicek) model class.
Definition: hullwhite.hpp:49
SparseMatrix toMatrix() const override
Array apply(const Array &r) const override
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)
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
Array Sqrt(const Array &v)
Definition: array.hpp:847
STL namespace.