1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | |
3 | /* |
4 | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
5 | Copyright (C) 2006 François du Vignaud |
6 | Copyright (C) 2006, 2008 Ferdinando Ametrano |
7 | Copyright (C) 2015 Peter Caspers |
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 | /*! \file swaptionvolmatrix.hpp |
24 | \brief Swaption at-the-money volatility matrix |
25 | */ |
26 | |
27 | #ifndef quantlib_swaption_volatility_matrix_hpp |
28 | #define quantlib_swaption_volatility_matrix_hpp |
29 | |
30 | #include <ql/termstructures/volatility/swaption/swaptionvoldiscrete.hpp> |
31 | #include <ql/math/interpolations/interpolation2d.hpp> |
32 | #include <ql/math/matrix.hpp> |
33 | #include <vector> |
34 | |
35 | namespace QuantLib { |
36 | |
37 | class Quote; |
38 | |
39 | //! At-the-money swaption-volatility matrix |
40 | /*! This class provides the at-the-money volatility for a given |
41 | swaption by interpolating a volatility matrix whose elements |
42 | are the market volatilities of a set of swaption with given |
43 | option date and swapLength. |
44 | |
45 | The volatility matrix <tt>M</tt> must be defined so that: |
46 | - the number of rows equals the number of option dates; |
47 | - the number of columns equals the number of swap tenors; |
48 | - <tt>M[i][j]</tt> contains the volatility corresponding |
49 | to the <tt>i</tt>-th option and <tt>j</tt>-th tenor. |
50 | */ |
51 | class SwaptionVolatilityMatrix : public SwaptionVolatilityDiscrete { |
52 | public: |
53 | //! floating reference date, floating market data |
54 | SwaptionVolatilityMatrix( |
55 | const Calendar& calendar, |
56 | BusinessDayConvention bdc, |
57 | const std::vector<Period>& optionTenors, |
58 | const std::vector<Period>& swapTenors, |
59 | const std::vector<std::vector<Handle<Quote> > >& vols, |
60 | const DayCounter& dayCounter, |
61 | bool = false, |
62 | VolatilityType type = ShiftedLognormal, |
63 | const std::vector<std::vector<Real> >& shifts = std::vector<std::vector<Real> >()); |
64 | //! fixed reference date, floating market data |
65 | SwaptionVolatilityMatrix( |
66 | const Date& referenceDate, |
67 | const Calendar& calendar, |
68 | BusinessDayConvention bdc, |
69 | const std::vector<Period>& optionTenors, |
70 | const std::vector<Period>& swapTenors, |
71 | const std::vector<std::vector<Handle<Quote> > >& vols, |
72 | const DayCounter& dayCounter, |
73 | bool = false, |
74 | VolatilityType type = ShiftedLognormal, |
75 | const std::vector<std::vector<Real> >& shifts = std::vector<std::vector<Real> >()); |
76 | //! floating reference date, fixed market data |
77 | SwaptionVolatilityMatrix(const Calendar& calendar, |
78 | BusinessDayConvention bdc, |
79 | const std::vector<Period>& optionTenors, |
80 | const std::vector<Period>& swapTenors, |
81 | const Matrix& volatilities, |
82 | const DayCounter& dayCounter, |
83 | bool = false, |
84 | VolatilityType type = ShiftedLognormal, |
85 | const Matrix& shifts = Matrix()); |
86 | //! fixed reference date, fixed market data |
87 | SwaptionVolatilityMatrix(const Date& referenceDate, |
88 | const Calendar& calendar, |
89 | BusinessDayConvention bdc, |
90 | const std::vector<Period>& optionTenors, |
91 | const std::vector<Period>& swapTenors, |
92 | const Matrix& volatilities, |
93 | const DayCounter& dayCounter, |
94 | bool = false, |
95 | VolatilityType type = ShiftedLognormal, |
96 | const Matrix& shifts = Matrix()); |
97 | //! fixed reference date and fixed market data, option dates |
98 | SwaptionVolatilityMatrix(const Date& referenceDate, |
99 | const Calendar& calendar, |
100 | BusinessDayConvention bdc, |
101 | const std::vector<Date>& optionDates, |
102 | const std::vector<Period>& swapTenors, |
103 | const Matrix& volatilities, |
104 | const DayCounter& dayCounter, |
105 | bool = false, |
106 | VolatilityType type = ShiftedLognormal, |
107 | const Matrix& shifts = Matrix()); |
108 | |
109 | // make class non-copyable and non-movable |
110 | SwaptionVolatilityMatrix(SwaptionVolatilityMatrix&&) = delete; |
111 | SwaptionVolatilityMatrix(const SwaptionVolatilityMatrix&) = delete; |
112 | SwaptionVolatilityMatrix& operator=(SwaptionVolatilityMatrix&&) = delete; |
113 | SwaptionVolatilityMatrix& operator=(const SwaptionVolatilityMatrix&) = delete; |
114 | |
115 | ~SwaptionVolatilityMatrix() override = default; |
116 | |
117 | //! \name LazyObject interface |
118 | //@{ |
119 | void performCalculations() const override; |
120 | //@} |
121 | //! \name TermStructure interface |
122 | //@{ |
123 | Date maxDate() const override; |
124 | //@} |
125 | //! \name VolatilityTermStructure interface |
126 | //@{ |
127 | Rate minStrike() const override; |
128 | Rate maxStrike() const override; |
129 | //@} |
130 | //! \name SwaptionVolatilityStructure interface |
131 | //@{ |
132 | const Period& maxSwapTenor() const override; |
133 | //@} |
134 | //! \name Other inspectors |
135 | //@{ |
136 | //! returns the lower indexes of surrounding volatility matrix corners |
137 | std::pair<Size,Size> locate(const Date& optionDate, |
138 | const Period& swapTenor) const { |
139 | return locate(optionTime: timeFromReference(d: optionDate), |
140 | swapLength: swapLength(swapTenor)); |
141 | } |
142 | //! returns the lower indexes of surrounding volatility matrix corners |
143 | std::pair<Size,Size> locate(Time optionTime, |
144 | Time swapLength) const { |
145 | return std::make_pair(x: interpolation_.locateY(y: optionTime), |
146 | y: interpolation_.locateX(x: swapLength)); |
147 | } |
148 | //@} |
149 | VolatilityType volatilityType() const override; |
150 | |
151 | protected: |
152 | // defining the following method would break CMS test suite |
153 | // to be further investigated |
154 | //ext::shared_ptr<SmileSection> smileSectionImpl(const Date&, |
155 | // const Period&) const; |
156 | ext::shared_ptr<SmileSection> smileSectionImpl(Time, Time) const override; |
157 | Volatility volatilityImpl(Time optionTime, Time swapLength, Rate strike) const override; |
158 | Real shiftImpl(Time optionTime, Time swapLength) const override; |
159 | |
160 | private: |
161 | void checkInputs(Size volRows, |
162 | Size volsColumns, |
163 | Size shiftRows, |
164 | Size shiftsColumns) const; |
165 | void registerWithMarketData(); |
166 | std::vector<std::vector<Handle<Quote> > > volHandles_; |
167 | std::vector<std::vector<Real> > shiftValues_; |
168 | mutable Matrix volatilities_, shifts_; |
169 | Interpolation2D interpolation_, interpolationShifts_; |
170 | VolatilityType volatilityType_; |
171 | }; |
172 | |
173 | // inline definitions |
174 | |
175 | inline Date SwaptionVolatilityMatrix::maxDate() const { |
176 | return optionDates_.back(); |
177 | } |
178 | |
179 | inline Rate SwaptionVolatilityMatrix::minStrike() const { |
180 | return -QL_MAX_REAL; |
181 | } |
182 | |
183 | inline Rate SwaptionVolatilityMatrix::maxStrike() const { |
184 | return QL_MAX_REAL; |
185 | } |
186 | |
187 | inline const Period& SwaptionVolatilityMatrix::maxSwapTenor() const { |
188 | return swapTenors_.back(); |
189 | } |
190 | |
191 | inline Volatility SwaptionVolatilityMatrix::volatilityImpl(Time optionTime, |
192 | Time swapLength, |
193 | Rate) const { |
194 | calculate(); |
195 | return interpolation_(swapLength, optionTime, true); |
196 | } |
197 | |
198 | inline VolatilityType SwaptionVolatilityMatrix::volatilityType() const { |
199 | return volatilityType_; |
200 | } |
201 | |
202 | inline Real SwaptionVolatilityMatrix::shiftImpl(Time optionTime, |
203 | Time swapLength) const { |
204 | calculate(); |
205 | Real tmp = interpolationShifts_(swapLength, optionTime, true); |
206 | return tmp; |
207 | } |
208 | } // namespace QuantLib |
209 | |
210 | #endif |
211 | |