QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
gjrgarchprocess.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Yee Man Chan
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#include <ql/math/distributions/chisquaredistribution.hpp>
21#include <ql/math/distributions/normaldistribution.hpp>
22#include <ql/processes/eulerdiscretization.hpp>
23#include <ql/processes/gjrgarchprocess.hpp>
24#include <ql/quotes/simplequote.hpp>
25#include <utility>
26
27namespace QuantLib {
28
30 Handle<YieldTermStructure> dividendYield,
32 Real v0,
33 Real omega,
34 Real alpha,
35 Real beta,
36 Real gamma,
37 Real lambda,
38 Real daysPerYear,
41 riskFreeRate_(std::move(riskFreeRate)), dividendYield_(std::move(dividendYield)),
42 s0_(std::move(s0)), v0_(v0), omega_(omega), alpha_(alpha), beta_(beta), gamma_(gamma),
43 lambda_(lambda), daysPerYear_(daysPerYear), discretization_(d) {
47 }
48
50 return 2;
51 }
52
54 return { s0_->value(), daysPerYear_*v0_ };
55 }
56
59 const Real n = std::exp(-lambda_*lambda_/2.0)/std::sqrt(2*M_PI);
60 const Real q2 = 1.0 + lambda_*lambda_;
61 const Real q3 = lambda_*n + N + lambda_*lambda_*N;
62 const Real vol = (x[1] > 0.0) ? std::sqrt(x[1])
63 : (discretization_ == Reflection) ? Real(-std::sqrt(-x[1]))
64 : 0.0;
65
66 return {
67 riskFreeRate_->forwardRate(t, t, Continuous).rate()
68 - dividendYield_->forwardRate(t, t, Continuous).rate()
69 - 0.5 * vol * vol,
71 + alpha_*q2 + gamma_*q3 - 1.0) *
72 ((discretization_==PartialTruncation) ? x[1] : vol*vol)
73 };
74 }
75
77 /* the correlation matrix is
78 | 1 rho |
79 | rho 1 |
80 whose square root (which is used here) is
81 | 1 0 |
82 | rho std::sqrt(1-rho^2) |
83 */
84 Matrix tmp(2,2);
86 const Real n = std::exp(-lambda_*lambda_/2.0)/std::sqrt(2*M_PI);
87 const Real sigma2 = 2.0 + 4.0*lambda_*lambda_;
88 const Real q3 = lambda_*n + N + lambda_*lambda_*N;
89 const Real Eml_e4 = lambda_*lambda_*lambda_*n + 5.0*lambda_*n
90 + 3.0*N + lambda_*lambda_*lambda_*lambda_*N
91 + 6.0*lambda_*lambda_*N;
92 const Real sigma3 = Eml_e4 - q3*q3;
93 const Real sigma12 = -2.0*lambda_;
94 const Real sigma13 = -2.0*n - 2*lambda_*N;
95 const Real sigma23 = 2.0*N + sigma12*sigma13;
96 const Real vol = (x[1] > 0.0) ? std::sqrt(x[1])
97 : (discretization_ == Reflection) ? Real(- std::sqrt(-x[1]))
98 : 1e-8; // set vol to (almost) zero but still
99 // expose some correlation information
100 const Real rho1 = std::sqrt(daysPerYear_)*(alpha_*sigma12
101 + gamma_*sigma13) * vol * vol;
102 const Real rho2 = vol*vol*std::sqrt(daysPerYear_)
103 *std::sqrt(alpha_*alpha_*(sigma2 - sigma12*sigma12)
104 + gamma_*gamma_*(sigma3 - sigma13*sigma13)
105 + 2.0*alpha_*gamma_*(sigma23 - sigma12*sigma13));
106
107 // tmp[0][0], tmp[0][1] are the coefficients of dW_1 and dW_2
108 // in asset return stochastic process
109 tmp[0][0] = vol; tmp[0][1] = 0.0;
110 tmp[1][0] = rho1; tmp[1][1] = rho2;
111 return tmp;
112 }
113
115 const Array& dx) const {
116 return { x0[0] * std::exp(dx[0]), x0[1] + dx[1] };
117 }
118
120 Time dt, const Array& dw) const {
121 Array retVal(2);
122 Real vol, mu, nu;
123
124 const Real sdt = std::sqrt(dt);
126 const Real n = std::exp(-lambda_*lambda_/2.0)/std::sqrt(2*M_PI);
127 const Real sigma2 = 2.0 + 4.0*lambda_*lambda_;
128 const Real q2 = 1.0 + lambda_*lambda_;
129 const Real q3 = lambda_*n + N + lambda_*lambda_*N;
130 const Real Eml_e4 = lambda_*lambda_*lambda_*n + 5.0*lambda_*n
131 + 3.0*N + lambda_*lambda_*lambda_*lambda_*N
132 + 6.0*lambda_*lambda_*N;
133 const Real sigma3 = Eml_e4 - q3*q3;
134 const Real sigma12 = -2.0*lambda_;
135 const Real sigma13 = -2.0*n - 2*lambda_*N;
136 const Real sigma23 = 2.0*N + sigma12*sigma13;
137 const Real rho1 = std::sqrt(daysPerYear_)*(alpha_*sigma12 + gamma_*sigma13);
138 const Real rho2 = std::sqrt(daysPerYear_)
139 *std::sqrt(alpha_*alpha_*(sigma2 - sigma12*sigma12)
140 + gamma_*gamma_*(sigma3 - sigma13*sigma13)
141 + 2.0*alpha_*gamma_*(sigma23 - sigma12*sigma13));
142
143 switch (discretization_) {
144 // For the definition of PartialTruncation, FullTruncation
145 // and Reflection see Lord, R., R. Koekkoek and D. van Dijk (2006),
146 // "A Comparison of biased simulation schemes for
147 // stochastic volatility models",
148 // Working Paper, Tinbergen Institute
150 vol = (x0[1] > 0.0) ? Real(std::sqrt(x0[1])) : 0.0;
151 mu = riskFreeRate_->forwardRate(t0, t0+dt, Continuous).rate()
152 - dividendYield_->forwardRate(t0, t0+dt, Continuous).rate()
153 - 0.5 * vol * vol;
155 + daysPerYear_*(beta_ + alpha_*q2 + gamma_*q3 - 1.0) * x0[1];
156
157 retVal[0] = x0[0] * std::exp(mu*dt+vol*dw[0]*sdt);
158 retVal[1] = x0[1] + nu*dt + sdt*vol*vol*(rho1*dw[0] + rho2*dw[1]);
159 break;
160 case FullTruncation:
161 vol = (x0[1] > 0.0) ? Real(std::sqrt(x0[1])) : 0.0;
162 mu = riskFreeRate_->forwardRate(t0, t0+dt, Continuous).rate()
163 - dividendYield_->forwardRate(t0, t0+dt, Continuous).rate()
164 - 0.5 * vol * vol;
166 + daysPerYear_*(beta_ + alpha_*q2 + gamma_*q3 - 1.0) * vol *vol;
167
168 retVal[0] = x0[0] * std::exp(mu*dt+vol*dw[0]*sdt);
169 retVal[1] = x0[1] + nu*dt + sdt*vol*vol*(rho1*dw[0] + rho2*dw[1]);
170 break;
171 case Reflection:
172 vol = std::sqrt(std::fabs(x0[1]));
173 mu = riskFreeRate_->forwardRate(t0, t0+dt, Continuous).rate()
174 - dividendYield_->forwardRate(t0, t0+dt, Continuous).rate()
175 - 0.5 * vol*vol;
177 + daysPerYear_*(beta_ + alpha_*q2 + gamma_*q3 - 1.0) * vol * vol;
178
179 retVal[0] = x0[0]*std::exp(mu*dt+vol*dw[0]*sdt);
180 retVal[1] = vol*vol
181 +nu*dt + sdt*vol*vol*(rho1*dw[0] + rho2*dw[1]);
182 break;
183 default:
184 QL_FAIL("unknown discretization schema");
185 }
186
187 return retVal;
188 }
189
191 return s0_;
192 }
193
195 return dividendYield_;
196 }
197
199 return riskFreeRate_;
200 }
201
203 return riskFreeRate_->dayCounter().yearFraction(
204 riskFreeRate_->referenceDate(), d);
205 }
206
207}
1-D array used in linear algebra.
Definition: array.hpp:52
Cumulative normal distribution function.
Concrete date class.
Definition: date.hpp:125
Euler discretization for stochastic processes.
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.
const Handle< YieldTermStructure > & dividendYield() const
Time time(const Date &) const override
GJRGARCHProcess(Handle< YieldTermStructure > riskFreeRate, Handle< YieldTermStructure > dividendYield, Handle< Quote > s0, Real v0, Real omega, Real alpha, Real beta, Real gamma, Real lambda, Real daysPerYear=252.0, Discretization d=FullTruncation)
Array initialValues() const override
returns the initial values of the state variables
Handle< YieldTermStructure > dividendYield_
Array apply(const Array &x0, const Array &dx) const override
const Handle< Quote > & s0() const
const Handle< YieldTermStructure > & riskFreeRate() const
Handle< YieldTermStructure > riskFreeRate_
Shared handle to an observable.
Definition: handle.hpp:41
Matrix used in linear algebra.
Definition: matrix.hpp:41
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
discretization of a stochastic process over a given time interval
multi-dimensional stochastic process class.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.