QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
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
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
61 const Period& tenor,
62 Integer settlementDays,
63 Calendar calendar,
64 Frequency frequency,
65 BusinessDayConvention paymentConvention,
67 DayCounter dayCounter,
68 Real recoveryRate,
69 const Handle<YieldTermStructure>& discountCurve,
70 bool settlesAccrual,
71 bool paysAtDefaultTime,
72 const Date& startDate,
73 DayCounter lastPeriodDayCounter,
74 const bool rebatesAccrual,
76 : RelativeDateDefaultProbabilityHelper(quote), tenor_(tenor), settlementDays_(settlementDays),
77 calendar_(std::move(calendar)), frequency_(frequency), paymentConvention_(paymentConvention),
78 rule_(rule), dayCounter_(std::move(dayCounter)), recoveryRate_(recoveryRate),
79 discountCurve_(discountCurve), settlesAccrual_(settlesAccrual),
80 paysAtDefaultTime_(paysAtDefaultTime), lastPeriodDC_(std::move(lastPeriodDayCounter)),
81 rebatesAccrual_(rebatesAccrual), model_(model), startDate_(startDate) {
82
84
85 registerWith(discountCurve);
86 }
87
90
91 probability_.linkTo(
92 ext::shared_ptr<DefaultProbabilityTermStructure>(ts, null_deleter()),
93 false);
94
96 }
97
100 resetEngine();
101 }
102
104
106
107 Date startDate = startDate_ == Date() ? protectionStart_ : startDate_;
108 // Only adjust start date if rule is not CDS or CDS2015. Unsure about OldCDS.
110 startDate = calendar_.adjust(startDate, paymentConvention_);
111 }
112
113 Date endDate;
115 Date refDate = startDate_ == Date() ? evaluationDate_ : startDate_;
116 endDate = cdsMaturity(refDate, tenor_, rule_);
117 } else {
118 // Keep the old logic here
120 endDate = refDate + tenor_;
121 }
122
123 schedule_ =
124 MakeSchedule().from(startDate)
125 .to(endDate)
130 .withRule(rule_);
131 earliestDate_ = schedule_.dates().front();
135 ++latestDate_;
136 }
137
139 const Handle<Quote>& runningSpread,
140 const Period& tenor,
141 Integer settlementDays,
142 const Calendar& calendar,
143 Frequency frequency,
144 BusinessDayConvention paymentConvention,
146 const DayCounter& dayCounter,
147 Real recoveryRate,
148 const Handle<YieldTermStructure>& discountCurve,
149 bool settlesAccrual,
150 bool paysAtDefaultTime,
151 const Date& startDate,
152 const DayCounter& lastPeriodDayCounter,
153 const bool rebatesAccrual,
155 : CdsHelper(runningSpread, tenor, settlementDays, calendar,
156 frequency, paymentConvention, rule, dayCounter,
157 recoveryRate, discountCurve, settlesAccrual, paysAtDefaultTime,
158 startDate, lastPeriodDayCounter, rebatesAccrual, model) {}
159
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 bool settlesAccrual,
172 bool paysAtDefaultTime,
173 const Date& startDate,
174 const DayCounter& lastPeriodDayCounter,
175 const bool rebatesAccrual,
177 : CdsHelper(runningSpread, tenor, settlementDays, calendar,
178 frequency, paymentConvention, rule, dayCounter,
179 recoveryRate, discountCurve, settlesAccrual, paysAtDefaultTime,
180 startDate, lastPeriodDayCounter,rebatesAccrual, model) {}
181
183 swap_->recalculate();
184 return swap_->fairSpread();
185 }
186
188 swap_ = ext::make_shared<CreditDefaultSwap>(
191 ext::shared_ptr<Claim>(), lastPeriodDC_, rebatesAccrual_, evaluationDate_);
192
193 switch (model_) {
195 swap_->setPricingEngine(ext::make_shared<IsdaCdsEngine>(
199 break;
201 swap_->setPricingEngine(ext::make_shared<MidPointCdsEngine>(
203 break;
204 default:
205 QL_FAIL("unknown CDS pricing model: " << model_);
206 }
207 }
208
210 const Handle<Quote>& upfront,
211 Rate runningSpread,
212 const Period& tenor,
213 Integer settlementDays,
214 const Calendar& calendar,
215 Frequency frequency,
216 BusinessDayConvention paymentConvention,
218 const DayCounter& dayCounter,
219 Real recoveryRate,
220 const Handle<YieldTermStructure>& discountCurve,
221 Natural upfrontSettlementDays,
222 bool settlesAccrual,
223 bool paysAtDefaultTime,
224 const Date& startDate,
225 const DayCounter& lastPeriodDayCounter,
226 const bool rebatesAccrual,
228 : CdsHelper(upfront, tenor, settlementDays, calendar,
229 frequency, paymentConvention, rule, dayCounter,
230 recoveryRate, discountCurve, settlesAccrual, paysAtDefaultTime,
231 startDate, lastPeriodDayCounter, rebatesAccrual, model),
232 upfrontSettlementDays_(upfrontSettlementDays),
233 upfrontDate_(upfrontDate()),
234 runningSpread_(runningSpread) {}
235
237 Rate upfrontSpread,
238 Rate runningSpread,
239 const Period& tenor,
240 Integer settlementDays,
241 const Calendar& calendar,
242 Frequency frequency,
243 BusinessDayConvention paymentConvention,
245 const DayCounter& dayCounter,
246 Real recoveryRate,
247 const Handle<YieldTermStructure>& discountCurve,
248 Natural upfrontSettlementDays,
249 bool settlesAccrual,
250 bool paysAtDefaultTime,
251 const Date& startDate,
252 const DayCounter& lastPeriodDayCounter,
253 const bool rebatesAccrual,
255 : CdsHelper(upfrontSpread, tenor, settlementDays, calendar,
256 frequency, paymentConvention, rule, dayCounter,
257 recoveryRate, discountCurve, settlesAccrual, paysAtDefaultTime,
258 startDate, lastPeriodDayCounter, rebatesAccrual, model),
259 upfrontSettlementDays_(upfrontSettlementDays),
260 upfrontDate_(upfrontDate()),
261 runningSpread_(runningSpread) {}
262
265 }
266
270 }
271
273 swap_ = ext::make_shared<CreditDefaultSwap>(
277 ext::shared_ptr<Claim>(), lastPeriodDC_, rebatesAccrual_,
279
280 switch (model_) {
282 swap_->setPricingEngine(ext::make_shared<IsdaCdsEngine>(
286 break;
288 swap_->setPricingEngine(ext::make_shared<MidPointCdsEngine>(
290 break;
291 default:
292 QL_FAIL("unknown CDS pricing model: " << model_);
293 }
294 }
295
297 SavedSettings backup;
299 swap_->recalculate();
300 return swap_->fairUpfront();
301 }
302
303}
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
CreditDefaultSwap::PricingModel model_
CdsHelper(const 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)
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:557
MakeSchedule & withTerminationDateConvention(BusinessDayConvention)
Definition: schedule.cpp:562
MakeSchedule & withRule(DateGeneration::Rule)
Definition: schedule.cpp:568
MakeSchedule & to(const Date &terminationDate)
Definition: schedule.cpp:537
MakeSchedule & from(const Date &effectiveDate)
Definition: schedule.cpp:532
MakeSchedule & withFrequency(Frequency)
Definition: schedule.cpp:547
MakeSchedule & withCalendar(const Calendar &)
Definition: schedule.cpp:552
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Bootstrap helper with date schedule relative to global evaluation date.
const std::vector< Date > & dates() const
Definition: schedule.hpp:75
ext::optional< bool > & includeTodaysCashFlows()
Definition: settings.hpp:163
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
SpreadCdsHelper(const 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 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:35
Date cdsMaturity(const Date &tradeDate, const Period &tenor, DateGeneration::Rule rule)
STL namespace.
empty deleter for shared_ptr