Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
BlackVolatilitySurfaceAbsolute Class Reference

#include <qle/termstructures/blackvolsurfaceabsolute.hpp>

+ Inheritance diagram for BlackVolatilitySurfaceAbsolute:
+ Collaboration diagram for BlackVolatilitySurfaceAbsolute:

Public Types

enum class  SmileInterpolation { Linear , Cubic }
 

Public Member Functions

 BlackVolatilitySurfaceAbsolute (Date referenceDate, const std::vector< Date > &dates, const std::vector< std::vector< Real > > &strikes, const std::vector< std::vector< Real > > &strikeQuotes, const DayCounter &dayCounter, const Calendar &calendar, const Handle< Quote > &spot, const Size spotDays, const Calendar spotCalendar, const Handle< YieldTermStructure > &domesticTS, const Handle< YieldTermStructure > &foreignTS, const DeltaVolQuote::DeltaType dt=DeltaVolQuote::DeltaType::Spot, const DeltaVolQuote::AtmType at=DeltaVolQuote::AtmType::AtmDeltaNeutral, const Period &switchTenor=2 *Years, const DeltaVolQuote::DeltaType ltdt=DeltaVolQuote::DeltaType::Fwd, const DeltaVolQuote::AtmType ltat=DeltaVolQuote::AtmType::AtmDeltaNeutral, const SmileInterpolation smileInterpolation=SmileInterpolation::Cubic, const bool flatExtrapolation=true)
 
Date maxDate () const override
 
Real minStrike () const override
 
Real maxStrike () const override
 
const std::vector< QuantLib::Date > & dates () const
 
const std::vector< std::vector< Real > > & strikes () const
 
const std::vector< std::vector< Real > > & strikeQuotes () const
 
const Handle< Quote > & spot () const
 
const Handle< YieldTermStructure > & domesticTS () const
 
const Handle< YieldTermStructure > & foreignTS () const
 
DeltaVolQuote::DeltaType deltaType () const
 
DeltaVolQuote::AtmType atmType () const
 
const Period & switchTenor () const
 
DeltaVolQuote::DeltaType longTermDeltaType () const
 
DeltaVolQuote::AtmType longTermAtmType () const
 
SmileInterpolation smileInterpolation () const
 

Private Member Functions

Volatility blackVolImpl (Time t, Real strike) const override
 
void update () override
 

Private Attributes

std::vector< Date > dates_
 
std::vector< std::vector< Real > > strikes_
 
std::vector< std::vector< Real > > strikeQuotes_
 
Handle< Quote > spot_
 
Size spotDays_
 
Calendar spotCalendar_
 
Handle< YieldTermStructure > domesticTS_
 
Handle< YieldTermStructure > foreignTS_
 
DeltaVolQuote::DeltaType dt_
 
DeltaVolQuote::AtmType at_
 
Period switchTenor_
 
DeltaVolQuote::DeltaType ltdt_
 
DeltaVolQuote::AtmType ltat_
 
SmileInterpolation smileInterpolation_
 
std::vector< QuantLib::ext::shared_ptr< Interpolation > > interpolation_
 
bool flatExtrapolation_
 
std::vector< Real > expiryTimes_
 
std::vector< Date > settlementDates_
 
std::map< std::pair< Real, Real >, Real > cachedInterpolatedVols_
 

Detailed Description

Definition at line 37 of file blackvolsurfaceabsolute.hpp.

Member Enumeration Documentation

◆ SmileInterpolation

enum class SmileInterpolation
strong

Constructor & Destructor Documentation

◆ BlackVolatilitySurfaceAbsolute()

BlackVolatilitySurfaceAbsolute ( Date  referenceDate,
const std::vector< Date > &  dates,
const std::vector< std::vector< Real > > &  strikes,
const std::vector< std::vector< Real > > &  strikeQuotes,
const DayCounter &  dayCounter,
const Calendar &  calendar,
const Handle< Quote > &  spot,
const Size  spotDays,
const Calendar  spotCalendar,
const Handle< YieldTermStructure > &  domesticTS,
const Handle< YieldTermStructure > &  foreignTS,
const DeltaVolQuote::DeltaType  dt = DeltaVolQuote::DeltaType::Spot,
const DeltaVolQuote::AtmType  at = DeltaVolQuote::AtmType::AtmDeltaNeutral,
const Period &  switchTenor = 2 * Years,
const DeltaVolQuote::DeltaType  ltdt = DeltaVolQuote::DeltaType::Fwd,
const DeltaVolQuote::AtmType  ltat = DeltaVolQuote::AtmType::AtmDeltaNeutral,
const SmileInterpolation  smileInterpolation = SmileInterpolation::Cubic,
const bool  flatExtrapolation = true 
)

Definition at line 31 of file blackvolsurfaceabsolute.cpp.

39 : BlackVolatilityTermStructure(referenceDate, calendar, Following, dayCounter), dates_(dates), strikes_(strikes),
40 strikeQuotes_(strikeQuotes), spot_(spot), spotDays_(spotDays), spotCalendar_(spotCalendar),
43
44 // checks
45
46 QL_REQUIRE(!dates_.empty(), "BlackVolatilitySurfaceAbsolute: no expiry dates given");
47 QL_REQUIRE(!strikes_.empty(), "BlackVolatilitySurfaceAbsolute: no strikes given");
48
49 for (Size i = 0; i < strikes_.size(); ++i) {
50 if (strikes_[i].size() > 1) {
51 for (Size j = 0; j < strikes_[i].size() - 1; ++j) {
52 QL_REQUIRE(strikes_[i][j + 1] > strikes_[i][j] && !close_enough(strikes_[i][j + 1], strikes_[i][j]),
53 "BlackVolatilitySurfaceAbsolute: strikes are not strictly ascending at index "
54 << i << ", " << j
55 << ": " << strikes_[i][j] << ", " << strikes_[i][j + 1]);
56 }
57 }
58 }
59
60 QL_REQUIRE(strikeQuotes_.size() == dates_.size(), "BlackVolatilitySurfaceAbsolute: strikeQuotes ("
61 << strikeQuotes_.size() << ") mismatch with expiry dates ("
62 << dates_.size() << ")");
63 QL_REQUIRE(strikeQuotes_.size() == strikes_.size(), "BlackVolatilitySurfaceAbsolute: strikeQuotes ("
64 << strikeQuotes_.size() << ") mismatch with number of dates in strikes ("
65 << strikes_.size() << ")");
66 for (Size i = 0; i < strikes_.size(); ++i) {
67 QL_REQUIRE(strikeQuotes_[i].size() == strikes_[i].size(), "BlackVolatilitySurfaceAbsolute: strikeQuotes inner vector ("
68 << strikeQuotes_[i].size() << ") mismatch with strikes (" << strikes_[i].size() << ")");
69 }
70
71 // calculate times associated to expiry dates
72
73 expiryTimes_.clear();
74 settlementDates_.clear();
75 for (auto const& d : dates_) {
76 expiryTimes_.push_back(timeFromReference(d));
77 settlementDates_.push_back(spotCalendar_.advance(d, spotDays_ * Days));
78 }
79
80 // generate interpolator
81 interpolation_.resize(strikeQuotes_.size());
82 for (Size i = 0; i < strikeQuotes_.size(); ++i) {
83 if (strikes_[i].size() > 1) {
85 interpolation_[i] = QuantLib::ext::make_shared<LinearInterpolation>(strikes_[i].begin(), strikes_[i].end(),
86 strikeQuotes_[i].begin());
87 interpolation_[i]->enableExtrapolation();
89 interpolation_[i] = QuantLib::ext::make_shared<CubicInterpolation>(strikes_[i].begin(), strikes_[i].end(), strikeQuotes_[i].begin(), CubicInterpolation::Spline, false,
90 CubicInterpolation::SecondDerivative, 0.0, CubicInterpolation::SecondDerivative, 0.0);
91 interpolation_[i]->enableExtrapolation();
92 } else {
93 QL_FAIL("BlackVolatilitySurfaceAbsolute: Invalid interpolation type.");
94 }
95 }
97 interpolation_[i] = QuantLib::ext::make_shared<FlatExtrapolation>(interpolation_[i]);
98 interpolation_[i]->enableExtrapolation();
99 }
100 }
101
102 // register with observables
103
104 registerWith(spot_);
105 registerWith(domesticTS_);
106 registerWith(foreignTS_);
107}
std::vector< QuantLib::ext::shared_ptr< Interpolation > > interpolation_
const std::vector< std::vector< Real > > & strikes() const
std::vector< std::vector< Real > > strikes_
const std::vector< std::vector< Real > > & strikeQuotes() const
const Handle< YieldTermStructure > & domesticTS() const
std::vector< std::vector< Real > > strikeQuotes_
const Handle< YieldTermStructure > & foreignTS() const
const std::vector< QuantLib::Date > & dates() const
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
+ Here is the call graph for this function:

Member Function Documentation

◆ maxDate()

Date maxDate ( ) const
override

Definition at line 51 of file blackvolsurfaceabsolute.hpp.

51{ return Date::maxDate(); }

◆ minStrike()

Real minStrike ( ) const
override

Definition at line 52 of file blackvolsurfaceabsolute.hpp.

52{ return 0; }

◆ maxStrike()

Real maxStrike ( ) const
override

Definition at line 53 of file blackvolsurfaceabsolute.hpp.

53{ return QL_MAX_REAL; }

◆ dates()

const std::vector< QuantLib::Date > & dates ( ) const

Definition at line 55 of file blackvolsurfaceabsolute.hpp.

55{ return dates_; }

◆ strikes()

const std::vector< std::vector< Real > > & strikes ( ) const

Definition at line 56 of file blackvolsurfaceabsolute.hpp.

56{ return strikes_; }

◆ strikeQuotes()

const std::vector< std::vector< Real > > & strikeQuotes ( ) const

Definition at line 57 of file blackvolsurfaceabsolute.hpp.

57{ return strikeQuotes_; }

◆ spot()

const Handle< Quote > & spot ( ) const

Definition at line 58 of file blackvolsurfaceabsolute.hpp.

58{ return spot_; }

◆ domesticTS()

const Handle< YieldTermStructure > & domesticTS ( ) const

Definition at line 59 of file blackvolsurfaceabsolute.hpp.

59{ return domesticTS_; }

◆ foreignTS()

const Handle< YieldTermStructure > & foreignTS ( ) const

Definition at line 60 of file blackvolsurfaceabsolute.hpp.

60{ return foreignTS_; }

◆ deltaType()

DeltaVolQuote::DeltaType deltaType ( ) const

Definition at line 61 of file blackvolsurfaceabsolute.hpp.

61{ return dt_; }

◆ atmType()

DeltaVolQuote::AtmType atmType ( ) const

Definition at line 62 of file blackvolsurfaceabsolute.hpp.

62{ return at_; }

◆ switchTenor()

const Period & switchTenor ( ) const

Definition at line 63 of file blackvolsurfaceabsolute.hpp.

63{ return switchTenor_; }

◆ longTermDeltaType()

DeltaVolQuote::DeltaType longTermDeltaType ( ) const

Definition at line 64 of file blackvolsurfaceabsolute.hpp.

64{ return ltdt_; }

◆ longTermAtmType()

DeltaVolQuote::AtmType longTermAtmType ( ) const

Definition at line 65 of file blackvolsurfaceabsolute.hpp.

65{ return ltat_; }

◆ smileInterpolation()

SmileInterpolation smileInterpolation ( ) const

Definition at line 66 of file blackvolsurfaceabsolute.hpp.

66{ return smileInterpolation_; }

◆ blackVolImpl()

Volatility blackVolImpl ( Time  t,
Real  strike 
) const
overrideprivate

Definition at line 113 of file blackvolsurfaceabsolute.cpp.

113 {
114
115 /* minimum supported time is 1D, i.e. if t is smaller, we return the vol at 1D */
116
117 t = std::max(t, 1.0 / 365.0);
118
119 t = t <= expiryTimes_.back() ? t : expiryTimes_.back();
120
121 /* if we have cached the interpolated smile at t, we use that */
122
123 auto s = cachedInterpolatedVols_.find(std::make_pair(t, strike));
124 if (s != cachedInterpolatedVols_.end()) {
125 return s->second;
126 }
127
128 /* find the indices ip and im such that t_im <= t < t_ip, im will be null if t < first expiry,
129 ip will be null if t >= last expiry */
130
131 Size index_p = std::upper_bound(expiryTimes_.begin(), expiryTimes_.end(), t) - expiryTimes_.begin();
132 Size index_m = index_p == 0 ? Null<Size>() : index_p - 1;
133 if (index_p == expiryTimes_.size())
134 index_p = Null<Size>();
135
136 /* build the smiles on the indices, if we do not have them yet */
137 Volatility vol_p = 0, vol_m = 0;
138 if (index_p != Null<Size>()) {
139 if (strikeQuotes_[index_p].size() == 1) {
140 vol_p = strikeQuotes_[index_p][0];
141 } else {
142 vol_p = (*interpolation_[index_p])(strike);
143 }
144 }
145 if (index_m != Null<Size>()) {
146 if (strikeQuotes_[index_m].size() == 1) {
147 vol_m = strikeQuotes_[index_m][0];
148 } else {
149 vol_m = (*interpolation_[index_m])(strike);
150 }
151 }
152
153 /* find the interpolated vols */
154
155 Real vol;
156
157 if (index_p == Null<Size>()) {
158 vol = vol_m;
159 } else if (index_m == Null<Size>()) {
160 vol = vol_p;
161 } else {
162 // interpolate between two expiries
163 Real a = (t - expiryTimes_[index_m]) / (expiryTimes_[index_p] - expiryTimes_[index_m]);
164 vol = (1.0 - a) * vol_m + a * vol_p;
165 }
166
167 /* store the new smile in the cache */
168
169 cachedInterpolatedVols_[std::make_pair(t, strike)] = vol;
170
171 return vol;
172}
std::map< std::pair< Real, Real >, Real > cachedInterpolatedVols_

◆ update()

void update ( )
overrideprivate

Definition at line 109 of file blackvolsurfaceabsolute.cpp.

109 {
110 BlackVolatilityTermStructure::update();
111}

Member Data Documentation

◆ dates_

std::vector<Date> dates_
private

Definition at line 72 of file blackvolsurfaceabsolute.hpp.

◆ strikes_

std::vector<std::vector<Real> > strikes_
private

Definition at line 73 of file blackvolsurfaceabsolute.hpp.

◆ strikeQuotes_

std::vector<std::vector<Real> > strikeQuotes_
private

Definition at line 74 of file blackvolsurfaceabsolute.hpp.

◆ spot_

Handle<Quote> spot_
private

Definition at line 75 of file blackvolsurfaceabsolute.hpp.

◆ spotDays_

Size spotDays_
private

Definition at line 76 of file blackvolsurfaceabsolute.hpp.

◆ spotCalendar_

Calendar spotCalendar_
private

Definition at line 77 of file blackvolsurfaceabsolute.hpp.

◆ domesticTS_

Handle<YieldTermStructure> domesticTS_
private

Definition at line 78 of file blackvolsurfaceabsolute.hpp.

◆ foreignTS_

Handle<YieldTermStructure> foreignTS_
private

Definition at line 79 of file blackvolsurfaceabsolute.hpp.

◆ dt_

DeltaVolQuote::DeltaType dt_
private

Definition at line 80 of file blackvolsurfaceabsolute.hpp.

◆ at_

DeltaVolQuote::AtmType at_
private

Definition at line 81 of file blackvolsurfaceabsolute.hpp.

◆ switchTenor_

Period switchTenor_
private

Definition at line 82 of file blackvolsurfaceabsolute.hpp.

◆ ltdt_

DeltaVolQuote::DeltaType ltdt_
private

Definition at line 83 of file blackvolsurfaceabsolute.hpp.

◆ ltat_

DeltaVolQuote::AtmType ltat_
private

Definition at line 84 of file blackvolsurfaceabsolute.hpp.

◆ smileInterpolation_

SmileInterpolation smileInterpolation_
private

Definition at line 85 of file blackvolsurfaceabsolute.hpp.

◆ interpolation_

std::vector<QuantLib::ext::shared_ptr<Interpolation> > interpolation_
private

Definition at line 86 of file blackvolsurfaceabsolute.hpp.

◆ flatExtrapolation_

bool flatExtrapolation_
private

Definition at line 87 of file blackvolsurfaceabsolute.hpp.

◆ expiryTimes_

std::vector<Real> expiryTimes_
mutableprivate

Definition at line 89 of file blackvolsurfaceabsolute.hpp.

◆ settlementDates_

std::vector<Date> settlementDates_
mutableprivate

Definition at line 90 of file blackvolsurfaceabsolute.hpp.

◆ cachedInterpolatedVols_

std::map<std::pair<Real, Real>, Real> cachedInterpolatedVols_
mutableprivate

Definition at line 91 of file blackvolsurfaceabsolute.hpp.