From c71e2531e3e39e981550b5d6a623b7a1c6d38af6 Mon Sep 17 00:00:00 2001 From: Luigi Ballabio Date: Tue, 2 Apr 2024 09:12:11 +0200 Subject: [PATCH] Restore backward compatibility --- Examples/Gaussian1dModels/Gaussian1dModels.cpp | 6 +++--- ql/experimental/basismodels/swaptioncfs.cpp | 2 +- ql/instruments/nonstandardswaption.cpp | 2 +- ql/instruments/swaption.cpp | 2 ++ ql/instruments/swaption.hpp | 13 ++++++++++++- .../shortrate/calibrationhelpers/swaptionhelper.hpp | 12 +++++++++++- test-suite/swaption.cpp | 4 ++-- test-suite/swaptionvolatilitymatrix.cpp | 4 ++-- 8 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Examples/Gaussian1dModels/Gaussian1dModels.cpp b/Examples/Gaussian1dModels/Gaussian1dModels.cpp index e411dad8cb9..fc6481c80c5 100644 --- a/Examples/Gaussian1dModels/Gaussian1dModels.cpp +++ b/Examples/Gaussian1dModels/Gaussian1dModels.cpp @@ -64,10 +64,10 @@ void printBasket( "==================" << std::endl; for (const auto& j : basket) { auto helper = ext::dynamic_pointer_cast(j); - Date endDate = helper->underlyingSwap()->fixedSchedule().dates().back(); - Real nominal = helper->underlyingSwap()->nominal(); + Date endDate = helper->underlying()->fixedSchedule().dates().back(); + Real nominal = helper->underlying()->nominal(); Real vol = helper->volatility()->value(); - Real rate = helper->underlyingSwap()->fixedRate(); + Real rate = helper->underlying()->fixedRate(); Date expiry = helper->swaption()->exercise()->date(0); Swap::Type type = helper->swaption()->type(); std::ostringstream expiryString, endDateString; diff --git a/ql/experimental/basismodels/swaptioncfs.cpp b/ql/experimental/basismodels/swaptioncfs.cpp index f17c005d105..af45acd6c70 100644 --- a/ql/experimental/basismodels/swaptioncfs.cpp +++ b/ql/experimental/basismodels/swaptioncfs.cpp @@ -119,7 +119,7 @@ namespace QuantLib { SwaptionCashFlows::SwaptionCashFlows(const ext::shared_ptr& swaption, const Handle& discountCurve, bool contTenorSpread) - : SwapCashFlows(swaption->underlyingSwap(), discountCurve, contTenorSpread), + : SwapCashFlows(swaption->underlying(), discountCurve, contTenorSpread), swaption_(swaption) { // assemble raw cash flow data... Actual365Fixed dc; diff --git a/ql/instruments/nonstandardswaption.cpp b/ql/instruments/nonstandardswaption.cpp index e0dc77b4343..f647dc4f1a4 100644 --- a/ql/instruments/nonstandardswaption.cpp +++ b/ql/instruments/nonstandardswaption.cpp @@ -27,7 +27,7 @@ namespace QuantLib { : Option(ext::shared_ptr(), const_cast(fromSwaption).exercise()), swap_(ext::make_shared( - *fromSwaption.underlyingSwap())), + *fromSwaption.underlying())), settlementType_(fromSwaption.settlementType()), settlementMethod_(fromSwaption.settlementMethod()) { diff --git a/ql/instruments/swaption.cpp b/ql/instruments/swaption.cpp index 1505631dbc6..e317a95e10f 100644 --- a/ql/instruments/swaption.cpp +++ b/ql/instruments/swaption.cpp @@ -146,6 +146,8 @@ namespace QuantLib { // wouldn't recalculate. To avoid this, we override the // default behavior of the underlying swap. swap_->alwaysForwardNotifications(); + + vanilla_ = ext::dynamic_pointer_cast(swap_); } void Swaption::deepUpdate() { diff --git a/ql/instruments/swaption.hpp b/ql/instruments/swaption.hpp index 5d6c1cdf3cf..a9727894645 100644 --- a/ql/instruments/swaption.hpp +++ b/ql/instruments/swaption.hpp @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -108,9 +109,17 @@ namespace QuantLib { return settlementMethod_; } Swap::Type type() const { return swap_->type(); } - const ext::shared_ptr& underlyingSwap() const { + const ext::shared_ptr& underlying() const { return swap_; } + /*! \deprecated Use the Swaption::underlying method instead. + Deprecated in version 1.34. + */ + [[deprecated("Use the Swaption::underlying method instead")]] + const ext::shared_ptr& underlyingSwap() const { + QL_REQUIRE(vanilla_, "underlying is not a vanilla swap"); + return vanilla_; + } //@} //! implied volatility Volatility impliedVolatility( @@ -129,6 +138,8 @@ namespace QuantLib { //Handle termStructure_; Settlement::Type settlementType_; Settlement::Method settlementMethod_; + // until we remove underlyingSwap(); + ext::shared_ptr vanilla_; }; //! %Arguments for swaption calculation diff --git a/ql/models/shortrate/calibrationhelpers/swaptionhelper.hpp b/ql/models/shortrate/calibrationhelpers/swaptionhelper.hpp index a5a4f9a81fe..8d037d37c44 100644 --- a/ql/models/shortrate/calibrationhelpers/swaptionhelper.hpp +++ b/ql/models/shortrate/calibrationhelpers/swaptionhelper.hpp @@ -93,10 +93,20 @@ namespace QuantLib { Real modelValue() const override; Real blackPrice(Volatility volatility) const override; - ext::shared_ptr underlyingSwap() const { + const ext::shared_ptr& underlying() const { calculate(); return swap_; } + /*! \deprecated Use the SwaptionHelper::underlying method instead. + Deprecated in version 1.34. + */ + [[deprecated("Use the SwaptionHelper::underlying method instead")]] + ext::shared_ptr underlyingSwap() const { + calculate(); + auto vanilla = ext::dynamic_pointer_cast(swap_); + QL_REQUIRE(vanilla, "underlying is not a vanilla swap"); + return vanilla; + } ext::shared_ptr swaption() const { calculate(); return swaption_; } private: diff --git a/test-suite/swaption.cpp b/test-suite/swaption.cpp index 553ccfd7ba7..d3aa8eeabe0 100644 --- a/test-suite/swaption.cpp +++ b/test-suite/swaption.cpp @@ -510,7 +510,7 @@ BOOST_AUTO_TEST_CASE(testVega) { << "\n strike: " << io::rate(strike) << "\n settlement: " << types[h] << "\n nominal: " - << swaption->underlyingSwap()->nominal() + << swaption->underlying()->nominal() << "\n npv: " << swaptionNPV << "\n calculated vega: " << analyticalVegaPerPoint << "\n expected vega: " << numericalVegaPerPoint @@ -1107,7 +1107,7 @@ void checkSwaptionDelta(bool useBachelierVol) << "\n swap tenor: " << length << "\n strike: " << strike << "\n settlement: " << types[h] << "\n method: " << methods[h] - << "\n nominal: " << swaption->underlyingSwap()->nominal() + << "\n nominal: " << swaption->underlying()->nominal() << "\n npv: " << value << "\n calculated delta: " << delta << "\n expected delta: " << approxDelta); } diff --git a/test-suite/swaptionvolatilitymatrix.cpp b/test-suite/swaptionvolatilitymatrix.cpp index de5e8f46720..addfe0b26ea 100644 --- a/test-suite/swaptionvolatilitymatrix.cpp +++ b/test-suite/swaptionvolatilitymatrix.cpp @@ -210,8 +210,8 @@ struct CommonVars { "\nactual option date: " << exerciseDate << "\n exp. option date: " << vol->optionDates()[i]); - Date start = swaption.underlyingSwap()->startDate(); - Date end = swaption.underlyingSwap()->maturityDate(); + Date start = swaption.underlying()->startDate(); + Date end = swaption.underlying()->maturityDate(); Time swapLength2 = vol->swapLength(start, end); if (!close(swapLength2,swapLength)) BOOST_FAIL("\nswapLength failure for " <<