| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2004, 2009 Ferdinando Ametrano |
| 5 | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
| 6 | Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl |
| 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 yieldtermstructure.hpp |
| 23 | \brief Interest-rate term structure |
| 24 | */ |
| 25 | |
| 26 | #ifndef quantlib_yield_term_structure_hpp |
| 27 | #define quantlib_yield_term_structure_hpp |
| 28 | |
| 29 | #include <ql/termstructure.hpp> |
| 30 | #include <ql/interestrate.hpp> |
| 31 | #include <ql/quote.hpp> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace QuantLib { |
| 35 | |
| 36 | //! Interest-rate term structure |
| 37 | /*! This abstract class defines the interface of concrete |
| 38 | interest rate structures which will be derived from this one. |
| 39 | |
| 40 | \ingroup yieldtermstructures |
| 41 | |
| 42 | \test observability against evaluation date changes is checked. |
| 43 | */ |
| 44 | class YieldTermStructure : public TermStructure { |
| 45 | public: |
| 46 | /*! \name Constructors |
| 47 | See the TermStructure documentation for issues regarding |
| 48 | constructors. |
| 49 | */ |
| 50 | //@{ |
| 51 | explicit YieldTermStructure(const DayCounter& dc = DayCounter()); |
| 52 | YieldTermStructure(const Date& referenceDate, |
| 53 | const Calendar& cal = Calendar(), |
| 54 | const DayCounter& dc = DayCounter(), |
| 55 | std::vector<Handle<Quote> > jumps = {}, |
| 56 | const std::vector<Date>& jumpDates = {}); |
| 57 | YieldTermStructure(Natural settlementDays, |
| 58 | const Calendar& cal, |
| 59 | const DayCounter& dc = DayCounter(), |
| 60 | std::vector<Handle<Quote> > jumps = {}, |
| 61 | const std::vector<Date>& jumpDates = {}); |
| 62 | //@} |
| 63 | |
| 64 | /*! \name Discount factors |
| 65 | |
| 66 | These methods return the discount factor from a given date or time |
| 67 | to the reference date. In the latter case, the time is calculated |
| 68 | as a fraction of year from the reference date. |
| 69 | */ |
| 70 | //@{ |
| 71 | DiscountFactor discount(const Date& d, |
| 72 | bool = false) const; |
| 73 | /*! The same day-counting rule used by the term structure |
| 74 | should be used for calculating the passed time t. |
| 75 | */ |
| 76 | DiscountFactor discount(Time t, |
| 77 | bool = false) const; |
| 78 | //@} |
| 79 | |
| 80 | /*! \name Zero-yield rates |
| 81 | |
| 82 | These methods return the implied zero-yield rate for a |
| 83 | given date or time. In the former case, the time is |
| 84 | calculated as a fraction of year from the reference date. |
| 85 | */ |
| 86 | //@{ |
| 87 | /*! The resulting interest rate has the required daycounting |
| 88 | rule. |
| 89 | */ |
| 90 | InterestRate zeroRate(const Date& d, |
| 91 | const DayCounter& resultDayCounter, |
| 92 | Compounding comp, |
| 93 | Frequency freq = Annual, |
| 94 | bool = false) const; |
| 95 | |
| 96 | /*! The resulting interest rate has the same day-counting rule |
| 97 | used by the term structure. The same rule should be used |
| 98 | for calculating the passed time t. |
| 99 | */ |
| 100 | InterestRate zeroRate(Time t, |
| 101 | Compounding comp, |
| 102 | Frequency freq = Annual, |
| 103 | bool = false) const; |
| 104 | //@} |
| 105 | |
| 106 | /*! \name Forward rates |
| 107 | |
| 108 | These methods returns the forward interest rate between two dates |
| 109 | or times. In the former case, times are calculated as fractions |
| 110 | of year from the reference date. |
| 111 | |
| 112 | If both dates (times) are equal the instantaneous forward rate is |
| 113 | returned. |
| 114 | */ |
| 115 | //@{ |
| 116 | /*! The resulting interest rate has the required day-counting |
| 117 | rule. |
| 118 | */ |
| 119 | InterestRate forwardRate(const Date& d1, |
| 120 | const Date& d2, |
| 121 | const DayCounter& resultDayCounter, |
| 122 | Compounding comp, |
| 123 | Frequency freq = Annual, |
| 124 | bool = false) const; |
| 125 | /*! The resulting interest rate has the required day-counting |
| 126 | rule. |
| 127 | \warning dates are not adjusted for holidays |
| 128 | */ |
| 129 | InterestRate forwardRate(const Date& d, |
| 130 | const Period& p, |
| 131 | const DayCounter& resultDayCounter, |
| 132 | Compounding comp, |
| 133 | Frequency freq = Annual, |
| 134 | bool = false) const; |
| 135 | |
| 136 | /*! The resulting interest rate has the same day-counting rule |
| 137 | used by the term structure. The same rule should be used |
| 138 | for calculating the passed times t1 and t2. |
| 139 | */ |
| 140 | InterestRate forwardRate(Time t1, |
| 141 | Time t2, |
| 142 | Compounding comp, |
| 143 | Frequency freq = Annual, |
| 144 | bool = false) const; |
| 145 | //@} |
| 146 | |
| 147 | //! \name Jump inspectors |
| 148 | //@{ |
| 149 | const std::vector<Date>& jumpDates() const; |
| 150 | const std::vector<Time>& jumpTimes() const; |
| 151 | //@} |
| 152 | |
| 153 | //! \name Observer interface |
| 154 | //@{ |
| 155 | void update() override; |
| 156 | //@} |
| 157 | protected: |
| 158 | /*! \name Calculations |
| 159 | |
| 160 | This method must be implemented in derived classes to |
| 161 | perform the actual calculations. When it is called, |
| 162 | range check has already been performed; therefore, it |
| 163 | must assume that extrapolation is required. |
| 164 | */ |
| 165 | //@{ |
| 166 | //! discount factor calculation |
| 167 | virtual DiscountFactor discountImpl(Time) const = 0; |
| 168 | //@} |
| 169 | private: |
| 170 | // methods |
| 171 | void setJumps(const Date& referenceDate); |
| 172 | // data members |
| 173 | std::vector<Handle<Quote> > jumps_; |
| 174 | std::vector<Date> jumpDates_; |
| 175 | std::vector<Time> jumpTimes_; |
| 176 | Size nJumps_ = 0; |
| 177 | Date latestReference_; |
| 178 | }; |
| 179 | |
| 180 | // inline definitions |
| 181 | |
| 182 | inline |
| 183 | DiscountFactor YieldTermStructure::discount(const Date& d, |
| 184 | bool ) const { |
| 185 | return discount(t: timeFromReference(d), extrapolate); |
| 186 | } |
| 187 | |
| 188 | inline |
| 189 | InterestRate YieldTermStructure::forwardRate(const Date& d, |
| 190 | const Period& p, |
| 191 | const DayCounter& dayCounter, |
| 192 | Compounding comp, |
| 193 | Frequency freq, |
| 194 | bool ) const { |
| 195 | return forwardRate(d1: d, d2: d+p, resultDayCounter: dayCounter, comp, freq, extrapolate); |
| 196 | } |
| 197 | |
| 198 | inline const std::vector<Date>& YieldTermStructure::jumpDates() const { |
| 199 | return this->jumpDates_; |
| 200 | } |
| 201 | |
| 202 | inline const std::vector<Time>& YieldTermStructure::jumpTimes() const { |
| 203 | return this->jumpTimes_; |
| 204 | } |
| 205 | |
| 206 | } |
| 207 | |
| 208 | #endif |
| 209 | |