QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
piecewisezerospreadedtermstructure.hpp
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
26#ifndef quantlib_piecewise_zero_spreaded_term_structure_hpp
27#define quantlib_piecewise_zero_spreaded_term_structure_hpp
28
29#include <ql/math/interpolations/linearinterpolation.hpp>
30#include <ql/quote.hpp>
31#include <ql/termstructures/yield/zeroyieldstructure.hpp>
32#include <utility>
33#include <vector>
34
35namespace QuantLib {
36
38
48 template <class Interpolator>
50 public:
52 std::vector<Handle<Quote> > spreads,
53 const std::vector<Date>& dates,
57 const Interpolator& factory = Interpolator());
59
60 DayCounter dayCounter() const override;
61 Natural settlementDays() const override;
62 Calendar calendar() const override;
63 const Date& referenceDate() const override;
64 Date maxDate() const override;
66 protected:
68 Rate zeroYieldImpl(Time) const override;
69 void update() override;
70
71 private:
73 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
87
90
91
92 // inline definitions
93
94 template <class T>
96 T>::InterpolatedPiecewiseZeroSpreadedTermStructure(Handle<YieldTermStructure> h,
97 std::vector<Handle<Quote> > spreads,
98 const std::vector<Date>& dates,
99 Compounding comp,
100 Frequency freq,
101 DayCounter dc,
102 const T& factory)
103 : originalCurve_(std::move(h)), spreads_(std::move(spreads)), dates_(dates),
104 times_(dates.size()), spreadValues_(dates.size()), comp_(comp), freq_(freq),
105 dc_(std::move(dc)), factory_(factory) {
106 QL_REQUIRE(!spreads_.empty(), "no spreads given");
107 QL_REQUIRE(spreads_.size() == dates_.size(),
108 "spread and date vector have different sizes");
110 for (auto& spread : spreads_)
111 registerWith(spread);
112 if (!originalCurve_.empty())
114 }
115
116 template <class T>
118 return originalCurve_->dayCounter();
119 }
120
121 template <class T>
123 return originalCurve_->calendar();
124 }
125
126 template <class T>
128 return originalCurve_->settlementDays();
129 }
130
131 template <class T>
132 inline const Date&
134 return originalCurve_->referenceDate();
135 }
136
137 template <class T>
139 return std::min(originalCurve_->maxDate(), dates_.back());
140 }
141
142 template <class T>
143 inline Rate
145 Spread spread = calcSpread(t);
146 InterestRate zeroRate = originalCurve_->zeroRate(t, comp_, freq_, true);
147 InterestRate spreadedRate(zeroRate + spread,
148 zeroRate.dayCounter(),
149 zeroRate.compounding(),
150 zeroRate.frequency());
151 return spreadedRate.equivalentRate(Continuous, NoFrequency, t);
152 }
153
154 template <class T>
155 inline Spread
157 if (t <= times_.front()) {
158 return spreads_.front()->value();
159 } else if (t >= times_.back()) {
160 return spreads_.back()->value();
161 } else {
162 return interpolator_(t, true);
163 }
164 }
165
166 template <class T>
168 if (!originalCurve_.empty()) {
169 updateInterpolation();
171 } else {
172 /* The implementation inherited from YieldTermStructure
173 asks for our reference date, which we don't have since
174 the original curve is still not set. Therefore, we skip
175 over that and just call the base-class behavior. */
176 // NOLINTNEXTLINE(bugprone-parent-virtual-call)
178 }
179 }
180
181 template <class T>
183 for (Size i = 0; i < dates_.size(); i++) {
184 times_[i] = timeFromReference(dates_[i]);
185 spreadValues_[i] = spreads_[i]->value();
186 }
187 interpolator_ = factory_.interpolate(times_.begin(),
188 times_.end(),
189 spreadValues_.begin());
190 }
191
192}
193
194
195#endif
196
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Shared handle to an observable.
Definition: handle.hpp:41
Concrete interest rate class.
const DayCounter & dayCounter() const
Compounding compounding() const
Frequency frequency() const
InterestRate equivalentRate(Compounding comp, Frequency freq, Time t) const
equivalent interest rate for a compounding period t.
Yield curve with an added vector of spreads on the zero-yield 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
Date maxDate() const override
the latest date for which the curve can return values
base class for 1-D interpolations.
virtual void update()=0
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
void update() override
Zero-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
InterpolatedPiecewiseZeroSpreadedTermStructure< Linear > PiecewiseZeroSpreadedTermStructure
Piecewise zero-spreaded yield curve based on linear interpolation of zero rates.
Definition: any.hpp:35
Compounding
Interest rate coumpounding rule.
Definition: compounding.hpp:32
STL namespace.