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                            const std::vector<Handle<Quote> >& jumps = std::vector<Handle<Quote> >(),
56                            const std::vector<Date>& jumpDates = std::vector<Date>());
57         YieldTermStructure(Natural settlementDays,
58                            const Calendar& cal,
59                            const DayCounter& dc = DayCounter(),
60                            const std::vector<Handle<Quote> >& jumps = std::vector<Handle<Quote> >(),
61                            const std::vector<Date>& jumpDates = std::vector<Date>());
62 
63         /*! \deprecated Passing jumps without a reference date never worked correctly.
64                         Use one of the other constructors instead.
65                         Deprecated in version 1.19.
66         */
67         QL_DEPRECATED
68         YieldTermStructure(const DayCounter& dc,
69                            const std::vector<Handle<Quote> >& jumps,
70                            const std::vector<Date>& jumpDates = std::vector<Date>());
71         //@}
72 
73         /*! \name Discount factors
74 
75             These methods return the discount factor from a given date or time
76             to the reference date.  In the latter case, the time is calculated
77             as a fraction of year from the reference date.
78         */
79         //@{
80         DiscountFactor discount(const Date& d,
81                                 bool extrapolate = false) const;
82         /*! The same day-counting rule used by the term structure
83             should be used for calculating the passed time t.
84         */
85         DiscountFactor discount(Time t,
86                                 bool extrapolate = false) const;
87         //@}
88 
89         /*! \name Zero-yield rates
90 
91             These methods return the implied zero-yield rate for a
92             given date or time.  In the former case, the time is
93             calculated as a fraction of year from the reference date.
94         */
95         //@{
96         /*! The resulting interest rate has the required daycounting
97             rule.
98         */
99         InterestRate zeroRate(const Date& d,
100                               const DayCounter& resultDayCounter,
101                               Compounding comp,
102                               Frequency freq = Annual,
103                               bool extrapolate = false) const;
104 
105         /*! The resulting interest rate has the same day-counting rule
106             used by the term structure. The same rule should be used
107             for calculating the passed time t.
108         */
109         InterestRate zeroRate(Time t,
110                               Compounding comp,
111                               Frequency freq = Annual,
112                               bool extrapolate = false) const;
113         //@}
114 
115         /*! \name Forward rates
116 
117             These methods returns the forward interest rate between two dates
118             or times.  In the former case, times are calculated as fractions
119             of year from the reference date.
120 
121             If both dates (times) are equal the instantaneous forward rate is
122             returned.
123         */
124         //@{
125         /*! The resulting interest rate has the required day-counting
126             rule.
127         */
128         InterestRate forwardRate(const Date& d1,
129                                  const Date& d2,
130                                  const DayCounter& resultDayCounter,
131                                  Compounding comp,
132                                  Frequency freq = Annual,
133                                  bool extrapolate = false) const;
134         /*! The resulting interest rate has the required day-counting
135             rule.
136             \warning dates are not adjusted for holidays
137         */
138         InterestRate forwardRate(const Date& d,
139                                  const Period& p,
140                                  const DayCounter& resultDayCounter,
141                                  Compounding comp,
142                                  Frequency freq = Annual,
143                                  bool extrapolate = false) const;
144 
145         /*! The resulting interest rate has the same day-counting rule
146             used by the term structure. The same rule should be used
147             for calculating the passed times t1 and t2.
148         */
149         InterestRate forwardRate(Time t1,
150                                  Time t2,
151                                  Compounding comp,
152                                  Frequency freq = Annual,
153                                  bool extrapolate = false) const;
154         //@}
155 
156         //! \name Jump inspectors
157         //@{
158         const std::vector<Date>& jumpDates() const;
159         const std::vector<Time>& jumpTimes() const;
160         //@}
161 
162         //! \name Observer interface
163         //@{
164         void update();
165         //@}
166       protected:
167         /*! \name Calculations
168 
169             This method must be implemented in derived classes to
170             perform the actual calculations. When it is called,
171             range check has already been performed; therefore, it
172             must assume that extrapolation is required.
173         */
174         //@{
175         //! discount factor calculation
176         virtual DiscountFactor discountImpl(Time) const = 0;
177         //@}
178       private:
179         // methods
180         void setJumps(const Date& referenceDate);
181         // data members
182         std::vector<Handle<Quote> > jumps_;
183         std::vector<Date> jumpDates_;
184         std::vector<Time> jumpTimes_;
185         Size nJumps_;
186         Date latestReference_;
187     };
188 
189     // inline definitions
190 
191     inline
discount(const Date & d,bool extrapolate) const192     DiscountFactor YieldTermStructure::discount(const Date& d,
193                                                 bool extrapolate) const {
194         return discount(timeFromReference(d), extrapolate);
195     }
196 
197     inline
forwardRate(const Date & d,const Period & p,const DayCounter & dayCounter,Compounding comp,Frequency freq,bool extrapolate) const198     InterestRate YieldTermStructure::forwardRate(const Date& d,
199                                                  const Period& p,
200                                                  const DayCounter& dayCounter,
201                                                  Compounding comp,
202                                                  Frequency freq,
203                                                  bool extrapolate) const {
204         return forwardRate(d, d+p, dayCounter, comp, freq, extrapolate);
205     }
206 
jumpDates() const207     inline const std::vector<Date>& YieldTermStructure::jumpDates() const {
208         return this->jumpDates_;
209     }
210 
jumpTimes() const211     inline const std::vector<Time>& YieldTermStructure::jumpTimes() const {
212         return this->jumpTimes_;
213     }
214 
215 }
216 
217 #endif
218