forked from auto-differentiation/QuantLib-Risks-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwardrateagreement_xad.cpp
190 lines (150 loc) · 6.2 KB
/
forwardrateagreement_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
/* -*- 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/indexes/ibor/usdlibor.hpp>
#include <ql/instruments/forwardrateagreement.hpp>
#include <ql/termstructures/yield/zerocurve.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/calendars/unitedstates.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/period.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(ForwardRateAgreementXadTests)
namespace {
struct ForwardRateAgreementData {
Real nominal;
Real spotRate1;
Real spotRate2;
Real spotRate3;
Real rate;
};
}
namespace {
template <class PriceFunc>
Real priceWithBumping(const ForwardRateAgreementData& value,
ForwardRateAgreementData& derivatives,
PriceFunc func) {
// Bumping
auto eps = 1e-7;
auto data = value;
auto v = func(data);
data.nominal += 1;
auto vplus = func(data);
derivatives.nominal = (vplus - v) / 1;
data = value;
data.spotRate1 += eps;
vplus = func(data);
derivatives.spotRate1 = (vplus - v) / eps;
data = value;
data.spotRate2 += eps;
vplus = func(data);
derivatives.spotRate2 = (vplus - v) / eps;
data = value;
data.spotRate3 += eps;
vplus = func(data);
derivatives.spotRate3 = (vplus - v) / eps;
data = value;
data.rate += eps;
vplus = func(data);
derivatives.rate = (vplus - v) / eps;
return v;
}
template <class PriceFunc>
Real priceWithAAD(const ForwardRateAgreementData& values,
ForwardRateAgreementData& derivatives,
PriceFunc func) {
// AAD
using tape_type = Real::tape_type;
tape_type tape;
auto data = values;
tape.registerInput(data.nominal);
tape.registerInput(data.spotRate1);
tape.registerInput(data.spotRate2);
tape.registerInput(data.spotRate3);
tape.registerInput(data.rate);
tape.newRecording();
auto price = func(data);
tape.registerOutput(price);
derivative(price) = 1.0;
tape.computeAdjoints();
derivatives.nominal = derivative(data.nominal);
derivatives.spotRate1 = derivative(data.spotRate1);
derivatives.spotRate2 = derivative(data.spotRate2);
derivatives.spotRate3 = derivative(data.spotRate3);
derivatives.rate = derivative(data.rate);
return price;
}
}
namespace {
Real priceForwardRateAgreement(const ForwardRateAgreementData& value) {
Date today(30, June, 2020);
Settings::instance().evaluationDate() = today;
std::vector<Date> spotDates;
spotDates.push_back(Date(30, June, 2020));
spotDates.push_back(Date(31, December, 2020));
spotDates.push_back(Date(30, June, 2021));
std::vector<Real> spotRates;
spotRates.push_back(value.spotRate1);
spotRates.push_back(value.spotRate2);
spotRates.push_back(value.spotRate3);
DayCounter dayConvention = Actual360();
Calendar calendar = UnitedStates(UnitedStates::GovernmentBond);
Date startDate = calendar.advance(today, Period(3, Months));
Date maturityDate = calendar.advance(startDate, Period(3, Months));
Compounding compounding = Simple;
Frequency compoundingFrequency = Annual;
Handle<YieldTermStructure> spotCurve(
ext::make_shared<ZeroCurve>(spotDates, spotRates, dayConvention, calendar, Linear(),
compounding, compoundingFrequency));
spotCurve->enableExtrapolation();
auto index = ext::make_shared<USDLibor>(Period(3, Months), spotCurve);
index->addFixing(Date(26, June, 2020), 0.05);
ForwardRateAgreement fra(index, startDate, maturityDate, Position::Long, value.rate,
value.nominal, spotCurve);
return fra.NPV();
}
}
BOOST_AUTO_TEST_CASE(testForwardRateAgreementDerivatives) {
SavedSettings save;
BOOST_TEST_MESSAGE("Testing forward rate agreement derivatives...");
// input
auto data = ForwardRateAgreementData{100000, 0.5, 0.5, 0.5, 0.06};
// bumping
auto derivatives_bumping = ForwardRateAgreementData{};
auto expected = priceWithBumping(data, derivatives_bumping, priceForwardRateAgreement);
// aad
auto derivatives_aad = ForwardRateAgreementData{};
auto actual = priceWithAAD(data, derivatives_aad, priceForwardRateAgreement);
// compare
QL_CHECK_CLOSE(expected, actual, 1e-9);
QL_CHECK_CLOSE(derivatives_bumping.nominal, derivatives_aad.nominal, 1e-3);
QL_CHECK_CLOSE(derivatives_bumping.spotRate1, derivatives_aad.spotRate1, 1e-3);
QL_CHECK_CLOSE(derivatives_bumping.spotRate2, derivatives_aad.spotRate2, 1e-3);
QL_CHECK_CLOSE(derivatives_bumping.spotRate3, derivatives_aad.spotRate3, 1e-3);
QL_CHECK_CLOSE(derivatives_bumping.rate, derivatives_aad.rate, 1e-3);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()