Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
creditvolcurve.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 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 creditvolcurve.hpp
20 \brief credit vol curve
21*/
22
23#pragma once
24
26
27#include <ql/handle.hpp>
28#include <ql/math/interpolation.hpp>
29#include <ql/patterns/lazyobject.hpp>
30#include <ql/quote.hpp>
31#include <ql/termstructures/volatility/equityfx/blackvoltermstructure.hpp>
32#include <map>
33#include <tuple>
34
35namespace QuantExt {
36
37class CreditVolCurve : public QuantLib::VolatilityTermStructure, public QuantLib::LazyObject {
38public:
39 enum class Type { Price, Spread };
40 CreditVolCurve(QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter& dc,
41 const std::vector<QuantLib::Period>& terms,
42 const std::vector<QuantLib::Handle<CreditCurve>>& termCurves, const Type& type);
43 CreditVolCurve(const QuantLib::Natural settlementDays, const QuantLib::Calendar& cal,
44 QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter& dc,
45 const std::vector<QuantLib::Period>& terms,
46 const std::vector<QuantLib::Handle<CreditCurve>>& termCurves, const Type& type);
47 CreditVolCurve(const QuantLib::Date& referenceDate, const QuantLib::Calendar& cal,
48 QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter& dc,
49 const std::vector<QuantLib::Period>& terms,
50 const std::vector<QuantLib::Handle<CreditCurve>>& termCurves, const Type& type);
51
52 QuantLib::Real volatility(const QuantLib::Date& exerciseDate, const QuantLib::Period& underlyingTerm,
53 const QuantLib::Real strike, const Type& targetType) const;
54 virtual QuantLib::Real volatility(const QuantLib::Date& exerciseDate, const QuantLib::Real underlyingLength,
55 const QuantLib::Real strike, const Type& targetType) const = 0;
56
57 /* Interpolates between the volatility for d and d+1 where d is a date such that
58 timeFromRef(d) <= exerciseTime <= timeFromRef(d+1). */
59 QuantLib::Real volatility(const QuantLib::Real exerciseTime, const QuantLib::Real underlyingLength,
60 const QuantLib::Real strike, const Type& targetType) const;
61
62 virtual const std::vector<QuantLib::Period>& terms() const;
63 virtual const std::vector<QuantLib::Handle<CreditCurve>>& termCurves() const;
64 const Type& type() const;
65
66 QuantLib::Real atmStrike(const QuantLib::Date& expiry, const QuantLib::Period& term) const;
67 QuantLib::Real atmStrike(const QuantLib::Date& expiry, const QuantLib::Real underlyingLength) const;
68
69 QuantLib::Real minStrike() const override;
70 QuantLib::Real maxStrike() const override;
71 QuantLib::Date maxDate() const override;
72
73protected:
74 void init();
75 void update() override { LazyObject::update(); }
76 void performCalculations() const override;
77 QuantLib::Real moneyness(const QuantLib::Real strike, const QuantLib::Real atmStrike) const;
78 QuantLib::Real strike(const QuantLib::Real moneyness, const QuantLib::Real atmStrike) const;
79
80 std::vector<QuantLib::Period> terms_;
81 std::vector<QuantLib::Handle<CreditCurve>> termCurves_;
83
84 mutable std::map<std::pair<QuantLib::Date, double>, double> atmStrikeCache_;
85};
86
88public:
89 InterpolatingCreditVolCurve(const QuantLib::Natural settlementDays, const QuantLib::Calendar& cal,
90 QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter& dc,
91 const std::vector<QuantLib::Period>& terms,
92 const std::vector<QuantLib::Handle<CreditCurve>>& termCurves,
93 const std::map<std::tuple<QuantLib::Date, QuantLib::Period, QuantLib::Real>,
94 QuantLib::Handle<QuantLib::Quote>>& quotes,
95 const Type& type);
96 InterpolatingCreditVolCurve(const QuantLib::Date& referenceDate, const QuantLib::Calendar& cal,
97 QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter& dc,
98 const std::vector<QuantLib::Period>& terms,
99 const std::vector<QuantLib::Handle<CreditCurve>>& termCurves,
100 const std::map<std::tuple<QuantLib::Date, QuantLib::Period, QuantLib::Real>,
101 QuantLib::Handle<QuantLib::Quote>>& quotes,
102 const Type& type);
103
104 QuantLib::Real volatility(const QuantLib::Date& exerciseDate, const QuantLib::Real underlyingLength,
105 const QuantLib::Real strike, const Type& targetType) const override;
106
107private:
108 using Smile = std::pair<QuantLib::Real, QuantLib::ext::shared_ptr<QuantLib::Interpolation>>;
109 using Key = std::pair<QuantLib::Date, QuantLib::Period>;
110
111 void init();
112 void performCalculations() const override;
113 void createSmile(const QuantLib::Date& expiry, const QuantLib::Period& term, const QuantLib::Date& expiry_m,
114 const QuantLib::Date& expiry_p) const;
115
116 std::map<std::tuple<QuantLib::Date, QuantLib::Period, QuantLib::Real>, QuantLib::Handle<QuantLib::Quote>> quotes_;
117
118 mutable std::vector<QuantLib::Period> smileTerms_;
119 mutable std::vector<QuantLib::Date> smileExpiries_;
120 mutable std::vector<QuantLib::Real> smileTermLengths_;
121 mutable std::vector<QuantLib::Real> smileExpiryTimes_;
122
123 mutable std::map<Key, std::vector<QuantLib::Real>> strikes_;
124 mutable std::map<Key, std::vector<QuantLib::Real>> vols_;
125 mutable std::map<Key, Smile> smiles_;
126};
127
129public:
130 // if no terms / termCurves are given, the source terms / termCurves are used for this surface
131 ProxyCreditVolCurve(const QuantLib::Handle<CreditVolCurve>& source, const std::vector<QuantLib::Period>& terms = {},
132 const std::vector<QuantLib::Handle<CreditCurve>>& termCurves = {});
133 QuantLib::Real volatility(const QuantLib::Date& exerciseDate, const QuantLib::Real underlyingLength,
134 const QuantLib::Real strike, const Type& targetType) const override;
135
136 const QuantLib::Date& referenceDate() const override;
137
138private:
139 QuantLib::Handle<CreditVolCurve> source_;
140};
141
143public:
144 /* for stickyMoneyness = true and a base curve that is strike dependent, both the base curve and this curve must have
145 terms and termCurves defined where the former do not react to spread movements while the latter represent the moving ATM level */
146 SpreadedCreditVolCurve(const QuantLib::Handle<CreditVolCurve> baseCurve, const std::vector<QuantLib::Date> expiries,
147 const std::vector<QuantLib::Handle<QuantLib::Quote>> spreads, const bool stickyMoneyness,
148 const std::vector<QuantLib::Period>& terms = {},
149 const std::vector<QuantLib::Handle<CreditCurve>>& termCurves = {});
150
151 QuantLib::Real volatility(const QuantLib::Date& exerciseDate, const QuantLib::Real underlyingLength,
152 const QuantLib::Real strike, const Type& targetType) const override;
153
154 const QuantLib::Date& referenceDate() const override;
155
156private:
157 void performCalculations() const override;
158
159 QuantLib::Handle<CreditVolCurve> baseCurve_;
160 std::vector<QuantLib::Date> expiries_;
161 std::vector<QuantLib::Handle<QuantLib::Quote>> spreads_;
163
164 mutable std::vector<QuantLib::Real> times_;
165 mutable std::vector<QuantLib::Real> spreadValues_;
166 mutable QuantLib::ext::shared_ptr<QuantLib::Interpolation> interpolatedSpreads_;
167};
168
170public:
171 explicit CreditVolCurveWrapper(const QuantLib::Handle<QuantLib::BlackVolTermStructure>& vol);
172
173 QuantLib::Real volatility(const QuantLib::Date& exerciseDate, const QuantLib::Real underlyingLength,
174 const QuantLib::Real strike, const Type& targetType) const override;
175
176 const QuantLib::Date& referenceDate() const override;
177
178private:
179 QuantLib::Handle<QuantLib::BlackVolTermStructure> vol_;
180};
181
182class BlackVolFromCreditVolWrapper : public QuantLib::BlackVolatilityTermStructure {
183public:
184 explicit BlackVolFromCreditVolWrapper(const QuantLib::Handle<QuantExt::CreditVolCurve>& vol,
185 const QuantLib::Real underlyingLength);
186
187 const QuantLib::Date& referenceDate() const override;
188 QuantLib::Real minStrike() const override;
189 QuantLib::Real maxStrike() const override;
190 QuantLib::Date maxDate() const override;
191
192private:
193 QuantLib::Real blackVolImpl(QuantLib::Real t, QuantLib::Real strike) const override;
194 QuantLib::Handle<QuantExt::CreditVolCurve> vol_;
195 QuantLib::Real underlyingLength_;
196};
197
198} // namespace QuantExt
QuantLib::Handle< QuantExt::CreditVolCurve > vol_
QuantLib::Real blackVolImpl(QuantLib::Real t, QuantLib::Real strike) const override
const QuantLib::Date & referenceDate() const override
QuantLib::Real minStrike() const override
QuantLib::Date maxDate() const override
QuantLib::Real maxStrike() const override
void performCalculations() const override
QuantLib::Real moneyness(const QuantLib::Real strike, const QuantLib::Real atmStrike) const
QuantLib::Real atmStrike(const QuantLib::Date &expiry, const QuantLib::Real underlyingLength) const
std::map< std::pair< QuantLib::Date, double >, double > atmStrikeCache_
virtual QuantLib::Real volatility(const QuantLib::Date &exerciseDate, const QuantLib::Real underlyingLength, const QuantLib::Real strike, const Type &targetType) const =0
QuantLib::Real atmStrike(const QuantLib::Date &expiry, const QuantLib::Period &term) const
QuantLib::Real minStrike() const override
std::vector< QuantLib::Handle< CreditCurve > > termCurves_
CreditVolCurve(const QuantLib::Natural settlementDays, const QuantLib::Calendar &cal, QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter &dc, const std::vector< QuantLib::Period > &terms, const std::vector< QuantLib::Handle< CreditCurve > > &termCurves, const Type &type)
CreditVolCurve(QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter &dc, const std::vector< QuantLib::Period > &terms, const std::vector< QuantLib::Handle< CreditCurve > > &termCurves, const Type &type)
QuantLib::Real volatility(const QuantLib::Date &exerciseDate, const QuantLib::Period &underlyingTerm, const QuantLib::Real strike, const Type &targetType) const
QuantLib::Real strike(const QuantLib::Real moneyness, const QuantLib::Real atmStrike) const
std::vector< QuantLib::Period > terms_
virtual const std::vector< QuantLib::Handle< CreditCurve > > & termCurves() const
QuantLib::Date maxDate() const override
CreditVolCurve(const QuantLib::Date &referenceDate, const QuantLib::Calendar &cal, QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter &dc, const std::vector< QuantLib::Period > &terms, const std::vector< QuantLib::Handle< CreditCurve > > &termCurves, const Type &type)
const Type & type() const
QuantLib::Real volatility(const QuantLib::Real exerciseTime, const QuantLib::Real underlyingLength, const QuantLib::Real strike, const Type &targetType) const
QuantLib::Real maxStrike() const override
virtual const std::vector< QuantLib::Period > & terms() const
const QuantLib::Date & referenceDate() const override
QuantLib::Real volatility(const QuantLib::Date &exerciseDate, const QuantLib::Real underlyingLength, const QuantLib::Real strike, const Type &targetType) const override
QuantLib::Handle< QuantLib::BlackVolTermStructure > vol_
std::vector< QuantLib::Date > smileExpiries_
std::map< Key, std::vector< QuantLib::Real > > strikes_
std::map< Key, std::vector< QuantLib::Real > > vols_
std::pair< QuantLib::Date, QuantLib::Period > Key
std::pair< QuantLib::Real, QuantLib::ext::shared_ptr< QuantLib::Interpolation > > Smile
QuantLib::Real volatility(const QuantLib::Date &exerciseDate, const QuantLib::Real underlyingLength, const QuantLib::Real strike, const Type &targetType) const override
std::vector< QuantLib::Real > smileTermLengths_
InterpolatingCreditVolCurve(const QuantLib::Natural settlementDays, const QuantLib::Calendar &cal, QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter &dc, const std::vector< QuantLib::Period > &terms, const std::vector< QuantLib::Handle< CreditCurve > > &termCurves, const std::map< std::tuple< QuantLib::Date, QuantLib::Period, QuantLib::Real >, QuantLib::Handle< QuantLib::Quote > > &quotes, const Type &type)
InterpolatingCreditVolCurve(const QuantLib::Date &referenceDate, const QuantLib::Calendar &cal, QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter &dc, const std::vector< QuantLib::Period > &terms, const std::vector< QuantLib::Handle< CreditCurve > > &termCurves, const std::map< std::tuple< QuantLib::Date, QuantLib::Period, QuantLib::Real >, QuantLib::Handle< QuantLib::Quote > > &quotes, const Type &type)
std::vector< QuantLib::Real > smileExpiryTimes_
std::map< std::tuple< QuantLib::Date, QuantLib::Period, QuantLib::Real >, QuantLib::Handle< QuantLib::Quote > > quotes_
std::vector< QuantLib::Period > smileTerms_
void createSmile(const QuantLib::Date &expiry, const QuantLib::Period &term, const QuantLib::Date &expiry_m, const QuantLib::Date &expiry_p) const
QuantLib::Real volatility(const QuantLib::Date &exerciseDate, const QuantLib::Real underlyingLength, const QuantLib::Real strike, const Type &targetType) const override
const QuantLib::Date & referenceDate() const override
QuantLib::Handle< CreditVolCurve > source_
void performCalculations() const override
std::vector< QuantLib::Handle< QuantLib::Quote > > spreads_
QuantLib::ext::shared_ptr< QuantLib::Interpolation > interpolatedSpreads_
const QuantLib::Date & referenceDate() const override
QuantLib::Real volatility(const QuantLib::Date &exerciseDate, const QuantLib::Real underlyingLength, const QuantLib::Real strike, const Type &targetType) const override
std::vector< QuantLib::Real > spreadValues_
std::vector< QuantLib::Real > times_
std::vector< QuantLib::Date > expiries_
QuantLib::Handle< CreditVolCurve > baseCurve_
wrapper for default curves, adding (index) reference data