File: MulticurveBootstrapping.cpp

package info (click to toggle)
quantlib 1.39-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 41,264 kB
  • sloc: cpp: 396,561; makefile: 6,539; python: 272; sh: 154; lisp: 86
file content (517 lines) | stat: -rw-r--r-- 21,391 bytes parent folder | download | duplicates (2)
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
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*!
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
 Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl
 Copyright (C) 2004 Ferdinando Ametrano
 Copyright (C) 2018 Jose Garcia

 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.
*/

/*  This example shows how to set up a term structure with OIS discounting
    and then price a simple 5 year swap.

    Example based on market data in paper by F. M. Ametrano and M. Bianchetti,
    Everything You Always Wanted to Know About Multiple Interest Rate Curve Boostrapping
    but Were Afraid to Ask (April 2, 2013).
    http://ssrn.com/abstract=2219548
    Eonia curve was taken from Figure 25 and Euribor 6m from figure 31.
*/

#include <ql/qldefines.hpp>
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
#  include <ql/auto_link.hpp>
#endif
#include <ql/termstructures/yield/piecewiseyieldcurve.hpp>
#include <ql/termstructures/yieldtermstructure.hpp>
#include <ql/termstructures/yield/ratehelpers.hpp>
#include <ql/termstructures/yield/oisratehelper.hpp>
#include <ql/pricingengines/swap/discountingswapengine.hpp>
#include <ql/indexes/ibor/eonia.hpp>
#include <ql/indexes/ibor/euribor.hpp>
#include <ql/time/imm.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <ql/math/interpolations/cubicinterpolation.hpp>
#include <ql/math/interpolations/loginterpolation.hpp>

#include <iostream>
#include <iomanip>

using namespace QuantLib;

int main(int, char* []) {

    try {

        std::cout << std::endl;

        /*************************
         ***  GLOBAL SETTINGS  ***
         *************************/

        Calendar calendar = TARGET();

        Date todaysDate(11, December, 2012);
        Settings::instance().evaluationDate() = todaysDate;
        todaysDate = Settings::instance().evaluationDate();

        Integer fixingDays = 2;
        Date settlementDate = calendar.advance(todaysDate, fixingDays, Days);
        // must be a business day
        settlementDate = calendar.adjust(settlementDate);


        std::cout << "Today: " << todaysDate.weekday()
                  << ", " << todaysDate << std::endl;

        std::cout << "Settlement date: " << settlementDate.weekday()
                  << ", " << settlementDate << std::endl;

        /*********************
        **   EONIA CURVE    **
        *********************/

        DayCounter termStructureDayCounter = Actual365Fixed();
        std::vector<ext::shared_ptr<RateHelper>> eoniaInstruments;

        auto eonia = ext::make_shared<Eonia>();

        // a SimpleQuote instance stores a value which can be manually changed;
        // other Quote subclasses could read the value from a database
        // or some kind of data feed.

        // RateHelpers are built from the quotes, together with
        // other info depending on the instrument.  Quotes are passed in
        // relinkable handles which could be relinked to some other
        // data source later.

        // deposits

        std::map<Natural, ext::shared_ptr<Quote>> depoQuotes = {
            // settlement days, quote
            {0, ext::make_shared<SimpleQuote>(0.0004)},
            {1, ext::make_shared<SimpleQuote>(0.0004)},
            {2, ext::make_shared<SimpleQuote>(0.0004)}
        };

        DayCounter depositDayCounter = Actual360();

        for (const auto& q : depoQuotes) {
            auto settlementDays = q.first;
            auto quote = q.second;
            auto helper = ext::make_shared<DepositRateHelper>(
                Handle<Quote>(quote),
                1 * Days, settlementDays,
                calendar, Following,
                false, depositDayCounter);
            eoniaInstruments.push_back(helper);
        }

        // short-term OIS

        std::map<Period, ext::shared_ptr<Quote>> shortOisQuotes = {
            {1 * Weeks, ext::make_shared<SimpleQuote>(0.00070)},
            {2 * Weeks, ext::make_shared<SimpleQuote>(0.00069)},
            {3 * Weeks, ext::make_shared<SimpleQuote>(0.00078)},
            {1 * Months, ext::make_shared<SimpleQuote>(0.00074)}
        };

        for (const auto& q : shortOisQuotes) {
            auto tenor = q.first;
            auto quote = q.second;
            auto helper = ext::make_shared<OISRateHelper>(
                2, tenor, Handle<Quote>(quote), eonia);
            eoniaInstruments.push_back(helper);
        }

        // Dated OIS

        std::map<std::pair<Date, Date>, ext::shared_ptr<Quote>> datedOisQuotes = {
            {{Date(16, January, 2013), Date(13, February, 2013)}, ext::make_shared<SimpleQuote>( 0.000460)},
            {{Date(13, February, 2013), Date(13, March, 2013)}, ext::make_shared<SimpleQuote>( 0.000160)},
            {{Date(13, March, 2013), Date(10, April, 2013)}, ext::make_shared<SimpleQuote>(-0.000070)},
            {{Date(10, April, 2013), Date(8, May, 2013)}, ext::make_shared<SimpleQuote>(-0.000130)},
            {{Date(8, May, 2013), Date(12, June, 2013)}, ext::make_shared<SimpleQuote>(-0.000140)},
        };

        for (const auto& q : datedOisQuotes) {
            auto startDate = q.first.first;
            auto endDate = q.first.second;
            auto quote = q.second;
            auto helper = ext::make_shared<OISRateHelper>(
                startDate, endDate, Handle<Quote>(quote), eonia);
            eoniaInstruments.push_back(helper);
        }

        // long-term OIS

        std::map<Period, ext::shared_ptr<Quote>> longOisQuotes = {
            {15 * Months, ext::make_shared<SimpleQuote>(0.00002)},
            {18 * Months, ext::make_shared<SimpleQuote>(0.00008)},
            {21 * Months, ext::make_shared<SimpleQuote>(0.00021)},
            {2 * Years, ext::make_shared<SimpleQuote>(0.00036)},
            {3 * Years, ext::make_shared<SimpleQuote>(0.00127)},
            {4 * Years, ext::make_shared<SimpleQuote>(0.00274)},
            {5 * Years, ext::make_shared<SimpleQuote>(0.00456)},
            {6 * Years, ext::make_shared<SimpleQuote>(0.00647)},
            {7 * Years, ext::make_shared<SimpleQuote>(0.00827)},
            {8 * Years, ext::make_shared<SimpleQuote>(0.00996)},
            {9 * Years, ext::make_shared<SimpleQuote>(0.01147)},
            {10 * Years, ext::make_shared<SimpleQuote>(0.0128)},
            {11 * Years, ext::make_shared<SimpleQuote>(0.01404)},
            {12 * Years, ext::make_shared<SimpleQuote>(0.01516)},
            {15 * Years, ext::make_shared<SimpleQuote>(0.01764)},
            {20 * Years, ext::make_shared<SimpleQuote>(0.01939)},
            {25 * Years, ext::make_shared<SimpleQuote>(0.02003)},
            {30 * Years, ext::make_shared<SimpleQuote>(0.02038)}
        };

        for (const auto& q : longOisQuotes) {
            auto tenor = q.first;
            auto quote = q.second;
            auto helper = ext::make_shared<OISRateHelper>(
                2, tenor, Handle<Quote>(quote), eonia);
            eoniaInstruments.push_back(helper);
        }

        // curve

        auto eoniaTermStructure = ext::make_shared<PiecewiseYieldCurve<Discount, Cubic>>(
                todaysDate, eoniaInstruments, termStructureDayCounter);

        eoniaTermStructure->enableExtrapolation();

        // This curve will be used for discounting cash flows
        RelinkableHandle<YieldTermStructure> discountingTermStructure;
        discountingTermStructure.linkTo(eoniaTermStructure);


        /**************************
        **    EURIBOR 6M CURVE   **
        ***************************/

        std::vector<ext::shared_ptr<RateHelper>> euribor6MInstruments;

        auto euribor6M = ext::make_shared<Euribor6M>();

        // deposits

        auto d6MRate = ext::make_shared<SimpleQuote>(0.00312);

        auto d6M = ext::make_shared<DepositRateHelper>(
            Handle<Quote>(d6MRate),
            6 * Months, 3,
            calendar, Following,
            false, depositDayCounter);

        euribor6MInstruments.push_back(d6M);

        // FRAs

        std::map<Natural, ext::shared_ptr<Quote>> fraQuotes = {
            {1, ext::make_shared<SimpleQuote>(0.002930)},
            {2, ext::make_shared<SimpleQuote>(0.002720)},
            {3, ext::make_shared<SimpleQuote>(0.002600)},
            {4, ext::make_shared<SimpleQuote>(0.002560)},
            {5, ext::make_shared<SimpleQuote>(0.002520)},
            {6, ext::make_shared<SimpleQuote>(0.002480)},
            {7, ext::make_shared<SimpleQuote>(0.002540)},
            {8, ext::make_shared<SimpleQuote>(0.002610)},
            {9, ext::make_shared<SimpleQuote>(0.002670)},
            {10, ext::make_shared<SimpleQuote>(0.002790)},
            {11, ext::make_shared<SimpleQuote>(0.002910)},
            {12, ext::make_shared<SimpleQuote>(0.003030)},
            {13, ext::make_shared<SimpleQuote>(0.003180)},
            {14, ext::make_shared<SimpleQuote>(0.003350)},
            {15, ext::make_shared<SimpleQuote>(0.003520)},
            {16, ext::make_shared<SimpleQuote>(0.003710)},
            {17, ext::make_shared<SimpleQuote>(0.003890)},
            {18, ext::make_shared<SimpleQuote>(0.004090)}
        };

        for (const auto& q : fraQuotes) {
            auto monthsToStart = q.first;
            auto quote = q.second;
            auto helper = ext::make_shared<FraRateHelper>(
                Handle<Quote>(quote),
                monthsToStart, euribor6M);
            euribor6MInstruments.push_back(helper);
        }

        // swaps

        std::map<Period, ext::shared_ptr<Quote>> swapQuotes = {
            {3 * Years, ext::make_shared<SimpleQuote>(0.004240)},
            {4 * Years, ext::make_shared<SimpleQuote>(0.005760)},
            {5 * Years, ext::make_shared<SimpleQuote>(0.007620)},
            {6 * Years, ext::make_shared<SimpleQuote>(0.009540)},
            {7 * Years, ext::make_shared<SimpleQuote>(0.011350)},
            {8 * Years, ext::make_shared<SimpleQuote>(0.013030)},
            {9 * Years, ext::make_shared<SimpleQuote>(0.014520)},
            {10 * Years, ext::make_shared<SimpleQuote>(0.015840)},
            {12 * Years, ext::make_shared<SimpleQuote>(0.018090)},
            {15 * Years, ext::make_shared<SimpleQuote>(0.020370)},
            {20 * Years, ext::make_shared<SimpleQuote>(0.021870)},
            {25 * Years, ext::make_shared<SimpleQuote>(0.022340)},
            {30 * Years, ext::make_shared<SimpleQuote>(0.022560)},
            {35 * Years, ext::make_shared<SimpleQuote>(0.022950)},
            {40 * Years, ext::make_shared<SimpleQuote>(0.023480)},
            {50 * Years, ext::make_shared<SimpleQuote>(0.024210)},
            {60 * Years, ext::make_shared<SimpleQuote>(0.024630)}
        };

        Frequency swFixedLegFrequency = Annual;
        BusinessDayConvention swFixedLegConvention = Unadjusted;
        DayCounter swFixedLegDayCounter = Thirty360(Thirty360::European);

        for (const auto& q : swapQuotes) {
            auto tenor = q.first;
            auto quote = q.second;
            auto helper = ext::make_shared<SwapRateHelper>(
                Handle<Quote>(quote), tenor,
                calendar, swFixedLegFrequency,
                swFixedLegConvention, swFixedLegDayCounter,
                euribor6M,
                Handle<Quote>(), 0 * Days,
                discountingTermStructure); // the Eonia curve is used for discounting
            euribor6MInstruments.push_back(helper);
        }

        // If needed, it's possible to change the tolerance; the default is 1.0e-12.
        // The tolerance is passed in an explicit bootstrap object. Depending on
        // the bootstrap algorithm, it's possible to pass other parameters.
        double tolerance = 1.0e-15;
        auto euribor6MTermStructure = ext::make_shared<PiecewiseYieldCurve<Discount, Cubic>>(
                settlementDate, euribor6MInstruments,
                termStructureDayCounter,
                PiecewiseYieldCurve<Discount, Cubic>::bootstrap_type(tolerance));

        // This curve will be used for forward-rate forecasting

        RelinkableHandle<YieldTermStructure> forecastingTermStructure;
        forecastingTermStructure.linkTo(euribor6MTermStructure);

        /*********************
        * SWAPS TO BE PRICED *
        **********************/

        // constant nominal 1,000,000 Euro
        Real nominal = 1000000.0;
        // fixed leg
        Frequency fixedLegFrequency = Annual;
        BusinessDayConvention fixedLegConvention = Unadjusted;
        BusinessDayConvention floatingLegConvention = ModifiedFollowing;
        DayCounter fixedLegDayCounter = Thirty360(Thirty360::European);
        Rate fixedRate = 0.007;
        DayCounter floatingLegDayCounter = Actual360();

        // floating leg
        Frequency floatingLegFrequency = Semiannual;
        auto euriborIndex = ext::make_shared<Euribor6M>(forecastingTermStructure);
        Spread spread = 0.0;

        Integer lengthInYears = 5;
        Swap::Type swapType = Swap::Payer;

        Date maturity = settlementDate + lengthInYears*Years;
        Schedule fixedSchedule(settlementDate, maturity,
                               Period(fixedLegFrequency),
                               calendar, fixedLegConvention,
                               fixedLegConvention,
                               DateGeneration::Forward, false);
        Schedule floatSchedule(settlementDate, maturity,
                               Period(floatingLegFrequency),
                               calendar, floatingLegConvention,
                               floatingLegConvention,
                               DateGeneration::Forward, false);
        VanillaSwap spot5YearSwap(swapType, nominal,
            fixedSchedule, fixedRate, fixedLegDayCounter,
            floatSchedule, euriborIndex, spread,
            floatingLegDayCounter);

        Date fwdStart = calendar.advance(settlementDate, 1, Years);
        Date fwdMaturity = fwdStart + lengthInYears*Years;
        Schedule fwdFixedSchedule(fwdStart, fwdMaturity,
                                  Period(fixedLegFrequency),
                                  calendar, fixedLegConvention,
                                  fixedLegConvention,
                                  DateGeneration::Forward, false);
        Schedule fwdFloatSchedule(fwdStart, fwdMaturity,
                                  Period(floatingLegFrequency),
                                  calendar, floatingLegConvention,
                                  floatingLegConvention,
                                  DateGeneration::Forward, false);
        VanillaSwap oneYearForward5YearSwap(swapType, nominal,
            fwdFixedSchedule, fixedRate, fixedLegDayCounter,
            fwdFloatSchedule, euriborIndex, spread,
            floatingLegDayCounter);


        /***************
        * SWAP PRICING *
        ****************/

        // utilities for formatting the report

        std::ostringstream s1;
        s1 << "5-years swap paying " << std::setprecision(2) << io::rate(fixedRate);
        std::string case1 = s1.str();

        std::ostringstream s2;
        s2 << "5-years, 1-year forward swap paying " << std::setprecision(2) << io::rate(fixedRate);
        std::string case2 = s2.str();

        std::vector<std::string> headers(4);
        headers[0] = std::string(std::max(case1.size(), case2.size()) + 1, ' ');
        headers[1] = "net present value";
        headers[2] = "fair spread";
        headers[3] = "fair fixed rate";
        std::string separator = " | ";
        std::string header = headers[0] + separator + headers[1] + separator + headers[2] + separator + headers[3];
        Size width = header.size();
        std::string rule(width, '-'), dblrule(width, '=');

        // calculations

        auto s5yRate = swapQuotes[5 * Years];

        std::cout << dblrule << std::endl;
        std::cout << " With 5-year market swap-rate = "
                  << std::setprecision(2) << io::rate(s5yRate->value())
                  << std::endl;
        std::cout << rule << std::endl;

        std::cout << header << std::endl;
        std::cout << rule << std::endl;

        Real NPV;
        Rate fairRate;
        Spread fairSpread;

        auto swapEngine = ext::make_shared<DiscountingSwapEngine>(discountingTermStructure);

        spot5YearSwap.setPricingEngine(swapEngine);
        oneYearForward5YearSwap.setPricingEngine(swapEngine);

        NPV = spot5YearSwap.NPV();
        fairSpread = spot5YearSwap.fairSpread();
        fairRate = spot5YearSwap.fairRate();

        std::cout << std::setw(headers[0].size())
                  << case1 << separator;
        std::cout << std::setw(headers[1].size())
                  << std::fixed << std::setprecision(2) << NPV << separator;
        std::cout << std::setw(headers[2].size())
                  << io::rate(fairSpread) << separator;
        std::cout << std::setw(headers[3].size())
                  << io::rate(fairRate);
        std::cout << std::endl;

        std::cout << rule << std::endl;

        // let's check that the 5 years swap has been correctly re-priced
        QL_REQUIRE(std::fabs(fairRate-s5yRate->value())<1e-8,
                   "5-years swap mispriced by "
                   << io::rate(std::fabs(fairRate-s5yRate->value())));

        // now let's price the 1Y forward 5Y swap

        NPV = oneYearForward5YearSwap.NPV();
        fairSpread = oneYearForward5YearSwap.fairSpread();
        fairRate = oneYearForward5YearSwap.fairRate();

        std::cout << std::setw(headers[0].size())
                  << case2 << separator;
        std::cout << std::setw(headers[1].size())
                  << std::fixed << std::setprecision(2) << NPV << separator;
        std::cout << std::setw(headers[2].size())
                  << io::rate(fairSpread) << separator;
        std::cout << std::setw(headers[3].size())
                  << io::rate(fairRate);
        std::cout << std::endl;

        // now let's say that the 5-years swap rate goes up to 0.90%.
        // A smarter market element--say, connected to a data source-- would
        // notice the change itself. Since we're using SimpleQuotes,
        // we'll have to change the value manually--which forces us to
        // downcast the handle and use the SimpleQuote
        // interface. In any case, the point here is that a change in the
        // value contained in the Quote triggers a new bootstrapping
        // of the curve and a repricing of the swap.

        auto fiveYearsRate = ext::dynamic_pointer_cast<SimpleQuote>(s5yRate);
        fiveYearsRate->setValue(0.0090);

        std::cout << dblrule << std::endl;
        std::cout << " With 5-year market swap-rate = "
                  << io::rate(s5yRate->value()) << std::endl;
        std::cout << rule << std::endl;

        std::cout << header << std::endl;
        std::cout << rule << std::endl;

        // now get the updated results

        NPV = spot5YearSwap.NPV();
        fairSpread = spot5YearSwap.fairSpread();
        fairRate = spot5YearSwap.fairRate();

        std::cout << std::setw(headers[0].size())
                  << case1 << separator;
        std::cout << std::setw(headers[1].size())
                  << std::fixed << std::setprecision(2) << NPV << separator;
        std::cout << std::setw(headers[2].size())
                  << io::rate(fairSpread) << separator;
        std::cout << std::setw(headers[3].size())
                  << io::rate(fairRate);
        std::cout << std::endl;

        QL_REQUIRE(std::fabs(fairRate-s5yRate->value())<1e-8,
                   "5-years swap mispriced!");

        std::cout << rule << std::endl;

        // the 1Y forward 5Y swap doesn't change;
        // it depends on the 1-year and 6-years rates, which didn't move

        NPV = oneYearForward5YearSwap.NPV();
        fairSpread = oneYearForward5YearSwap.fairSpread();
        fairRate = oneYearForward5YearSwap.fairRate();

        std::cout << std::setw(headers[0].size())
                  << case2 << separator;
        std::cout << std::setw(headers[1].size())
                  << std::fixed << std::setprecision(2) << NPV << separator;
        std::cout << std::setw(headers[2].size())
                  << io::rate(fairSpread) << separator;
        std::cout << std::setw(headers[3].size())
                  << io::rate(fairRate);
        std::cout << std::endl;

        std::cout << dblrule << std::endl;

        return 0;

    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
        return 1;
    } catch (...) {
        std::cerr << "unknown error" << std::endl;
        return 1;
    }
}