QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
gsr.cpp
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
22#include <utility>
23
24namespace QuantLib {
25
27 std::vector<Date> volstepdates,
28 const std::vector<Real>& volatilities,
29 const Real reversion,
30 const Real T)
31 : Gaussian1dModel(termStructure), CalibratedModel(2), reversion_(arguments_[0]),
32 sigma_(arguments_[1]), volstepdates_(std::move(volstepdates)) {
33
34 QL_REQUIRE(!termStructure.empty(), "yield term structure handle is empty");
35
36 volatilities_.resize(volatilities.size());
37 for (Size i = 0; i < volatilities.size(); ++i)
38 volatilities_[i] = Handle<Quote>(ext::make_shared<SimpleQuote>(volatilities[i]));
39 reversions_.resize(1);
40 reversions_[0] = Handle<Quote>(ext::make_shared<SimpleQuote>(reversion));
41
43 }
44
46 std::vector<Date> volstepdates,
47 const std::vector<Real>& volatilities,
48 const std::vector<Real>& reversions,
49 const Real T)
50 : Gaussian1dModel(termStructure), CalibratedModel(2), reversion_(arguments_[0]),
51 sigma_(arguments_[1]), volstepdates_(std::move(volstepdates)) {
52
53 QL_REQUIRE(!termStructure.empty(), "yield term structure handle is empty");
54
55 volatilities_.resize(volatilities.size());
56 for (Size i = 0; i < volatilities.size(); ++i)
57 volatilities_[i] = Handle<Quote>(ext::make_shared<SimpleQuote>(volatilities[i]));
58 reversions_.resize(reversions.size());
59 for (Size i = 0; i < reversions.size(); ++i)
60 reversions_[i] = Handle<Quote>(ext::make_shared<SimpleQuote>(reversions[i]));
61
63 }
64
66 std::vector<Date> volstepdates,
67 std::vector<Handle<Quote> > volatilities,
68 const Handle<Quote>& reversion,
69 const Real T)
70 : Gaussian1dModel(termStructure), CalibratedModel(2), reversion_(arguments_[0]),
71 sigma_(arguments_[1]), volatilities_(std::move(volatilities)),
72 reversions_(std::vector<Handle<Quote> >(1, reversion)),
73 volstepdates_(std::move(volstepdates)) {
74
75 QL_REQUIRE(!termStructure.empty(), "yield term structure handle is empty");
77 }
78
80 std::vector<Date> volstepdates,
81 std::vector<Handle<Quote> > volatilities,
82 std::vector<Handle<Quote> > reversions,
83 const Real T)
84 : Gaussian1dModel(termStructure), CalibratedModel(2), reversion_(arguments_[0]),
85 sigma_(arguments_[1]), volatilities_(std::move(volatilities)),
86 reversions_(std::move(reversions)), volstepdates_(std::move(volstepdates)) {
87
88 QL_REQUIRE(!termStructure.empty(), "yield term structure handle is empty");
90 }
91
93 if (stateProcess_ != nullptr)
94 ext::static_pointer_cast<GsrProcess>(stateProcess_)->flushCache();
96}
97
98void Gsr::updateTimes() const {
99 volsteptimes_.clear();
100 int j = 0;
101 for (auto i = volstepdates_.begin(); i != volstepdates_.end(); ++i, ++j) {
102 volsteptimes_.push_back(termStructure()->timeFromReference(*i));
104 if (j == 0)
105 QL_REQUIRE(volsteptimes_[0] > 0.0, "volsteptimes must be positive ("
106 << volsteptimes_[0] << ")");
107 else
109 "volsteptimes must be strictly increasing ("
110 << volsteptimes_[j - 1] << "@" << (j - 1) << ", "
111 << volsteptimes_[j] << "@" << j << ")");
112 }
113 if (stateProcess_ != nullptr)
114 ext::static_pointer_cast<GsrProcess>(stateProcess_)->flushCache();
115}
116
118 for (Size i = 0; i < sigma_.size(); i++) {
120 }
121 update();
122}
123
125 for (Size i = 0; i < reversion_.size(); i++) {
127 }
128 update();
129}
130
132
134
135 updateTimes();
136
137 QL_REQUIRE(volatilities_.size() == volsteptimes_.size() + 1,
138 "there must be n+1 volatilities ("
139 << volatilities_.size() << ") for n volatility step times ("
140 << volsteptimes_.size() << ")");
141
142 QL_REQUIRE(reversions_.size() == 1 ||
143 reversions_.size() == volsteptimes_.size() + 1,
144 "there must be 1 or n+1 reversions ("
145 << reversions_.size() << ") for n volatility step times ("
146 << volsteptimes_.size() << ")");
147 if (reversions_.size() == 1) {
149 } else {
151 for (Size i = 0; i < reversion_.size(); i++) {
153 }
154 }
155
156 // sigma_ =
157 // PiecewiseConstantParameter(volsteptimes_,PositiveConstraint());
159 for (Size i = 0; i < sigma_.size(); i++) {
161 }
162
163 stateProcess_ = ext::make_shared<GsrProcess>(
165
167
169
170 volatilityObserver_ = ext::make_shared<VolatilityObserver>(this);
171 reversionObserver_ = ext::make_shared<ReversionObserver>(this);
172
173 for (auto& reversion : reversions_)
174 reversionObserver_->registerWith(reversion);
175
176 for (auto& volatilitie : volatilities_)
177 volatilityObserver_->registerWith(volatilitie);
178}
179
180Real Gsr::zerobondImpl(const Time T, const Time t, const Real y,
181 const Handle<YieldTermStructure> &yts) const {
182
183 calculate();
184
185 if (t == 0.0)
186 return yts.empty() ? this->termStructure()->discount(T, true)
187 : yts->discount(T, true);
188
189 ext::shared_ptr<GsrProcess> p =
190 ext::dynamic_pointer_cast<GsrProcess>(stateProcess_);
191
192 Real x = y * stateProcess_->stdDeviation(0.0, 0.0, t) +
193 stateProcess_->expectation(0.0, 0.0, t);
194 Real gtT = p->G(t, T, x);
195
196 Real d = yts.empty()
197 ? termStructure()->discount(T, true) /
198 termStructure()->discount(t, true)
199 : yts->discount(T, true) / yts->discount(t, true);
200
201 return d * exp(-x * gtT - 0.5 * p->y(t) * gtT * gtT);
202}
203
205 const Handle<YieldTermStructure> &yts) const {
206
207 calculate();
208
209 ext::shared_ptr<GsrProcess> p =
210 ext::dynamic_pointer_cast<GsrProcess>(stateProcess_);
211
212 if (t == 0)
213 return yts.empty()
214 ? this->termStructure()->discount(p->getForwardMeasureTime(),
215 true)
216 : yts->discount(p->getForwardMeasureTime());
217 return zerobond(p->getForwardMeasureTime(), t, y, yts);
218}
219}
1-D array used in linear algebra.
Definition: array.hpp:52
Calibrated model class.
Definition: model.hpp:86
Real value(const Array &params, const std::vector< ext::shared_ptr< CalibrationHelper > > &)
Definition: model.cpp:117
Standard constant parameter .
Definition: parameter.hpp:71
Real zerobond(Time T, Time t=0.0, Real y=0.0, const Handle< YieldTermStructure > &yts=Handle< YieldTermStructure >()) const
ext::shared_ptr< StochasticProcess1D > stateProcess_
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
Parameter & sigma_
Definition: gsr.hpp:163
void update() override
Definition: gsr.cpp:92
ext::shared_ptr< ReversionObserver > reversionObserver_
Definition: gsr.hpp:186
void initialize(Real)
Definition: gsr.cpp:131
std::vector< Handle< Quote > > volatilities_
Definition: gsr.hpp:165
std::vector< Handle< Quote > > reversions_
Definition: gsr.hpp:166
void updateReversion()
Definition: gsr.cpp:124
Gsr(const Handle< YieldTermStructure > &termStructure, std::vector< Date > volstepdates, const std::vector< Real > &volatilities, Real reversion, Real T=60.0)
Definition: gsr.cpp:26
const Array & reversion() const
Definition: gsr.hpp:65
Parameter & reversion_
Definition: gsr.hpp:163
ext::shared_ptr< VolatilityObserver > volatilityObserver_
Definition: gsr.hpp:185
Array volsteptimesArray_
Definition: gsr.hpp:171
void updateTimes() const
Definition: gsr.cpp:98
std::vector< Time > volsteptimes_
Definition: gsr.hpp:170
void updateVolatility()
Definition: gsr.cpp:117
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
bool empty() const
checks if the contained shared pointer points to anything
Definition: handle.hpp:191
virtual void calculate() const
Definition: lazyobject.hpp:253
void update() override
Definition: lazyobject.hpp:188
No constraint.
Definition: constraint.hpp:79
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
void setParam(Size i, Real x)
Definition: parameter.hpp:51
const Array & params() const
Definition: parameter.hpp:50
Size size() const
Definition: parameter.hpp:55
Piecewise-constant parameter.
Definition: parameter.hpp:119
purely virtual base class for market observables
Definition: quote.hpp:37
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
Date d
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 1 factor model.
Definition: any.hpp:35
STL namespace.
simple quote class