QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
defaultprobabilityhelpers.cpp
Go to the documentation of this file.
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
28#include <utility>
29
30namespace QuantLib {
31
32 CdsHelper::CdsHelper(const std::variant<Rate, Handle<Quote>>& quote,
33 const Period& tenor,
34 Integer settlementDays,
35 Calendar calendar,
36 Frequency frequency,
37 BusinessDayConvention paymentConvention,
39 DayCounter dayCounter,
40 Real recoveryRate,
41 const Handle<YieldTermStructure>& discountCurve,
42 bool settlesAccrual,
43 bool paysAtDefaultTime,
44 const Date& startDate,
45 DayCounter lastPeriodDayCounter,
46 const bool rebatesAccrual,
48 : RelativeDateDefaultProbabilityHelper(quote), tenor_(tenor), settlementDays_(settlementDays),
49 calendar_(std::move(calendar)), frequency_(frequency), paymentConvention_(paymentConvention),
50 rule_(rule), dayCounter_(std::move(dayCounter)), recoveryRate_(recoveryRate),
51 discountCurve_(discountCurve), settlesAccrual_(settlesAccrual),
52 paysAtDefaultTime_(paysAtDefaultTime), lastPeriodDC_(std::move(lastPeriodDayCounter)),
53 rebatesAccrual_(rebatesAccrual), model_(model), startDate_(startDate) {
54
56
57 registerWith(discountCurve);
58 }
59
62
63 probability_.linkTo(
64 ext::shared_ptr<DefaultProbabilityTermStructure>(ts, null_deleter()),
65 false);
66
68 }
69
73 }
74
76
78
79 Date startDate = startDate_ == Date() ? protectionStart_ : startDate_;
80 // Only adjust start date if rule is not CDS or CDS2015. Unsure about OldCDS.
82 startDate = calendar_.adjust(startDate, paymentConvention_);
83 }
84
85 Date endDate;
88 endDate = cdsMaturity(refDate, tenor_, rule_);
89 } else {
90 // Keep the old logic here
92 endDate = refDate + tenor_;
93 }
94
95 schedule_ =
96 MakeSchedule().from(startDate)
97 .to(endDate)
102 .withRule(rule_);
103 earliestDate_ = schedule_.dates().front();
107 ++latestDate_;
108 }
109
111 const std::variant<Rate, Handle<Quote>>& runningSpread,
112 const Period& tenor,
113 Integer settlementDays,
114 const Calendar& calendar,
115 Frequency frequency,
116 BusinessDayConvention paymentConvention,
118 const DayCounter& dayCounter,
119 Real recoveryRate,
120 const Handle<YieldTermStructure>& discountCurve,
121 bool settlesAccrual,
122 bool paysAtDefaultTime,
123 const Date& startDate,
124 const DayCounter& lastPeriodDayCounter,
125 const bool rebatesAccrual,
127 : CdsHelper(runningSpread, tenor, settlementDays, calendar,
128 frequency, paymentConvention, rule, dayCounter,
129 recoveryRate, discountCurve, settlesAccrual, paysAtDefaultTime,
130 startDate, lastPeriodDayCounter, rebatesAccrual, model) {}
131
133 swap_->recalculate();
134 return swap_->fairSpread();
135 }
136
138 swap_ = ext::make_shared<CreditDefaultSwap>(
141 ext::shared_ptr<Claim>(), lastPeriodDC_, rebatesAccrual_, evaluationDate_);
142
143 switch (model_) {
145 swap_->setPricingEngine(ext::make_shared<IsdaCdsEngine>(
149 break;
151 swap_->setPricingEngine(ext::make_shared<MidPointCdsEngine>(
153 break;
154 default:
155 QL_FAIL("unknown CDS pricing model: " << model_);
156 }
157 }
158
160 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,
168 const DayCounter& dayCounter,
169 Real recoveryRate,
170 const Handle<YieldTermStructure>& discountCurve,
171 Natural upfrontSettlementDays,
172 bool settlesAccrual,
173 bool paysAtDefaultTime,
174 const Date& startDate,
175 const DayCounter& lastPeriodDayCounter,
176 const bool rebatesAccrual,
178 : CdsHelper(upfront, tenor, settlementDays, calendar,
179 frequency, paymentConvention, rule, dayCounter,
180 recoveryRate, discountCurve, settlesAccrual, paysAtDefaultTime,
181 startDate, lastPeriodDayCounter, rebatesAccrual, model),
182 upfrontSettlementDays_(upfrontSettlementDays),
183 upfrontDate_(upfrontDate()),
184 runningSpread_(runningSpread) {}
185
188 }
189
193 }
194
196 swap_ = ext::make_shared<CreditDefaultSwap>(
200 ext::shared_ptr<Claim>(), lastPeriodDC_, rebatesAccrual_,
202
203 switch (model_) {
205 swap_->setPricingEngine(ext::make_shared<IsdaCdsEngine>(
209 break;
211 swap_->setPricingEngine(ext::make_shared<MidPointCdsEngine>(
213 break;
214 default:
215 QL_FAIL("unknown CDS pricing model: " << model_);
216 }
217 }
218
220 SavedSettings backup;
222 swap_->recalculate();
223 return swap_->fairUpfront();
224 }
225
226}
const YieldTermStructure & discountCurve_
Definition: cashflows.cpp:418
virtual void setTermStructure(TS *)
sets the term structure to be used for pricing
calendar class
Definition: calendar.hpp:61
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Definition: calendar.cpp:84
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
Definition: calendar.cpp:130
Base class for CDS helpers.
Handle< YieldTermStructure > discountCurve_
RelinkableHandle< DefaultProbabilityTermStructure > probability_
void setTermStructure(DefaultProbabilityTermStructure *) override
CdsHelper(const std::variant< Rate, Handle< Quote > > &quote, const Period &tenor, Integer settlementDays, Calendar calendar, Frequency frequency, BusinessDayConvention paymentConvention, DateGeneration::Rule rule, DayCounter dayCounter, Real recoveryRate, const Handle< YieldTermStructure > &discountCurve, bool settlesAccrual=true, bool paysAtDefaultTime=true, const Date &startDate=Date(), DayCounter lastPeriodDayCounter=DayCounter(), bool rebatesAccrual=true, CreditDefaultSwap::PricingModel model=CreditDefaultSwap::Midpoint)
CreditDefaultSwap::PricingModel model_
BusinessDayConvention paymentConvention_
virtual void resetEngine()=0
ext::shared_ptr< CreditDefaultSwap > swap_
Date protectionStart_
protection effective date.
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Default probability term structure.
Shared handle to an observable.
Definition: handle.hpp:41
MakeSchedule & withConvention(BusinessDayConvention)
Definition: schedule.cpp:552
MakeSchedule & withTerminationDateConvention(BusinessDayConvention)
Definition: schedule.cpp:557
MakeSchedule & withRule(DateGeneration::Rule)
Definition: schedule.cpp:563
MakeSchedule & to(const Date &terminationDate)
Definition: schedule.cpp:532
MakeSchedule & from(const Date &effectiveDate)
Definition: schedule.cpp:527
MakeSchedule & withFrequency(Frequency)
Definition: schedule.cpp:542
MakeSchedule & withCalendar(const Calendar &)
Definition: schedule.cpp:547
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:226
Bootstrap helper with date schedule relative to global evaluation date.
const std::vector< Date > & dates() const
Definition: schedule.hpp:73
ext::optional< bool > & includeTodaysCashFlows()
Definition: settings.hpp:163
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
SpreadCdsHelper(const std::variant< Rate, Handle< Quote > > &runningSpread, const Period &tenor, Integer settlementDays, const Calendar &calendar, Frequency frequency, BusinessDayConvention paymentConvention, DateGeneration::Rule rule, const DayCounter &dayCounter, Real recoveryRate, const Handle< YieldTermStructure > &discountCurve, bool settlesAccrual=true, bool paysAtDefaultTime=true, const Date &startDate=Date(), const DayCounter &lastPeriodDayCounter=DayCounter(), bool rebatesAccrual=true, CreditDefaultSwap::PricingModel model=CreditDefaultSwap::Midpoint)
UpfrontCdsHelper(const std::variant< Rate, Handle< Quote > > &upfront, Rate runningSpread, const Period &tenor, Integer settlementDays, const Calendar &calendar, Frequency frequency, BusinessDayConvention paymentConvention, DateGeneration::Rule rule, const DayCounter &dayCounter, Real recoveryRate, const Handle< YieldTermStructure > &discountCurve, Natural upfrontSettlementDays=3, bool settlesAccrual=true, bool paysAtDefaultTime=true, const Date &startDate=Date(), const DayCounter &lastPeriodDayCounter=DayCounter(), bool rebatesAccrual=true, CreditDefaultSwap::PricingModel model=CreditDefaultSwap::Midpoint)
bootstrap helpers for default-probability term structures
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Frequency
Frequency of events.
Definition: frequency.hpp:37
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Rate
interest rates
Definition: types.hpp:70
ISDA engine for credit default swaps.
Mid-point engine for credit default swaps.
Definition: any.hpp:37
Date cdsMaturity(const Date &tradeDate, const Period &tenor, DateGeneration::Rule rule)
STL namespace.
empty deleter for shared_ptr