QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
hybridhestonhullwhiteprocess.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) 2007, 2008 Klaus Spanderen
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
20/*! \file hybridhestonhullwhiteprocess.hpp
21 \brief hybrid equity (heston model)
22 with stochastic interest rates (hull white model)
23*/
24
27
28namespace QuantLib {
29
31 const ext::shared_ptr<HestonProcess> & hestonProcess,
32 const ext::shared_ptr<HullWhiteForwardProcess> & hullWhiteProcess,
33 Real corrEquityShortRate,
35 : hestonProcess_(hestonProcess),
36 hullWhiteProcess_(hullWhiteProcess),
37 hullWhiteModel_(new HullWhite(hestonProcess->riskFreeRate(),
38 hullWhiteProcess->a(),
39 hullWhiteProcess->sigma())),
40 corrEquityShortRate_(corrEquityShortRate),
41 discretization_(discretization),
42 maxRho_(std::sqrt(1-hestonProcess->rho()*hestonProcess->rho())
43 - std::sqrt(QL_EPSILON) /* reserve for rounding errors */),
44
45 T_(hullWhiteProcess->getForwardMeasureTime()),
46 endDiscount_(hestonProcess->riskFreeRate()->discount(T_)) {
47
48 QL_REQUIRE( corrEquityShortRate*corrEquityShortRate
49 +hestonProcess->rho()*hestonProcess->rho() <= 1.0,
50 "correlation matrix is not positive definite");
51
52 QL_REQUIRE(hullWhiteProcess->sigma() > 0.0,
53 "positive vol of Hull White process is required");
54 }
55
57 return 3;
58 }
59
61 return {
62 hestonProcess_->s0()->value(),
63 hestonProcess_->v0(),
65 };
66 }
67
69 Array x0 = { x[0], x[1] };
70 Array y0 = hestonProcess_->drift(t, x0);
71
72 return {
73 y0[0],
74 y0[1],
75 hullWhiteProcess_->drift(t, x[2])
76 };
77 }
78
80 Array xt = { x0[0], x0[1] }, dxt = { dx[0], dx[1] };
81 Array yt = hestonProcess_->apply(xt, dxt);
82
83 return {
84 yt[0],
85 yt[1],
86 hullWhiteProcess_->apply(x0[2], dx[2])
87 };
88 }
89
91 Matrix retVal(3,3);
92
93 Array xt(2); xt[0] = x[0]; xt[1] = x[1];
94 Matrix m = hestonProcess_->diffusion(t, xt);
95 retVal[0][0] = m[0][0]; retVal[0][1] = 0.0; retVal[0][2] = 0.0;
96 retVal[1][0] = m[1][0]; retVal[1][1] = m[1][1]; retVal[1][2] = 0.0;
97
98 const Real sigma = hullWhiteProcess_->sigma();
99 retVal[2][0] = corrEquityShortRate_ * sigma;
100 retVal[2][1] = - retVal[2][0]*retVal[1][0] / retVal[1][1];
101 retVal[2][2] = std::sqrt( sigma*sigma - retVal[2][1]*retVal[2][1]
102 - retVal[2][0]*retVal[2][0] );
103
104 return retVal;
105 }
106
108 Time dt, const Array& dw) const {
109
110 const Rate r = x0[2];
111 const Real a = hullWhiteProcess_->a();
112 const Real sigma = hullWhiteProcess_->sigma();
114 const Real xi = hestonProcess_->rho();
115 const Volatility eta = (x0[1] > 0.0) ? Real(std::sqrt(x0[1])) : 0.0;
116 const Time s = t0;
117 const Time t = t0 + dt;
118 const Time T = T_;
119 const Rate dy
120 = hestonProcess_->dividendYield()->forwardRate(s, t, Continuous,
122
123 const Real df
124 = std::log( hestonProcess_->riskFreeRate()->discount(t)
125 / hestonProcess_->riskFreeRate()->discount(s));
126
127 const Real eaT=std::exp(-a*T);
128 const Real eat=std::exp(-a*t);
129 const Real eas=std::exp(-a*s);
130 const Real iat=1.0/eat;
131 const Real ias=1.0/eas;
132
133 const Real m1 = -(dy+0.5*eta*eta)*dt - df;
134
135 const Real m2 = -rho*sigma*eta/a*(dt-1/a*eaT*(iat-ias));
136
137 const Real m3 = (r - hullWhiteProcess_->alpha(s))
138 *hullWhiteProcess_->B(s,t);
139
140 const Real m4 = sigma*sigma/(2*a*a)
141 *(dt + 2/a*(eat-eas) - 1/(2*a)*(eat*eat-eas*eas));
142
143 const Real m5 = -sigma*sigma/(a*a)
144 *(dt - 1/a*(1-eat*ias) - 1/(2*a)*eaT*(iat-2*ias+eat*ias*ias));
145
146 const Real mu = m1 + m2 + m3 + m4 + m5;
147
148 Array retVal(3);
149
150 const Real eta2 = hestonProcess_->sigma() * eta;
151 const Real nu
152 = hestonProcess_->kappa()*(hestonProcess_->theta() - eta*eta);
153
154 retVal[1] = x0[1] + nu*dt + eta2*std::sqrt(dt)
155 *(xi*dw[0]+std::sqrt(1-xi*xi)*dw[1]);
156
158 const Real v1 = eta*eta*dt
159 + sigma*sigma/(a*a)*(dt - 2/a*(1 - eat*ias)
160 + 1/(2*a)*(1 - eat*eat*ias*ias))
161 + 2*sigma*eta/a*rho*(dt - 1/a*(1 - eat*ias));
162 const Real v2 = hullWhiteProcess_->variance(t0, r, dt);
163 const Real v12 = (1-eat*ias)*(sigma*eta/a*rho + sigma*sigma/(a*a))
164 - sigma*sigma/(2*a*a)*(1 - eat*eat*ias*ias);
165
166 QL_REQUIRE(v1 > 0.0 && v2 > 0.0, "zero or negative variance given");
167
168 // terminal rho must be between -maxRho and +maxRho
169 const Real rhoT
170 = std::min(maxRho_, std::max(-maxRho_, v12/std::sqrt(v1*v2)));
171 QL_REQUIRE( rhoT <= 1.0 && rhoT >= -1.0
172 && 1-rhoT*rhoT/(1-xi*xi) >= 0.0,
173 "invalid terminal correlation");
174
175 const Real dw_0 = dw[0];
176 const Real dw_2 = rhoT*dw[0]- rhoT*xi/std::sqrt(1-xi*xi)*dw[1]
177 + std::sqrt(1 - rhoT*rhoT/(1-xi*xi))*dw[2];
178
179 retVal[2] = hullWhiteProcess_->evolve(t0, r, dt, dw_2);
180
181 const Real vol = std::sqrt(v1)*dw_0;
182 retVal[0] = x0[0]*std::exp(mu + vol);
183 }
184 else if (discretization_ == Euler) {
185 const Real dw_2 = rho*dw[0]- rho*xi/std::sqrt(1-xi*xi)*dw[1]
186 + std::sqrt(1 - rho*rho/(1-xi*xi))*dw[2];
187
188 retVal[2] = hullWhiteProcess_->evolve(t0, r, dt, dw_2);
189
190 const Real vol = eta*std::sqrt(dt)*dw[0];
191 retVal[0] = x0[0]*std::exp(mu + vol);
192 }
193 else
194 QL_FAIL("unknown discretization scheme");
195
196 return retVal;
197 }
198
201
202 return hullWhiteModel_->discountBond(t, T_, x[2]) / endDiscount_;
203 }
204
207 }
208
209 const ext::shared_ptr<HestonProcess>&
211 return hestonProcess_;
212 }
213
214 const ext::shared_ptr<HullWhiteForwardProcess>&
216 return hullWhiteProcess_;
217 }
218
221 return discretization_;
222 }
223
225 return hestonProcess_->time(date);
226 }
227
229 endDiscount_ = hestonProcess_->riskFreeRate()->discount(T_);
230 }
231}
1-D array used in linear algebra.
Definition: array.hpp:52
Concrete date class.
Definition: date.hpp:125
Single-factor Hull-White (extended Vasicek) model class.
Definition: hullwhite.hpp:49
Array drift(Time t, const Array &x) const override
returns the drift part of the equation, i.e.,
Size size() const override
returns the number of dimensions of the stochastic process
Array evolve(Time t0, const Array &x0, Time dt, const Array &dw) const override
Matrix diffusion(Time t, const Array &x) const override
returns the diffusion part of the equation, i.e.
Time time(const Date &date) const override
const ext::shared_ptr< HullWhiteForwardProcess > & hullWhiteProcess() const
const ext::shared_ptr< HestonProcess > & hestonProcess() const
HybridHestonHullWhiteProcess(const ext::shared_ptr< HestonProcess > &hestonProcess, const ext::shared_ptr< HullWhiteForwardProcess > &hullWhiteProcess, Real corrEquityShortRate, Discretization discretization=BSMHullWhite)
Array initialValues() const override
returns the initial values of the state variables
const ext::shared_ptr< HullWhite > hullWhiteModel_
DiscountFactor numeraire(Time t, const Array &x) const
Array apply(const Array &x0, const Array &dx) const override
const ext::shared_ptr< HestonProcess > hestonProcess_
const ext::shared_ptr< HullWhiteForwardProcess > hullWhiteProcess_
Matrix used in linear algebra.
Definition: matrix.hpp:41
discretization of a stochastic process over a given time interval
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
flat forward rate term structure
@ NoFrequency
null frequency
Definition: frequency.hpp:37
#define QL_EPSILON
Definition: qldefines.hpp:178
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Real rho
Real sigma
hybrid equity (heston model) with stochastic interest rates (hull white model)
Definition: any.hpp:35
STL namespace.
ext::shared_ptr< YieldTermStructure > r
Real nu
Definition: sabr.cpp:200