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

lballabio / QuantLib / 18089177727

29 Sep 2025 07:23AM UTC coverage: 73.914% (+0.1%) from 73.816%
18089177727

Pull #2265

github

web-flow
Merge 13a7b3b53 into 787317697
Pull Request #2265: Fix potential dangling reference in MultiCubicSpline

2 of 2 new or added lines in 1 file covered. (100.0%)

219 existing lines in 29 files now uncovered.

57063 of 77202 relevant lines covered (73.91%)

8763673.17 hits per line

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

91.95
/ql/termstructures/inflation/inflationhelpers.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
 Copyright (C) 2007 StatPro Italia srl
6

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

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

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

21
#include <ql/cashflows/inflationcouponpricer.hpp>
22
#include <ql/indexes/inflationindex.hpp>
23
#include <ql/pricingengines/swap/discountingswapengine.hpp>
24
#include <ql/shared_ptr.hpp>
25
#include <ql/termstructures/inflation/inflationhelpers.hpp>
26
#include <ql/termstructures/yield/flatforward.hpp>
27
#include <ql/utilities/null_deleter.hpp>
28
#include <utility>
29

30
namespace QuantLib {
31

32
    QL_DEPRECATED_DISABLE_WARNING
33

34
    ZeroCouponInflationSwapHelper::ZeroCouponInflationSwapHelper(
161✔
35
        const Handle<Quote>& quote,
36
        const Period& swapObsLag,
37
        const Date& maturity,
38
        Calendar calendar,
39
        BusinessDayConvention paymentConvention,
40
        const DayCounter& dayCounter,
41
        const ext::shared_ptr<ZeroInflationIndex>& zii,
42
        CPI::InterpolationType observationInterpolation)
161✔
43
    : ZeroCouponInflationSwapHelper(
44
        quote, swapObsLag, maturity, std::move(calendar), paymentConvention,
45
        dayCounter, zii, observationInterpolation,
46
        // any nominal term structure will give the same result;
47
        // when calculating the fair rate, the equal discount factors
48
        // for the payments on the two legs will cancel out.
49
        Handle<YieldTermStructure>(ext::make_shared<FlatForward>(0, NullCalendar(), 0.0, dayCounter))) {}
644✔
50

51
    ZeroCouponInflationSwapHelper::ZeroCouponInflationSwapHelper(
175✔
52
        const Handle<Quote>& quote,
53
        const Period& swapObsLag,
54
        const Date& maturity,
55
        Calendar calendar,
56
        BusinessDayConvention paymentConvention,
57
        DayCounter dayCounter,
58
        const ext::shared_ptr<ZeroInflationIndex>& zii,
59
        CPI::InterpolationType observationInterpolation,
60
        Handle<YieldTermStructure> nominalTermStructure)
175✔
61
    : RelativeDateBootstrapHelper<ZeroInflationTermStructure>(quote), swapObsLag_(swapObsLag),
175✔
62
      maturity_(maturity), calendar_(std::move(calendar)), paymentConvention_(paymentConvention),
175✔
63
      dayCounter_(std::move(dayCounter)), observationInterpolation_(observationInterpolation),
175✔
64
      nominalTermStructure_(std::move(nominalTermStructure)) {
350✔
65
        zii_ = zii->clone(termStructureHandle_);
175✔
66
        // We want to be notified of changes of fixings, but we don't
67
        // want notifications from termStructureHandle_ (they would
68
        // interfere with bootstrapping.)
69
        zii_->unregisterWith(termStructureHandle_);
175✔
70

71
        auto fixingPeriod = inflationPeriod(maturity_ - swapObsLag_, zii_->frequency());
175✔
72
        auto interpolationPeriod = inflationPeriod(maturity, zii_->frequency());
175✔
73

74
        if (detail::CPI::isInterpolated(observationInterpolation_) && maturity > interpolationPeriod.first) {
175✔
75
            // if interpolated, we need to cover the end of the interpolation period
UNCOV
76
            earliestDate_ = fixingPeriod.first;
×
UNCOV
77
            latestDate_ = fixingPeriod.second + 1;
×
78
        } else {
79
            // if not interpolated, the date of the initial fixing is enough
80
            earliestDate_ = fixingPeriod.first;
175✔
81
            latestDate_ = fixingPeriod.first;
175✔
82
        }
83

84
        // check that the observation lag of the swap
85
        // is compatible with the availability lag of the index AND
86
        // it's interpolation (assuming the start day is spot)
87
        if (detail::CPI::isInterpolated(observationInterpolation_)) {
175✔
UNCOV
88
            Period pShift(zii_->frequency());
×
UNCOV
89
            QL_REQUIRE(swapObsLag_ - pShift >= zii_->availabilityLag(),
×
90
                       "inconsistency between swap observation lag "
91
                           << swapObsLag_ << ", index period " << pShift << " and index availability "
92
                           << zii_->availabilityLag() << ": need (obsLag-index period) >= availLag");
93
        }
94

95
        registerWith(zii_);
350✔
96
        registerWith(nominalTermStructure_);
175✔
97
        ZeroCouponInflationSwapHelper::initializeDates();
175✔
98
    }
175✔
99

100
    QL_DEPRECATED_ENABLE_WARNING
101

102

103
    Real ZeroCouponInflationSwapHelper::impliedQuote() const {
1,533✔
104
        zciis_->deepUpdate();
1,533✔
105
        return zciis_->fairRate();
1,533✔
106
    }
107

108
    void ZeroCouponInflationSwapHelper::initializeDates() {
175✔
109
        zciis_ = ext::make_shared<ZeroCouponInflationSwap>(
350✔
110
            Swap::Payer, 1.0, evaluationDate_, maturity_, calendar_,
350✔
111
            paymentConvention_, dayCounter_, 0.0, zii_, swapObsLag_,
350✔
112
            observationInterpolation_);
175✔
113
        // Because very simple instrument only takes
114
        // standard discounting swap engine.
115
        zciis_->setPricingEngine(
525✔
116
            ext::make_shared<DiscountingSwapEngine>(nominalTermStructure_));
175✔
117
    }
175✔
118

119
    void ZeroCouponInflationSwapHelper::setTermStructure(ZeroInflationTermStructure* z) {
251✔
120
        // do not set the relinkable handle as an observer -
121
        // force recalculation when needed
122
        bool observer = false;
123

124
        ext::shared_ptr<ZeroInflationTermStructure> temp(z, null_deleter());
125
        termStructureHandle_.linkTo(std::move(temp), observer);
251✔
126

127
        RelativeDateBootstrapHelper<ZeroInflationTermStructure>::setTermStructure(z);
251✔
128
    }
251✔
129

130

131
    YearOnYearInflationSwapHelper::YearOnYearInflationSwapHelper(
165✔
132
        const Handle<Quote>& quote,
133
        const Period& swapObsLag,
134
        const Date& maturity,
135
        Calendar calendar,
136
        BusinessDayConvention paymentConvention,
137
        DayCounter dayCounter,
138
        const ext::shared_ptr<YoYInflationIndex>& yii,
139
        CPI::InterpolationType interpolation,
140
        Handle<YieldTermStructure> nominalTermStructure)
165✔
141
    : RelativeDateBootstrapHelper<YoYInflationTermStructure>(quote), swapObsLag_(swapObsLag),
165✔
142
      maturity_(maturity), calendar_(std::move(calendar)), paymentConvention_(paymentConvention),
165✔
143
      dayCounter_(std::move(dayCounter)), interpolation_(interpolation),
165✔
144
      nominalTermStructure_(std::move(nominalTermStructure)) {
330✔
145
        yii_ = yii->clone(termStructureHandle_);
165✔
146
        // We want to be notified of changes of fixings, but we don't
147
        // want notifications from termStructureHandle_ (they would
148
        // interfere with bootstrapping.)
149
        yii_->unregisterWith(termStructureHandle_);
165✔
150

151
        auto fixingPeriod = inflationPeriod(maturity_ - swapObsLag_, yii_->frequency());
165✔
152
        auto interpolationPeriod = inflationPeriod(maturity, yii_->frequency());
165✔
153

154
        if (detail::CPI::isInterpolated(interpolation_, yii_) && maturity > interpolationPeriod.first) {
165✔
155
            // if interpolated, we need to cover the end of the interpolation period
156
            earliestDate_ = fixingPeriod.first;
60✔
157
            latestDate_ = fixingPeriod.second + 1;
60✔
158
        } else {
159
            // if not interpolated, the date of the initial fixing is enough
160
            earliestDate_ = fixingPeriod.first;
105✔
161
            latestDate_ = fixingPeriod.first;
105✔
162
        }
163

164
        // check that the observation lag of the swap
165
        // is compatible with the availability lag of the index AND
166
        // its interpolation (assuming the start day is spot)
167
        if (detail::CPI::isInterpolated(interpolation_, yii_)) {
165✔
168
            Period pShift(yii_->frequency());
60✔
169
            QL_REQUIRE(swapObsLag_ - pShift >= yii_->availabilityLag(),
120✔
170
                       "inconsistency between swap observation lag "
171
                       << swapObsLag_ << ", index period " << pShift << " and index availability "
172
                       << yii_->availabilityLag() << ": need (obsLag-index period) >= availLag");
173
        }
174

175
        registerWith(yii_);
330✔
176
        registerWith(nominalTermStructure_);
165✔
177
        YearOnYearInflationSwapHelper::initializeDates();
165✔
178
    }
165✔
179

UNCOV
180
    YearOnYearInflationSwapHelper::YearOnYearInflationSwapHelper(
×
181
        const Handle<Quote>& quote,
182
        const Period& swapObsLag,
183
        const Date& maturity,
184
        Calendar calendar,
185
        BusinessDayConvention paymentConvention,
186
        DayCounter dayCounter,
187
        const ext::shared_ptr<YoYInflationIndex>& yii,
UNCOV
188
        Handle<YieldTermStructure> nominalTermStructure)
×
189
    : YearOnYearInflationSwapHelper(quote, swapObsLag, maturity, std::move(calendar), paymentConvention,
UNCOV
190
                                    std::move(dayCounter), yii, CPI::AsIndex, std::move(nominalTermStructure)) {}
×
191

192

193
    Real YearOnYearInflationSwapHelper::impliedQuote() const {
970✔
194
        yyiis_->deepUpdate();
970✔
195
        return yyiis_->fairRate();
970✔
196
    }
197

198
    void YearOnYearInflationSwapHelper::initializeDates() {
165✔
199
        // always works because tenor is always 1 year so
200
        // no problem with different days-in-month
201
        Schedule fixedSchedule = MakeSchedule()
165✔
202
                                     .from(evaluationDate_)
165✔
203
                                     .to(maturity_)
165✔
204
                                     .withTenor(1 * Years)
330✔
205
                                     .withConvention(Unadjusted)
165✔
206
                                     .withCalendar(calendar_) // fixed leg gets cal from sched
165✔
207
                                     .backwards();
165✔
208
        const Schedule& yoySchedule = fixedSchedule;
209

210
        yyiis_ = ext::make_shared<YearOnYearInflationSwap>(
330✔
211
            Swap::Payer, 1.0, fixedSchedule, 0.0, dayCounter_,
330✔
212
            yoySchedule, yii_, swapObsLag_, interpolation_,
165✔
213
            0.0, dayCounter_, calendar_, paymentConvention_);
165✔
214

215
        // The instrument takes a standard discounting swap engine.
216
        // The inflation-related work is done by the coupons.
217

218
        yyiis_->setPricingEngine(
495✔
219
            ext::make_shared<DiscountingSwapEngine>(nominalTermStructure_));
165✔
220
    }
165✔
221

222
    void YearOnYearInflationSwapHelper::setTermStructure(YoYInflationTermStructure* y) {
165✔
223
        // do not set the relinkable handle as an observer -
224
        // force recalculation when needed
225
        bool observer = false;
226

227
        ext::shared_ptr<YoYInflationTermStructure> temp(y, null_deleter());
228
        termStructureHandle_.linkTo(std::move(temp), observer);
165✔
229

230
        RelativeDateBootstrapHelper<YoYInflationTermStructure>::setTermStructure(y);
165✔
231
    }
165✔
232

233
}
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