/
creditdefaultswap.cpp
762 lines (620 loc) · 29 KB
/
creditdefaultswap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2008, 2009 StatPro Italia srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include "creditdefaultswap.hpp"
#include "utilities.hpp"
#include <ql/cashflows/iborcoupon.hpp>
#include <ql/instruments/creditdefaultswap.hpp>
#include <ql/instruments/makecds.hpp>
#include <ql/pricingengines/credit/midpointcdsengine.hpp>
#include <ql/pricingengines/credit/integralcdsengine.hpp>
#include <ql/pricingengines/credit/isdacdsengine.hpp>
#include <ql/termstructures/credit/flathazardrate.hpp>
#include <ql/termstructures/credit/interpolatedhazardratecurve.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/termstructures/yield/discountcurve.hpp>
#include <ql/termstructures/yield/piecewiseyieldcurve.hpp>
#include <ql/termstructures/yield/ratehelpers.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/math/interpolations/backwardflatinterpolation.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/calendars/unitedstates.hpp>
#include <ql/time/calendars/weekendsonly.hpp>
#include <ql/currencies/america.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <map>
#include <iomanip>
#include <iostream>
using namespace QuantLib;
using namespace boost::unit_test_framework;
using std::map;
void CreditDefaultSwapTest::testCachedValue() {
BOOST_TEST_MESSAGE("Testing credit-default swap against cached values...");
SavedSettings backup;
// Initialize curves
Settings::instance().evaluationDate() = Date(9,June,2006);
Date today = Settings::instance().evaluationDate();
Calendar calendar = TARGET();
Handle<Quote> hazardRate = Handle<Quote>(
ext::shared_ptr<Quote>(new SimpleQuote(0.01234)));
RelinkableHandle<DefaultProbabilityTermStructure> probabilityCurve;
probabilityCurve.linkTo(
ext::shared_ptr<DefaultProbabilityTermStructure>(
new FlatHazardRate(0, calendar, hazardRate, Actual360())));
RelinkableHandle<YieldTermStructure> discountCurve;
discountCurve.linkTo(ext::shared_ptr<YieldTermStructure>(
new FlatForward(today,0.06,Actual360())));
// Build the schedule
Date issueDate = calendar.advance(today, -1, Years);
Date maturity = calendar.advance(issueDate, 10, Years);
Frequency frequency = Semiannual;
BusinessDayConvention convention = ModifiedFollowing;
Schedule schedule(issueDate, maturity, Period(frequency), calendar,
convention, convention, DateGeneration::Forward, false);
// Build the CDS
Rate fixedRate = 0.0120;
DayCounter dayCount = Actual360();
Real notional = 10000.0;
Real recoveryRate = 0.4;
CreditDefaultSwap cds(Protection::Seller, notional, fixedRate,
schedule, convention, dayCount, true, true);
cds.setPricingEngine(ext::shared_ptr<PricingEngine>(
new MidPointCdsEngine(probabilityCurve,recoveryRate,discountCurve)));
Real npv = 295.0153398;
Rate fairRate = 0.007517539081;
Real calculatedNpv = cds.NPV();
Rate calculatedFairRate = cds.fairSpread();
Real tolerance = 1.0e-7;
if (std::fabs(calculatedNpv - npv) > tolerance)
BOOST_ERROR(
"Failed to reproduce NPV with mid-point engine\n"
<< std::setprecision(10)
<< " calculated NPV: " << calculatedNpv << "\n"
<< " expected NPV: " << npv);
if (std::fabs(calculatedFairRate - fairRate) > tolerance)
BOOST_ERROR(
"Failed to reproduce fair rate with mid-point engine\n"
<< std::setprecision(10)
<< " calculated fair rate: " << calculatedFairRate << "\n"
<< " expected fair rate: " << fairRate);
cds.setPricingEngine(ext::shared_ptr<PricingEngine>(
new IntegralCdsEngine(1*Days,probabilityCurve,
recoveryRate,discountCurve)));
calculatedNpv = cds.NPV();
calculatedFairRate = cds.fairSpread();
tolerance = 1.0e-5;
if (std::fabs(calculatedNpv - npv) > notional*tolerance*10)
BOOST_ERROR(
"Failed to reproduce NPV with integral engine "
"(step = 1 day)\n"
<< std::setprecision(10)
<< " calculated NPV: " << calculatedNpv << "\n"
<< " expected NPV: " << npv);
if (std::fabs(calculatedFairRate - fairRate) > tolerance)
BOOST_ERROR(
"Failed to reproduce fair rate with integral engine "
"(step = 1 day)\n"
<< std::setprecision(10)
<< " calculated fair rate: " << calculatedFairRate << "\n"
<< " expected fair rate: " << fairRate);
cds.setPricingEngine(ext::shared_ptr<PricingEngine>(
new IntegralCdsEngine(1*Weeks,probabilityCurve,
recoveryRate,discountCurve)));
calculatedNpv = cds.NPV();
calculatedFairRate = cds.fairSpread();
tolerance = 1.0e-5;
if (std::fabs(calculatedNpv - npv) > notional*tolerance*10)
BOOST_ERROR(
"Failed to reproduce NPV with integral engine "
"(step = 1 week)\n"
<< std::setprecision(10)
<< " calculated NPV: " << calculatedNpv << "\n"
<< " expected NPV: " << npv);
if (std::fabs(calculatedFairRate - fairRate) > tolerance)
BOOST_ERROR(
"Failed to reproduce fair rate with integral engine "
"(step = 1 week)\n"
<< std::setprecision(10)
<< " calculated fair rate: " << calculatedFairRate << "\n"
<< " expected fair rate: " << fairRate);
}
void CreditDefaultSwapTest::testCachedMarketValue() {
BOOST_TEST_MESSAGE(
"Testing credit-default swap against cached market values...");
SavedSettings backup;
Settings::instance().evaluationDate() = Date(9,June,2006);
Date evalDate = Settings::instance().evaluationDate();
Calendar calendar = UnitedStates(UnitedStates::GovernmentBond);
std::vector<Date> discountDates = {
evalDate,
calendar.advance(evalDate, 1, Weeks, ModifiedFollowing),
calendar.advance(evalDate, 1, Months, ModifiedFollowing),
calendar.advance(evalDate, 2, Months, ModifiedFollowing),
calendar.advance(evalDate, 3, Months, ModifiedFollowing),
calendar.advance(evalDate, 6, Months, ModifiedFollowing),
calendar.advance(evalDate,12, Months, ModifiedFollowing),
calendar.advance(evalDate, 2, Years, ModifiedFollowing),
calendar.advance(evalDate, 3, Years, ModifiedFollowing),
calendar.advance(evalDate, 4, Years, ModifiedFollowing),
calendar.advance(evalDate, 5, Years, ModifiedFollowing),
calendar.advance(evalDate, 6, Years, ModifiedFollowing),
calendar.advance(evalDate, 7, Years, ModifiedFollowing),
calendar.advance(evalDate, 8, Years, ModifiedFollowing),
calendar.advance(evalDate, 9, Years, ModifiedFollowing),
calendar.advance(evalDate,10, Years, ModifiedFollowing),
calendar.advance(evalDate,15, Years, ModifiedFollowing)
};
std::vector<DiscountFactor> dfs = {
1.0,
0.9990151375768731,
0.99570502636871183,
0.99118260474528685,
0.98661167950906203,
0.9732592953359388,
0.94724424481038083,
0.89844996737120875,
0.85216647839921411,
0.80775477692556874,
0.76517289234200347,
0.72401019553182933,
0.68503909569219212,
0.64797499814013748,
0.61263171936255534,
0.5791942350748791,
0.43518868769953606
};
const DayCounter& curveDayCounter=Actual360();
RelinkableHandle<YieldTermStructure> discountCurve;
discountCurve.linkTo(
ext::shared_ptr<YieldTermStructure>(
new DiscountCurve(discountDates, dfs, curveDayCounter)));
DayCounter dayCounter = Thirty360(Thirty360::BondBasis);
std::vector<Date> dates = {
evalDate,
calendar.advance(evalDate, 6, Months, ModifiedFollowing),
calendar.advance(evalDate, 1, Years, ModifiedFollowing),
calendar.advance(evalDate, 2, Years, ModifiedFollowing),
calendar.advance(evalDate, 3, Years, ModifiedFollowing),
calendar.advance(evalDate, 4, Years, ModifiedFollowing),
calendar.advance(evalDate, 5, Years, ModifiedFollowing),
calendar.advance(evalDate, 7, Years, ModifiedFollowing),
calendar.advance(evalDate,10, Years, ModifiedFollowing)
};
std::vector<Probability> defaultProbabilities = {
0.0000,
0.0047,
0.0093,
0.0286,
0.0619,
0.0953,
0.1508,
0.2288,
0.3666
};
std::vector<Real> hazardRates;
hazardRates.push_back(0.0);
for (Size i=1; i<dates.size(); ++i) {
Time t1 = dayCounter.yearFraction(dates[0], dates[i-1]);
Time t2 = dayCounter.yearFraction(dates[0], dates[i]);
Probability S1 = 1.0 - defaultProbabilities[i-1];
Probability S2 = 1.0 - defaultProbabilities[i];
hazardRates.push_back(std::log(S1/S2)/(t2-t1));
}
RelinkableHandle<DefaultProbabilityTermStructure> piecewiseFlatHazardRate;
piecewiseFlatHazardRate.linkTo(
ext::shared_ptr<DefaultProbabilityTermStructure>(
new InterpolatedHazardRateCurve<BackwardFlat>(dates,
hazardRates,
Thirty360(Thirty360::BondBasis))));
// Testing credit default swap
// Build the schedule
Date issueDate(20, March, 2006);
Date maturity(20, June, 2013);
Frequency cdsFrequency = Semiannual;
BusinessDayConvention cdsConvention = ModifiedFollowing;
Schedule schedule(issueDate, maturity, Period(cdsFrequency), calendar,
cdsConvention, cdsConvention,
DateGeneration::Forward, false);
// Build the CDS
Real recoveryRate = 0.25;
Rate fixedRate=0.0224;
DayCounter dayCount=Actual360();
Real cdsNotional=100.0;
CreditDefaultSwap cds(Protection::Seller, cdsNotional, fixedRate,
schedule, cdsConvention, dayCount, true, true);
cds.setPricingEngine(ext::shared_ptr<PricingEngine>(
new MidPointCdsEngine(piecewiseFlatHazardRate,
recoveryRate,discountCurve)));
Real calculatedNpv = cds.NPV();
Real calculatedFairRate = cds.fairSpread();
double npv = -1.364048777; // from Bloomberg we have 98.15598868 - 100.00;
double fairRate = 0.0248429452; // from Bloomberg we have 0.0258378;
Real tolerance = 1e-9;
if (std::fabs(npv - calculatedNpv) > tolerance)
BOOST_ERROR(
"Failed to reproduce the npv for the given credit-default swap\n"
<< std::setprecision(10)
<< " computed NPV: " << calculatedNpv << "\n"
<< " Given NPV: " << npv);
if (std::fabs(fairRate - calculatedFairRate) > tolerance)
BOOST_ERROR(
"Failed to reproduce the fair rate for the given credit-default swap\n"
<< std::setprecision(10)
<< " computed fair rate: " << calculatedFairRate << "\n"
<< " Given fair rate: " << fairRate);
}
void CreditDefaultSwapTest::testImpliedHazardRate() {
BOOST_TEST_MESSAGE("Testing implied hazard-rate for credit-default swaps...");
SavedSettings backup;
// Initialize curves
Calendar calendar = TARGET();
Date today = calendar.adjust(Date::todaysDate());
Settings::instance().evaluationDate() = today;
Rate h1 = 0.30, h2 = 0.40;
DayCounter dayCounter = Actual365Fixed();
std::vector<Date> dates(3);
std::vector<Real> hazardRates(3);
dates[0] = today;
hazardRates[0] = h1;
dates[1] = today + 5*Years;
hazardRates[1] = h1;
dates[2] = today + 10*Years;
hazardRates[2] = h2;
RelinkableHandle<DefaultProbabilityTermStructure> probabilityCurve;
probabilityCurve.linkTo(ext::shared_ptr<DefaultProbabilityTermStructure>(
new InterpolatedHazardRateCurve<BackwardFlat>(dates,
hazardRates,
dayCounter)));
RelinkableHandle<YieldTermStructure> discountCurve;
discountCurve.linkTo(ext::shared_ptr<YieldTermStructure>(
new FlatForward(today,0.03,Actual360())));
Frequency frequency = Semiannual;
BusinessDayConvention convention = ModifiedFollowing;
Date issueDate = calendar.advance(today, -6, Months);
Rate fixedRate = 0.0120;
DayCounter cdsDayCount = Actual360();
Real notional = 10000.0;
Real recoveryRate = 0.4;
Rate latestRate = Null<Rate>();
for (Integer n=6; n<=10; ++n) {
Date maturity = calendar.advance(issueDate, n, Years);
Schedule schedule(issueDate, maturity, Period(frequency), calendar,
convention, convention,
DateGeneration::Forward, false);
CreditDefaultSwap cds(Protection::Seller, notional, fixedRate,
schedule, convention, cdsDayCount,
true, true);
cds.setPricingEngine(ext::shared_ptr<PricingEngine>(
new MidPointCdsEngine(probabilityCurve,
recoveryRate, discountCurve)));
Real NPV = cds.NPV();
Rate flatRate = cds.impliedHazardRate(NPV, discountCurve,
dayCounter,
recoveryRate);
if (flatRate < h1 || flatRate > h2) {
BOOST_ERROR("implied hazard rate outside expected range\n"
<< " maturity: " << n << " years\n"
<< " expected minimum: " << h1 << "\n"
<< " expected maximum: " << h2 << "\n"
<< " implied rate: " << flatRate);
}
if (n > 6 && flatRate < latestRate) {
BOOST_ERROR("implied hazard rate decreasing with swap maturity\n"
<< " maturity: " << n << " years\n"
<< " previous rate: " << latestRate << "\n"
<< " implied rate: " << flatRate);
}
latestRate = flatRate;
RelinkableHandle<DefaultProbabilityTermStructure> probability;
probability.linkTo(ext::shared_ptr<DefaultProbabilityTermStructure>(
new FlatHazardRate(
today,
Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(flatRate))),
dayCounter)));
CreditDefaultSwap cds2(Protection::Seller, notional, fixedRate,
schedule, convention, cdsDayCount,
true, true);
cds2.setPricingEngine(ext::shared_ptr<PricingEngine>(
new MidPointCdsEngine(probability,recoveryRate,
discountCurve)));
Real NPV2 = cds2.NPV();
Real tolerance = 1.0;
if (std::fabs(NPV-NPV2) > tolerance) {
BOOST_ERROR("failed to reproduce NPV with implied rate\n"
<< " expected: " << NPV << "\n"
<< " calculated: " << NPV2);
}
}
}
void CreditDefaultSwapTest::testFairSpread() {
BOOST_TEST_MESSAGE(
"Testing fair-spread calculation for credit-default swaps...");
SavedSettings backup;
// Initialize curves
Calendar calendar = TARGET();
Date today = calendar.adjust(Date::todaysDate());
Settings::instance().evaluationDate() = today;
Handle<Quote> hazardRate = Handle<Quote>(
ext::shared_ptr<Quote>(new SimpleQuote(0.01234)));
RelinkableHandle<DefaultProbabilityTermStructure> probabilityCurve;
probabilityCurve.linkTo(
ext::shared_ptr<DefaultProbabilityTermStructure>(
new FlatHazardRate(0, calendar, hazardRate, Actual360())));
RelinkableHandle<YieldTermStructure> discountCurve;
discountCurve.linkTo(ext::shared_ptr<YieldTermStructure>(
new FlatForward(today,0.06,Actual360())));
// Build the schedule
Date issueDate = calendar.advance(today, -1, Years);
Date maturity = calendar.advance(issueDate, 10, Years);
BusinessDayConvention convention = Following;
Schedule schedule =
MakeSchedule().from(issueDate)
.to(maturity)
.withFrequency(Quarterly)
.withCalendar(calendar)
.withTerminationDateConvention(convention)
.withRule(DateGeneration::TwentiethIMM);
// Build the CDS
Rate fixedRate = 0.001;
DayCounter dayCount = Actual360();
Real notional = 10000.0;
Real recoveryRate = 0.4;
ext::shared_ptr<PricingEngine> engine(
new MidPointCdsEngine(probabilityCurve,recoveryRate,discountCurve));
CreditDefaultSwap cds(Protection::Seller, notional, fixedRate,
schedule, convention, dayCount, true, true);
cds.setPricingEngine(engine);
Rate fairRate = cds.fairSpread();
CreditDefaultSwap fairCds(Protection::Seller, notional, fairRate,
schedule, convention, dayCount, true, true);
fairCds.setPricingEngine(engine);
Real fairNPV = fairCds.NPV();
Real tolerance = 1e-9;
if (std::fabs(fairNPV) > tolerance)
BOOST_ERROR(
"Failed to reproduce null NPV with calculated fair spread\n"
<< " calculated spread: " << io::rate(fairRate) << "\n"
<< " calculated NPV: " << fairNPV);
}
void CreditDefaultSwapTest::testFairUpfront() {
BOOST_TEST_MESSAGE(
"Testing fair-upfront calculation for credit-default swaps...");
SavedSettings backup;
// Initialize curves
Calendar calendar = TARGET();
Date today = calendar.adjust(Date::todaysDate());
Settings::instance().evaluationDate() = today;
Handle<Quote> hazardRate = Handle<Quote>(
ext::shared_ptr<Quote>(new SimpleQuote(0.01234)));
RelinkableHandle<DefaultProbabilityTermStructure> probabilityCurve;
probabilityCurve.linkTo(
ext::shared_ptr<DefaultProbabilityTermStructure>(
new FlatHazardRate(0, calendar, hazardRate, Actual360())));
RelinkableHandle<YieldTermStructure> discountCurve;
discountCurve.linkTo(ext::shared_ptr<YieldTermStructure>(
new FlatForward(today,0.06,Actual360())));
// Build the schedule
Date issueDate = today;
Date maturity = calendar.advance(issueDate, 10, Years);
BusinessDayConvention convention = Following;
Schedule schedule =
MakeSchedule().from(issueDate)
.to(maturity)
.withFrequency(Quarterly)
.withCalendar(calendar)
.withTerminationDateConvention(convention)
.withRule(DateGeneration::TwentiethIMM);
// Build the CDS
Rate fixedRate = 0.05;
Rate upfront = 0.001;
DayCounter dayCount = Actual360();
Real notional = 10000.0;
Real recoveryRate = 0.4;
ext::shared_ptr<PricingEngine> engine(
new MidPointCdsEngine(probabilityCurve, recoveryRate,
discountCurve, true));
CreditDefaultSwap cds(Protection::Seller, notional, upfront, fixedRate,
schedule, convention, dayCount, true, true);
cds.setPricingEngine(engine);
Rate fairUpfront = cds.fairUpfront();
CreditDefaultSwap fairCds(Protection::Seller, notional,
fairUpfront, fixedRate,
schedule, convention, dayCount, true, true);
fairCds.setPricingEngine(engine);
Real fairNPV = fairCds.NPV();
Real tolerance = 1e-9;
if (std::fabs(fairNPV) > tolerance)
BOOST_ERROR(
"Failed to reproduce null NPV with calculated fair upfront\n"
<< " calculated upfront: " << io::rate(fairUpfront) << "\n"
<< " calculated NPV: " << fairNPV);
// same with null upfront to begin with
upfront = 0.0;
CreditDefaultSwap cds2(Protection::Seller, notional, upfront, fixedRate,
schedule, convention, dayCount, true, true);
cds2.setPricingEngine(engine);
fairUpfront = cds2.fairUpfront();
CreditDefaultSwap fairCds2(Protection::Seller, notional,
fairUpfront, fixedRate,
schedule, convention, dayCount, true, true);
fairCds2.setPricingEngine(engine);
fairNPV = fairCds2.NPV();
if (std::fabs(fairNPV) > tolerance)
BOOST_ERROR(
"Failed to reproduce null NPV with calculated fair upfront\n"
<< " calculated upfront: " << io::rate(fairUpfront) << "\n"
<< " calculated NPV: " << fairNPV);
}
void CreditDefaultSwapTest::testIsdaEngine() {
BOOST_TEST_MESSAGE(
"Testing ISDA engine calculations for credit-default swaps...");
const auto & iborcoupon_settings = IborCoupon::Settings::instance();
SavedSettings backup;
Date tradeDate(21, May, 2009);
Settings::instance().evaluationDate() = tradeDate;
//build an ISDA compliant yield curve
//data comes from Markit published rates
std::vector<ext::shared_ptr<RateHelper> > isdaRateHelpers;
int dep_tenors[] = {1, 2, 3, 6, 9, 12};
double dep_quotes[] = {0.003081,
0.005525,
0.007163,
0.012413,
0.014,
0.015488};
for(size_t i = 0; i < sizeof(dep_tenors) / sizeof(int); i++) {
isdaRateHelpers.push_back(ext::make_shared<DepositRateHelper>(
dep_quotes[i], dep_tenors[i] * Months, 2,
WeekendsOnly(), ModifiedFollowing,
false, Actual360()
)
);
}
int swap_tenors[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20, 25, 30};
double swap_quotes[] = {0.011907,
0.01699,
0.021198,
0.02444,
0.026937,
0.028967,
0.030504,
0.031719,
0.03279,
0.034535,
0.036217,
0.036981,
0.037246,
0.037605};
ext::shared_ptr<IborIndex> isda_ibor = ext::make_shared<IborIndex>(
"IsdaIbor", 3 * Months, 2, USDCurrency(), WeekendsOnly(),
ModifiedFollowing, false, Actual360());
for(size_t i = 0; i < sizeof(swap_tenors) / sizeof(int); i++) {
isdaRateHelpers.push_back(ext::make_shared<SwapRateHelper>(
swap_quotes[i], swap_tenors[i] * Years,
WeekendsOnly(),
Semiannual,
ModifiedFollowing, Thirty360(Thirty360::BondBasis), isda_ibor
)
);
}
RelinkableHandle<YieldTermStructure> discountCurve;
discountCurve.linkTo(
ext::make_shared<PiecewiseYieldCurve<Discount, LogLinear> >(
0, WeekendsOnly(), isdaRateHelpers, Actual365Fixed())
);
RelinkableHandle<DefaultProbabilityTermStructure> probabilityCurve;
Date termDates[] = {Date(20, June, 2010),
Date(20, June, 2011),
Date(20, June, 2012),
Date(20, June, 2016),
Date(20, June, 2019)};
Rate spreads[] = {0.001, 0.1};
Rate recoveries[] = {0.2, 0.4};
double markitValues[] = {97798.29358, //0.001
97776.11889, //0.001
-914971.5977, //0.1
-894985.6298, //0.1
186921.3594, //0.001
186839.8148, //0.001
-1646623.672, //0.1
-1579803.626, //0.1
274298.9203,
274122.4725,
-2279730.93,
-2147972.527,
592420.2297,
591571.2294,
-3993550.206,
-3545843.418,
797501.1422,
795915.9787,
-4702034.688,
-4042340.999};
Real tolerance;
if (iborcoupon_settings.usingAtParCoupons()) {
tolerance = 1.0e-6;
} else {
/* The risk-free curve is a bit off. We might skip the tests
altogether and rely on running them with indexed coupons
disabled, but leaving them can be useful anyway. */
tolerance = 1.0e-3;
}
size_t l = 0;
for (auto termDate : termDates) {
for (double spread : spreads) {
for (double& recoverie : recoveries) {
ext::shared_ptr<CreditDefaultSwap> quotedTrade =
MakeCreditDefaultSwap(termDate, spread).withNominal(10000000.);
Rate h = quotedTrade->impliedHazardRate(0., discountCurve, Actual365Fixed(),
recoverie, 1e-10, CreditDefaultSwap::ISDA);
probabilityCurve.linkTo(
ext::make_shared<FlatHazardRate>(0, WeekendsOnly(), h, Actual365Fixed()));
ext::shared_ptr<IsdaCdsEngine> engine = ext::make_shared<IsdaCdsEngine>(
probabilityCurve, recoverie, discountCurve, boost::none, IsdaCdsEngine::Taylor,
IsdaCdsEngine::HalfDayBias, IsdaCdsEngine::Piecewise);
ext::shared_ptr<CreditDefaultSwap> conventionalTrade =
MakeCreditDefaultSwap(termDate, 0.01)
.withNominal(10000000.)
.withPricingEngine(engine);
BOOST_CHECK_CLOSE(conventionalTrade->notional() * conventionalTrade->fairUpfront(),
markitValues[l], tolerance);
l++;
}
}
}
}
void CreditDefaultSwapTest::testAccrualRebateAmounts() {
BOOST_TEST_MESSAGE("Testing accrual rebate amounts on credit default swaps...");
SavedSettings backup;
// The accrual values are taken from various test results on the ISDA CDS model website
// https://www.cdsmodel.com/cdsmodel/documentation.html.
// Inputs
Real notional = 10000000;
Real spread = 0.0100;
Date maturity(20, Jun, 2014);
// key is trade date and value is expected accrual
typedef map<Date, Real> InputData;
InputData inputs = {
{Date(18, Mar, 2009), 24166.67},
{Date(19, Mar, 2009), 0.00},
{Date(20, Mar, 2009), 277.78},
{Date(23, Mar, 2009), 1111.11},
{Date(19, Jun, 2009), 25555.56},
{Date(20, Jun, 2009), 25833.33},
{Date(21, Jun, 2009), 0.00},
{Date(22, Jun, 2009), 277.78},
{Date(18, Jun, 2014), 25277.78},
{Date(19, Jun, 2014), 25555.56}
};
for (auto& input: inputs) {
Settings::instance().evaluationDate() = input.first;
CreditDefaultSwap cds = MakeCreditDefaultSwap(maturity, spread)
.withNominal(notional);
BOOST_CHECK_SMALL(input.second - cds.accrualRebate()->amount(), 0.01);
}
}
test_suite* CreditDefaultSwapTest::suite() {
auto* suite = BOOST_TEST_SUITE("Credit-default swap tests");
suite->add(QUANTLIB_TEST_CASE(&CreditDefaultSwapTest::testCachedValue));
suite->add(QUANTLIB_TEST_CASE(
&CreditDefaultSwapTest::testCachedMarketValue));
suite->add(QUANTLIB_TEST_CASE(
&CreditDefaultSwapTest::testImpliedHazardRate));
suite->add(QUANTLIB_TEST_CASE(&CreditDefaultSwapTest::testFairSpread));
suite->add(QUANTLIB_TEST_CASE(&CreditDefaultSwapTest::testFairUpfront));
suite->add(QUANTLIB_TEST_CASE(&CreditDefaultSwapTest::testIsdaEngine));
suite->add(QUANTLIB_TEST_CASE(&CreditDefaultSwapTest::testAccrualRebateAmounts));
return suite;
}