24#ifndef quantext_cap_floor_term_volatility_curve_hpp
25#define quantext_cap_floor_term_volatility_curve_hpp
27#include <ql/math/interpolation.hpp>
28#include <ql/patterns/lazyobject.hpp>
29#include <ql/quote.hpp>
30#include <ql/termstructures/interpolatedcurve.hpp>
31#include <ql/termstructures/volatility/capfloor/capfloortermvolatilitystructure.hpp>
32#include <ql/time/daycounters/actual365fixed.hpp>
33#include <ql/utilities/dataformatters.hpp>
45 CapFloorTermVolCurve(QuantLib::BusinessDayConvention bdc,
const QuantLib::DayCounter& dc = QuantLib::DayCounter())
46 :
QuantLib::CapFloorTermVolatilityStructure(bdc, dc) {}
49 QuantLib::BusinessDayConvention bdc,
const QuantLib::DayCounter& dc = QuantLib::DayCounter())
50 :
QuantLib::CapFloorTermVolatilityStructure(referenceDate, cal, bdc, dc) {}
54 QuantLib::BusinessDayConvention bdc,
const QuantLib::DayCounter& dc = QuantLib::DayCounter())
55 :
QuantLib::CapFloorTermVolatilityStructure(settlementDays, cal, bdc, dc) {}
69template <
class Interpolator>
72 protected QuantLib::InterpolatedCurve<Interpolator> {
91 QuantLib::BusinessDayConvention bdc,
93 const std::vector<QuantLib::Handle<QuantLib::Quote> >& volatilities,
94 const QuantLib::DayCounter& dayCounter = QuantLib::Actual365Fixed(),
95 bool flatFirstPeriod =
true,
const Interpolator& interpolator = Interpolator());
113 QuantLib::BusinessDayConvention bdc,
115 const std::vector<QuantLib::Handle<QuantLib::Quote> >& volatilities,
116 const QuantLib::DayCounter& dayCounter = QuantLib::Actual365Fixed(),
117 bool flatFirstPeriod =
true,
const Interpolator& interpolator = Interpolator());
121 QuantLib::Date
maxDate()
const override;
126 QuantLib::Rate
minStrike()
const override;
127 QuantLib::Rate
maxStrike()
const override;
138 std::vector<QuantLib::Period>
optionTenors()
const override;
143 const std::vector<QuantLib::Date>&
optionDates()
const;
144 const std::vector<QuantLib::Time>&
optionTimes()
const;
150 QuantLib::Volatility
volatilityImpl(QuantLib::Time length, QuantLib::Rate)
const override;
183template <
class Interpolator>
185 QuantLib::Natural settlementDays,
const QuantLib::Calendar& calendar, QuantLib::BusinessDayConvention bdc,
186 const std::vector<QuantLib::Period>& optionTenors,
187 const std::vector<QuantLib::Handle<QuantLib::Quote> >& volatilities,
const QuantLib::DayCounter& dayCounter,
188 bool flatFirstPeriod,
const Interpolator& interpolator)
190 optionTenors.size() + 1, interpolator),
191 nOptionTenors_(optionTenors.size()), optionTenors_(optionTenors), optionDates_(nOptionTenors_),
192 optionTimes_(nOptionTenors_), volatilities_(volatilities), flatFirstPeriod_(flatFirstPeriod) {
198template <
class Interpolator>
200 const QuantLib::Date& settlementDate,
const QuantLib::Calendar& calendar, QuantLib::BusinessDayConvention bdc,
201 const std::vector<QuantLib::Period>& optionTenors,
202 const std::vector<QuantLib::Handle<QuantLib::Quote> >& volatilities,
const QuantLib::DayCounter& dayCounter,
203 bool flatFirstPeriod,
const Interpolator& interpolator)
205 optionTenors.size() + 1, interpolator),
206 nOptionTenors_(optionTenors.size()), optionTenors_(optionTenors), optionDates_(nOptionTenors_),
207 optionTimes_(nOptionTenors_), volatilities_(volatilities), flatFirstPeriod_(flatFirstPeriod) {
215 return optionDates_.back();
227 CapFloorTermVolatilityStructure::update();
228 LazyObject::update();
236 this->times_[0] = 0.0;
237 this->data_[0] = 0.0;
238 for (Size i = 0; i < nOptionTenors_; ++i) {
239 optionDates_[i] = optionDateFromTenor(optionTenors_[i]);
240 optionTimes_[i] = timeFromReference(optionDates_[i]);
241 this->times_[i + 1] = optionTimes_[i];
242 this->data_[i + 1] = volatilities_[i]->value();
246 this->interpolation_ =
247 this->interpolator_.interpolate(this->times_.begin(), this->times_.end(), this->data_.begin());
250template <
class Interpolator>
253 return optionTenors_;
256template <
class Interpolator>
262template <
class Interpolator>
268template <
class Interpolator>
270 QuantLib::Rate)
const {
274 if (flatFirstPeriod_ && length < this->times_[1])
275 return this->data_[1];
277 return this->interpolation_(length,
true);
281 QL_REQUIRE(!optionTenors_.empty(),
"The option tenor vector cannot be empty");
282 QL_REQUIRE(nOptionTenors_ == volatilities_.size(),
"Mismatch between number of option tenors ("
283 << nOptionTenors_ <<
") and number of volatilities ("
284 << volatilities_.size() <<
")");
285 QL_REQUIRE(optionTenors_[0] > 0 * Days,
"First option tenor needs to be positive but is: " << optionTenors_[0]);
286 for (QuantLib::Size i = 1; i < nOptionTenors_; ++i) {
287 QL_REQUIRE(optionTenors_[i] > optionTenors_[i - 1],
288 "Non increasing option tenor: " << QuantLib::io::ordinal(i) <<
" is " << optionTenors_[i - 1]
289 <<
" and " << QuantLib::io::ordinal(i + 1) <<
" is "
290 << optionTenors_[i]);
295 for (QuantLib::Size i = 0; i < volatilities_.size(); ++i)
296 registerWith(volatilities_[i]);
CapFloorTermVolCurve(const QuantLib::Date &referenceDate, const QuantLib::Calendar &cal, QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter &dc=QuantLib::DayCounter())
CapFloorTermVolCurve(QuantLib::Natural settlementDays, const QuantLib::Calendar &cal, QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter &dc=QuantLib::DayCounter())
calculate the reference date based on the global evaluation date
virtual std::vector< QuantLib::Period > optionTenors() const =0
Return the tenors used in the CapFloorTermVolCurve.
CapFloorTermVolCurve(QuantLib::BusinessDayConvention bdc, const QuantLib::DayCounter &dc=QuantLib::DayCounter())
Interpolated cap floor term volatility curve.
void registerWithMarketData()
Register this curve with the volatility quotes.
void performCalculations() const override
const std::vector< QuantLib::Date > & optionDates() const
InterpolatedCapFloorTermVolCurve(QuantLib::Natural settlementDays, const QuantLib::Calendar &calendar, QuantLib::BusinessDayConvention bdc, const std::vector< QuantLib::Period > &optionTenors, const std::vector< QuantLib::Handle< QuantLib::Quote > > &volatilities, const QuantLib::DayCounter &dayCounter=QuantLib::Actual365Fixed(), bool flatFirstPeriod=true, const Interpolator &interpolator=Interpolator())
std::vector< QuantLib::Period > optionTenors() const override
Return the tenors used in the CapFloorTermVolCurve.
std::vector< QuantLib::Time > optionTimes_
void checkInputs() const
Check the constructor arguments for consistency.
QuantLib::Volatility volatilityImpl(QuantLib::Time length, QuantLib::Rate) const override
const std::vector< QuantLib::Time > & optionTimes() const
std::vector< QuantLib::Handle< QuantLib::Quote > > volatilities_
Cap floor term volatility quotes.
std::vector< QuantLib::Date > optionDates_
QuantLib::Date maxDate() const override
QuantLib::Rate minStrike() const override
QuantLib::Size nOptionTenors_
Number of underlying cap floor instruments.
QuantLib::Rate maxStrike() const override
std::vector< QuantLib::Period > optionTenors_
Underlying cap floor tenors.
bool flatFirstPeriod_
True for flat volatility from time zero to the first cap floor date.