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

lballabio / QuantLib / 18905857194

29 Oct 2025 11:09AM UTC coverage: 74.32% (+0.4%) from 73.914%
18905857194

Pull #2344

github

web-flow
Merge d353587bf into d823f4ecb
Pull Request #2344: add multicurve bootstrap

99 of 103 new or added lines in 8 files covered. (96.12%)

216 existing lines in 13 files now uncovered.

57072 of 76792 relevant lines covered (74.32%)

8781066.47 hits per line

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

41.82
/ql/instruments/yearonyearinflationswap.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) 2009 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/cashflows.hpp>
22
#include <ql/cashflows/cashflowvectors.hpp>
23
#include <ql/cashflows/couponpricer.hpp>
24
#include <ql/cashflows/fixedratecoupon.hpp>
25
#include <ql/cashflows/yoyinflationcoupon.hpp>
26
#include <ql/indexes/inflationindex.hpp>
27
#include <ql/instruments/yearonyearinflationswap.hpp>
28
#include <ql/termstructures/yieldtermstructure.hpp>
29
#include <ql/time/schedule.hpp>
30
#include <utility>
31

32
namespace QuantLib {
33

34
    YearOnYearInflationSwap::YearOnYearInflationSwap(Type type,
1,873✔
35
                                                     Real nominal,
36
                                                     Schedule fixedSchedule,
37
                                                     Rate fixedRate,
38
                                                     DayCounter fixedDayCount,
39
                                                     Schedule yoySchedule,
40
                                                     ext::shared_ptr<YoYInflationIndex> yoyIndex,
41
                                                     const Period& observationLag,
42
                                                     CPI::InterpolationType interpolation,
43
                                                     Spread spread,
44
                                                     DayCounter yoyDayCount,
45
                                                     Calendar paymentCalendar,
46
                                                     BusinessDayConvention paymentConvention)
1,873✔
47
    : Swap(2), type_(type), nominal_(nominal), fixedSchedule_(std::move(fixedSchedule)),
1,873✔
48
      fixedRate_(fixedRate), fixedDayCount_(std::move(fixedDayCount)),
1,873✔
49
      yoySchedule_(std::move(yoySchedule)), yoyIndex_(std::move(yoyIndex)),
1,873✔
50
      observationLag_(observationLag), spread_(spread), yoyDayCount_(std::move(yoyDayCount)),
1,873✔
51
      paymentCalendar_(std::move(paymentCalendar)), paymentConvention_(paymentConvention) {
1,873✔
52
        // N.B. fixed leg gets its calendar from the schedule!
53
        Leg fixedLeg = FixedRateLeg(fixedSchedule_)
3,746✔
54
        .withNotionals(nominal_)
1,873✔
55
        .withCouponRates(fixedRate_, fixedDayCount_) // Simple compounding by default
1,873✔
56
        .withPaymentAdjustment(paymentConvention_);
1,873✔
57

58
        Leg yoyLeg = yoyInflationLeg(yoySchedule_, paymentCalendar_, yoyIndex_,
3,746✔
59
                                     observationLag_, interpolation)
1,873✔
60
        .withNotionals(nominal_)
1,873✔
61
        .withPaymentDayCounter(yoyDayCount_)
1,873✔
62
        .withPaymentAdjustment(paymentConvention_)
1,873✔
63
        .withSpreads(spread_);
1,873✔
64

65
        Leg::const_iterator i;
66
        for (i = yoyLeg.begin(); i < yoyLeg.end(); ++i)
17,434✔
67
            registerWith(*i);
31,122✔
68

69
        legs_[0] = fixedLeg;
1,873✔
70
        legs_[1] = yoyLeg;
1,873✔
71
        if (type_==Payer) {
1,873✔
72
            payer_[0] = -1.0;
1,873✔
73
            payer_[1] = +1.0;
1,873✔
74
        } else {
75
            payer_[0] = +1.0;
×
76
            payer_[1] = -1.0;
×
77
        }
78
    }
1,873✔
79

80
     void YearOnYearInflationSwap::setupArguments(PricingEngine::arguments* args) const {
2,678✔
81

82
        Swap::setupArguments(args);
2,678✔
83

84
        auto* arguments = dynamic_cast<YearOnYearInflationSwap::arguments*>(args);
2,678✔
85

86
        if (arguments == nullptr) // it's a swap engine...
2,678✔
87
            return;
88

UNCOV
89
        arguments->type = type_;
×
UNCOV
90
        arguments->nominal = nominal_;
×
91

UNCOV
92
        const Leg& fixedCoupons = fixedLeg();
×
93

94
        arguments->fixedResetDates = arguments->fixedPayDates =
UNCOV
95
        std::vector<Date>(fixedCoupons.size());
×
UNCOV
96
        arguments->fixedCoupons = std::vector<Real>(fixedCoupons.size());
×
97

UNCOV
98
        for (Size i=0; i<fixedCoupons.size(); ++i) {
×
99
            ext::shared_ptr<FixedRateCoupon> coupon =
UNCOV
100
            ext::dynamic_pointer_cast<FixedRateCoupon>(fixedCoupons[i]);
×
101

UNCOV
102
            arguments->fixedPayDates[i] = coupon->date();
×
UNCOV
103
            arguments->fixedResetDates[i] = coupon->accrualStartDate();
×
UNCOV
104
            arguments->fixedCoupons[i] = coupon->amount();
×
105
        }
106

UNCOV
107
        const Leg& yoyCoupons = yoyLeg();
×
108

109
        arguments->yoyResetDates = arguments->yoyPayDates =
110
        arguments->yoyFixingDates =
111
        std::vector<Date>(yoyCoupons.size());
×
112
        arguments->yoyAccrualTimes =
UNCOV
113
        std::vector<Time>(yoyCoupons.size());
×
114
        arguments->yoySpreads =
UNCOV
115
        std::vector<Spread>(yoyCoupons.size());
×
116
        arguments->yoyCoupons = std::vector<Real>(yoyCoupons.size());
×
UNCOV
117
        for (Size i=0; i<yoyCoupons.size(); ++i) {
×
118
            ext::shared_ptr<YoYInflationCoupon> coupon =
119
            ext::dynamic_pointer_cast<YoYInflationCoupon>(yoyCoupons[i]);
×
120

UNCOV
121
            arguments->yoyResetDates[i] = coupon->accrualStartDate();
×
UNCOV
122
            arguments->yoyPayDates[i] = coupon->date();
×
123

UNCOV
124
            arguments->yoyFixingDates[i] = coupon->fixingDate();
×
UNCOV
125
            arguments->yoyAccrualTimes[i] = coupon->accrualPeriod();
×
UNCOV
126
            arguments->yoySpreads[i] = coupon->spread();
×
127
            try {
UNCOV
128
                arguments->yoyCoupons[i] = coupon->amount();
×
129
            } catch (Error&) {
×
UNCOV
130
                arguments->yoyCoupons[i] = Null<Real>();
×
131
            }
×
132
        }
133
    }
134

135

136
    Rate YearOnYearInflationSwap::fairRate() const {
984✔
137
        calculate();
984✔
138
        QL_REQUIRE(fairRate_ != Null<Rate>(), "result not available");
984✔
139
        return fairRate_;
984✔
140
    }
141

142
    Spread YearOnYearInflationSwap::fairSpread() const {
×
UNCOV
143
        calculate();
×
144
        QL_REQUIRE(fairSpread_ != Null<Spread>(), "result not available");
×
145
        return fairSpread_;
×
146
    }
147

148

UNCOV
149
    Real YearOnYearInflationSwap::fixedLegNPV() const {
×
UNCOV
150
        calculate();
×
UNCOV
151
        QL_REQUIRE(legNPV_[0] != Null<Real>(), "result not available");
×
UNCOV
152
        return legNPV_[0];
×
153
    }
154

UNCOV
155
    Real YearOnYearInflationSwap::yoyLegNPV() const {
×
UNCOV
156
        calculate();
×
UNCOV
157
        QL_REQUIRE(legNPV_[1] != Null<Real>(), "result not available");
×
158
        return legNPV_[1];
×
159
    }
160

161
    void YearOnYearInflationSwap::setupExpired() const {
×
UNCOV
162
        Swap::setupExpired();
×
UNCOV
163
        legBPS_[0] = legBPS_[1] = 0.0;
×
UNCOV
164
        fairRate_ = Null<Rate>();
×
165
        fairSpread_ = Null<Spread>();
×
166
    }
×
167

168
    void YearOnYearInflationSwap::fetchResults(const PricingEngine::results* r) const {
2,678✔
169
        static const Spread basisPoint = 1.0e-4;
170

171
        // copy from VanillaSwap
172
        // works because similarly simple instrument
173
        // that we always expect to be priced with a swap engine
174

175
        Swap::fetchResults(r);
2,678✔
176

177
        const auto* results = dynamic_cast<const YearOnYearInflationSwap::results*>(r);
2,678✔
178
        if (results != nullptr) { // might be a swap engine, so no error is thrown
2,678✔
179
            fairRate_ = results->fairRate;
×
180
            fairSpread_ = results->fairSpread;
×
181
        } else {
182
            fairRate_ = Null<Rate>();
2,678✔
183
            fairSpread_ = Null<Spread>();
2,678✔
184
        }
185

186
        if (fairRate_ == Null<Rate>()) {
2,678✔
187
            // calculate it from other results
188
            if (legBPS_[0] != Null<Real>())
2,678✔
189
                fairRate_ = fixedRate_ - NPV_/(legBPS_[0]/basisPoint);
2,678✔
190
        }
191
        if (fairSpread_ == Null<Spread>()) {
2,678✔
192
            // ditto
193
            if (legBPS_[1] != Null<Real>())
2,678✔
194
                fairSpread_ = spread_ - NPV_/(legBPS_[1]/basisPoint);
2,678✔
195
        }
196

197
    }
2,678✔
198

UNCOV
199
    void YearOnYearInflationSwap::arguments::validate() const {
×
UNCOV
200
        Swap::arguments::validate();
×
UNCOV
201
        QL_REQUIRE(nominal != Null<Real>(), "nominal null or not set");
×
UNCOV
202
        QL_REQUIRE(fixedResetDates.size() == fixedPayDates.size(),
×
203
                   "number of fixed start dates different from "
204
                   "number of fixed payment dates");
UNCOV
205
        QL_REQUIRE(fixedPayDates.size() == fixedCoupons.size(),
×
206
                   "number of fixed payment dates different from "
207
                   "number of fixed coupon amounts");
UNCOV
208
        QL_REQUIRE(yoyResetDates.size() == yoyPayDates.size(),
×
209
                   "number of yoy start dates different from "
210
                   "number of yoy payment dates");
UNCOV
211
        QL_REQUIRE(yoyFixingDates.size() == yoyPayDates.size(),
×
212
                   "number of yoy fixing dates different from "
213
                   "number of yoy payment dates");
UNCOV
214
        QL_REQUIRE(yoyAccrualTimes.size() == yoyPayDates.size(),
×
215
                   "number of yoy accrual Times different from "
216
                   "number of yoy payment dates");
217
        QL_REQUIRE(yoySpreads.size() == yoyPayDates.size(),
×
218
                   "number of yoy spreads different from "
219
                   "number of yoy payment dates");
UNCOV
220
        QL_REQUIRE(yoyPayDates.size() == yoyCoupons.size(),
×
221
                   "number of yoy payment dates different from "
222
                   "number of yoy coupon amounts");
UNCOV
223
    }
×
224

UNCOV
225
    void YearOnYearInflationSwap::results::reset() {
×
UNCOV
226
        Swap::results::reset();
×
227
        fairRate = Null<Rate>();
×
UNCOV
228
        fairSpread = Null<Spread>();
×
UNCOV
229
    }
×
230

231
}
232

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

© 2025 Coveralls, Inc