QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
garch.hpp
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) 2006 Joseph Wang
5 Copyright (C) 2012 Liquidnet Holdings, Inc.
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file garch.hpp
22 \brief GARCH volatility model
23*/
24
25#ifndef quantlib_garch_volatility_model_hpp
26#define quantlib_garch_volatility_model_hpp
27
31#include <vector>
32
33namespace QuantLib {
34
35 //! GARCH volatility model
36 /*! Volatilities are assumed to be expressed on an annual basis.
37 */
39 public:
41
42 enum Mode {
43 MomentMatchingGuess, /*!< The initial guess is a moment
44 matching estimates for
45 mean(r2), acf(0), and acf(1). */
46 GammaGuess, /*!< The initial guess is an
47 estimate of gamma based on the
48 property:
49 acf(i+1) = gamma*acf(i) for i > 1. */
50 BestOfTwo, /*!< The best of the two above modes */
51 DoubleOptimization /*!< Double optimization */
52 };
53
54 //! \name Constructors
55 //@{
57 : alpha_(a), beta_(b), gamma_(1 - a - b),
59
61 : alpha_(0), beta_(0), vl_(0), logLikelihood_(0), mode_(mode) {
62 calibrate(qs);
63 };
64 //@}
65
66 //! \name Inspectors
67 //@{
68 Real alpha() const { return alpha_; }
69 Real beta() const { return beta_; }
70 Real omega() const { return vl_ * gamma_; }
71 Real ltVol() const { return vl_; }
72 Real logLikelihood() const { return logLikelihood_; }
73 Mode mode() const { return mode_; }
74 //@}
75
76 //! \name VolatilityCompositor interface
77 //@{
78 time_series calculate(const time_series& quoteSeries) override {
79 return calculate(quoteSeries, alpha(), beta(), omega());
80 }
81 void calibrate(const time_series& quoteSeries) override {
82 const auto values = quoteSeries.values();
83 calibrate(values.cbegin(), values.cend());
84 }
85 //@}
86
87 //! \name Additional interface
88 //@{
89 static time_series calculate(const time_series& quoteSeries,
91
92 void calibrate(const time_series& quoteSeries,
93 OptimizationMethod& method,
94 const EndCriteria& endCriteria) {
95 const auto values = quoteSeries.values();
96 calibrate(values.cbegin(), values.cend(),
97 method, endCriteria);
98 }
99
100 void calibrate(const time_series& quoteSeries,
101 OptimizationMethod& method,
102 const EndCriteria& endCriteria,
103 const Array& initialGuess) {
104 const auto values = quoteSeries.values();
105 calibrate(values.cbegin(), values.cend(),
106 method, endCriteria, initialGuess);
107 }
108
109 template <typename ForwardIterator>
110 void calibrate(ForwardIterator begin, ForwardIterator end) {
111 std::vector<Volatility> r2;
112 Real mean_r2 = to_r2(begin, end, r2);
113 ext::shared_ptr<Problem> p =
114 calibrate_r2(mode_, r2, mean_r2, alpha_, beta_, vl_);
115 gamma_ = 1 - alpha_ - beta_;
116 vl_ /= gamma_;
117 logLikelihood_ = p ? -p->functionValue() :
118 -costFunction(begin, end);
119 }
120
121 template <typename ForwardIterator>
122 void calibrate(ForwardIterator begin, ForwardIterator end,
123 OptimizationMethod& method,
124 EndCriteria endCriteria) {
125 std::vector<Volatility> r2;
126 Real mean_r2 = to_r2(begin, end, r2);
127 ext::shared_ptr<Problem> p =
128 calibrate_r2(mode_, r2, mean_r2, method,
129 endCriteria, alpha_, beta_, vl_);
130 gamma_ = 1 - alpha_ - beta_;
131 vl_ /= gamma_;
132 logLikelihood_ = p ? -p->functionValue() :
133 -costFunction(begin, end);
134 }
135
136 template <typename ForwardIterator>
137 void calibrate(ForwardIterator begin, ForwardIterator end,
138 OptimizationMethod& method,
139 EndCriteria endCriteria,
140 const Array& initialGuess) {
141 std::vector<Volatility> r2;
142 to_r2(begin, end, r2);
143 ext::shared_ptr<Problem> p =
144 calibrate_r2(r2, method, endCriteria, initialGuess,
145 alpha_, beta_, vl_);
146 gamma_ = 1 - alpha_ - beta_;
147 vl_ /= gamma_;
148 logLikelihood_ = p ? -p->functionValue() :
149 -costFunction(begin, end);
150 }
151
152 Real forecast(Real r, Real sigma2) const {
153 return gamma_* vl_ + alpha_ * r * r + beta_ * sigma2;
154 }
155
156 // a helper for calculation of r^2 and <r^2>
157 template <typename InputIterator>
158 static Real to_r2(InputIterator begin, InputIterator end,
159 std::vector<Volatility>& r2) {
160 Real u2(0.0), mean_r2(0.0), w(1.0);
161 for (; begin != end; ++begin) {
162 u2 = *begin; u2 *= u2;
163 mean_r2 = (1.0 - w) * mean_r2 + w * u2;
164 r2.push_back(u2);
165 w /= (w + 1.0);
166 }
167 return mean_r2;
168 }
169
170 /*! calibrates GARCH for r^2 */
171 static ext::shared_ptr<Problem> calibrate_r2(
172 Mode mode,
173 const std::vector<Volatility>& r2,
174 Real mean_r2,
175 Real& alpha,
176 Real& beta,
177 Real& omega);
178
179 /*! calibrates GARCH for r^2 with user-defined optimization
180 method and end criteria */
181 static ext::shared_ptr<Problem> calibrate_r2(
182 Mode mode,
183 const std::vector<Volatility>& r2,
184 Real mean_r2,
185 OptimizationMethod& method,
186 const EndCriteria& endCriteria,
187 Real& alpha,
188 Real& beta,
189 Real& omega);
190
191 /*! calibrates GARCH for r^2 with user-defined optimization
192 method, end criteria and initial guess */
193 static ext::shared_ptr<Problem> calibrate_r2(
194 const std::vector<Volatility>& r2,
195 Real mean_r2,
196 OptimizationMethod& method,
197 const EndCriteria& endCriteria,
198 const Array& initialGuess,
199 Real& alpha,
200 Real& beta,
201 Real& omega);
202
203 /*! calibrates GARCH for r^2 with user-defined optimization
204 method, end criteria and initial guess */
205 static ext::shared_ptr<Problem> calibrate_r2(
206 const std::vector<Volatility> &r2,
207 OptimizationMethod& method,
208 const EndCriteria& endCriteria,
209 const Array& initialGuess,
210 Real& alpha,
211 Real& beta,
212 Real& omega);
213
214 /*! calibrates GARCH for r^2 with user-defined optimization
215 method, end criteria, constraints and initial guess */
216 static ext::shared_ptr<Problem> calibrate_r2(
217 const std::vector<Volatility>& r2,
218 Real mean_r2,
219 OptimizationMethod& method,
220 Constraint& constraints,
221 const EndCriteria& endCriteria,
222 const Array& initialGuess,
223 Real& alpha,
224 Real& beta,
225 Real& omega);
226
227 static ext::shared_ptr<Problem> calibrate_r2(
228 const std::vector<Volatility> &r2,
229 OptimizationMethod& method,
230 Constraint& constraints,
231 const EndCriteria& endCriteria,
232 const Array& initialGuess,
233 Real& alpha,
234 Real& beta,
235 Real& omega);
236
237 template<class InputIterator>
238 static Real costFunction(InputIterator begin, InputIterator end,
240 Real retval(0.0);
241 Real u2(0.0), sigma2(0.0);
242 Size N = 0;
243 for (; begin != end; ++begin, ++N) {
244 sigma2 = omega + alpha * u2 + beta * sigma2;
245 u2 = *begin; u2 *= u2;
246 retval += std::log(sigma2) + u2 / sigma2;
247 }
248 return N > 0 ? Real(retval / (2*N)) : 0.0;
249 }
250 //@}
251 private:
255
256 template<class InputIterator>
257 Real costFunction(InputIterator begin, InputIterator end) const {
258 return costFunction(begin, end, alpha(), beta(), omega());
259 }
260 };
261
262}
263
264
265#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Base constraint class.
Definition: constraint.hpp:35
Criteria to end optimization process:
Definition: endcriteria.hpp:40
GARCH volatility model.
Definition: garch.hpp:38
Real omega() const
Definition: garch.hpp:70
Real costFunction(InputIterator begin, InputIterator end) const
Definition: garch.hpp:257
Garch11(const time_series &qs, Mode mode=BestOfTwo)
Definition: garch.hpp:60
time_series calculate(const time_series &quoteSeries) override
Definition: garch.hpp:78
Real ltVol() const
Definition: garch.hpp:71
static ext::shared_ptr< Problem > calibrate_r2(Mode mode, const std::vector< Volatility > &r2, Real mean_r2, Real &alpha, Real &beta, Real &omega)
Definition: garch.cpp:393
Garch11(Real a, Real b, Real vl)
Definition: garch.hpp:56
void calibrate(ForwardIterator begin, ForwardIterator end, OptimizationMethod &method, EndCriteria endCriteria)
Definition: garch.hpp:122
void calibrate(const time_series &quoteSeries, OptimizationMethod &method, const EndCriteria &endCriteria, const Array &initialGuess)
Definition: garch.hpp:100
Real forecast(Real r, Real sigma2) const
Definition: garch.hpp:152
Mode mode() const
Definition: garch.hpp:73
static Real to_r2(InputIterator begin, InputIterator end, std::vector< Volatility > &r2)
Definition: garch.hpp:158
void calibrate(const time_series &quoteSeries, OptimizationMethod &method, const EndCriteria &endCriteria)
Definition: garch.hpp:92
static Real costFunction(InputIterator begin, InputIterator end, Real alpha, Real beta, Real omega)
Definition: garch.hpp:238
Real logLikelihood() const
Definition: garch.hpp:72
TimeSeries< Volatility > time_series
Definition: garch.hpp:40
Real beta() const
Definition: garch.hpp:69
Real alpha() const
Definition: garch.hpp:68
void calibrate(ForwardIterator begin, ForwardIterator end)
Definition: garch.hpp:110
Real logLikelihood_
Definition: garch.hpp:253
void calibrate(ForwardIterator begin, ForwardIterator end, OptimizationMethod &method, EndCriteria endCriteria, const Array &initialGuess)
Definition: garch.hpp:137
void calibrate(const time_series &quoteSeries) override
Definition: garch.hpp:81
Abstract class for constrained optimization method.
Definition: method.hpp:36
Container for historical data.
Definition: timeseries.hpp:51
std::vector< T > values() const
returns the historical data
Definition: timeseries.hpp:254
Abstract constraint class.
std::function< Real(Real)> b
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:37
ext::shared_ptr< YieldTermStructure > r
Abstract optimization problem class.
Volatility term structures.