| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2007, 2009 Chris Kenyon |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | /*! \file inflationtermstructure.hpp |
| 21 | \brief Base classes for inflation term structures. |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_inflation_termstructure_hpp |
| 25 | #define quantlib_inflation_termstructure_hpp |
| 26 | |
| 27 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 28 | #include <ql/termstructures/inflation/seasonality.hpp> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | class InflationIndex; |
| 33 | |
| 34 | //! Interface for inflation term structures. |
| 35 | /*! \ingroup inflationtermstructures */ |
| 36 | class InflationTermStructure : public TermStructure { |
| 37 | public: |
| 38 | //! \name Constructors |
| 39 | //@{ |
| 40 | InflationTermStructure(Rate baseRate, |
| 41 | const Period& observationLag, |
| 42 | Frequency frequency, |
| 43 | const DayCounter& dayCounter = DayCounter(), |
| 44 | ext::shared_ptr<Seasonality> seasonality = {}); |
| 45 | InflationTermStructure(const Date& referenceDate, |
| 46 | Rate baseRate, |
| 47 | const Period& observationLag, |
| 48 | Frequency frequency, |
| 49 | const Calendar& calendar = Calendar(), |
| 50 | const DayCounter& dayCounter = DayCounter(), |
| 51 | ext::shared_ptr<Seasonality> seasonality = {}); |
| 52 | InflationTermStructure(Natural settlementDays, |
| 53 | const Calendar& calendar, |
| 54 | Rate baseRate, |
| 55 | const Period& observationLag, |
| 56 | Frequency frequency, |
| 57 | const DayCounter& dayCounter = DayCounter(), |
| 58 | ext::shared_ptr<Seasonality> seasonality = {}); |
| 59 | //@} |
| 60 | |
| 61 | //! \name Inflation interface |
| 62 | //@{ |
| 63 | //! The TS observes with a lag that is usually different from the |
| 64 | //! availability lag of the index. An inflation rate is given, |
| 65 | //! by default, for the maturity requested assuming this lag. |
| 66 | virtual Period observationLag() const; |
| 67 | virtual Frequency frequency() const; |
| 68 | virtual Rate baseRate() const; |
| 69 | |
| 70 | //! minimum (base) date |
| 71 | /*! Important in inflation since it starts before nominal |
| 72 | reference date. Changes depending whether index is |
| 73 | interpolated or not. When interpolated the base date |
| 74 | is just observation lag before nominal. When not |
| 75 | interpolated it is the beginning of the relevant period |
| 76 | (hence it is easy to create interpolated fixings from |
| 77 | a not-interpolated curve because interpolation, usually, |
| 78 | of fixings is forward looking). |
| 79 | */ |
| 80 | virtual Date baseDate() const = 0; |
| 81 | //@} |
| 82 | |
| 83 | //! Functions to set and get seasonality. |
| 84 | /*! Calling setSeasonality with no arguments means unsetting |
| 85 | as the default is used to choose unsetting. |
| 86 | */ |
| 87 | void setSeasonality(const ext::shared_ptr<Seasonality>& seasonality = {}); |
| 88 | ext::shared_ptr<Seasonality> seasonality() const; |
| 89 | bool hasSeasonality() const; |
| 90 | |
| 91 | protected: |
| 92 | |
| 93 | // This next part is required for piecewise- constructors |
| 94 | // because, for inflation, they need more than just the |
| 95 | // instruments to build the term structure, since the rate at |
| 96 | // time 0-lag is non-zero, since we deal (effectively) with |
| 97 | // "forwards". |
| 98 | virtual void setBaseRate(const Rate &r) { baseRate_ = r; } |
| 99 | |
| 100 | |
| 101 | // range-checking |
| 102 | void checkRange(const Date&, |
| 103 | bool ) const; |
| 104 | void checkRange(Time t, |
| 105 | bool ) const; |
| 106 | |
| 107 | ext::shared_ptr<Seasonality> seasonality_; |
| 108 | Period observationLag_; |
| 109 | Frequency frequency_; |
| 110 | mutable Rate baseRate_; |
| 111 | }; |
| 112 | |
| 113 | //! Interface for zero inflation term structures. |
| 114 | // Child classes use templates but do not want that exposed to |
| 115 | // general users. |
| 116 | class ZeroInflationTermStructure : public InflationTermStructure { |
| 117 | public: |
| 118 | //! \name Constructors |
| 119 | //@{ |
| 120 | ZeroInflationTermStructure(const DayCounter& dayCounter, |
| 121 | Rate baseZeroRate, |
| 122 | const Period& lag, |
| 123 | Frequency frequency, |
| 124 | const ext::shared_ptr<Seasonality> &seasonality = {}); |
| 125 | |
| 126 | ZeroInflationTermStructure(const Date& referenceDate, |
| 127 | const Calendar& calendar, |
| 128 | const DayCounter& dayCounter, |
| 129 | Rate baseZeroRate, |
| 130 | const Period& lag, |
| 131 | Frequency frequency, |
| 132 | const ext::shared_ptr<Seasonality>& seasonality = {}); |
| 133 | |
| 134 | ZeroInflationTermStructure(Natural settlementDays, |
| 135 | const Calendar& calendar, |
| 136 | const DayCounter& dayCounter, |
| 137 | Rate baseZeroRate, |
| 138 | const Period& lag, |
| 139 | Frequency frequency, |
| 140 | const ext::shared_ptr<Seasonality> &seasonality = {}); |
| 141 | //@} |
| 142 | |
| 143 | //! \name Inspectors |
| 144 | //@{ |
| 145 | //! zero-coupon inflation rate. |
| 146 | /*! Essentially the fair rate for a zero-coupon inflation swap |
| 147 | (by definition), i.e. the zero term structure uses yearly |
| 148 | compounding, which is assumed for ZCIIS instrument quotes. |
| 149 | |
| 150 | \note by default you get the same as lag and interpolation |
| 151 | as the term structure. |
| 152 | If you want to get predictions of RPI/CPI/etc then use an |
| 153 | index. |
| 154 | */ |
| 155 | Rate zeroRate(const Date &d, const Period& instObsLag = Period(-1,Days), |
| 156 | bool forceLinearInterpolation = false, |
| 157 | bool = false) const; |
| 158 | //! zero-coupon inflation rate. |
| 159 | /*! \warning Since inflation is highly linked to dates (lags, |
| 160 | interpolation, months for seasonality, etc) this |
| 161 | method cannot account for all effects. If you |
| 162 | call it, You'll have to manage lag, seasonality |
| 163 | etc. yourself. |
| 164 | */ |
| 165 | Rate zeroRate(Time t, |
| 166 | bool = false) const; |
| 167 | //@} |
| 168 | protected: |
| 169 | //! to be defined in derived classes |
| 170 | virtual Rate zeroRateImpl(Time t) const = 0; |
| 171 | }; |
| 172 | |
| 173 | |
| 174 | //! Base class for year-on-year inflation term structures. |
| 175 | class YoYInflationTermStructure : public InflationTermStructure { |
| 176 | public: |
| 177 | //! \name Constructors |
| 178 | //@{ |
| 179 | YoYInflationTermStructure(const DayCounter& dayCounter, |
| 180 | Rate baseYoYRate, |
| 181 | const Period& lag, |
| 182 | Frequency frequency, |
| 183 | bool indexIsInterpolated, |
| 184 | const ext::shared_ptr<Seasonality> &seasonality = {}); |
| 185 | |
| 186 | YoYInflationTermStructure(const Date& referenceDate, |
| 187 | const Calendar& calendar, |
| 188 | const DayCounter& dayCounter, |
| 189 | Rate baseYoYRate, |
| 190 | const Period& lag, |
| 191 | Frequency frequency, |
| 192 | bool indexIsInterpolated, |
| 193 | const ext::shared_ptr<Seasonality> &seasonality = {}); |
| 194 | |
| 195 | YoYInflationTermStructure(Natural settlementDays, |
| 196 | const Calendar& calendar, |
| 197 | const DayCounter& dayCounter, |
| 198 | Rate baseYoYRate, |
| 199 | const Period& lag, |
| 200 | Frequency frequency, |
| 201 | bool indexIsInterpolated, |
| 202 | const ext::shared_ptr<Seasonality> &seasonality = {}); |
| 203 | //@} |
| 204 | |
| 205 | //! \name Inspectors |
| 206 | //@{ |
| 207 | //! year-on-year inflation rate. |
| 208 | /*! The forceLinearInterpolation parameter is relative to the |
| 209 | frequency of the TS. |
| 210 | |
| 211 | \note this is not the year-on-year swap (YYIIS) rate. |
| 212 | */ |
| 213 | Rate yoyRate(const Date &d, const Period& instObsLag = Period(-1,Days), |
| 214 | bool forceLinearInterpolation = false, |
| 215 | bool = false) const; |
| 216 | //! year-on-year inflation rate. |
| 217 | /*! \warning Since inflation is highly linked to dates (lags, |
| 218 | interpolation, months for seasonality, etc) this |
| 219 | method cannot account for all effects. If you |
| 220 | call it, You'll have to manage lag, seasonality |
| 221 | etc. yourself. |
| 222 | */ |
| 223 | Rate yoyRate(Time t, |
| 224 | bool = false) const; |
| 225 | //@} |
| 226 | |
| 227 | virtual bool indexIsInterpolated() const; |
| 228 | protected: |
| 229 | //! to be defined in derived classes |
| 230 | virtual Rate yoyRateImpl(Time time) const = 0; |
| 231 | private: |
| 232 | bool indexIsInterpolated_; |
| 233 | }; |
| 234 | |
| 235 | |
| 236 | //! utility function giving the inflation period for a given date |
| 237 | std::pair<Date,Date> inflationPeriod(const Date &, |
| 238 | Frequency); |
| 239 | |
| 240 | //! utility function giving the time between two dates depending on |
| 241 | //! index frequency and interpolation, and a day counter |
| 242 | Time inflationYearFraction(Frequency , |
| 243 | bool indexIsInterpolated, |
| 244 | const DayCounter &, |
| 245 | const Date &, const Date &); |
| 246 | |
| 247 | |
| 248 | // inline |
| 249 | |
| 250 | inline Period InflationTermStructure::observationLag() const { |
| 251 | return observationLag_; |
| 252 | } |
| 253 | |
| 254 | inline Frequency InflationTermStructure::frequency() const { |
| 255 | return frequency_; |
| 256 | } |
| 257 | |
| 258 | inline Rate InflationTermStructure::baseRate() const { |
| 259 | return baseRate_; |
| 260 | } |
| 261 | |
| 262 | inline ext::shared_ptr<Seasonality> InflationTermStructure::seasonality() const { |
| 263 | return seasonality_; |
| 264 | } |
| 265 | |
| 266 | inline bool InflationTermStructure::hasSeasonality() const { |
| 267 | return static_cast<bool>(seasonality_); |
| 268 | } |
| 269 | |
| 270 | inline bool YoYInflationTermStructure::indexIsInterpolated() const { |
| 271 | return indexIsInterpolated_; |
| 272 | } |
| 273 | |
| 274 | } |
| 275 | |
| 276 | #endif |
| 277 | |