forked from auto-differentiation/QuantLib-Risks-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap_xad.cpp
193 lines (150 loc) · 6.17 KB
/
swap_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
/* -*- 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/cashflows/couponpricer.hpp>
#include <ql/cashflows/fixedratecoupon.hpp>
#include <ql/cashflows/iborcoupon.hpp>
#include <ql/currencies/europe.hpp>
#include <ql/indexes/ibor/euribor.hpp>
#include <ql/instruments/vanillaswap.hpp>
#include <ql/pricingengines/swap/discountingswapengine.hpp>
#include <ql/termstructures/volatility/optionlet/constantoptionletvol.hpp>
#include <ql/time/daycounters/simpledaycounter.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(SwapXadTests)
namespace {
struct SwapData {
VanillaSwap::Type type;
Real n; // nominal
Real s; // spread
Rate g; // gearing
Volatility v; // volatility
};
}
namespace {
template <class PriceFunc>
Real priceWithBumping(const SwapData& value, SwapData& derivatives, PriceFunc func) {
// bumping
auto eps = 1e-7;
auto data = value;
auto v = func(data);
data.n += 1;
auto vplus = func(data);
derivatives.n = (vplus - v) / 1;
data = value;
data.s += eps;
vplus = func(data);
derivatives.s = (vplus - v) / eps;
data = value;
data.g += eps;
vplus = func(data);
derivatives.g = (vplus - v) / eps;
data = value;
data.v += eps;
vplus = func(data);
derivatives.v = (vplus - v) / eps;
data = value;
return v;
}
template <class PriceFunc>
Real priceWithAAD(const SwapData& values, SwapData& derivatives, PriceFunc func) {
// AAD
using tape_type = Real::tape_type;
tape_type tape;
auto data = values;
tape.registerInput(data.n);
tape.registerInput(data.s);
tape.registerInput(data.g);
tape.registerInput(data.v);
tape.newRecording();
auto price = func(data);
tape.registerOutput(price);
derivative(price) = 1.0;
tape.computeAdjoints();
derivatives.n = derivative(data.n);
derivatives.s = derivative(data.s);
derivatives.g = derivative(data.g);
derivatives.v = derivative(data.v);
return price;
}
}
namespace {
Real priceSwap(const SwapData& value) {
RelinkableHandle<YieldTermStructure> termStructure;
Calendar calendar = NullCalendar();
Date today = calendar.adjust(Settings::instance().evaluationDate());
Date settlement = calendar.advance(today, 2, Days);
termStructure.linkTo(flatRate(settlement, 0.05, Actual365Fixed()));
Date maturity = today + 5 * Years;
Natural fixingDays = 0;
Schedule schedule(today, maturity, Period(Annual), calendar, Following, Following,
DateGeneration::Forward, false);
DayCounter dayCounter = SimpleDayCounter();
auto index = ext::make_shared<IborIndex>("dummy", 1 * Years, 0, EURCurrency(), calendar,
Following, false, dayCounter, termStructure);
Rate oneYear = 0.05;
Rate r = std::log(1.0 + oneYear);
termStructure.linkTo(flatRate(today, r, dayCounter));
std::vector<Rate> coupons(1, oneYear);
Leg fixedLeg =
FixedRateLeg(schedule).withNotionals(value.n).withCouponRates(coupons, dayCounter);
Handle<OptionletVolatilityStructure> vol(ext::make_shared<ConstantOptionletVolatility>(
today, NullCalendar(), Following, value.v, dayCounter));
auto pricer = ext::make_shared<BlackIborCouponPricer>(vol);
Leg floatingLeg = IborLeg(schedule, index)
.withNotionals(value.n)
.withPaymentDayCounter(dayCounter)
.withFixingDays(fixingDays)
.withGearings(value.g)
.withSpreads(value.s)
.inArrears();
setCouponPricer(floatingLeg, pricer);
auto swap = ext::make_shared<Swap>(floatingLeg, fixedLeg);
swap->setPricingEngine(
ext::shared_ptr<PricingEngine>(new DiscountingSwapEngine(termStructure)));
return swap->NPV();
}
}
BOOST_AUTO_TEST_CASE(testSwapDerivatives) {
SavedSettings save;
BOOST_TEST_MESSAGE("Testing European options derivatives...");
// input
auto data = SwapData{VanillaSwap::Payer, 1000000.0, -0.001, 0.01, 0.22};
// Bumping
auto derivatives_Bumping = SwapData{};
auto expected = priceWithBumping(data, derivatives_Bumping, priceSwap);
// aad
auto derivatives_aad = SwapData{};
auto actual = priceWithAAD(data, derivatives_aad, priceSwap);
// compare
QL_CHECK_CLOSE(expected, actual, 1e-9);
QL_CHECK_CLOSE(derivatives_Bumping.n, derivatives_aad.n, 1e-3);
QL_CHECK_CLOSE(derivatives_Bumping.s, derivatives_aad.s, 1e-3);
QL_CHECK_CLOSE(derivatives_Bumping.g, derivatives_aad.g, 1e-3);
QL_CHECK_CLOSE(derivatives_Bumping.v, derivatives_aad.v, 1e-3);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()