/
g2.hpp
186 lines (151 loc) · 6.25 KB
/
g2.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
Copyright (C) 2004 Mike Parker
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file g2.hpp
\brief Two-factor additive Gaussian Model G2++
*/
#ifndef quantlib_two_factor_models_g2_h
#define quantlib_two_factor_models_g2_h
#include <ql/instruments/swaption.hpp>
#include <ql/models/shortrate/twofactormodel.hpp>
#include <ql/processes/ornsteinuhlenbeckprocess.hpp>
#include <utility>
namespace QuantLib {
//! Two-additive-factor gaussian model class.
/*! This class implements a two-additive-factor model defined by
\f[
dr_t = \varphi(t) + x_t + y_t
\f]
where \f$ x_t \f$ and \f$ y_t \f$ are defined by
\f[
dx_t = -a x_t dt + \sigma dW^1_t, x_0 = 0
\f]
\f[
dy_t = -b y_t dt + \sigma dW^2_t, y_0 = 0
\f]
and \f$ dW^1_t dW^2_t = \rho dt \f$.
\bug This class was not tested enough to guarantee
its functionality.
\ingroup shortrate
*/
class G2 : public TwoFactorModel,
public AffineModel,
public TermStructureConsistentModel {
public:
G2(const Handle<YieldTermStructure>& termStructure,
Real a = 0.1,
Real sigma = 0.01,
Real b = 0.1,
Real eta = 0.01,
Real rho = -0.75);
ext::shared_ptr<ShortRateDynamics> dynamics() const override;
Real discountBond(Time now, Time maturity, Array factors) const override {
QL_REQUIRE(factors.size()>1,
"g2 model needs two factors to compute discount bond");
return discountBond(now, maturity, factors[0], factors[1]);
}
Real discountBond(Time, Time, Rate, Rate) const;
Real discountBondOption(Option::Type type,
Real strike,
Time maturity,
Time bondMaturity) const override;
Real swaption(const Swaption::arguments& arguments,
Rate fixedRate,
Real range,
Size intervals) const;
DiscountFactor discount(Time t) const override { return termStructure()->discount(t); }
Real a() const { return a_(0.0); }
Real sigma() const { return sigma_(0.0); }
Real b() const { return b_(0.0); }
Real eta() const { return eta_(0.0); }
Real rho() const { return rho_(0.0); }
protected:
void generateArguments() override;
Real A(Time t, Time T) const;
Real B(Real x, Time t) const;
private:
class Dynamics;
class FittingParameter;
Real sigmaP(Time t, Time s) const;
Parameter& a_;
Parameter& sigma_;
Parameter& b_;
Parameter& eta_;
Parameter& rho_;
Parameter phi_;
Real V(Time t) const;
class SwaptionPricingFunction;
};
class G2::Dynamics : public TwoFactorModel::ShortRateDynamics {
public:
Dynamics(Parameter fitting, Real a, Real sigma, Real b, Real eta, Real rho)
: ShortRateDynamics(
ext::shared_ptr<StochasticProcess1D>(new OrnsteinUhlenbeckProcess(a, sigma)),
ext::shared_ptr<StochasticProcess1D>(new OrnsteinUhlenbeckProcess(b, eta)),
rho),
fitting_(std::move(fitting)) {}
Rate shortRate(Time t, Real x, Real y) const override { return fitting_(t) + x + y; }
private:
Parameter fitting_;
};
//! Analytical term-structure fitting parameter \f$ \varphi(t) \f$.
/*! \f$ \varphi(t) \f$ is analytically defined by
\f[
\varphi(t) = f(t) +
\frac{1}{2}(\frac{\sigma(1-e^{-at})}{a})^2 +
\frac{1}{2}(\frac{\eta(1-e^{-bt})}{b})^2 +
\rho\frac{\sigma(1-e^{-at})}{a}\frac{\eta(1-e^{-bt})}{b},
\f]
where \f$ f(t) \f$ is the instantaneous forward rate at \f$ t \f$.
*/
class G2::FittingParameter : public TermStructureFittingParameter {
private:
class Impl : public Parameter::Impl {
public:
Impl(Handle<YieldTermStructure> termStructure,
Real a,
Real sigma,
Real b,
Real eta,
Real rho)
: termStructure_(std::move(termStructure)), a_(a), sigma_(sigma), b_(b), eta_(eta),
rho_(rho) {}
Real value(const Array&, Time t) const override {
Rate forward = termStructure_->forwardRate(t, t,
Continuous,
NoFrequency);
Real temp1 = sigma_*(1.0-std::exp(-a_*t))/a_;
Real temp2 = eta_*(1.0-std::exp(-b_*t))/b_;
Real value = 0.5*temp1*temp1 + 0.5*temp2*temp2 +
rho_*temp1*temp2 + forward;
return value;
}
private:
Handle<YieldTermStructure> termStructure_;
Real a_, sigma_, b_, eta_, rho_;
};
public:
FittingParameter(const Handle<YieldTermStructure>& termStructure,
Real a,
Real sigma,
Real b,
Real eta,
Real rho)
: TermStructureFittingParameter(ext::shared_ptr<Parameter::Impl>(
new FittingParameter::Impl(termStructure, a, sigma,
b, eta, rho))) {}
};
}
#endif