File: inflationtermstructure.cpp

package info (click to toggle)
quantlib 0.9.0.20071224-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 22,216 kB
  • ctags: 34,951
  • sloc: cpp: 167,744; ansic: 21,483; sh: 8,947; makefile: 3,327; lisp: 86
file content (224 lines) | stat: -rw-r--r-- 9,440 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2007 Chris Kenyon

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <http://quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

#include <ql/termstructures/inflationtermstructure.hpp>

namespace QuantLib {

    InflationTermStructure::InflationTermStructure(
                                        const Period& lag,
                                        Frequency frequency,
                                        Rate baseRate,
                                        const Handle<YieldTermStructure>& yTS,
                                        const DayCounter& dayCounter)
    : TermStructure(dayCounter), nominalTermStructure_(yTS),
      lag_(lag), frequency_(frequency), baseRate_(baseRate) {
        registerWith(nominalTermStructure_);
    }

    InflationTermStructure::InflationTermStructure(
                                        const Date& referenceDate,
                                        const Period& lag,
                                        Frequency frequency,
                                        Rate baseRate,
                                        const Handle<YieldTermStructure>& yTS,
                                        const Calendar& calendar,
                                        const DayCounter& dayCounter)
    : TermStructure(referenceDate, calendar, dayCounter),
      nominalTermStructure_(yTS), lag_(lag),
      frequency_(frequency), baseRate_(baseRate) {
        registerWith(nominalTermStructure_);
    }

    InflationTermStructure::InflationTermStructure(
                                        Natural settlementDays,
                                        const Calendar& calendar,
                                        const Period& lag,
                                        Frequency frequency,
                                        Rate baseRate,
                                        const Handle<YieldTermStructure>& yTS,
                                        const DayCounter &dayCounter)
    : TermStructure(settlementDays, calendar, dayCounter),
      nominalTermStructure_(yTS), lag_(lag),
      frequency_(frequency), baseRate_(baseRate) {
        registerWith(nominalTermStructure_);
    }


    Period InflationTermStructure::lag() const {
        return lag_;
    }


    Frequency InflationTermStructure::frequency() const {
        return frequency_;
    }

    Handle<YieldTermStructure>
    InflationTermStructure::nominalTermStructure() const {
        return nominalTermStructure_;
    }

    Rate InflationTermStructure::baseRate() const {
        return baseRate_;
    }

    void InflationTermStructure::checkRange(const Date& d,
                                            bool extrapolate) const {
        QL_REQUIRE(d >= baseDate(),
                   "date (" << d << ") is before base date");
        QL_REQUIRE(extrapolate || allowsExtrapolation() || d <= maxDate(),
                   "date (" << d << ") is past max curve date ("
                   << maxDate() << ")");
    }

    void InflationTermStructure::checkRange(Time t,
                                            bool extrapolate) const {
        QL_REQUIRE(t >= timeFromReference(baseDate()),
                   "time (" << t << ") is before base date");
        QL_REQUIRE(extrapolate || allowsExtrapolation() || t <= maxTime(),
                   "time (" << t << ") is past max curve time ("
                   << maxTime() << ")");
    }



    ZeroInflationTermStructure::ZeroInflationTermStructure(
                                    const DayCounter& dayCounter,
                                    const Period& lag,
                                    Frequency frequency,
                                    Rate baseZeroRate,
                                    const Handle<YieldTermStructure>& yTS)
    : InflationTermStructure(lag, frequency, baseZeroRate, yTS, dayCounter) {}

    ZeroInflationTermStructure::ZeroInflationTermStructure(
                                    const Date& referenceDate,
                                    const Calendar& calendar,
                                    const DayCounter& dayCounter,
                                    const Period& lag,
                                    Frequency frequency,
                                    Rate baseZeroRate,
                                    const Handle<YieldTermStructure>& yTS)
    : InflationTermStructure(referenceDate, lag, frequency, baseZeroRate,
                             yTS, calendar, dayCounter) {}

    ZeroInflationTermStructure::ZeroInflationTermStructure(
                                    Natural settlementDays,
                                    const Calendar& calendar,
                                    const DayCounter& dayCounter,
                                    const Period& lag,
                                    Frequency frequency,
                                    Rate baseZeroRate,
                                    const Handle<YieldTermStructure>& yTS)
    : InflationTermStructure(settlementDays, calendar, lag, frequency,
                             baseZeroRate, yTS, dayCounter) {}

    Rate ZeroInflationTermStructure::zeroRate(const Date &d,
                                              bool extrapolate) const {
        InflationTermStructure::checkRange(d, extrapolate);
        return zeroRateImpl(timeFromReference(d));
    }

    Rate ZeroInflationTermStructure::zeroRate(Time t,
                                              bool extrapolate) const {
        InflationTermStructure::checkRange(t, extrapolate);
        return zeroRateImpl(t);
    }


    YoYInflationTermStructure::YoYInflationTermStructure(
                                    const DayCounter& dayCounter,
                                    const Period& lag,
                                    Frequency frequency,
                                    Rate baseYoYRate,
                                    const Handle<YieldTermStructure>& yTS)
    : InflationTermStructure(lag, frequency, baseYoYRate, yTS, dayCounter) {}

    YoYInflationTermStructure::YoYInflationTermStructure(
                                    const Date& referenceDate,
                                    const Calendar& calendar,
                                    const DayCounter& dayCounter,
                                    const Period& lag,
                                    Frequency frequency,
                                    Rate baseYoYRate,
                                    const Handle<YieldTermStructure>& yTS)
    : InflationTermStructure(referenceDate, lag, frequency, baseYoYRate,
                             yTS, calendar, dayCounter) {}

    YoYInflationTermStructure::YoYInflationTermStructure(
                                    Natural settlementDays,
                                    const Calendar& calendar,
                                    const DayCounter& dayCounter,
                                    const Period& lag,
                                    Frequency frequency,
                                    Rate baseYoYRate,
                                    const Handle<YieldTermStructure>& yTS)
    : InflationTermStructure(settlementDays, calendar,lag, frequency,
                             baseYoYRate, yTS, dayCounter) {}

    Rate YoYInflationTermStructure::yoyRate(const Date &d,
                                            bool extrapolate) const {
        InflationTermStructure::checkRange(d, extrapolate);
        return yoyRateImpl(timeFromReference(d));
    }

    Rate YoYInflationTermStructure::yoyRate(Time t,
                                            bool extrapolate) const {
        InflationTermStructure::checkRange(t, extrapolate);
        return yoyRateImpl(t);
    }



    std::pair<Date,Date> inflationPeriod(const Date& d,
                                         Frequency frequency) {

        Month month = d.month();
        Year year = d.year();

        Month startMonth, endMonth;
        switch (frequency) {
          case Annual:
            startMonth = January;
            endMonth = December;
            break;
          case Semiannual:
            startMonth = Month((month-1)/6 + 1);
            endMonth = Month(startMonth + 5);
            break;
          case Quarterly:
            startMonth = Month((month-1)/3 + 1);
            endMonth = Month(startMonth + 2);
            break;
          case Monthly:
            startMonth = endMonth = month;
            break;
          default:
            QL_FAIL("Frequency not handled: " << frequency);
            break;
        };

        Date startDate = Date(1, startMonth, year);
        Date endDate = Date::endOfMonth(Date(1, endMonth, year));

        return std::make_pair(startDate,endDate);
    }

}