• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 20957719125

13 Jan 2026 01:02PM UTC coverage: 74.151% (-0.02%) from 74.172%
20957719125

push

github

web-flow
Remove features deprecated in version 1.37 (#2424)

6 of 6 new or added lines in 2 files covered. (100.0%)

2 existing lines in 2 files now uncovered.

57305 of 77282 relevant lines covered (74.15%)

8772418.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.14
/ql/termstructures/inflationtermstructure.cpp
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
 <https://www.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
#include <ql/indexes/inflationindex.hpp>
21
#include <ql/termstructures/inflationtermstructure.hpp>
22
#include <utility>
23

24
namespace QuantLib {
25

26
    QL_DEPRECATED_DISABLE_WARNING
27

28
    InflationTermStructure::InflationTermStructure(
×
29
                                        Date baseDate,
30
                                        Frequency frequency,
31
                                        const DayCounter& dayCounter,
32
                                        ext::shared_ptr<Seasonality> seasonality,
33
                                        Rate baseRate)
×
34
    : TermStructure(dayCounter), seasonality_(std::move(seasonality)),
×
35
      frequency_(frequency), baseRate_(baseRate), baseDate_(baseDate) {
×
36
        if (seasonality_ != nullptr) {
×
37
            QL_REQUIRE(seasonality_->isConsistent(*this),
×
38
                       "Seasonality inconsistent with inflation term structure");
39
        }
40
    }
×
41

42
    InflationTermStructure::InflationTermStructure(
28✔
43
                                        const Date& referenceDate,
44
                                        Date baseDate,
45
                                        Frequency frequency,
46
                                        const DayCounter& dayCounter,
47
                                        ext::shared_ptr<Seasonality> seasonality,
48
                                        Rate baseRate)
×
49
    : TermStructure(referenceDate, Calendar(), dayCounter), seasonality_(std::move(seasonality)),
56✔
50
      frequency_(frequency), baseRate_(baseRate), baseDate_(baseDate) {
56✔
51
        if (seasonality_ != nullptr) {
28✔
52
            QL_REQUIRE(seasonality_->isConsistent(*this),
×
53
                       "Seasonality inconsistent with inflation term structure");
54
        }
55
    }
28✔
56

57
    InflationTermStructure::InflationTermStructure(
×
58
                                        Natural settlementDays,
59
                                        const Calendar& calendar,
60
                                        Date baseDate,
61
                                        Frequency frequency,
62
                                        const DayCounter& dayCounter,
63
                                        ext::shared_ptr<Seasonality> seasonality,
64
                                        Rate baseRate)
×
65
    : TermStructure(settlementDays, calendar, dayCounter), seasonality_(std::move(seasonality)),
×
66
      frequency_(frequency), baseRate_(baseRate), baseDate_(baseDate) {
×
67
        if (seasonality_ != nullptr) {
×
68
            QL_REQUIRE(seasonality_->isConsistent(*this),
×
69
                       "Seasonality inconsistent with inflation term structure");
70
        }
71
    }
×
72

73
    QL_DEPRECATED_ENABLE_WARNING
74

75
    Date InflationTermStructure::baseDate() const {
243,743✔
76
        return baseDate_;
243,743✔
77
    }
78

79
    void InflationTermStructure::setSeasonality(
5✔
80
                          const ext::shared_ptr<Seasonality>& seasonality) {
81
        // always reset, whether with null or new pointer
82
        seasonality_ = seasonality;
5✔
83
        if (seasonality_ != nullptr) {
5✔
84
            QL_REQUIRE(seasonality_->isConsistent(*this),
4✔
85
                       "Seasonality inconsistent with inflation term structure");
86
        }
87
        update();
5✔
88
    }
5✔
89

90

91
    void InflationTermStructure::checkRange(const Date& d,
239,992✔
92
                                            bool extrapolate) const {
93
        QL_REQUIRE(d >= baseDate(),
239,992✔
94
                   "date (" << d << ") is before base date (" << baseDate() << ")");
95
        QL_REQUIRE(extrapolate || allowsExtrapolation() || d <= maxDate(),
239,992✔
96
                   "date (" << d << ") is past max curve date ("
97
                   << maxDate() << ")");
98
    }
239,992✔
99

100
    void InflationTermStructure::checkRange(Time t,
2✔
101
                                            bool extrapolate) const {
102
        QL_REQUIRE(t >= timeFromReference(baseDate()),
2✔
103
                   "time (" << t << ") is before base date");
104
        QL_REQUIRE(extrapolate || allowsExtrapolation() || t <= maxTime(),
2✔
105
                   "time (" << t << ") is past max curve time ("
106
                   << maxTime() << ")");
107
    }
2✔
108

109

110
    ZeroInflationTermStructure::ZeroInflationTermStructure(
×
111
                                   Date baseDate,
112
                                   Frequency frequency,
113
                                   const DayCounter& dayCounter,
114
                                   const ext::shared_ptr<Seasonality>& seasonality)
×
115
    : InflationTermStructure(baseDate, frequency, dayCounter, seasonality) {}
×
116

117
    ZeroInflationTermStructure::ZeroInflationTermStructure(
17✔
118
                                   const Date& referenceDate,
119
                                   Date baseDate,
120
                                   Frequency frequency,
121
                                   const DayCounter& dayCounter,
122
                                   const ext::shared_ptr<Seasonality>& seasonality)
×
123
    : InflationTermStructure(referenceDate, baseDate, frequency, dayCounter, seasonality) {}
34✔
124

125
    ZeroInflationTermStructure::ZeroInflationTermStructure(
×
126
                                   Natural settlementDays,
127
                                   const Calendar& calendar,
128
                                   Date baseDate,
129
                                   Frequency frequency,
130
                                   const DayCounter& dayCounter,
131
                                   const ext::shared_ptr<Seasonality>& seasonality)
×
132
    : InflationTermStructure(settlementDays, calendar, baseDate, frequency, dayCounter, seasonality) {}
×
133

134
    Rate ZeroInflationTermStructure::zeroRate(const Date &d, bool extrapolate) const {
4,080✔
135
        QL_DEPRECATED_DISABLE_WARNING
136
        return zeroRate(d, Period(0, Days), false, extrapolate);
4,080✔
137
        QL_DEPRECATED_ENABLE_WARNING
138
    }
139

140
    Rate ZeroInflationTermStructure::zeroRate(const Date &d, const Period& instObsLag,
4,677✔
141
                                              bool forceLinearInterpolation,
142
                                              bool extrapolate) const {
143

144
        Period useLag = instObsLag;
4,677✔
145
        if (instObsLag == Period(-1,Days)) {
4,677✔
146
            useLag = Period(0, Days);
147
        }
148

149
        Rate zeroRate;
150
        if (forceLinearInterpolation) {
4,677✔
151
            std::pair<Date,Date> dd = inflationPeriod(d-useLag, frequency());
×
152
            dd.second = dd.second + Period(1,Days);
×
153
            Real dp = dd.second - dd.first;
×
154
            Real dt = d - dd.first;
×
155
            // if we are interpolating we only check the exact point
156
            // this prevents falling off the end at curve maturity
157
            InflationTermStructure::checkRange(d, extrapolate);
×
158
            Time t1 = timeFromReference(dd.first);
×
159
            Time t2 = timeFromReference(dd.second);
×
160
            Rate z1 = zeroRateImpl(t1);
×
161
            Rate z2 = zeroRateImpl(t2);
×
162
            zeroRate = z1 + (z2-z1) * (dt/dp);
×
163
        } else {
164
            std::pair<Date,Date> dd = inflationPeriod(d-useLag, frequency());
9,354✔
165
            InflationTermStructure::checkRange(dd.first, extrapolate);
4,677✔
166
            Time t = timeFromReference(dd.first);
4,677✔
167
            zeroRate = zeroRateImpl(t);
4,677✔
168
        }
169

170
        if (hasSeasonality()) {
4,677✔
171
            zeroRate = seasonality()->correctZeroRate(d-useLag, zeroRate, *this);
452✔
172
        }
173
        return zeroRate;
4,677✔
174
    }
175

176
    Rate ZeroInflationTermStructure::zeroRate(Time t,
1✔
177
                                              bool extrapolate) const {
178
        checkRange(t, extrapolate);
1✔
179
        return zeroRateImpl(t);
1✔
180
    }
181

182

UNCOV
183
    YoYInflationTermStructure::YoYInflationTermStructure(
×
184
                                    Date baseDate,
185
                                    Rate baseYoYRate,
186
                                    Frequency frequency,
187
                                    const DayCounter& dayCounter,
188
                                    const ext::shared_ptr<Seasonality> &seasonality)
×
189
    : InflationTermStructure(baseDate, frequency, dayCounter, seasonality, baseYoYRate) {}
×
190

191
    YoYInflationTermStructure::YoYInflationTermStructure(
11✔
192
                                    const Date& referenceDate,
193
                                    Date baseDate,
194
                                    Rate baseYoYRate,
195
                                    Frequency frequency,
196
                                    const DayCounter& dayCounter,
197
                                    const ext::shared_ptr<Seasonality> &seasonality)
×
198
    : InflationTermStructure(referenceDate, baseDate, frequency, dayCounter, seasonality, baseYoYRate) {}
22✔
199

200
    YoYInflationTermStructure::YoYInflationTermStructure(
×
201
                                    Natural settlementDays,
202
                                    const Calendar& calendar,
203
                                    Date baseDate,
204
                                    Rate baseYoYRate,
205
                                    Frequency frequency,
206
                                    const DayCounter& dayCounter,
207
                                    const ext::shared_ptr<Seasonality> &seasonality)
×
208
    : InflationTermStructure(settlementDays, calendar, baseDate, frequency, dayCounter, seasonality, baseYoYRate) {}
×
209

210
    Rate YoYInflationTermStructure::yoyRate(const Date &d, bool extrapolate) const {
235,315✔
211
        QL_DEPRECATED_DISABLE_WARNING
212
        return yoyRate(d, Period(0, Days), false, extrapolate);
235,315✔
213
        QL_DEPRECATED_ENABLE_WARNING
214
    }
215

216
    Rate YoYInflationTermStructure::yoyRate(const Date &d, const Period& instObsLag,
235,315✔
217
                                            bool forceLinearInterpolation,
218
                                            bool extrapolate) const {
219

220
        Period useLag = instObsLag;
235,315✔
221
        if (instObsLag == Period(-1,Days)) {
235,315✔
222
            useLag = Period(0, Days);
223
        }
224

225
        Rate yoyRate;
226
        if (forceLinearInterpolation) {
235,315✔
227
            std::pair<Date,Date> dd = inflationPeriod(d-useLag, frequency());
×
228
            dd.second = dd.second + Period(1,Days);
×
229
            Real dp = dd.second - dd.first;
×
230
            Real dt = (d-useLag) - dd.first;
×
231
            // if we are interpolating we only check the exact point
232
            // this prevents falling off the end at curve maturity
233
            InflationTermStructure::checkRange(d, extrapolate);
×
234
            Time t1 = timeFromReference(dd.first);
×
235
            Time t2 = timeFromReference(dd.second);
×
236
            Rate y1 = yoyRateImpl(t1);
×
237
            Rate y2 = yoyRateImpl(t2);
×
238
            yoyRate = y1 + (y2-y1) * (dt/dp);
×
239
        } else {
240
            std::pair<Date,Date> dd = inflationPeriod(d-useLag, frequency());
470,630✔
241
            InflationTermStructure::checkRange(dd.first, extrapolate);
235,315✔
242
            Time t = timeFromReference(dd.first);
235,315✔
243
            yoyRate = yoyRateImpl(t);
235,315✔
244
        }
245

246
        if (hasSeasonality()) {
235,315✔
247
            yoyRate = seasonality()->correctYoYRate(d-useLag, yoyRate, *this);
×
248
        }
249
        return yoyRate;
235,315✔
250
    }
251

252
    Rate YoYInflationTermStructure::yoyRate(Time t,
1✔
253
                                            bool extrapolate) const {
254
        checkRange(t, extrapolate);
1✔
255
        return yoyRateImpl(t);
1✔
256
    }
257

258

259

260

261
    std::pair<Date,Date> inflationPeriod(const Date& d,
2,037,724✔
262
                                         Frequency frequency) {
263
        Month month = d.month();
2,037,724✔
264
        Year year = d.year();
2,037,724✔
265

266
        Month startMonth, endMonth;
267
        switch (frequency) {
2,037,724✔
268
          case Annual:
1,413,662✔
269
          case Semiannual:
270
          case EveryFourthMonth:
271
          case Quarterly:
272
          case Bimonthly: {
273
                int nMonths = 12 / frequency;
1,413,662✔
274
                startMonth = Month(month - (month - 1) % nMonths);
1,413,662✔
275
                endMonth = Month(startMonth + nMonths - 1);
1,413,662✔
276
            }
277
            break;
1,413,662✔
278
          case Monthly:
279
            startMonth = endMonth = month;
280
            break;
281
          default:
×
282
            QL_FAIL("Frequency not handled: " << frequency);
×
283
            break;
284
        };
285

286
        return {Date(1, startMonth, year), Date::endOfMonth(Date(1, endMonth, year))};
2,037,724✔
287
    }
288

289

290
    Time inflationYearFraction(Frequency f, bool indexIsInterpolated,
5,307✔
291
                               const DayCounter &dayCounter,
292
                               const Date &d1, const Date &d2) {
293

294
        Time t=0;
295
        if (indexIsInterpolated) {
5,307✔
296
            // N.B. we do not use linear interpolation between flat
297
            // fixing forecasts for forecasts.  This avoids awkwardnesses
298
            // when bootstrapping the inflation curve.
299
            t = dayCounter.yearFraction(d1, d2);
×
300
        } else {
301
            // I.e. fixing is constant for the whole inflation period.
302
            // Use the value for half way along the period.
303
            // But the inflation time is the time between period starts
304
            std::pair<Date,Date> limD1 = inflationPeriod(d1, f);
5,307✔
305
            std::pair<Date,Date> limD2 = inflationPeriod(d2, f);
5,307✔
306
            t = dayCounter.yearFraction(limD1.first, limD2.first);
5,307✔
307
        }
308

309
        return t;
5,307✔
310
    }
311

312

313
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc