Skip to content
Navigation Menu
{{ message }}
forked from eddelbuettel/rquantlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsabr.cpp
More file actions
296 lines (237 loc) · 13.3 KB
/
Copy pathsabr.cpp
File metadata and controls
296 lines (237 loc) · 13.3 KB
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
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2012 Peter Caspers
Copyright (C) 2016 Terry Leitch
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 "rquantlib.h"
using namespace QuantLib;
//using namespace boost::unit_test_framework;
using std::fabs;
namespace {
typedef SwaptionVolatilityStructure SwpVolStr; // shorthand used below
typedef SwaptionVolatilityMatrix SwpVolMat; // shorthand used below
Handle<SwpVolStr> swptnVolCube(Rcpp::NumericVector &expiries,
Rcpp::NumericVector &tenors,
Rcpp::NumericMatrix &atmVols,
Rcpp::NumericVector &strikes,
Rcpp::NumericMatrix &smirkVols,
QuantLib::Handle<QuantLib::YieldTermStructure> &yldCrv
) {
std::vector<Period> optionTenors;
std::vector<Period> swapTenors;
QuantLib::Size numExp = expiries.size();
for (Size i = 0; i <numExp ; i++){
if(expiries[i]<1.0){
int n = static_cast<int>(12*expiries[i]);
optionTenors.push_back(Months*n);
} else {
int n = static_cast<int>(expiries[i]);
optionTenors.push_back(Years*n);
}
}
QuantLib::Size numTenor = tenors.size();
for (Size i = 0; i < numTenor; i++){
if(tenors[i]<1.0){
int n = static_cast<int>(12*tenors[i]);
swapTenors.push_back(Months*n);
} else {
int n = static_cast<int>(tenors[i]);
swapTenors.push_back(Years*n);
}
}
std::vector<std::vector<Handle<Quote> > > qSwAtm;
for (unsigned int i = 0; i < numExp; i++) {
std::vector<Handle<Quote> > qSwAtmTmp;
for (unsigned int j = 0; j < numTenor; j++) {
double qt=atmVols(i,j);
qSwAtmTmp.push_back(Handle<Quote>(boost::shared_ptr<Quote>(new SimpleQuote(qt))));
}
qSwAtm.push_back(qSwAtmTmp);
}
Handle<SwpVolStr>
swaptionVolAtm(boost::shared_ptr<SwpVolStr>(new SwpVolMat(TARGET(),
ModifiedFollowing,
optionTenors, swapTenors, qSwAtm,
Actual365Fixed())));
std::vector<Period> optionTenorsSmile;
std::vector<Period> swapTenorsSmile;
std::vector<Real> strikeSpreads;
for (Size i = 0; i < numExp; i++){
if(expiries[i]<1.0){
int n = static_cast<int>(12*expiries[i]);
optionTenorsSmile.push_back(n*Months);
} else {
int n = static_cast<int>(expiries[i]);
optionTenorsSmile.push_back(n*Years);
}
}
for (Size i = 0; i < numTenor; i++){
if(tenors[i]<1.0){
int n = static_cast<int>(12*tenors[i]);
swapTenorsSmile.push_back(Months*n);
} else {
int n = static_cast<int>(tenors[i]);
swapTenorsSmile.push_back(Years*n);
}
}
QuantLib::Size numStrike = strikes.size();
for (Size i = 0; i < numStrike; i++)
strikeSpreads.push_back(strikes[i]);
std::vector<std::vector<Handle<Quote> > > qSwSmile;
for (unsigned int i = 0; i < numExp*numTenor; i++) {
std::vector<Handle<Quote> > qSwSmileTmp;
for (unsigned int j = 0; j < numStrike; j++) {
double qt=smirkVols(i,j);
qSwSmileTmp.push_back(Handle<Quote>(boost::shared_ptr<Quote>(new SimpleQuote(qt))));
}
qSwSmile.push_back(qSwSmileTmp);
}
double qSwSmileh1[] = {
0.01, 0.2, 0.8, -0.2
};
std::vector<bool> parameterFixed;
parameterFixed.push_back(false);
parameterFixed.push_back(false); // beta could be fixed
parameterFixed.push_back(false);
parameterFixed.push_back(false);
std::vector<std::vector<Handle<Quote> > > parameterGuess;
for (unsigned int i = 0; i < numExp*numTenor; i++) {
std::vector<Handle<Quote> > parameterGuessTmp;
for (unsigned int j = 0; j < 4; j++) {
parameterGuessTmp.push_back(Handle<Quote>(boost::shared_ptr<Quote>(new SimpleQuote(qSwSmileh1[j]))));
}
parameterGuess.push_back(parameterGuessTmp);
}
boost::shared_ptr<EndCriteria> ec(new EndCriteria(5000000, 250, 1E-6, 1E-6, 1E-6));
boost::shared_ptr<SwapIndex> swapIndex(new EuriborSwapIsdaFixA(30 * Years, Handle<YieldTermStructure>(yldCrv)));
boost::shared_ptr<SwapIndex> shortSwapIndex(new EuriborSwapIsdaFixA(1 * Years, Handle<YieldTermStructure>(yldCrv)));
Handle<SwpVolStr>
res(boost::shared_ptr<SwpVolStr>(new SwaptionVolCube1(swaptionVolAtm, optionTenorsSmile,
swapTenorsSmile, strikeSpreads, qSwSmile,
swapIndex, shortSwapIndex, true,
parameterGuess, parameterFixed, true, ec,
.050)));
// put a big error tolerance here ... we just want a
// smooth cube for testing
res->enableExtrapolation();
return res;
}
} /// closes name space
// [[Rcpp::export]]
Rcpp::List sabrengine(Rcpp::List rparam,
Rcpp::List legParams,
std::vector<QuantLib::Date> dateVec,
std::vector<double> zeroVec,
Rcpp::NumericVector swaptionMat,
Rcpp::NumericVector swapLengths,
Rcpp::NumericMatrix atmVols,
Rcpp::NumericVector strikes,
Rcpp::NumericMatrix smirkVols){
QuantLib::Date todaysDate(Rcpp::as<QuantLib::Date>(rparam["tradeDate"]));
QuantLib::Date settlementDate(Rcpp::as<QuantLib::Date>(rparam["settleDate"]));
QuantLib::Date startDate(Rcpp::as<QuantLib::Date>(rparam["startDate"]));
QuantLib::Date expiryDate(Rcpp::as<QuantLib::Date>(rparam["expiryDate"]));
QuantLib::Date maturity(Rcpp::as<QuantLib::Date>(rparam["maturity"]));
bool european = Rcpp::as<bool>(rparam["european"]);
double strike = Rcpp::as<double>(rparam["strike"]);
//double fixDayCount = Rcpp::as<double>(legParams["dayCounter"]); // currently not used, fix in future
double fixFreq = Rcpp::as<double>(legParams["fixFreq"]) ;
int floatFreq = Rcpp::as<int>(legParams["floatFreq"]);
const Real tol1 = 0.0001; // 1bp tolerance for model engine call put premia
// BOOST_TEST_MESSAGE("Testing Markov functional vanilla engines...");
Date savedEvalDate = Settings::instance().evaluationDate();
Settings::instance().evaluationDate() = todaysDate;
QuantLib::Handle<QuantLib::YieldTermStructure> yldCrv(rebuildCurveFromZeroRates(dateVec, zeroVec));
Handle<SwaptionVolatilityStructure> volCube = swptnVolCube(swaptionMat,swapLengths,atmVols,
strikes,smirkVols,yldCrv);
boost::shared_ptr<IborIndex> iborIndex1(new Euribor(floatFreq * Months, yldCrv));
boost::shared_ptr<SwapIndex> swapIndexBase (new EuriborSwapIsdaFixA(fixFreq * Years));
// create swaps for european swaption here to get atm fwd rate, these are ignored for bermudan //
boost::shared_ptr<VanillaSwap> underlyingCall =
MakeVanillaSwap(Years*(((maturity-expiryDate)/365.0)), iborIndex1, strike)
.withEffectiveDate(expiryDate)
.receiveFixed(false);
boost::shared_ptr<VanillaSwap> underlyingPut =
MakeVanillaSwap(Years*((maturity-expiryDate)/365.0), iborIndex1, strike)
.withEffectiveDate(expiryDate)
.receiveFixed(true);
boost::shared_ptr<PricingEngine> swapEngine(new DiscountingSwapEngine(yldCrv));
underlyingCall->setPricingEngine(swapEngine);
underlyingPut->setPricingEngine(swapEngine);
Real vol,pricePay,priceRcv,rate;
vol=volCube->volatility(Years*((expiryDate-settlementDate)/365.0), Years*((maturity-expiryDate)/365.0), strike);
rate=underlyingCall->fairRate();
// calculate if bermudan here //
if(!european) {
boost::shared_ptr<VanillaSwap> underlyingCall2 =
MakeVanillaSwap(Years*(((maturity-startDate)/365.0)), iborIndex1, strike)
.withEffectiveDate(startDate)
.receiveFixed(false);
boost::shared_ptr<VanillaSwap> underlyingPut2 =
MakeVanillaSwap(Years*((maturity-startDate)/365.0), iborIndex1, strike)
.withEffectiveDate(startDate)
.receiveFixed(true);
boost::shared_ptr<PricingEngine> swapEngine(new DiscountingSwapEngine(yldCrv));
underlyingCall2->setPricingEngine(swapEngine);
underlyingPut2->setPricingEngine(swapEngine);
std::vector<Date> volStepDates;
std::vector<Real> vols;
vols.push_back(1.0);
std::vector<QuantLib::Date> exerciseDates;
std::vector<QuantLib::Period> underlyingTenors;
const std::vector<boost::shared_ptr<QuantLib::CashFlow> >& leg = underlyingCall2->fixedLeg();
for (unsigned int i=0; i<leg.size(); i++) {
boost::shared_ptr<QuantLib::Coupon> coupon = boost::dynamic_pointer_cast<QuantLib::Coupon>(leg[i]);
if(coupon->accrualStartDate() <= expiryDate && coupon->accrualStartDate() >= startDate) {
exerciseDates.push_back(coupon->accrualStartDate());
underlyingTenors.push_back(Years*((maturity-(coupon->accrualStartDate()))/365.0));
}
}
// create expiry backet, get maturity
boost::shared_ptr<MarkovFunctional>
mf1(new MarkovFunctional(yldCrv, 0.01, volStepDates, vols,volCube,
exerciseDates, underlyingTenors,
swapIndexBase, MarkovFunctional::ModelSettings()
.withYGridPoints(32)
.withYStdDevs(7.0)
.withGaussHermitePoints(16)
.withMarketRateAccuracy(1e-7)
.withDigitalGap(1e-5)
.withLowerRateBound(0.0)
.withUpperRateBound(2.0)));
boost::shared_ptr<PricingEngine> mfSwaptionEngine1(new Gaussian1dSwaptionEngine(mf1, 64, 7.0));
boost::shared_ptr<Exercise> bermudanExercise(new BermudanExercise(exerciseDates));
Swaption bermudanSwaptionC(underlyingCall2, bermudanExercise);
Swaption bermudanSwaptionP(underlyingPut2, bermudanExercise);
bermudanSwaptionC.setPricingEngine(mfSwaptionEngine1);
bermudanSwaptionP.setPricingEngine(mfSwaptionEngine1);
pricePay = bermudanSwaptionC.NPV();
priceRcv = bermudanSwaptionP.NPV();
} else {
// calculate european here //
boost::shared_ptr<BlackSwaptionEngine> blackSwaptionEngine1(new BlackSwaptionEngine(yldCrv, volCube));
boost::shared_ptr<Exercise> exercise(new EuropeanExercise(startDate));
// Rprintf("%d %d %d\n",outputs1.expiries_[i].dayOfMonth(),outputs1.expiries_[i].month(),outputs1.expiries_[i].year());
Swaption swaptionC(underlyingCall, exercise);
Swaption swaptionP(underlyingPut, exercise);
swaptionC.setPricingEngine(blackSwaptionEngine1);
swaptionP.setPricingEngine(blackSwaptionEngine1);
pricePay = swaptionC.NPV();
priceRcv = swaptionP.NPV();
}
////////men at work/////////////////
return Rcpp::List::create(Rcpp::Named("pay") = pricePay,
Rcpp::Named("rcv") = priceRcv,
Rcpp::Named("sigma") = vol,
Rcpp::Named("atmRate") = rate);
}
You can’t perform that action at this time.
