-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcreditdefaultswap_xad.cpp
195 lines (154 loc) · 6.55 KB
/
creditdefaultswap_xad.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
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2003, 2004 Ferdinando Ametrano
Copyright (C) 2005, 2007 StatPro Italia srl
Copyright (C) 2005 Joseph Wang
Copyright (C) 2023, 2024 Xcelerit Computing Limited
This file is part of QuantLib / XAD integration module.
It is modified from 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
<[email protected]>. 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 "toplevelfixture.hpp"
#include "utilities_xad.hpp"
#include <ql/instruments/creditdefaultswap.hpp>
#include <ql/pricingengines/credit/midpointcdsengine.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/termstructures/credit/flathazardrate.hpp>
#include <ql/termstructures/defaulttermstructure.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/dategenerationrule.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/schedule.hpp>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace QuantLib;
using namespace boost::unit_test_framework;
BOOST_FIXTURE_TEST_SUITE(QuantLibRisksTests, TopLevelFixture)
BOOST_AUTO_TEST_SUITE(CreditDefaultSwapXadTests)
namespace {
struct CreditDefaultSwapData {
// Build the CDS
Rate fixedRate;
Real notional;
Real recoveryRate;
Real hazardRate;
Real riskFreeRate;
};
}
namespace {
template <class PriceFunc>
Real priceWithBumping(const CreditDefaultSwapData& value,
CreditDefaultSwapData& derivatives,
PriceFunc func) {
// Bumping
auto eps = 1e-7;
auto data = value;
auto v = func(data);
data.fixedRate += eps;
auto vplus = func(data);
derivatives.fixedRate = (vplus - v) / eps;
data = value;
data.notional += 1;
vplus = func(data);
derivatives.notional = (vplus - v) / 1;
data = value;
data.recoveryRate += eps;
vplus = func(data);
derivatives.recoveryRate = (vplus - v) / eps;
data = value;
data.hazardRate += eps;
vplus = func(data);
derivatives.hazardRate = (vplus - v) / eps;
data = value;
data.riskFreeRate += eps;
vplus = func(data);
derivatives.riskFreeRate = (vplus - v) / eps;
data = value;
return v;
}
template <class PriceFunc>
Real priceWithAAD(const CreditDefaultSwapData& values,
CreditDefaultSwapData& derivatives,
PriceFunc func) {
// AAD
using tape_type = Real::tape_type;
tape_type tape;
auto data = values;
tape.registerInput(data.notional);
tape.registerInput(data.hazardRate);
tape.registerInput(data.recoveryRate);
tape.registerInput(data.fixedRate);
tape.registerInput(data.riskFreeRate);
tape.newRecording();
auto price = func(data);
tape.registerOutput(price);
derivative(price) = 1.0;
tape.computeAdjoints();
derivatives.notional = derivative(data.notional);
derivatives.hazardRate = derivative(data.hazardRate);
derivatives.recoveryRate = derivative(data.recoveryRate);
derivatives.fixedRate = derivative(data.fixedRate);
derivatives.riskFreeRate = derivative(data.riskFreeRate);
return price;
}
}
namespace {
Real priceCreditDefaultSwap(const CreditDefaultSwapData& value) {
DayCounter dayCount = Actual360();
// Initialize curves
Settings::instance().evaluationDate() = Date(9, June, 2006);
Date today = Settings::instance().evaluationDate();
Calendar calendar = TARGET();
Handle<Quote> hazardRate(ext::make_shared<SimpleQuote>(value.hazardRate));
RelinkableHandle<DefaultProbabilityTermStructure> probabilityCurve;
probabilityCurve.linkTo(
ext::make_shared<FlatHazardRate>(0, calendar, hazardRate, Actual360()));
RelinkableHandle<YieldTermStructure> discountCurve;
discountCurve.linkTo(ext::make_shared<FlatForward>(today, value.riskFreeRate, 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);
auto cds =
ext::make_shared<CreditDefaultSwap>(Protection::Seller, value.notional, value.fixedRate,
schedule, convention, dayCount, true, true);
cds->setPricingEngine(ext::make_shared<MidPointCdsEngine>(
probabilityCurve, value.recoveryRate, discountCurve));
return cds->NPV();
}
}
BOOST_AUTO_TEST_CASE(testCreditDefaultSwapDerivatives) {
SavedSettings save;
BOOST_TEST_MESSAGE("Testing credit default swap derivatives...");
// input
auto data = CreditDefaultSwapData{0.0120, 10000.0, 0.4, 0.01234, 0.06};
// bumping
auto derivatives_bumping = CreditDefaultSwapData{};
auto expected = priceWithBumping(data, derivatives_bumping, priceCreditDefaultSwap);
// aad
auto derivatives_aad = CreditDefaultSwapData{};
auto actual = priceWithAAD(data, derivatives_aad, priceCreditDefaultSwap);
// compare
QL_CHECK_CLOSE(expected, actual, 1e-9);
QL_CHECK_CLOSE(derivatives_bumping.notional, derivatives_aad.notional, 1e-3);
QL_CHECK_CLOSE(derivatives_bumping.hazardRate, derivatives_aad.hazardRate, 1e-3);
QL_CHECK_CLOSE(derivatives_bumping.fixedRate, derivatives_aad.fixedRate, 1e-3);
QL_CHECK_CLOSE(derivatives_bumping.recoveryRate, derivatives_aad.recoveryRate, 1e-3);
QL_CHECK_CLOSE(derivatives_bumping.riskFreeRate, derivatives_aad.riskFreeRate, 1e-3);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()