QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
interpolatedswaptionvolatilitycube.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006 Ferdinando Ametrano
5 Copyright (C) 2006 Katiuscia Manzoni
6 Copyright (C) 2015 Peter Caspers
7 Copyright (C) 2023 Ignacio Anguita
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#include <ql/termstructures/volatility/swaption/interpolatedswaptionvolatilitycube.hpp>
24#include <ql/termstructures/volatility/interpolatedsmilesection.hpp>
25#include <ql/math/interpolations/bilinearinterpolation.hpp>
26#include <ql/math/rounding.hpp>
27#include <ql/indexes/swapindex.hpp>
28
29namespace QuantLib {
30
32 const Handle<SwaptionVolatilityStructure>& atmVolStructure,
33 const std::vector<Period>& optionTenors,
34 const std::vector<Period>& swapTenors,
35 const std::vector<Spread>& strikeSpreads,
36 const std::vector<std::vector<Handle<Quote> > >& volSpreads,
37 const ext::shared_ptr<SwapIndex>& swapIndexBase,
38 const ext::shared_ptr<SwapIndex>& shortSwapIndexBase,
39 bool vegaWeightedSmileFit)
40 : SwaptionVolatilityCube(atmVolStructure, optionTenors, swapTenors,
41 strikeSpreads, volSpreads, swapIndexBase,
42 shortSwapIndexBase,
43 vegaWeightedSmileFit),
44 volSpreadsInterpolator_(nStrikes_),
45 volSpreadsMatrix_(nStrikes_, Matrix(optionTenors.size(), swapTenors.size(), 0.0)) {
46 }
47
49
52 for (Size i=0; i<nStrikes_; i++)
53 for (Size j=0; j<nOptionTenors_; j++)
54 for (Size k=0; k<nSwapTenors_; k++) {
55 volSpreadsMatrix_[i][j][k] =
56 volSpreads_[j*nSwapTenors_+k][i]->value();
57 }
59 for (Size i=0; i<nStrikes_; i++) {
61 swapLengths_.begin(), swapLengths_.end(),
62 optionTimes_.begin(), optionTimes_.end(),
64 volSpreadsInterpolator_[i].enableExtrapolation();
65 }
66 }
67
68 ext::shared_ptr<SmileSection>
70 Time swapLength) const {
71
72 calculate();
73 Date optionDate = optionDateFromTime(optionTime);
74 Rounding rounder(0);
75 Period swapTenor(static_cast<Integer>(rounder(swapLength*12.0)), Months);
76 // ensure that option date is valid fixing date
77 optionDate =
78 swapTenor > shortSwapIndexBase_->tenor()
79 ? swapIndexBase_->fixingCalendar().adjust(optionDate, Following)
80 : shortSwapIndexBase_->fixingCalendar().adjust(optionDate,
81 Following);
82 return smileSectionImpl(optionDate, swapTenor);
83 }
84
85 ext::shared_ptr<SmileSection>
87 const Period& swapTenor) const {
88 calculate();
89 Rate atmForward = atmStrike(optionDate, swapTenor);
90 Volatility atmVol = atmVol_->volatility(optionDate,
91 swapTenor,
92 atmForward);
93 Time optionTime = timeFromReference(optionDate);
94 Real exerciseTimeSqrt = std::sqrt(optionTime);
95 std::vector<Real> strikes, stdDevs;
96 strikes.reserve(nStrikes_);
97 stdDevs.reserve(nStrikes_);
98 Time length = swapLength(swapTenor);
99 for (Size i=0; i<nStrikes_; ++i) {
100 strikes.push_back(atmForward + strikeSpreads_[i]);
101 stdDevs.push_back(exerciseTimeSqrt*(
102 atmVol + volSpreadsInterpolator_[i](length, optionTime)));
103 }
104 Real shift = atmVol_->shift(optionTime,length);
105 return ext::shared_ptr<SmileSection>(new
107 strikes,
108 stdDevs,
109 atmForward,
110 Linear(),
113 shift));
114 }
115}
Actual/365 (Fixed) day count convention.
bilinear interpolation between discrete points
Concrete date class.
Definition: date.hpp:125
Shared handle to an observable.
Definition: handle.hpp:41
ext::shared_ptr< SmileSection > smileSectionImpl(const Date &optionDate, const Period &swapTenor) const override
InterpolatedSwaptionVolatilityCube(const Handle< SwaptionVolatilityStructure > &atmVolStructure, const std::vector< Period > &optionTenors, const std::vector< Period > &swapTenors, const std::vector< Spread > &strikeSpreads, const std::vector< std::vector< Handle< Quote > > > &volSpreads, const ext::shared_ptr< SwapIndex > &swapIndexBase, const ext::shared_ptr< SwapIndex > &shortSwapIndexBase, bool vegaWeightedSmileFit)
virtual void calculate() const
Definition: lazyobject.hpp:253
Linear-interpolation factory and traits
Matrix used in linear algebra.
Definition: matrix.hpp:41
basic rounding class
Definition: rounding.hpp:35
swaption-volatility cube
Rate atmStrike(const Date &optionDate, const Period &swapTenor) const
void performCalculations() const override
ext::shared_ptr< SwapIndex > swapIndexBase_
std::vector< Spread > strikeSpreads_
Handle< SwaptionVolatilityStructure > atmVol() const
Handle< SwaptionVolatilityStructure > atmVol_
ext::shared_ptr< SwapIndex > shortSwapIndexBase_
VolatilityType volatilityType() const override
volatility type
std::vector< std::vector< Handle< Quote > > > volSpreads_
Date optionDateFromTime(Time optionTime) const
additional inspectors
Real shift(const Period &optionTenor, const Period &swapTenor, bool extrapolate=false) const
returns the shift for a given option tenor and swap tenor
Time swapLength(const Period &swapTenor) const
implements the conversion between swap tenor and swap (time) length
Time timeFromReference(const Date &date) const
date/time conversion
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35