From 17883f220952299fd20bf4caa047bad1b43a4f1d Mon Sep 17 00:00:00 2001 From: Peter Caspers Date: Sat, 26 Aug 2023 19:53:28 +0200 Subject: [PATCH 01/95] fix handling of past payment dates, add missing include --- ql/cashflows/lineartsrpricer.cpp | 72 ++++++++++++++++++-------------- ql/cashflows/lineartsrpricer.hpp | 4 +- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/ql/cashflows/lineartsrpricer.cpp b/ql/cashflows/lineartsrpricer.cpp index 687a93b0667..4ece4cb19e2 100644 --- a/ql/cashflows/lineartsrpricer.cpp +++ b/ql/cashflows/lineartsrpricer.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -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 = boost::dynamic_pointer_cast(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(); + } ext::shared_ptr sectionTmp = swaptionVolatility()->smileSection(fixingDate_, swapTenor_); @@ -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 c = ext::dynamic_pointer_cast(i); Real yf = c->accrualPeriod(); Date d = c->date(); @@ -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_)) / @@ -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 { @@ -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); @@ -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 { @@ -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); @@ -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 { @@ -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_; diff --git a/ql/cashflows/lineartsrpricer.hpp b/ql/cashflows/lineartsrpricer.hpp index 54a8a8aaa35..eda7b3d49c8 100644 --- a/ql/cashflows/lineartsrpricer.hpp +++ b/ql/cashflows/lineartsrpricer.hpp @@ -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_; ext::shared_ptr swap_; + ext::shared_ptr onSwap_; ext::shared_ptr smileSection_; Settings settings_; From d2cff826c49bf8c6e7e28becca0ba29da499c378 Mon Sep 17 00:00:00 2001 From: Peter Caspers Date: Sat, 26 Aug 2023 20:10:14 +0200 Subject: [PATCH 02/95] fix namespace --- ql/cashflows/lineartsrpricer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ql/cashflows/lineartsrpricer.cpp b/ql/cashflows/lineartsrpricer.cpp index 4ece4cb19e2..da07bc46d6e 100644 --- a/ql/cashflows/lineartsrpricer.cpp +++ b/ql/cashflows/lineartsrpricer.cpp @@ -147,7 +147,7 @@ namespace QuantLib { swapTenor_ = swapIndex_->tenor(); Leg swapFixedLeg; - if(auto on = boost::dynamic_pointer_cast(swapIndex_)) { + if(auto on = ext::dynamic_pointer_cast(swapIndex_)) { onSwap_ = on->underlyingSwap(fixingDate_); swapRateValue_ = onSwap_->fairRate(); annuity_ = 1.0E4 * std::fabs(onSwap_->fixedLegBPS()); From 905d8fe5d0cd001a8e89a8eaa327fa6aa07a6f23 Mon Sep 17 00:00:00 2001 From: JZ <135723295+jez6@users.noreply.github.com> Date: Thu, 16 Nov 2023 17:48:29 -0500 Subject: [PATCH 03/95] Build/install pkgconfig files in CMake builds - Generate and install quantlib.pc in addition to quantlib-config as part of CMake builds Issues: jez6/QuantLib#1 --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 05418566772..8e3ed321119 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -230,6 +230,9 @@ set(exec_prefix "\${prefix}") set(includedir "\${prefix}/include") set(libdir "\${exec_prefix}/lib") configure_file(quantlib-config.in quantlib-config @ONLY) +configure_file(quantlib.pc.in quantlib.pc @ONLY) +install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/quantlib-config TYPE BIN) +install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/quantlib.pc DESTINATION lib/pkgconfig) include(Platform) From 22d077080177125894fbad30ec91b5aa7c9b0434 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Wed, 8 Feb 2023 10:58:32 -0500 Subject: [PATCH 04/95] allow swaptions to take OvernightIndexedSwap --- ql/cashflows/overnightindexedcoupon.cpp | 2 +- ql/cashflows/overnightindexedcoupon.hpp | 3 + ql/experimental/basismodels/swaptioncfs.cpp | 2 +- ql/experimental/basismodels/swaptioncfs.hpp | 2 +- .../basismodels/tenorswaptionvts.cpp | 13 ++-- ql/instruments/nonstandardswap.cpp | 2 +- ql/instruments/nonstandardswap.hpp | 4 +- ql/instruments/swaption.cpp | 10 +-- ql/instruments/swaption.hpp | 12 +-- .../libormarketmodels/lfmswaptionengine.cpp | 17 ++--- .../fdmaffinemodelswapinnervalue.hpp | 26 +++---- .../calibrationhelpers/swaptionhelper.cpp | 76 ++++++++++++------- .../calibrationhelpers/swaptionhelper.hpp | 22 ++++-- .../swaption/blackswaptionengine.hpp | 50 ++++++------ .../swaption/g2swaptionengine.hpp | 10 +-- .../swaption/gaussian1dswaptionengine.cpp | 6 +- 16 files changed, 148 insertions(+), 109 deletions(-) diff --git a/ql/cashflows/overnightindexedcoupon.cpp b/ql/cashflows/overnightindexedcoupon.cpp index 877ce8422b3..03c0af0a4b6 100644 --- a/ql/cashflows/overnightindexedcoupon.cpp +++ b/ql/cashflows/overnightindexedcoupon.cpp @@ -153,7 +153,7 @@ namespace QuantLib { overnightIndex, gearing, spread, refPeriodStart, refPeriodEnd, - dayCounter, false) { + dayCounter, false), averagingMethod_(averagingMethod) { // value dates Date tmpEndDate = endDate; diff --git a/ql/cashflows/overnightindexedcoupon.hpp b/ql/cashflows/overnightindexedcoupon.hpp index c13113996f6..8c0d2cf6b6d 100644 --- a/ql/cashflows/overnightindexedcoupon.hpp +++ b/ql/cashflows/overnightindexedcoupon.hpp @@ -71,6 +71,8 @@ namespace QuantLib { const std::vector& indexFixings() const; //! value dates for the rates to be compounded const std::vector& valueDates() const { return valueDates_; } + //! averaging method + const RateAveraging::Type averagingMethod() const { return averagingMethod_; } //@} //! \name FloatingRateCoupon interface //@{ @@ -87,6 +89,7 @@ namespace QuantLib { mutable std::vector fixings_; Size n_; std::vector