QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
piecewiseforwardspreadedtermstructure.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 Roland Lichters
5 Copyright (C) 2006, 2008, 2014 StatPro Italia srl
6 Copyright (C) 2010 Robert Philipp
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22/*! \file piecewiseforwardspreadedtermstructure.hpp
23 \brief Piecewise-forward-spreaded term structure
24*/
25
26#ifndef quantlib_piecewise_forward_spreaded_term_structure_hpp
27#define quantlib_piecewise_forward_spreaded_term_structure_hpp
28
30#include <ql/quote.hpp>
32#include <utility>
33#include <vector>
34
35namespace QuantLib {
36
37 //! Term structure with an added vector of spreads on the instantaneous forward rate
38 /*! The forward rate spread at any given date is interpolated
39 between the input data.
40
41 \note This term structure will remain linked to the original
42 structure, i.e., any changes in the latter will be
43 reflected in this structure as well.
44
45 \ingroup yieldtermstructures
46 */
47
48 template <class Interpolator>
50 public:
52 std::vector<Handle<Quote> > spreads,
53 const std::vector<Date>& dates,
55 const Interpolator& factory = Interpolator());
56 //! \name YieldTermStructure interface
57 //@{
58 DayCounter dayCounter() const override;
59 Natural settlementDays() const override;
60 Calendar calendar() const override;
61 const Date& referenceDate() const override;
62 Date maxDate() const override;
63 //@}
64 protected:
65 //! returns the spreaded zero yield rate
66 Rate zeroYieldImpl(Time) const override;
67 Rate forwardImpl(Time) const override;
68 void update() override;
69
70 private:
72 Real calcSpread(Time t) const;
75 std::vector<Handle<Quote> > spreads_;
76 std::vector<Date> dates_;
77 std::vector<Time> times_;
78 std::vector<Spread> spreadValues_;
82 Interpolator factory_;
84 };
85
86 // inline definitions
87
88 template <class T>
90 T>::InterpolatedPiecewiseForwardSpreadedTermStructure(Handle<YieldTermStructure> h,
91 std::vector<Handle<Quote> > spreads,
92 const std::vector<Date>& dates,
93 DayCounter dc,
94 const T& factory)
95 : originalCurve_(std::move(h)), spreads_(std::move(spreads)), dates_(dates),
96 times_(dates.size()), spreadValues_(dates.size()), dc_(std::move(dc)),
97 factory_(factory) {
98 QL_REQUIRE(!spreads_.empty(), "no spreads given");
99 QL_REQUIRE(spreads_.size() == dates_.size(),
100 "spread and date vector have different sizes");
102 for (auto& spread : spreads_)
103 registerWith(spread);
104 if (!originalCurve_.empty())
106 }
107
108 template <class T>
110 return originalCurve_->dayCounter();
111 }
112
113 template <class T>
115 return originalCurve_->calendar();
116 }
117
118 template <class T>
120 return originalCurve_->settlementDays();
121 }
122
123 template <class T>
124 inline const Date&
126 return originalCurve_->referenceDate();
127 }
128
129 template <class T>
131 return std::min(originalCurve_->maxDate(), dates_.back());
132 }
133
134 template <class T>
135 inline Rate
137 Spread spreadPrimitive = calcSpreadPrimitive(t);
138 InterestRate zeroRate = originalCurve_->zeroRate(t, Continuous, NoFrequency, true);
139 return zeroRate + spreadPrimitive;
140 }
141
142 template <class T>
143 inline Rate
145 Spread spread = calcSpread(t);
146 Rate forwardRate = originalCurve_->forwardRate(t, t, Continuous, NoFrequency, true);
147 return forwardRate + spread;
148 }
149
150 template <class T>
151 inline Spread
153 if (t <= times_.front()) {
154 return spreads_.front()->value();
155 } else if (t >= times_.back()) {
156 return spreads_.back()->value();
157 } else {
158 return interpolator_(t, true);
159 }
160 }
161
162 template <class T>
163 inline Spread
165 if (t == 0.0)
166 return calcSpread(0.0);
167
168 Real integral;
169 if (t <= this->times_.back()) {
170 integral = this->interpolator_.primitive(t, true);
171 } else {
172 integral = this->interpolator_.primitive(this->times_.back(), true)
173 + this->spreads_.back()->value() * (t - this->times_.back());
174 }
175 return integral/t;
176 }
177
178 template <class T>
180 if (!originalCurve_.empty()) {
181 updateInterpolation();
183 } else {
184 /* The implementation inherited from YieldTermStructure
185 asks for our reference date, which we don't have since
186 the original curve is still not set. Therefore, we skip
187 over that and just call the base-class behavior. */
188 // NOLINTNEXTLINE(bugprone-parent-virtual-call)
190 }
191 }
192
193 template <class T>
195 for (Size i = 0; i < dates_.size(); i++) {
196 times_[i] = timeFromReference(dates_[i]);
197 spreadValues_[i] = spreads_[i]->value();
198 }
199 interpolator_ = factory_.interpolate(times_.begin(),
200 times_.end(),
201 spreadValues_.begin());
202 }
203
204}
205
206
207#endif
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Forward-rate term structure
Shared handle to an observable.
Definition: handle.hpp:41
Concrete interest rate class.
Term structure with an added vector of spreads on the instantaneous forward rate.
Calendar calendar() const override
the calendar used for reference and/or option date calculation
Rate zeroYieldImpl(Time) const override
returns the spreaded zero yield rate
const Date & referenceDate() const override
the date at which discount = 1.0 and/or variance = 0.0
Natural settlementDays() const override
the settlementDays used for reference date calculation
DayCounter dayCounter() const override
the day counter used for date/time conversion
Rate forwardImpl(Time) const override
instantaneous forward-rate calculation
Date maxDate() const override
the latest date for which the curve can return values
base class for 1-D interpolations.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:226
void update() override
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Forward-based yield term structure.
Frequency
Frequency of events.
Definition: frequency.hpp:37
@ NoFrequency
null frequency
Definition: frequency.hpp:37
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
linear interpolation between discrete points
Definition: any.hpp:37
Compounding
Interest rate coumpounding rule.
Definition: compounding.hpp:32
STL namespace.
purely virtual base class for market observables