| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2007 Cristina Duminuco |
| 5 | Copyright (C) 2006 Ferdinando Ametrano |
| 6 | Copyright (C) 2006 François du Vignaud |
| 7 | Copyright (C) 2006 Giorgio Facchinetti |
| 8 | |
| 9 | This file is part of QuantLib, a free-software/open-source library |
| 10 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 11 | |
| 12 | QuantLib is free software: you can redistribute it and/or modify it |
| 13 | under the terms of the QuantLib license. You should have received a |
| 14 | copy of the license along with this program; if not, please email |
| 15 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 16 | <http://quantlib.org/license.shtml>. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 19 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 21 | */ |
| 22 | |
| 23 | #ifndef quantlib_test_swaption_volatility_structures_utilities_hpp |
| 24 | #define quantlib_test_swaption_volatility_structures_utilities_hpp |
| 25 | |
| 26 | #include <ql/time/period.hpp> |
| 27 | #include <ql/math/matrix.hpp> |
| 28 | #include <ql/quotes/simplequote.hpp> |
| 29 | #include <ql/time/calendars/target.hpp> |
| 30 | #include <ql/time/daycounters/actual365fixed.hpp> |
| 31 | #include <ql/termstructures/volatility/swaption/swaptionvolmatrix.hpp> |
| 32 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 33 | #include <iostream> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace QuantLib { |
| 37 | |
| 38 | struct SwaptionTenors { |
| 39 | std::vector<Period> options; |
| 40 | std::vector<Period> swaps; |
| 41 | }; |
| 42 | struct SwaptionMarketConventions { |
| 43 | Calendar calendar; |
| 44 | BusinessDayConvention optionBdc; |
| 45 | DayCounter dayCounter; |
| 46 | void setConventions() { |
| 47 | calendar = TARGET(); |
| 48 | optionBdc = ModifiedFollowing; |
| 49 | dayCounter = Actual365Fixed(); |
| 50 | } |
| 51 | }; |
| 52 | struct AtmVolatility { |
| 53 | SwaptionTenors tenors; |
| 54 | Matrix vols; |
| 55 | std::vector<std::vector<Handle<Quote> > > volsHandle; |
| 56 | void setMarketData() { |
| 57 | tenors.options.resize(new_size: 6); |
| 58 | tenors.options[0] = Period(1, Months); |
| 59 | tenors.options[1] = Period(6, Months); |
| 60 | tenors.options[2] = Period(1, Years); |
| 61 | tenors.options[3] = Period(5, Years); |
| 62 | tenors.options[4] = Period(10, Years); |
| 63 | tenors.options[5] = Period(30, Years); |
| 64 | tenors.swaps.resize(new_size: 4); |
| 65 | tenors.swaps[0] = Period(1, Years); |
| 66 | tenors.swaps[1] = Period(5, Years); |
| 67 | tenors.swaps[2] = Period(10, Years); |
| 68 | tenors.swaps[3] = Period(30, Years); |
| 69 | vols = Matrix(tenors.options.size(), tenors.swaps.size()); |
| 70 | vols[0][0]=0.1300; vols[0][1]=0.1560; vols[0][2]=0.1390; vols[0][3]=0.1220; |
| 71 | vols[1][0]=0.1440; vols[1][1]=0.1580; vols[1][2]=0.1460; vols[1][3]=0.1260; |
| 72 | vols[2][0]=0.1600; vols[2][1]=0.1590; vols[2][2]=0.1470; vols[2][3]=0.1290; |
| 73 | vols[3][0]=0.1640; vols[3][1]=0.1470; vols[3][2]=0.1370; vols[3][3]=0.1220; |
| 74 | vols[4][0]=0.1400; vols[4][1]=0.1300; vols[4][2]=0.1250; vols[4][3]=0.1100; |
| 75 | vols[5][0]=0.1130; vols[5][1]=0.1090; vols[5][2]=0.1070; vols[5][3]=0.0930; |
| 76 | volsHandle.resize(new_size: tenors.options.size()); |
| 77 | for (Size i=0; i<tenors.options.size(); i++){ |
| 78 | volsHandle[i].resize(new_size: tenors.swaps.size()); |
| 79 | for (Size j=0; j<tenors.swaps.size(); j++) |
| 80 | // every handle must be reassigned, as the ones created by |
| 81 | // default are all linked together. |
| 82 | volsHandle[i][j] = Handle<Quote>(ext::shared_ptr<Quote>(new |
| 83 | SimpleQuote(vols[i][j]))); |
| 84 | } |
| 85 | }; |
| 86 | }; |
| 87 | struct VolatilityCube { |
| 88 | SwaptionTenors tenors; |
| 89 | Matrix volSpreads; |
| 90 | std::vector<std::vector<Handle<Quote> > > volSpreadsHandle; |
| 91 | std::vector<Spread> strikeSpreads; |
| 92 | void setMarketData() { |
| 93 | tenors.options.resize(new_size: 3); |
| 94 | tenors.options[0] = Period(1, Years); |
| 95 | tenors.options[1] = Period(10, Years); |
| 96 | tenors.options[2] = Period(30, Years); |
| 97 | tenors.swaps.resize(new_size: 3); |
| 98 | tenors.swaps[0] = Period(2, Years); |
| 99 | tenors.swaps[1] = Period(10, Years); |
| 100 | tenors.swaps[2] = Period(30, Years); |
| 101 | strikeSpreads.resize(new_size: 5); |
| 102 | strikeSpreads[0] = -0.020; |
| 103 | strikeSpreads[1] = -0.005; |
| 104 | strikeSpreads[2] = +0.000; |
| 105 | strikeSpreads[3] = +0.005; |
| 106 | strikeSpreads[4] = +0.020; |
| 107 | volSpreads = Matrix(tenors.options.size()*tenors.swaps.size(), strikeSpreads.size()); |
| 108 | volSpreads[0][0] = 0.0599; volSpreads[0][1] = 0.0049; |
| 109 | volSpreads[0][2] = 0.0000; |
| 110 | volSpreads[0][3] =-0.0001; volSpreads[0][4] = 0.0127; |
| 111 | volSpreads[1][0] = 0.0729; volSpreads[1][1] = 0.0086; |
| 112 | volSpreads[1][2] = 0.0000; |
| 113 | volSpreads[1][3] =-0.0024; volSpreads[1][4] = 0.0098; |
| 114 | volSpreads[2][0] = 0.0738; volSpreads[2][1] = 0.0102; |
| 115 | volSpreads[2][2] = 0.0000; |
| 116 | volSpreads[2][3] =-0.0039; volSpreads[2][4] = 0.0065; |
| 117 | volSpreads[3][0] = 0.0465; volSpreads[3][1] = 0.0063; |
| 118 | volSpreads[3][2] = 0.0000; |
| 119 | volSpreads[3][3] =-0.0032; volSpreads[3][4] =-0.0010; |
| 120 | volSpreads[4][0] = 0.0558; volSpreads[4][1] = 0.0084; |
| 121 | volSpreads[4][2] = 0.0000; |
| 122 | volSpreads[4][3] =-0.0050; volSpreads[4][4] =-0.0057; |
| 123 | volSpreads[5][0] = 0.0576; volSpreads[5][1] = 0.0083; |
| 124 | volSpreads[5][2] = 0.0000; |
| 125 | volSpreads[5][3] =-0.0043; volSpreads[5][4] = -0.0014; |
| 126 | volSpreads[6][0] = 0.0437; volSpreads[6][1] = 0.0059; |
| 127 | volSpreads[6][2] = 0.0000; |
| 128 | volSpreads[6][3] =-0.0030; volSpreads[6][4] =-0.0006; |
| 129 | volSpreads[7][0] = 0.0533; volSpreads[7][1] = 0.0078; |
| 130 | volSpreads[7][2] = 0.0000; |
| 131 | volSpreads[7][3] =-0.0045; volSpreads[7][4] =-0.0046; |
| 132 | volSpreads[8][0] = 0.0545; volSpreads[8][1] = 0.0079; |
| 133 | volSpreads[8][2] = 0.0000; |
| 134 | volSpreads[8][3] =-0.0042; volSpreads[8][4] =-0.0020; |
| 135 | volSpreadsHandle = std::vector<std::vector<Handle<Quote> > >(tenors.options.size()*tenors.swaps.size()); |
| 136 | for (Size i=0; i<tenors.options.size()*tenors.swaps.size(); i++){ |
| 137 | volSpreadsHandle[i] = std::vector<Handle<Quote> >(strikeSpreads.size()); |
| 138 | for (Size j=0; j<strikeSpreads.size(); j++) { |
| 139 | // every handle must be reassigned, as the ones created by |
| 140 | // default are all linked together. |
| 141 | volSpreadsHandle[i][j] = Handle<Quote>(ext::shared_ptr<Quote>(new |
| 142 | SimpleQuote(volSpreads[i][j]))); |
| 143 | } |
| 144 | } |
| 145 | }; |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | /* static void setupCubeUtilities() { |
| 150 | conventions_.calendar = TARGET(); |
| 151 | conventions_.optionBdc = Following; |
| 152 | conventions_.dayCounter = Actual365Fixed(); |
| 153 | atm_.setMarketData(); |
| 154 | cube_.setMarketData(); |
| 155 | atmVolMatrix_ = RelinkableHandle<SwaptionVolatilityStructure>( |
| 156 | ext::shared_ptr<SwaptionVolatilityStructure>(new |
| 157 | SwaptionVolatilityMatrix(conventions_.calendar, |
| 158 | atm_.tenors.options, |
| 159 | atm_.tenors.swaps, |
| 160 | atm_.volsHandle, |
| 161 | conventions_.dayCounter, |
| 162 | conventions_.optionBdc))); |
| 163 | }*/ |
| 164 | |
| 165 | } |
| 166 | |
| 167 | #endif |
| 168 | |