Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/termstructures/credit/defaultprobabilityhelpers.hpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2008, 2009 Jose Aparicio
5
 Copyright (C) 2008 Chris Kenyon
6
 Copyright (C) 2008 Roland Lichters
7
 Copyright (C) 2008 StatPro Italia srl
8
 Copyright (C) 2023 Andrea Pellegatta
9
10
 This file is part of QuantLib, a free-software/open-source library
11
 for financial quantitative analysts and developers - http://quantlib.org/
12
13
 QuantLib is free software: you can redistribute it and/or modify it
14
 under the terms of the QuantLib license.  You should have received a
15
 copy of the license along with this program; if not, please email
16
 <quantlib-dev@lists.sf.net>. The license is also available online at
17
 <http://quantlib.org/license.shtml>.
18
19
 This program is distributed in the hope that it will be useful, but WITHOUT
20
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21
 FOR A PARTICULAR PURPOSE.  See the license for more details.
22
*/
23
24
/*! \file defaultprobabilityhelpers.hpp
25
    \brief bootstrap helpers for default-probability term structures
26
*/
27
28
#ifndef quantlib_default_probability_helpers_hpp
29
#define quantlib_default_probability_helpers_hpp
30
31
#include <ql/termstructures/defaulttermstructure.hpp>
32
#include <ql/termstructures/bootstraphelper.hpp>
33
#include <ql/time/schedule.hpp>
34
#include <ql/instruments/creditdefaultswap.hpp>
35
36
namespace QuantLib {
37
38
    class YieldTermStructure;
39
    class CreditDefaultSwap;
40
41
    //! alias for default-probability bootstrap helpers
42
    typedef BootstrapHelper<DefaultProbabilityTermStructure>
43
                                                     DefaultProbabilityHelper;
44
    typedef RelativeDateBootstrapHelper<DefaultProbabilityTermStructure>
45
                                         RelativeDateDefaultProbabilityHelper;
46
47
    //! Base class for CDS helpers
48
    class CdsHelper : public RelativeDateDefaultProbabilityHelper {
49
      public:
50
        /*! Constructor taking CDS market quote
51
            @param quote  The helper's market quote.
52
            @param tenor  CDS tenor.
53
            @param settlementDays  The number of days from evaluation date to the start of the protection period.
54
                                   Prior to the CDS Big Bang in 2009, this was typically 1 calendar day. After the 
55
                                   CDS Big Bang, this is typically 0 calendar days i.e. protection starts 
56
                                   immediately.
57
            @param calendar  CDS calendar. Typically weekends only for standard non-JPY CDS and TYO for JPY.
58
            @param frequency  Coupon frequency. Typically 3 months for standard CDS.
59
            @param paymentConvention  The convention applied to coupons schedules and settlement dates.
60
            @param rule  The date generation rule for generating the CDS schedule. Typically, for CDS prior to the 
61
                         Big Bang, \c OldCDS should be used. After the Big Bang, \c CDS was typical and since 2015 
62
                         \c CDS2015 is standard.
63
            @param dayCounter  The day counter for CDS fee leg coupons. Typically it is Actual/360, excluding 
64
                               accrual end, for all but the final coupon period with Actual/360, including accrual 
65
                               end, for the final coupon. The \p lastPeriodDayCounter below allows for this 
66
                               distinction.
67
            @param recoveryRate  The recovery rate of the underlying reference entity.
68
            @param discountCurve  A handle to the relevant discount curve.
69
            @param settlesAccrual  Set to \c true if accrued fee is paid on the occurrence of a credit event and set 
70
                                   to \c false if it is not. Typically this is \c true.
71
            @param paysAtDefaultTime  Set to \c true if default payment is made at time of credit event or postponed 
72
                                      to the end of the coupon period. Typically this is \c true.
73
            @param startDate  Used to specify an explicit start date for the CDS schedule and the date from which the
74
                              CDS maturity is calculated via the \p tenor. Useful for off-the-run index schedules.
75
            @param lastPeriodDayCounter  The day counter for the last fee leg coupon. See comment on \p dayCounter.
76
            @param rebatesAccrual  Set to \c true if the fee leg accrual is rebated on the cash settlement date. For 
77
                                   CDS after the Big Bang, this is typically \c true.
78
            @param model  The pricing model to use for the helper.
79
        */
80
        CdsHelper(const std::variant<Rate, Handle<Quote>>& quote,
81
                  const Period& tenor,
82
                  Integer settlementDays,
83
                  Calendar calendar,
84
                  Frequency frequency,
85
                  BusinessDayConvention paymentConvention,
86
                  DateGeneration::Rule rule,
87
                  DayCounter dayCounter,
88
                  Real recoveryRate,
89
                  const Handle<YieldTermStructure>& discountCurve,
90
                  bool settlesAccrual = true,
91
                  bool paysAtDefaultTime = true,
92
                  const Date& startDate = Date(),
93
                  DayCounter lastPeriodDayCounter = DayCounter(),
94
                  bool rebatesAccrual = true,
95
                  CreditDefaultSwap::PricingModel model = CreditDefaultSwap::Midpoint);
96
97
        void setTermStructure(DefaultProbabilityTermStructure*) override;
98
        // NOLINTNEXTLINE(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
99
0
        ext::shared_ptr<CreditDefaultSwap> swap() const {
100
0
            return swap_;
101
0
        }
102
        void update() override;
103
104
      protected:
105
        void initializeDates() override;
106
        virtual void resetEngine() = 0;
107
        Period tenor_;
108
        Integer settlementDays_;
109
        Calendar calendar_;
110
        Frequency frequency_;
111
        BusinessDayConvention paymentConvention_;
112
        DateGeneration::Rule rule_;
113
        DayCounter dayCounter_;
114
        Real recoveryRate_;
115
        Handle<YieldTermStructure> discountCurve_;
116
        bool settlesAccrual_;
117
        bool paysAtDefaultTime_;
118
        DayCounter lastPeriodDC_;
119
        bool rebatesAccrual_;
120
        CreditDefaultSwap::PricingModel model_;
121
122
        Schedule schedule_;
123
        ext::shared_ptr<CreditDefaultSwap> swap_;
124
        RelinkableHandle<DefaultProbabilityTermStructure> probability_;
125
        //! protection effective date.
126
        Date protectionStart_;
127
        Date startDate_;
128
    };
129
130
    //! Spread-quoted CDS hazard rate bootstrap helper.
131
    class SpreadCdsHelper : public CdsHelper {
132
      public:
133
        SpreadCdsHelper(const std::variant<Rate, Handle<Quote>>& runningSpread,
134
                        const Period& tenor,
135
                        Integer settlementDays,
136
                        const Calendar& calendar,
137
                        Frequency frequency,
138
                        BusinessDayConvention paymentConvention,
139
                        DateGeneration::Rule rule,
140
                        const DayCounter& dayCounter,
141
                        Real recoveryRate,
142
                        const Handle<YieldTermStructure>& discountCurve,
143
                        bool settlesAccrual = true,
144
                        bool paysAtDefaultTime = true,
145
                        const Date& startDate = Date(),
146
                        const DayCounter& lastPeriodDayCounter = DayCounter(),
147
                        bool rebatesAccrual = true,
148
                        CreditDefaultSwap::PricingModel model = CreditDefaultSwap::Midpoint);
149
150
        Real impliedQuote() const override;
151
152
      private:
153
        void resetEngine() override;
154
    };
155
156
    //! Upfront-quoted CDS hazard rate bootstrap helper.
157
    class UpfrontCdsHelper : public CdsHelper {
158
      public:
159
        /*! \note the upfront must be quoted in fractional units. */
160
        UpfrontCdsHelper(const std::variant<Rate, Handle<Quote>>& upfront,
161
                         Rate runningSpread,
162
                         const Period& tenor,
163
                         Integer settlementDays,
164
                         const Calendar& calendar,
165
                         Frequency frequency,
166
                         BusinessDayConvention paymentConvention,
167
                         DateGeneration::Rule rule,
168
                         const DayCounter& dayCounter,
169
                         Real recoveryRate,
170
                         const Handle<YieldTermStructure>& discountCurve,
171
                         Natural upfrontSettlementDays = 3,
172
                         bool settlesAccrual = true,
173
                         bool paysAtDefaultTime = true,
174
                         const Date& startDate = Date(),
175
                         const DayCounter& lastPeriodDayCounter = DayCounter(),
176
                         bool rebatesAccrual = true,
177
                         CreditDefaultSwap::PricingModel model = CreditDefaultSwap::Midpoint);
178
179
        Real impliedQuote() const override;
180
181
      private:
182
        Date upfrontDate();
183
        void initializeDates() override;
184
        void resetEngine() override;
185
        Natural upfrontSettlementDays_;
186
        Date upfrontDate_;
187
        Rate runningSpread_;
188
    };
189
190
}
191
192
193
#endif