QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
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 /*! \deprecated Use auto or time_series::const_iterator instead.
43 Deprecated in version 1.31.
44 */
45 [[deprecated("If needed, use time_series::const_iterator instead.")]]
47
49
50 /*! \deprecated If needed, use auto or time_series::const_value_iterator.
51 Prefer time_series::const_iterator instead.
52 Deprecated in version 1.31.
53 */
54 [[deprecated("If needed, use auto or time_series::const_value_iterator. Prefer time_series::const_iterator instead.")]]
56
58
59 enum Mode {
60 MomentMatchingGuess, /*!< The initial guess is a moment
61 matching estimates for
62 mean(r2), acf(0), and acf(1). */
63 GammaGuess, /*!< The initial guess is an
64 estimate of gamma based on the
65 property:
66 acf(i+1) = gamma*acf(i) for i > 1. */
67 BestOfTwo, /*!< The best of the two above modes */
68 DoubleOptimization /*!< Double optimization */
69 };
70
71 //! \name Constructors
72 //@{
74 : alpha_(a), beta_(b), gamma_(1 - a - b),
76
78 : alpha_(0), beta_(0), vl_(0), logLikelihood_(0), mode_(mode) {
79 calibrate(qs);
80 };
81 //@}
82
83 //! \name Inspectors
84 //@{
85 Real alpha() const { return alpha_; }
86 Real beta() const { return beta_; }
87 Real omega() const { return vl_ * gamma_; }
88 Real ltVol() const { return vl_; }
89 Real logLikelihood() const { return logLikelihood_; }
90 Mode mode() const { return mode_; }
91 //@}
92
93 //! \name VolatilityCompositor interface
94 //@{
95 time_series calculate(const time_series& quoteSeries) override {
96 return calculate(quoteSeries, alpha(), beta(), omega());
97 }
98 void calibrate(const time_series& quoteSeries) override {
99 const auto values = quoteSeries.values();
100 calibrate(values.cbegin(), values.cend());
101 }
102 //@}
103
104 //! \name Additional interface
105 //@{
106 static time_series calculate(const time_series& quoteSeries,
108
109 void calibrate(const time_series& quoteSeries,
110 OptimizationMethod& method,
111 const EndCriteria& endCriteria) {
112 const auto values = quoteSeries.values();
113 calibrate(values.cbegin(), values.cend(),
114 method, endCriteria);
115 }
116
117 void calibrate(const time_series& quoteSeries,
118 OptimizationMethod& method,
119 const EndCriteria& endCriteria,
120 const Array& initialGuess) {
121 const auto values = quoteSeries.values();
122 calibrate(values.cbegin(), values.cend(),
123 method, endCriteria, initialGuess);
124 }
125
126 template <typename ForwardIterator>
127 void calibrate(ForwardIterator begin, ForwardIterator end) {
128 std::vector<Volatility> r2;
129 Real mean_r2 = to_r2(begin, end, r2);
130 ext::shared_ptr<Problem> p =
131 calibrate_r2(mode_, r2, mean_r2, alpha_, beta_, vl_);
132 gamma_ = 1 - alpha_ - beta_;
133 vl_ /= gamma_;
134 logLikelihood_ = p ? -p->functionValue() :
135 -costFunction(begin, end);
136 }
137
138 template <typename ForwardIterator>
139 void calibrate(ForwardIterator begin, ForwardIterator end,
140 OptimizationMethod& method,
141 EndCriteria endCriteria) {
142 std::vector<Volatility> r2;
143 Real mean_r2 = to_r2(begin, end, r2);
144 ext::shared_ptr<Problem> p =
145 calibrate_r2(mode_, r2, mean_r2, method,
146 endCriteria, alpha_, beta_, vl_);
147 gamma_ = 1 - alpha_ - beta_;
148 vl_ /= gamma_;
149 logLikelihood_ = p ? -p->functionValue() :
150 -costFunction(begin, end);
151 }
152
153 template <typename ForwardIterator>
154 void calibrate(ForwardIterator begin, ForwardIterator end,
155 OptimizationMethod& method,
156 EndCriteria endCriteria,
157 const Array& initialGuess) {
158 std::vector<Volatility> r2;
159 to_r2(begin, end, r2);
160 ext::shared_ptr<Problem> p =
161 calibrate_r2(r2, method, endCriteria, initialGuess,
162 alpha_, beta_, vl_);
163 gamma_ = 1 - alpha_ - beta_;
164 vl_ /= gamma_;
165 logLikelihood_ = p ? -p->functionValue() :
166 -costFunction(begin, end);
167 }
168
169 Real forecast(Real r, Real sigma2) const {
170 return gamma_* vl_ + alpha_ * r * r + beta_ * sigma2;
171 }
172
173 // a helper for calculation of r^2 and <r^2>
174 template <typename InputIterator>
175 static Real to_r2(InputIterator begin, InputIterator end,
176 std::vector<Volatility>& r2) {
177 Real u2(0.0), mean_r2(0.0), w(1.0);
178 for (; begin != end; ++begin) {
179 u2 = *begin; u2 *= u2;
180 mean_r2 = (1.0 - w) * mean_r2 + w * u2;
181 r2.push_back(u2);
182 w /= (w + 1.0);
183 }
184 return mean_r2;
185 }
186
187 /*! calibrates GARCH for r^2 */
188 static ext::shared_ptr<Problem> calibrate_r2(
189 Mode mode,
190 const std::vector<Volatility>& r2,
191 Real mean_r2,
192 Real& alpha,
193 Real& beta,
194 Real& omega);
195
196 /*! calibrates GARCH for r^2 with user-defined optimization
197 method and end criteria */
198 static ext::shared_ptr<Problem> calibrate_r2(
199 Mode mode,
200 const std::vector<Volatility>& r2,
201 Real mean_r2,
202 OptimizationMethod& method,
203 const EndCriteria& endCriteria,
204 Real& alpha,
205 Real& beta,
206 Real& omega);
207
208 /*! calibrates GARCH for r^2 with user-defined optimization
209 method, end criteria and initial guess */
210 static ext::shared_ptr<Problem> calibrate_r2(
211 const std::vector<Volatility>& r2,
212 Real mean_r2,
213 OptimizationMethod& method,
214 const EndCriteria& endCriteria,
215 const Array& initialGuess,
216 Real& alpha,
217 Real& beta,
218 Real& omega);
219
220 /*! calibrates GARCH for r^2 with user-defined optimization
221 method, end criteria and initial guess */
222 static ext::shared_ptr<Problem> calibrate_r2(
223 const std::vector<Volatility> &r2,
224 OptimizationMethod& method,
225 const EndCriteria& endCriteria,
226 const Array& initialGuess,
227 Real& alpha,
228 Real& beta,
229 Real& omega);
230
231 /*! calibrates GARCH for r^2 with user-defined optimization
232 method, end criteria, constraints and initial guess */
233 static ext::shared_ptr<Problem> calibrate_r2(
234 const std::vector<Volatility>& r2,
235 Real mean_r2,
236 OptimizationMethod& method,
237 Constraint& constraints,
238 const EndCriteria& endCriteria,
239 const Array& initialGuess,
240 Real& alpha,
241 Real& beta,
242 Real& omega);
243
244 static ext::shared_ptr<Problem> calibrate_r2(
245 const std::vector<Volatility> &r2,
246 OptimizationMethod& method,
247 Constraint& constraints,
248 const EndCriteria& endCriteria,
249 const Array& initialGuess,
250 Real& alpha,
251 Real& beta,
252 Real& omega);
253
254 template<class InputIterator>
255 static Real costFunction(InputIterator begin, InputIterator end,
257 Real retval(0.0);
258 Real u2(0.0), sigma2(0.0);
259 Size N = 0;
260 for (; begin != end; ++begin, ++N) {
261 sigma2 = omega + alpha * u2 + beta * sigma2;
262 u2 = *begin; u2 *= u2;
263 retval += std::log(sigma2) + u2 / sigma2;
264 }
265 return N > 0 ? Real(retval / (2*N)) : 0.0;
266 }
267 //@}
268 private:
272
273 template<class InputIterator>
274 Real costFunction(InputIterator begin, InputIterator end) const {
275 return costFunction(begin, end, alpha(), beta(), omega());
276 }
277 };
278
279}
280
281
282#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:87
Real costFunction(InputIterator begin, InputIterator end) const
Definition: garch.hpp:274
Garch11(const time_series &qs, Mode mode=BestOfTwo)
Definition: garch.hpp:77
time_series calculate(const time_series &quoteSeries) override
Definition: garch.hpp:95
Real ltVol() const
Definition: garch.hpp:88
time_series::const_iterator const_iterator
Definition: garch.hpp:46
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:73
void calibrate(ForwardIterator begin, ForwardIterator end, OptimizationMethod &method, EndCriteria endCriteria)
Definition: garch.hpp:139
void calibrate(const time_series &quoteSeries, OptimizationMethod &method, const EndCriteria &endCriteria, const Array &initialGuess)
Definition: garch.hpp:117
Real forecast(Real r, Real sigma2) const
Definition: garch.hpp:169
Mode mode() const
Definition: garch.hpp:90
QL_DEPRECATED_DISABLE_WARNING typedef time_series::const_value_iterator const_value_iterator
Definition: garch.hpp:55
static Real to_r2(InputIterator begin, InputIterator end, std::vector< Volatility > &r2)
Definition: garch.hpp:175
void calibrate(const time_series &quoteSeries, OptimizationMethod &method, const EndCriteria &endCriteria)
Definition: garch.hpp:109
static Real costFunction(InputIterator begin, InputIterator end, Real alpha, Real beta, Real omega)
Definition: garch.hpp:255
Real logLikelihood() const
Definition: garch.hpp:89
TimeSeries< Volatility > time_series
Definition: garch.hpp:40
Real beta() const
Definition: garch.hpp:86
Real alpha() const
Definition: garch.hpp:85
void calibrate(ForwardIterator begin, ForwardIterator end)
Definition: garch.hpp:127
Real logLikelihood_
Definition: garch.hpp:270
void calibrate(ForwardIterator begin, ForwardIterator end, OptimizationMethod &method, EndCriteria endCriteria, const Array &initialGuess)
Definition: garch.hpp:154
void calibrate(const time_series &quoteSeries) override
Definition: garch.hpp:98
Abstract class for constrained optimization method.
Definition: method.hpp:36
Container for historical data.
Definition: timeseries.hpp:51
Container::const_iterator const_iterator
Definition: timeseries.hpp:110
std::vector< T > values() const
returns the historical data
Definition: timeseries.hpp:349
boost::transform_iterator< projection_value, const_iterator > const_value_iterator
Definition: timeseries.hpp:191
Abstract constraint class.
ext::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:35
ext::shared_ptr< YieldTermStructure > r
Abstract optimization problem class.
#define QL_DEPRECATED_DISABLE_WARNING
Definition: qldefines.hpp:216
#define QL_DEPRECATED_ENABLE_WARNING
Definition: qldefines.hpp:217
Volatility term structures.