QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
discountcurve.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) 2002, 2003 Decillion Pty(Ltd)
5 Copyright (C) 2005, 2006, 2008, 2009 StatPro Italia srl
6 Copyright (C) 2009, 2015 Ferdinando Ametrano
7 Copyright (C) 2015 Paolo Mazzocchi
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23/*! \file discountcurve.hpp
24 \brief interpolated discount factor structure
25*/
26
27#ifndef quantlib_discount_curve_hpp
28#define quantlib_discount_curve_hpp
29
34#include <utility>
35
36namespace QuantLib {
37
38 //! YieldTermStructure based on interpolation of discount factors
39 /*! \ingroup yieldtermstructures */
40 template <class Interpolator>
42 : public YieldTermStructure,
43 protected InterpolatedCurve<Interpolator> {
44 public:
46 const std::vector<Date>& dates,
47 const std::vector<DiscountFactor>& dfs,
49 const Calendar& cal = Calendar(),
50 const std::vector<Handle<Quote> >& jumps = {},
51 const std::vector<Date>& jumpDates = {},
52 const Interpolator& interpolator = {});
54 const std::vector<Date>& dates,
55 const std::vector<DiscountFactor>& dfs,
57 const Calendar& calendar,
58 const Interpolator& interpolator);
60 const std::vector<Date>& dates,
61 const std::vector<DiscountFactor>& dfs,
63 const Interpolator& interpolator);
64 //! \name TermStructure interface
65 //@{
66 Date maxDate() const override;
67 //@}
68 //! \name other inspectors
69 //@{
70 const std::vector<Time>& times() const;
71 const std::vector<Date>& dates() const;
72 const std::vector<Real>& data() const;
73 const std::vector<DiscountFactor>& discounts() const;
74 std::vector<std::pair<Date, Real> > nodes() const;
75 //@}
76
77 protected:
79 const DayCounter&,
80 const Interpolator& interpolator = {});
82 const Date& referenceDate,
83 const DayCounter&,
84 const std::vector<Handle<Quote> >& jumps = {},
85 const std::vector<Date>& jumpDates = {},
86 const Interpolator& interpolator = {});
89 const Calendar&,
90 const DayCounter&,
91 const std::vector<Handle<Quote> >& jumps = {},
92 const std::vector<Date>& jumpDates = {},
93 const Interpolator& interpolator = {});
94
95 //! \name YieldTermStructure implementation
96 //@{
98 //@}
99 mutable std::vector<Date> dates_;
100 private:
101 void initialize();
102 };
103
104 //! Term structure based on log-linear interpolation of discount factors
105 /*! Log-linear interpolation guarantees piecewise-constant forward
106 rates.
107
108 \ingroup yieldtermstructures
109 */
111
112
113 // inline definitions
114
115 template <class T>
117 if (this->maxDate_ != Date())
118 return this->maxDate_;
119 return dates_.back();
120 }
121
122 template <class T>
123 inline const std::vector<Time>&
125 return this->times_;
126 }
127
128 template <class T>
129 inline const std::vector<Date>&
131 return dates_;
132 }
133
134 template <class T>
135 inline const std::vector<Real>&
137 return this->data_;
138 }
139
140 template <class T>
141 inline const std::vector<DiscountFactor>&
143 return this->data_;
144 }
145
146 template <class T>
147 inline std::vector<std::pair<Date, Real> >
149 std::vector<std::pair<Date, Real> > results(dates_.size());
150 for (Size i=0; i<dates_.size(); ++i)
151 results[i] = std::make_pair(dates_[i], this->data_[i]);
152 return results;
153 }
154
155 #ifndef __DOXYGEN__
156
157 // template definitions
158
159 template <class T>
161 if (t <= this->times_.back())
162 return this->interpolation_(t, true);
163
164 // flat fwd extrapolation
165 Time tMax = this->times_.back();
166 DiscountFactor dMax = this->data_.back();
167 Rate instFwdMax = - this->interpolation_.derivative(tMax) / dMax;
168 return dMax * std::exp(- instFwdMax * (t-tMax));
169 }
170
171 template <class T>
173 const DayCounter& dayCounter,
174 const T& interpolator)
175 : YieldTermStructure(dayCounter),
176 InterpolatedCurve<T>(interpolator) {}
177
178 template <class T>
180 const Date& referenceDate,
181 const DayCounter& dayCounter,
182 const std::vector<Handle<Quote> >& jumps,
183 const std::vector<Date>& jumpDates,
184 const T& interpolator)
185 : YieldTermStructure(referenceDate, Calendar(), dayCounter, jumps, jumpDates),
186 InterpolatedCurve<T>(interpolator) {}
187
188 template <class T>
190 Natural settlementDays,
191 const Calendar& calendar,
192 const DayCounter& dayCounter,
193 const std::vector<Handle<Quote> >& jumps,
194 const std::vector<Date>& jumpDates,
195 const T& interpolator)
196 : YieldTermStructure(settlementDays, calendar, dayCounter, jumps, jumpDates),
197 InterpolatedCurve<T>(interpolator) {}
198
199 template <class T>
201 const std::vector<Date>& dates,
202 const std::vector<DiscountFactor>& discounts,
203 const DayCounter& dayCounter,
204 const Calendar& calendar,
205 const std::vector<Handle<Quote> >& jumps,
206 const std::vector<Date>& jumpDates,
207 const T& interpolator)
208 : YieldTermStructure(dates.at(0), calendar, dayCounter, jumps, jumpDates),
209 InterpolatedCurve<T>(std::vector<Time>(), discounts, interpolator),
210 dates_(dates)
211 {
212 initialize();
213 }
214
215 template <class T>
217 const std::vector<Date>& dates,
218 const std::vector<DiscountFactor>& discounts,
219 const DayCounter& dayCounter,
220 const Calendar& calendar,
221 const T& interpolator)
222 : YieldTermStructure(dates.at(0), calendar, dayCounter),
223 InterpolatedCurve<T>(std::vector<Time>(), discounts, interpolator),
224 dates_(dates)
225 {
226 initialize();
227 }
228
229 template <class T>
231 const std::vector<Date>& dates,
232 const std::vector<DiscountFactor>& discounts,
233 const DayCounter& dayCounter,
234 const T& interpolator)
235 : YieldTermStructure(dates.at(0), Calendar(), dayCounter),
236 InterpolatedCurve<T>(std::vector<Time>(), discounts, interpolator),
237 dates_(dates)
238 {
239 initialize();
240 }
241
242 #endif
243
244 template <class T>
246 {
247 QL_REQUIRE(dates_.size() >= T::requiredPoints,
248 "not enough input dates given");
249 QL_REQUIRE(this->data_.size() == dates_.size(),
250 "dates/data count mismatch");
251 QL_REQUIRE(this->data_[0] == 1.0,
252 "the first discount must be == 1.0 "
253 "to flag the corresponding date as reference date");
254 for (Size i=1; i<dates_.size(); ++i) {
255 QL_REQUIRE(this->data_[i] > 0.0, "negative discount");
256 }
257
258 this->setupTimes(dates_, dates_[0], dayCounter());
259 this->setupInterpolation();
260 this->interpolation_.update();
261 }
262
263}
264
265#endif
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
Helper class to build interpolated term structures.
YieldTermStructure based on interpolation of discount factors.
const std::vector< DiscountFactor > & discounts() const
const std::vector< Date > & dates() const
InterpolatedDiscountCurve(const std::vector< Date > &dates, const std::vector< DiscountFactor > &dfs, const DayCounter &dayCounter, const Calendar &cal=Calendar(), const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
InterpolatedDiscountCurve(const std::vector< Date > &dates, const std::vector< DiscountFactor > &dfs, const DayCounter &dayCounter, const Interpolator &interpolator)
InterpolatedDiscountCurve(const std::vector< Date > &dates, const std::vector< DiscountFactor > &dfs, const DayCounter &dayCounter, const Calendar &calendar, const Interpolator &interpolator)
const std::vector< Real > & data() const
InterpolatedDiscountCurve(const Date &referenceDate, const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
std::vector< std::pair< Date, Real > > nodes() const
const std::vector< Time > & times() const
DiscountFactor discountImpl(Time) const override
discount factor calculation
Date maxDate() const override
the latest date for which the curve can return values
InterpolatedDiscountCurve(const DayCounter &, const Interpolator &interpolator={})
InterpolatedDiscountCurve(Natural settlementDays, const Calendar &, const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, 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
Interest-rate term structure.
const std::vector< Date > & jumpDates() const
floating-point comparisons
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
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
InterpolatedDiscountCurve< LogLinear > DiscountCurve
Term structure based on log-linear interpolation of discount factors.
Helper class to build interpolated term structures.
log-linear and log-cubic interpolation between discrete points
Definition: any.hpp:35
STL namespace.
Interest-rate term structure.