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

LinearTsrPricer: fix past payment dates, support overnight swap indices, add missing include #1775

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
72 changes: 41 additions & 31 deletions ql/cashflows/lineartsrpricer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <ql/cashflows/lineartsrpricer.hpp>
#include <ql/indexes/iborindex.hpp>
#include <ql/instruments/vanillaswap.hpp>
#include <ql/instruments/overnightindexedswap.hpp>
#include <ql/math/integrals/kronrodintegral.hpp>
#include <ql/math/solvers1d/brent.hpp>
#include <ql/pricingengines/blackformula.hpp>
Expand Down Expand Up @@ -122,24 +123,42 @@ namespace QuantLib {

today_ = QuantLib::Settings::instance().evaluationDate();

if (paymentDate_ > today_ && !couponDiscountCurve_.empty())
couponDiscountRatio_ =
couponDiscountCurve_->discount(paymentDate_) /
discountCurve_->discount(paymentDate_);
else
couponDiscountRatio_ = 1.;
Real couponCurvePaymentDiscount;
if (!couponDiscountCurve_.empty() && paymentDate_ > couponDiscountCurve_->referenceDate()) {
couponCurvePaymentDiscount = couponDiscountCurve_->discount(paymentDate_);
} else {
couponCurvePaymentDiscount = 1.0;
}

if (paymentDate_ > discountCurve_->referenceDate()) {
discountCurvePaymentDiscount_ = discountCurve_->discount(paymentDate_);
} else {
discountCurvePaymentDiscount_ = 1.0;
}

spreadLegValue_ = spread_ * coupon_->accrualPeriod() *
discountCurve_->discount(paymentDate_) *

couponDiscountRatio_ = couponCurvePaymentDiscount / discountCurvePaymentDiscount_;

spreadLegValue_ = spread_ * coupon_->accrualPeriod() * discountCurvePaymentDiscount_ *
couponDiscountRatio_;

if (fixingDate_ > today_) {

swapTenor_ = swapIndex_->tenor();
swap_ = swapIndex_->underlyingSwap(fixingDate_);

swapRateValue_ = swap_->fairRate();
annuity_ = 1.0E4 * std::fabs(swap_->fixedLegBPS());
Leg swapFixedLeg;
if(auto on = ext::dynamic_pointer_cast<OvernightIndexedSwapIndex>(swapIndex_)) {
onSwap_ = on->underlyingSwap(fixingDate_);
swapRateValue_ = onSwap_->fairRate();
annuity_ = 1.0E4 * std::fabs(onSwap_->fixedLegBPS());
swapFixedLeg = onSwap_->fixedLeg();
}
else {
swap_ = swapIndex_->underlyingSwap(fixingDate_);
swapRateValue_ = swap_->fairRate();
annuity_ = 1.0E4 * std::fabs(swap_->fixedLegBPS());
swapFixedLeg = swap_->fixedLeg();
}
Copy link
Owner

Choose a reason for hiding this comment

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

This can probably be reworked now that #1789 is merged


ext::shared_ptr<SmileSection> sectionTmp =
swaptionVolatility()->smileSection(fixingDate_, swapTenor_);
Expand Down Expand Up @@ -169,7 +188,7 @@ namespace QuantLib {
// compute linear model's parameters

Real gx = 0.0, gy = 0.0;
for (const auto& i : swap_->fixedLeg()) {
for (const auto& i : swapFixedLeg) {
ext::shared_ptr<Coupon> c = ext::dynamic_pointer_cast<Coupon>(i);
Real yf = c->accrualPeriod();
Date d = c->date();
Expand All @@ -179,7 +198,7 @@ namespace QuantLib {
}

Real gamma = gx / gy;
Date lastd = swap_->fixedLeg().back()->date();
Date lastd = swapFixedLeg.back()->date();

a_ = discountCurve_->discount(paymentDate_) *
(gamma - GsrG(paymentDate_)) /
Expand Down Expand Up @@ -360,8 +379,7 @@ namespace QuantLib {

Rate LinearTsrPricer::swapletRate() const {
return swapletPrice() /
(coupon_->accrualPeriod() *
discountCurve_->discount(paymentDate_) * couponDiscountRatio_);
(coupon_->accrualPeriod() * discountCurvePaymentDiscount_ * couponDiscountRatio_);
}

Real LinearTsrPricer::capletPrice(Rate effectiveCap) const {
Expand All @@ -370,10 +388,8 @@ namespace QuantLib {
// the fixing is determined
const Rate Rs = std::max(
coupon_->swapIndex()->fixing(fixingDate_) - effectiveCap, 0.);
Rate price =
(gearing_ * Rs) *
(coupon_->accrualPeriod() *
discountCurve_->discount(paymentDate_) * couponDiscountRatio_);
Rate price = (gearing_ * Rs) * (coupon_->accrualPeriod() *
discountCurvePaymentDiscount_ * couponDiscountRatio_);
return price;
} else {
Real capletPrice = optionletPrice(Option::Call, effectiveCap);
Expand All @@ -383,8 +399,7 @@ namespace QuantLib {

Rate LinearTsrPricer::capletRate(Rate effectiveCap) const {
return capletPrice(effectiveCap) /
(coupon_->accrualPeriod() *
discountCurve_->discount(paymentDate_) * couponDiscountRatio_);
(coupon_->accrualPeriod() * discountCurvePaymentDiscount_ * couponDiscountRatio_);
}

Real LinearTsrPricer::floorletPrice(Rate effectiveFloor) const {
Expand All @@ -393,10 +408,8 @@ namespace QuantLib {
// the fixing is determined
const Rate Rs = std::max(
effectiveFloor - coupon_->swapIndex()->fixing(fixingDate_), 0.);
Rate price =
(gearing_ * Rs) *
(coupon_->accrualPeriod() *
discountCurve_->discount(paymentDate_) * couponDiscountRatio_);
Rate price = (gearing_ * Rs) * (coupon_->accrualPeriod() *
discountCurvePaymentDiscount_ * couponDiscountRatio_);
return price;
} else {
Real floorletPrice = optionletPrice(Option::Put, effectiveFloor);
Expand All @@ -406,8 +419,7 @@ namespace QuantLib {

Rate LinearTsrPricer::floorletRate(Rate effectiveFloor) const {
return floorletPrice(effectiveFloor) /
(coupon_->accrualPeriod() *
discountCurve_->discount(paymentDate_) * couponDiscountRatio_);
(coupon_->accrualPeriod() * discountCurvePaymentDiscount_ * couponDiscountRatio_);
}

Real LinearTsrPricer::swapletPrice() const {
Expand All @@ -416,14 +428,12 @@ namespace QuantLib {
const Rate Rs = coupon_->swapIndex()->fixing(fixingDate_);
Rate price =
(gearing_ * Rs + spread_) *
(coupon_->accrualPeriod() *
discountCurve_->discount(paymentDate_) * couponDiscountRatio_);
(coupon_->accrualPeriod() * discountCurvePaymentDiscount_ * couponDiscountRatio_);
return price;
} else {
Real atmCapletPrice = optionletPrice(Option::Call, swapRateValue_);
Real atmFloorletPrice = optionletPrice(Option::Put, swapRateValue_);
return gearing_ * (coupon_->accrualPeriod() *
discountCurve_->discount(paymentDate_) *
return gearing_ * (coupon_->accrualPeriod() * discountCurvePaymentDiscount_ *
swapRateValue_ * couponDiscountRatio_ +
atmCapletPrice - atmFloorletPrice) +
spreadLegValue_;
Expand Down
4 changes: 3 additions & 1 deletion ql/cashflows/lineartsrpricer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,12 @@ namespace QuantLib {
Real gearing_, spread_;

Period swapTenor_;
Real spreadLegValue_, swapRateValue_, couponDiscountRatio_, annuity_;
Real spreadLegValue_, swapRateValue_, couponDiscountRatio_, discountCurvePaymentDiscount_,
annuity_;

ext::shared_ptr<SwapIndex> swapIndex_;
ext::shared_ptr<VanillaSwap> swap_;
ext::shared_ptr<OvernightIndexedSwap> onSwap_;
ext::shared_ptr<SmileSection> smileSection_;

Settings settings_;
Expand Down