Skip to content

Commit

Permalink
Include time zero / evaluation date in option date interpolation (#1783)
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio authored Sep 29, 2023
2 parents 1e58438 + 7df75ad commit eaa249d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
2 changes: 0 additions & 2 deletions ql/termstructures/volatility/swaption/swaptionvolcube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ namespace QuantLib {
swapIndexBase_->tenor() << ")");

registerWithVolatilitySpread();
registerWith(Settings::instance().evaluationDate());
evaluationDate_ = Settings::instance().evaluationDate();
}

void SwaptionVolatilityCube::registerWithVolatilitySpread()
Expand Down
42 changes: 24 additions & 18 deletions ql/termstructures/volatility/swaption/swaptionvoldiscrete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace QuantLib {
optionDates_(nOptionTenors_),
optionTimes_(nOptionTenors_),
optionDatesAsReal_(nOptionTenors_),
optionInterpolatorTimes_(nOptionTenors_ + 1),
optionInterpolatorDatesAsReal_(nOptionTenors_ + 1),
nSwapTenors_(swapTenors.size()),
swapTenors_(swapTenors),
swapLengths_(nSwapTenors_) {
Expand All @@ -47,14 +49,12 @@ namespace QuantLib {
checkSwapTenors();
initializeSwapLengths();

optionInterpolator_= LinearInterpolation(optionTimes_.begin(),
optionTimes_.end(),
optionDatesAsReal_.begin());
optionInterpolator_= LinearInterpolation(optionInterpolatorTimes_.begin(),
optionInterpolatorTimes_.end(),
optionInterpolatorDatesAsReal_.begin());
optionInterpolator_.update();
optionInterpolator_.enableExtrapolation();

registerWith(Settings::instance().evaluationDate());
evaluationDate_ = Settings::instance().evaluationDate();
cachedReferenceDate_ = referenceDate();
}

SwaptionVolatilityDiscrete::SwaptionVolatilityDiscrete(
Expand All @@ -70,6 +70,8 @@ namespace QuantLib {
optionDates_(nOptionTenors_),
optionTimes_(nOptionTenors_),
optionDatesAsReal_(nOptionTenors_),
optionInterpolatorTimes_(nOptionTenors_ + 1),
optionInterpolatorDatesAsReal_(nOptionTenors_ + 1),
nSwapTenors_(swapTenors.size()),
swapTenors_(swapTenors),
swapLengths_(nSwapTenors_) {
Expand All @@ -80,9 +82,9 @@ namespace QuantLib {
checkSwapTenors();
initializeSwapLengths();

optionInterpolator_= LinearInterpolation(optionTimes_.begin(),
optionTimes_.end(),
optionDatesAsReal_.begin());
optionInterpolator_= LinearInterpolation(optionInterpolatorTimes_.begin(),
optionInterpolatorTimes_.end(),
optionInterpolatorDatesAsReal_.begin());
optionInterpolator_.update();
optionInterpolator_.enableExtrapolation();
}
Expand All @@ -100,6 +102,8 @@ namespace QuantLib {
optionDates_(optionDates),
optionTimes_(nOptionTenors_),
optionDatesAsReal_(nOptionTenors_),
optionInterpolatorTimes_(nOptionTenors_ + 1),
optionInterpolatorDatesAsReal_(nOptionTenors_ + 1),
nSwapTenors_(swapTenors.size()),
swapTenors_(swapTenors),
swapLengths_(nSwapTenors_) {
Expand All @@ -110,9 +114,9 @@ namespace QuantLib {
checkSwapTenors();
initializeSwapLengths();

optionInterpolator_= LinearInterpolation(optionTimes_.begin(),
optionTimes_.end(),
optionDatesAsReal_.begin());
optionInterpolator_= LinearInterpolation(optionInterpolatorTimes_.begin(),
optionInterpolatorTimes_.end(),
optionInterpolatorDatesAsReal_.begin());
optionInterpolator_.update();
optionInterpolator_.enableExtrapolation();
}
Expand Down Expand Up @@ -152,17 +156,20 @@ namespace QuantLib {
}

void SwaptionVolatilityDiscrete::initializeOptionDatesAndTimes() const {
optionInterpolatorDatesAsReal_[0] = static_cast<Real>(referenceDate().serialNumber());
for (Size i=0; i<nOptionTenors_; ++i) {
optionDates_[i] = optionDateFromTenor(optionTenors_[i]);
optionDatesAsReal_[i] =
optionDatesAsReal_[i] = optionInterpolatorDatesAsReal_[i + 1] =
static_cast<Real>(optionDates_[i].serialNumber());
}
initializeOptionTimes();
}

void SwaptionVolatilityDiscrete::initializeOptionTimes() const {
for (Size i=0; i<nOptionTenors_; ++i)
optionTimes_[i] = timeFromReference(optionDates_[i]);
optionInterpolatorTimes_[0] = 0.0;
for (Size i = 0; i < nOptionTenors_; ++i) {
optionTimes_[i] = optionInterpolatorTimes_[i + 1] = timeFromReference(optionDates_[i]);
}
}

void SwaptionVolatilityDiscrete::initializeSwapLengths() const {
Expand All @@ -173,9 +180,8 @@ namespace QuantLib {
void SwaptionVolatilityDiscrete::performCalculations() const {
// recalculate dates if necessary...
if (moving_) {
Date d = Settings::instance().evaluationDate();
if (evaluationDate_ != d) {
evaluationDate_ = d;
if (cachedReferenceDate_ != referenceDate()) {
cachedReferenceDate_ = referenceDate();
initializeOptionDatesAndTimes();
initializeSwapLengths();
optionInterpolator_.update();
Expand Down
6 changes: 4 additions & 2 deletions ql/termstructures/volatility/swaption/swaptionvoldiscrete.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ namespace QuantLib {
std::vector<Period> optionTenors_;
mutable std::vector<Date> optionDates_;
mutable std::vector<Time> optionTimes_;
mutable std::vector<Real> optionDatesAsReal_;
mutable Interpolation optionInterpolator_;
mutable std::vector<Real> optionDatesAsReal_;
mutable std::vector<Time> optionInterpolatorTimes_;
mutable std::vector<Real> optionInterpolatorDatesAsReal_;

Size nSwapTenors_;
std::vector<Period> swapTenors_;
mutable std::vector<Time> swapLengths_;
mutable Date evaluationDate_;
mutable Date cachedReferenceDate_;
private:
void checkOptionTenors() const;
void checkOptionDates(const Date& reference) const;
Expand Down

0 comments on commit eaa249d

Please sign in to comment.