QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
gsr.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) 2013, 2015 Peter Caspers
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 gsr.hpp
21 \brief GSR 1 factor model
22*/
23
24#ifndef quantlib_gsr_hpp
25#define quantlib_gsr_hpp
26
29
30namespace QuantLib {
31
32//! One factor gsr model, formulation is in forward measure
33
34class Gsr : public Gaussian1dModel, public CalibratedModel {
35
36 public:
37 // constant mean reversion
39 std::vector<Date> volstepdates,
40 const std::vector<Real>& volatilities,
42 Real T = 60.0);
43 // piecewise mean reversion (with same step dates as volatilities)
45 std::vector<Date> volstepdates,
46 const std::vector<Real>& volatilities,
47 const std::vector<Real>& reversions,
48 Real T = 60.0);
49 // constant mean reversion with floating model data
51 std::vector<Date> volstepdates,
52 std::vector<Handle<Quote> > volatilities,
54 Real T = 60.0);
55 // piecewise mean reversion with floating model data
57 std::vector<Date> volstepdates,
58 std::vector<Handle<Quote> > volatilities,
59 std::vector<Handle<Quote> > reversions,
60 Real T = 60.0);
61
62 Real numeraireTime() const;
63 void numeraireTime(Real T);
64
65 const Array &reversion() const { return reversion_.params(); }
66 const Array &volatility() const { return sigma_.params(); }
67
68 // calibration constraints
69
70 // fixed reversions, only volatilities are free
71 std::vector<bool> FixedReversions() {
72 std::vector<bool> res(reversions_.size(), true);
73 std::vector<bool> vol(volatilities_.size(), false);
74 res.insert(res.end(), vol.begin(), vol.end());
75 return res;
76 }
77
78 // fixed volatilities, only reversions are free
79 std::vector<bool> FixedVolatilities() {
80 std::vector<bool> res(reversions_.size(), false);
81 std::vector<bool> vol(volatilities_.size(), true);
82 res.insert(res.end(), vol.begin(), vol.end());
83 return res;
84 }
85
86 std::vector<bool> MoveVolatility(Size i) {
87 QL_REQUIRE(i < volatilities_.size(),
88 "volatility with index " << i << " does not exist (0..."
89 << volatilities_.size() - 1 << ")");
90 std::vector<bool> res(reversions_.size() + volatilities_.size(), true);
91 res[reversions_.size() + i] = false;
92 return res;
93 }
94
95 std::vector<bool> MoveReversion(Size i) {
96 QL_REQUIRE(i < reversions_.size(),
97 "reversion with index " << i << " does not exist (0..."
98 << reversions_.size() - 1 << ")");
99 std::vector<bool> res(reversions_.size() + volatilities_.size(), true);
100 res[i] = false;
101 return res;
102 }
103
104 // With fixed reversion calibrate the volatilities one by one
105 // to the given helpers. It is assumed that that volatility step
106 // dates are suitable for this, i.e. they should be identical to
107 // the fixing dates of the helpers (except for the last one where
108 // we do not need a step). Also note that the endcritera reflect
109 // only the status of the last calibration when using this method.
111 const std::vector<ext::shared_ptr<BlackCalibrationHelper> > &helpers,
114 const std::vector<Real> &weights = std::vector<Real>()) {
115
116 for (Size i = 0; i < helpers.size(); i++) {
117 std::vector<ext::shared_ptr<CalibrationHelper> > h(1, helpers[i]);
118 calibrate(h, method, endCriteria, constraint, weights,
119 MoveVolatility(i));
120 }
121 }
122
123 // With fixed volatility calibrate the reversions one by one
124 // to the given helpers. In this case the step dates must be chosen
125 // according to the maturities of the calibration instruments.
127 const std::vector<ext::shared_ptr<BlackCalibrationHelper> > &helpers,
130 const std::vector<Real> &weights = std::vector<Real>()) {
131
132 for (Size i = 0; i < helpers.size(); i++) {
133 std::vector<ext::shared_ptr<CalibrationHelper> > h(1, helpers[i]);
134 calibrate(h, method, endCriteria, constraint, weights,
135 MoveReversion(i));
136 }
137 }
138
139 protected:
140 Real numeraireImpl(Time t, Real y, const Handle<YieldTermStructure>& yts) const override;
141
142 Real zerobondImpl(Time T, Time t, Real y, const Handle<YieldTermStructure>& yts) const override;
143
144 void generateArguments() override {
145 ext::static_pointer_cast<GsrProcess>(stateProcess_)->flushCache();
147 }
148
149 void update() override;
150
151 void performCalculations() const override {
153 updateTimes();
154 }
155
156 private:
157 void updateTimes() const;
158 void updateVolatility();
159 void updateReversion();
160
161 void initialize(Real);
162
164
165 std::vector<Handle<Quote> > volatilities_;
166 std::vector<Handle<Quote> > reversions_;
167 std::vector<Date> volstepdates_; // this is shared between vols,
168 // adjusters and reverisons in
169 // case of piecewise reversions
170 mutable std::vector<Time> volsteptimes_;
171 mutable Array volsteptimesArray_; // FIXME this is redundant (just a copy of
172 // volsteptimes_)
173
174 struct VolatilityObserver : public Observer {
175 explicit VolatilityObserver(Gsr *p) : p_(p) {}
176 void update() override { p_->updateVolatility(); }
178 };
179 struct ReversionObserver : public Observer {
180 explicit ReversionObserver(Gsr *p) : p_(p) {}
181 void update() override { p_->updateReversion(); }
183 };
184
185 ext::shared_ptr<VolatilityObserver> volatilityObserver_;
186 ext::shared_ptr<ReversionObserver> reversionObserver_;
187};
188
189inline Real Gsr::numeraireTime() const {
190 return ext::dynamic_pointer_cast<GsrProcess>(stateProcess_)
191 ->getForwardMeasureTime();
192}
193
194inline void Gsr::numeraireTime(const Real T) {
195 ext::dynamic_pointer_cast<GsrProcess>(stateProcess_)
196 ->setForwardMeasureTime(T);
197}
198}
199
200#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Calibrated model class.
Definition: model.hpp:86
EndCriteria::Type endCriteria() const
Returns end criteria result.
Definition: model.hpp:113
const ext::shared_ptr< Constraint > & constraint() const
Definition: model.hpp:160
virtual void calibrate(const std::vector< ext::shared_ptr< CalibrationHelper > > &, OptimizationMethod &method, const EndCriteria &endCriteria, const Constraint &constraint=Constraint(), const std::vector< Real > &weights=std::vector< Real >(), const std::vector< bool > &fixParameters=std::vector< bool >())
Calibrate to a set of market instruments (usually caps/swaptions)
Definition: model.cpp:75
Base constraint class.
Definition: constraint.hpp:35
Criteria to end optimization process:
Definition: endcriteria.hpp:40
void performCalculations() const override
ext::shared_ptr< StochasticProcess1D > stateProcess_
One factor gsr model, formulation is in forward measure.
Definition: gsr.hpp:34
std::vector< Date > volstepdates_
Definition: gsr.hpp:167
Real zerobondImpl(Time T, Time t, Real y, const Handle< YieldTermStructure > &yts) const override
Definition: gsr.cpp:180
void performCalculations() const override
Definition: gsr.hpp:151
Parameter & sigma_
Definition: gsr.hpp:163
void update() override
Definition: gsr.cpp:92
ext::shared_ptr< ReversionObserver > reversionObserver_
Definition: gsr.hpp:186
std::vector< bool > MoveReversion(Size i)
Definition: gsr.hpp:95
void initialize(Real)
Definition: gsr.cpp:131
void generateArguments() override
Definition: gsr.hpp:144
std::vector< Handle< Quote > > volatilities_
Definition: gsr.hpp:165
const Array & volatility() const
Definition: gsr.hpp:66
std::vector< Handle< Quote > > reversions_
Definition: gsr.hpp:166
Real numeraireTime() const
Definition: gsr.hpp:189
void updateReversion()
Definition: gsr.cpp:124
const Array & reversion() const
Definition: gsr.hpp:65
Parameter & reversion_
Definition: gsr.hpp:163
ext::shared_ptr< VolatilityObserver > volatilityObserver_
Definition: gsr.hpp:185
void calibrateVolatilitiesIterative(const std::vector< ext::shared_ptr< BlackCalibrationHelper > > &helpers, OptimizationMethod &method, const EndCriteria &endCriteria, const Constraint &constraint=Constraint(), const std::vector< Real > &weights=std::vector< Real >())
Definition: gsr.hpp:110
void calibrateReversionsIterative(const std::vector< ext::shared_ptr< BlackCalibrationHelper > > &helpers, OptimizationMethod &method, const EndCriteria &endCriteria, const Constraint &constraint=Constraint(), const std::vector< Real > &weights=std::vector< Real >())
Definition: gsr.hpp:126
std::vector< bool > MoveVolatility(Size i)
Definition: gsr.hpp:86
Array volsteptimesArray_
Definition: gsr.hpp:171
void updateTimes() const
Definition: gsr.cpp:98
std::vector< bool > FixedReversions()
Definition: gsr.hpp:71
std::vector< Time > volsteptimes_
Definition: gsr.hpp:170
void updateVolatility()
Definition: gsr.cpp:117
std::vector< bool > FixedVolatilities()
Definition: gsr.hpp:79
Real numeraireImpl(Time t, Real y, const Handle< YieldTermStructure > &yts) const override
Definition: gsr.cpp:204
Shared handle to an observable.
Definition: handle.hpp:41
Object that gets notified when a given observable changes.
Definition: observable.hpp:116
Abstract class for constrained optimization method.
Definition: method.hpp:36
Base class for model arguments.
Definition: parameter.hpp:38
const Array & params() const
Definition: parameter.hpp:50
const Handle< YieldTermStructure > & termStructure() const
Definition: model.hpp:77
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
basic interface for one factor interest rate models
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
GSR model process with piecewise volatilities and mean reversions, the dynamic is expressed in some T...
Definition: any.hpp:35
void update() override
Definition: gsr.hpp:181