Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include time zero / evaluation date in option date interpolation #1783

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Comment on lines -79 to -80
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because it's already done in the base class, right? We still need a notification if the evaluation date changes, don't we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think it is done in the TermStructure constructor.

}

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 @@
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_;

Check notice on line 77 in ql/termstructures/volatility/swaption/swaptionvoldiscrete.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

ql/termstructures/volatility/swaption/swaptionvoldiscrete.hpp#L77

class member 'SwaptionVolatilityDiscrete::optionDatesAsReal_' is never used.
mutable std::vector<Time> optionInterpolatorTimes_;

Check notice on line 78 in ql/termstructures/volatility/swaption/swaptionvoldiscrete.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

ql/termstructures/volatility/swaption/swaptionvoldiscrete.hpp#L78

class member 'SwaptionVolatilityDiscrete::optionInterpolatorTimes_' is never used.
mutable std::vector<Real> optionInterpolatorDatesAsReal_;

Check notice on line 79 in ql/termstructures/volatility/swaption/swaptionvoldiscrete.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

ql/termstructures/volatility/swaption/swaptionvoldiscrete.hpp#L79

class member 'SwaptionVolatilityDiscrete::optionInterpolatorDatesAsReal_' is never used.

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