QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
forwardcurve.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005, 2006, 2007, 2008, 2009 StatPro Italia srl
5 Copyright (C) 2009, 2015 Ferdinando Ametrano
6 Copyright (C) 2015 Paolo Mazzocchi
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_forward_curve_hpp
27#define quantlib_forward_curve_hpp
28
29#include <ql/termstructures/yield/forwardstructure.hpp>
30#include <ql/termstructures/interpolatedcurve.hpp>
31#include <ql/math/interpolations/backwardflatinterpolation.hpp>
32#include <ql/math/comparison.hpp>
33#include <utility>
34
35namespace QuantLib {
36
38
39 template <class Interpolator>
41 protected InterpolatedCurve<Interpolator> {
42 public:
43 // constructor
45 const std::vector<Date>& dates,
46 const std::vector<Rate>& forwards,
48 const Calendar& cal = Calendar(),
49 const std::vector<Handle<Quote> >& jumps = {},
50 const std::vector<Date>& jumpDates = {},
51 const Interpolator& interpolator = {});
53 const std::vector<Date>& dates,
54 const std::vector<Rate>& forwards,
56 const Calendar& calendar,
57 const Interpolator& interpolator);
59 const std::vector<Date>& dates,
60 const std::vector<Rate>& forwards,
62 const Interpolator& interpolator);
64
65 Date maxDate() const override;
67
69 const std::vector<Time>& times() const;
70 const std::vector<Date>& dates() const;
71 const std::vector<Real>& data() const;
72 const std::vector<Rate>& forwards() const;
73 std::vector<std::pair<Date, Real> > nodes() const;
75
76 protected:
78 const DayCounter&,
79 const Interpolator& interpolator = {});
81 const Date& referenceDate,
82 const DayCounter&,
83 const std::vector<Handle<Quote> >& jumps = {},
84 const std::vector<Date>& jumpDates = {},
85 const Interpolator& interpolator = {});
88 const Calendar&,
89 const DayCounter&,
90 const std::vector<Handle<Quote> >& jumps = {},
91 const std::vector<Date>& jumpDates = {},
92 const Interpolator& interpolator = {});
93
95
96 Rate forwardImpl(Time t) const override;
97 Rate zeroYieldImpl(Time t) const override;
99 mutable std::vector<Date> dates_;
100 private:
101 void initialize();
102 };
103
105
108
109
110 // inline definitions
111
112 template <class T>
114 if (this->maxDate_ != Date())
115 return this->maxDate_;
116 return dates_.back();
117 }
118
119 template <class T>
120 inline const std::vector<Time>&
122 return this->times_;
123 }
124
125 template <class T>
126 inline const std::vector<Date>&
128 return dates_;
129 }
130
131 template <class T>
132 inline const std::vector<Real>&
134 return this->data_;
135 }
136
137 template <class T>
138 inline const std::vector<Rate>&
140 return this->data_;
141 }
142
143 template <class T>
144 inline std::vector<std::pair<Date, Real> >
146 std::vector<std::pair<Date, Real> > results(dates_.size());
147 for (Size i=0; i<dates_.size(); ++i)
148 results[i] = std::make_pair(dates_[i], this->data_[i]);
149 return results;
150 }
151
152 #ifndef __DOXYGEN__
153
154 // template definitions
155
156 template <class T>
158 if (t <= this->times_.back())
159 return this->interpolation_(t, true);
160
161 // flat fwd extrapolation
162 return this->data_.back();
163 }
164
165 template <class T>
167 if (t == 0.0)
168 return forwardImpl(0.0);
169
170 Real integral;
171 if (t <= this->times_.back()) {
172 integral = this->interpolation_.primitive(t, true);
173 } else {
174 // flat fwd extrapolation
175 integral = this->interpolation_.primitive(this->times_.back(), true)
176 + this->data_.back()*(t - this->times_.back());
177 }
178 return integral/t;
179 }
180
181 template <class T>
183 const DayCounter& dayCounter,
184 const T& interpolator)
185 : ForwardRateStructure(dayCounter), InterpolatedCurve<T>(interpolator) {}
186
187 template <class T>
189 const Date& referenceDate,
190 const DayCounter& dayCounter,
191 const std::vector<Handle<Quote> >& jumps,
192 const std::vector<Date>& jumpDates,
193 const T& interpolator)
194 : ForwardRateStructure(referenceDate, Calendar(), dayCounter, jumps, jumpDates),
195 InterpolatedCurve<T>(interpolator) {}
196
197 template <class T>
199 Natural settlementDays,
200 const Calendar& calendar,
201 const DayCounter& dayCounter,
202 const std::vector<Handle<Quote> >& jumps,
203 const std::vector<Date>& jumpDates,
204 const T& interpolator)
205 : ForwardRateStructure(settlementDays, calendar, dayCounter, jumps, jumpDates),
206 InterpolatedCurve<T>(interpolator) {}
207
208 template <class T>
210 const std::vector<Date>& dates,
211 const std::vector<Rate>& forwards,
212 const DayCounter& dayCounter,
213 const Calendar& calendar,
214 const std::vector<Handle<Quote> >& jumps,
215 const std::vector<Date>& jumpDates,
216 const T& interpolator)
217 : ForwardRateStructure(dates.at(0), calendar, dayCounter, jumps, jumpDates),
218 InterpolatedCurve<T>(std::vector<Time>(), forwards, interpolator),
219 dates_(dates)
220 {
221 initialize();
222 }
223
224 template <class T>
226 const std::vector<Date>& dates,
227 const std::vector<Rate>& forwards,
228 const DayCounter& dayCounter,
229 const Calendar& calendar,
230 const T& interpolator)
231 : ForwardRateStructure(dates.at(0), calendar, dayCounter),
232 InterpolatedCurve<T>(std::vector<Time>(), forwards, interpolator),
233 dates_(dates)
234 {
235 initialize();
236 }
237
238 template <class T>
240 const std::vector<Date>& dates,
241 const std::vector<Rate>& forwards,
242 const DayCounter& dayCounter,
243 const T& interpolator)
244 : ForwardRateStructure(dates.at(0), Calendar(), dayCounter),
245 InterpolatedCurve<T>(std::vector<Time>(), forwards, interpolator),
246 dates_(dates)
247 {
248 initialize();
249 }
250
251 #endif
252
253 template <class T>
255 {
256 QL_REQUIRE(dates_.size() >= T::requiredPoints,
257 "not enough input dates given");
258 QL_REQUIRE(this->data_.size() == dates_.size(),
259 "dates/data count mismatch");
260
261 this->setupTimes(dates_, dates_[0], dayCounter());
262 this->setupInterpolation();
263 this->interpolation_.update();
264 }
265
266}
267
268#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
Helper class to build interpolated term structures.
YieldTermStructure based on interpolation of forward rates.
Rate zeroYieldImpl(Time t) const override
Rate forwardImpl(Time t) const override
instantaneous forward-rate calculation
InterpolatedForwardCurve(const std::vector< Date > &dates, const std::vector< Rate > &forwards, const DayCounter &dayCounter, const Calendar &cal=Calendar(), const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
const std::vector< Rate > & forwards() const
const std::vector< Date > & dates() const
const std::vector< Real > & data() const
std::vector< std::pair< Date, Real > > nodes() const
InterpolatedForwardCurve(const std::vector< Date > &dates, const std::vector< Rate > &forwards, const DayCounter &dayCounter, const Interpolator &interpolator)
const std::vector< Time > & times() const
InterpolatedForwardCurve(const Date &referenceDate, const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
Date maxDate() const override
the latest date for which the curve can return values
InterpolatedForwardCurve(const DayCounter &, const Interpolator &interpolator={})
InterpolatedForwardCurve(Natural settlementDays, const Calendar &, const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
InterpolatedForwardCurve(const std::vector< Date > &dates, const std::vector< Rate > &forwards, const DayCounter &dayCounter, const Calendar &calendar, const Interpolator &interpolator)
virtual Natural settlementDays() const
the settlementDays used for reference date calculation
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
virtual Calendar calendar() const
the calendar used for reference and/or option date calculation
virtual DayCounter dayCounter() const
the day counter used for date/time conversion
const std::vector< Date > & jumpDates() const
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 Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
InterpolatedForwardCurve< BackwardFlat > ForwardCurve
Term structure based on flat interpolation of forward rates.
Definition: any.hpp:35
STL namespace.