Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
capfloortermvolsurfacesparse.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file capfloortermvolsurface.hpp
20 \brief Cap/floor smile volatility surface
21*/
22
23#ifndef quantext_cap_floor_term_vol_surface_sparse_hpp
24#define quantext_cap_floor_term_vol_surface_sparse_hpp
25
27#include <ql/math/interpolations/bicubicsplineinterpolation.hpp>
28#include <ql/math/interpolations/bilinearinterpolation.hpp>
29#include <ql/quotes/simplequote.hpp>
30#include <ql/utilities/dataformatters.hpp>
31#include <ql/patterns/lazyobject.hpp>
32#include <ql/quote.hpp>
34#include <ql/time/daycounters/actual365fixed.hpp>
35#include <vector>
36
37namespace QuantExt {
38
39//! Cap/floor smile volatility surface sparse
40/*! This class provides the volatility for a given cap/floor interpolating
41 a volatility surface whose elements are the market term volatilities
42 of a set of caps/floors.
43*/
44template <class InterpolatorStrike, class InterpolatorExpiry>
46
47public:
48 //! fixed reference date, fixed market data
49 CapFloorTermVolSurfaceSparse(const QuantLib::Date& referenceDate, const QuantLib::Calendar& calendar, const QuantLib::BusinessDayConvention& bdc,
50 const QuantLib::DayCounter& dc, const std::vector<QuantLib::Period>& tenors, const std::vector<QuantLib::Real>& strikes,
51 const std::vector<QuantLib::Volatility>& volatilities, bool lowerStrikeConstExtrap = true,
52 bool upperStrikeConstExtrap = true, bool timeFlatExtrapolation = false);
53
54 //! floating reference date, fixed market data
55 CapFloorTermVolSurfaceSparse(QuantLib::Natural settlementDays, const QuantLib::Calendar& calendar, const QuantLib::BusinessDayConvention& bdc,
56 const QuantLib::DayCounter& dc, const std::vector<QuantLib::Period>& tenors, const std::vector<QuantLib::Real>& strikes,
57 const std::vector<QuantLib::Volatility>& volatilities, bool lowerStrikeConstExtrap = true,
58 bool upperStrikeConstExtrap = true, bool timeFlatExtrapolation = false);
59
60 //! \name TermStructure interface
61 //@{
62 QuantLib::Date maxDate() const override;
63 //@}
64 //! \name VolatilityTermStructure interface
65 //@{
66 QuantLib::Real minStrike() const override;
67 QuantLib::Real maxStrike() const override;
68 //@}
69 //! \name LazyObject interface
70 //@{
71 void performCalculations() const override;
72 //@}
73
74protected:
75 QuantLib::Volatility volatilityImpl(Time t, Rate strike) const override;
76
77private:
79 mutable QuantLib::ext::shared_ptr<OptionInterpolator2d<InterpolatorStrike, InterpolatorExpiry>> optionInterpolator_;
80 std::vector<QuantLib::Period> allTenors_;
81 std::vector<QuantLib::Real> allStrikes_;
82 std::vector<QuantLib::Volatility> allVols_;
85};
86
87// inline definitions
88
89template <class IS, class IE>
90CapFloorTermVolSurfaceSparse<IS, IE>::CapFloorTermVolSurfaceSparse(const QuantLib::Date& referenceDate, const QuantLib::Calendar& calendar,
91 const QuantLib::BusinessDayConvention& bdc, const QuantLib::DayCounter& dc, const std::vector<QuantLib::Period>& tenors,
92 const std::vector<QuantLib::Real>& strikes, const std::vector<QuantLib::Volatility>& volatilities, bool lowerStrikeConstExtrap,
93 bool upperStrikeConstExtrap, bool timeFlatExtrapolation)
94 : CapFloorTermVolSurface(referenceDate, calendar, bdc, dc), allTenors_(tenors), allStrikes_(strikes), allVols_(volatilities),
95 lowerStrikeConstExtrap_(lowerStrikeConstExtrap), upperStrikeConstExtrap_(upperStrikeConstExtrap) {
96
98}
99
100template <class IS, class IE>
101CapFloorTermVolSurfaceSparse<IS, IE>::CapFloorTermVolSurfaceSparse(QuantLib::Natural settlementDays, const QuantLib::Calendar& calendar,
102 const QuantLib::BusinessDayConvention& bdc, const QuantLib::DayCounter& dc, const std::vector<QuantLib::Period>& tenors,
103 const std::vector<QuantLib::Real>& strikes, const std::vector<QuantLib::Volatility>& volatilities, bool lowerStrikeConstExtrap,
104 bool upperStrikeConstExtrap, bool timeFlatExtrapolation)
105 : CapFloorTermVolSurface(settlementDays, calendar, bdc, dc), allTenors_(tenors), allStrikes_(strikes), allVols_(volatilities),
106 lowerStrikeConstExtrap_(lowerStrikeConstExtrap), upperStrikeConstExtrap_(upperStrikeConstExtrap) {
107
109}
110
111template <class IS, class IE>
113
114 for (auto t : allTenors_) {
115 // add to the unique list of option tenors
116 auto fnd = find(optionTenors_.begin(), optionTenors_.end(), t);
117 if (fnd == optionTenors_.end()) {
118 optionTenors_.push_back(t);
119 }
120 }
121 // sort the tenors
122 std::sort(optionTenors_.begin(), optionTenors_.end());
123
124 for (auto s : allStrikes_) {
125 std::vector<Real>::iterator fnd =
126 find_if(strikes_.begin(), strikes_.end(), CloseEnoughComparator(s));
127 if (fnd == strikes_.end()) {
128 strikes_.push_back(s);
129 }
130 }
131 // sort the strikes
132 std::sort(strikes_.begin(), strikes_.end());
133
134 // create the option interpolator
135 performCalculations();
136}
137
138template <class IS, class IE>
140 return optionDateFromTenor(optionTenors().back());
141}
142
143template <class IS, class IE>
144QuantLib::Real CapFloorTermVolSurfaceSparse<IS, IE>::minStrike() const { return strikes().front(); }
145
146template <class IS, class IE>
147QuantLib::Real CapFloorTermVolSurfaceSparse<IS, IE>::maxStrike() const { return strikes().back(); }
148
149template <class IS, class IE>
150QuantLib::Volatility CapFloorTermVolSurfaceSparse<IS, IE>::volatilityImpl(Time t, Rate strike) const {
151 return optionInterpolator_->getValue(t, strike);
152}
153
154template <class IS, class IE>
156 optionInterpolator_ = QuantLib::ext::make_shared<OptionInterpolator2d<IS, IE>>(referenceDate(), calendar(), businessDayConvention(), dayCounter(),
157 allTenors_, allStrikes_, allVols_, lowerStrikeConstExtrap_, upperStrikeConstExtrap_);
158}
159
160} // namespace QuantExt
161
162#endif
Cap/floor smile volatility surface.
Cap/floor term-volatility surface.
const std::vector< QuantLib::Rate > & strikes() const
Cap/floor smile volatility surface sparse.
CapFloorTermVolSurfaceSparse(const QuantLib::Date &referenceDate, const QuantLib::Calendar &calendar, const QuantLib::BusinessDayConvention &bdc, const QuantLib::DayCounter &dc, const std::vector< QuantLib::Period > &tenors, const std::vector< QuantLib::Real > &strikes, const std::vector< QuantLib::Volatility > &volatilities, bool lowerStrikeConstExtrap=true, bool upperStrikeConstExtrap=true, bool timeFlatExtrapolation=false)
fixed reference date, fixed market data
std::vector< QuantLib::Volatility > allVols_
QuantLib::ext::shared_ptr< OptionInterpolator2d< InterpolatorStrike, InterpolatorExpiry > > optionInterpolator_
QuantLib::Volatility volatilityImpl(Time t, Rate strike) const override
vector< Real > strikes