| 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 | |
| 22 | /*! \file forwardcurve.hpp |
| 23 | \brief interpolated forward-rate structure |
| 24 | */ |
| 25 | |
| 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 | |
| 35 | namespace QuantLib { |
| 36 | |
| 37 | //! YieldTermStructure based on interpolation of forward rates |
| 38 | /*! \ingroup yieldtermstructures */ |
| 39 | template <class Interpolator> |
| 40 | class InterpolatedForwardCurve : public ForwardRateStructure, |
| 41 | protected InterpolatedCurve<Interpolator> { |
| 42 | public: |
| 43 | // constructor |
| 44 | InterpolatedForwardCurve( |
| 45 | const std::vector<Date>& dates, |
| 46 | const std::vector<Rate>& forwards, |
| 47 | const DayCounter& dayCounter, |
| 48 | const Calendar& cal = Calendar(), |
| 49 | const std::vector<Handle<Quote> >& jumps = {}, |
| 50 | const std::vector<Date>& jumpDates = {}, |
| 51 | const Interpolator& interpolator = {}); |
| 52 | InterpolatedForwardCurve( |
| 53 | const std::vector<Date>& dates, |
| 54 | const std::vector<Rate>& forwards, |
| 55 | const DayCounter& dayCounter, |
| 56 | const Calendar& calendar, |
| 57 | const Interpolator& interpolator); |
| 58 | InterpolatedForwardCurve( |
| 59 | const std::vector<Date>& dates, |
| 60 | const std::vector<Rate>& forwards, |
| 61 | const DayCounter& dayCounter, |
| 62 | const Interpolator& interpolator); |
| 63 | //! \name TermStructure interface |
| 64 | //@{ |
| 65 | Date maxDate() const override; |
| 66 | //@} |
| 67 | //! \name other inspectors |
| 68 | //@{ |
| 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; |
| 74 | //@} |
| 75 | |
| 76 | protected: |
| 77 | explicit InterpolatedForwardCurve( |
| 78 | const DayCounter&, |
| 79 | const Interpolator& interpolator = {}); |
| 80 | InterpolatedForwardCurve( |
| 81 | const Date& referenceDate, |
| 82 | const DayCounter&, |
| 83 | const std::vector<Handle<Quote> >& jumps = {}, |
| 84 | const std::vector<Date>& jumpDates = {}, |
| 85 | const Interpolator& interpolator = {}); |
| 86 | InterpolatedForwardCurve( |
| 87 | Natural settlementDays, |
| 88 | const Calendar&, |
| 89 | const DayCounter&, |
| 90 | const std::vector<Handle<Quote> >& jumps = {}, |
| 91 | const std::vector<Date>& jumpDates = {}, |
| 92 | const Interpolator& interpolator = {}); |
| 93 | |
| 94 | //! \name ForwardRateStructure implementation |
| 95 | //@{ |
| 96 | Rate forwardImpl(Time t) const override; |
| 97 | Rate zeroYieldImpl(Time t) const override; |
| 98 | //@} |
| 99 | mutable std::vector<Date> dates_; |
| 100 | private: |
| 101 | void initialize(); |
| 102 | }; |
| 103 | |
| 104 | //! Term structure based on flat interpolation of forward rates |
| 105 | /*! \ingroup yieldtermstructures */ |
| 106 | |
| 107 | typedef InterpolatedForwardCurve<BackwardFlat> ForwardCurve; |
| 108 | |
| 109 | |
| 110 | // inline definitions |
| 111 | |
| 112 | template <class T> |
| 113 | inline Date InterpolatedForwardCurve<T>::maxDate() const { |
| 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>& |
| 121 | InterpolatedForwardCurve<T>::times() const { |
| 122 | return this->times_; |
| 123 | } |
| 124 | |
| 125 | template <class T> |
| 126 | inline const std::vector<Date>& |
| 127 | InterpolatedForwardCurve<T>::dates() const { |
| 128 | return dates_; |
| 129 | } |
| 130 | |
| 131 | template <class T> |
| 132 | inline const std::vector<Real>& |
| 133 | InterpolatedForwardCurve<T>::data() const { |
| 134 | return this->data_; |
| 135 | } |
| 136 | |
| 137 | template <class T> |
| 138 | inline const std::vector<Rate>& |
| 139 | InterpolatedForwardCurve<T>::forwards() const { |
| 140 | return this->data_; |
| 141 | } |
| 142 | |
| 143 | template <class T> |
| 144 | inline std::vector<std::pair<Date, Real> > |
| 145 | InterpolatedForwardCurve<T>::nodes() const { |
| 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> |
| 157 | Rate InterpolatedForwardCurve<T>::forwardImpl(Time t) const { |
| 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> |
| 166 | Rate InterpolatedForwardCurve<T>::zeroYieldImpl(Time t) const { |
| 167 | if (t == 0.0) |
| 168 | return forwardImpl(t: 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> |
| 182 | InterpolatedForwardCurve<T>::InterpolatedForwardCurve( |
| 183 | const DayCounter& dayCounter, |
| 184 | const T& interpolator) |
| 185 | : ForwardRateStructure(dayCounter), InterpolatedCurve<T>(interpolator) {} |
| 186 | |
| 187 | template <class T> |
| 188 | InterpolatedForwardCurve<T>::InterpolatedForwardCurve( |
| 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> |
| 198 | InterpolatedForwardCurve<T>::InterpolatedForwardCurve( |
| 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> |
| 209 | InterpolatedForwardCurve<T>::InterpolatedForwardCurve( |
| 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(n: 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> |
| 225 | InterpolatedForwardCurve<T>::InterpolatedForwardCurve( |
| 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(n: 0), calendar, dayCounter), |
| 232 | InterpolatedCurve<T>(std::vector<Time>(), forwards, interpolator), |
| 233 | dates_(dates) |
| 234 | { |
| 235 | initialize(); |
| 236 | } |
| 237 | |
| 238 | template <class T> |
| 239 | InterpolatedForwardCurve<T>::InterpolatedForwardCurve( |
| 240 | const std::vector<Date>& dates, |
| 241 | const std::vector<Rate>& forwards, |
| 242 | const DayCounter& dayCounter, |
| 243 | const T& interpolator) |
| 244 | : ForwardRateStructure(dates.at(n: 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> |
| 254 | void InterpolatedForwardCurve<T>::initialize() |
| 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 |
| 269 | |