Skip to content

Commit

Permalink
notification pass through utility (#1761)
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio authored Aug 11, 2023
2 parents 1a02fe6 + a3ded70 commit 120de4a
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 0 deletions.
2 changes: 2 additions & 0 deletions QuantLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@
<ClInclude Include="ql\instruments\quantobarrieroption.hpp" />
<ClInclude Include="ql\instruments\quantoforwardvanillaoption.hpp" />
<ClInclude Include="ql\instruments\quantovanillaoption.hpp" />
<ClInclude Include="ql\instruments\simplifynotificationgraph.hpp" />
<ClInclude Include="ql\instruments\stickyratchet.hpp" />
<ClInclude Include="ql\instruments\stock.hpp" />
<ClInclude Include="ql\instruments\swap.hpp" />
Expand Down Expand Up @@ -2205,6 +2206,7 @@
<ClCompile Include="ql\instruments\quantobarrieroption.cpp" />
<ClCompile Include="ql\instruments\quantoforwardvanillaoption.cpp" />
<ClCompile Include="ql\instruments\quantovanillaoption.cpp" />
<ClCompile Include="ql\instruments\simplifynotificationgraph.cpp" />
<ClCompile Include="ql\instruments\stickyratchet.cpp" />
<ClCompile Include="ql\instruments\stock.cpp" />
<ClCompile Include="ql\instruments\swap.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions QuantLib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,9 @@
<ClInclude Include="ql\instruments\quantovanillaoption.hpp">
<Filter>instruments</Filter>
</ClInclude>
<ClInclude Include="ql\instruments\simplifynotificationgraph.hpp">
<Filter>instruments</Filter>
</ClInclude>
<ClInclude Include="ql\instruments\stickyratchet.hpp">
<Filter>instruments</Filter>
</ClInclude>
Expand Down Expand Up @@ -4733,6 +4736,9 @@
<ClCompile Include="ql\instruments\quantovanillaoption.cpp">
<Filter>instruments</Filter>
</ClCompile>
<ClCompile Include="ql\instruments\simplifynotificationgraph.cpp">
<Filter>instruments</Filter>
</ClCompile>
<ClCompile Include="ql\instruments\stickyratchet.cpp">
<Filter>instruments</Filter>
</ClCompile>
Expand Down
2 changes: 2 additions & 0 deletions ql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ set(QL_SOURCES
instruments/quantobarrieroption.cpp
instruments/quantoforwardvanillaoption.cpp
instruments/quantovanillaoption.cpp
instruments/simplifynotificationgraph.cpp
instruments/stickyratchet.cpp
instruments/stock.cpp
instruments/swap.cpp
Expand Down Expand Up @@ -1361,6 +1362,7 @@ set(QL_HEADERS
instruments/quantobarrieroption.hpp
instruments/quantoforwardvanillaoption.hpp
instruments/quantovanillaoption.hpp
instruments/simplifynotificationgraph.hpp
instruments/stickyratchet.hpp
instruments/stock.hpp
instruments/swap.hpp
Expand Down
2 changes: 2 additions & 0 deletions ql/instruments/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ this_include_HEADERS = \
quantobarrieroption.hpp \
quantoforwardvanillaoption.hpp \
quantovanillaoption.hpp \
simplifynotificationgraph.hpp \
stickyratchet.hpp \
stock.hpp \
swap.hpp \
Expand Down Expand Up @@ -119,6 +120,7 @@ cpp_files = \
quantobarrieroption.cpp \
quantoforwardvanillaoption.cpp \
quantovanillaoption.cpp \
simplifynotificationgraph.cpp \
stickyratchet.cpp \
stock.cpp \
swap.cpp \
Expand Down
45 changes: 45 additions & 0 deletions ql/instruments/simplifynotificationgraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2023 Peter Caspers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<[email protected]>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/

#include <ql/instruments/simplifynotificationgraph.hpp>

namespace QuantLib {


void simplifyNotificationGraph(Instrument& instrument, const Leg& leg, bool unregisterCoupons) {
for (auto const& coupon : leg) {
instrument.unregisterWith(coupon);
instrument.registerWithObservables(coupon);
if (unregisterCoupons) {
coupon->unregisterWithAll();
}
}
}

void simplifyNotificationGraph(Swap& swap, const bool unregisterCoupons) {
for (auto const& leg : swap.legs())
simplifyNotificationGraph(swap, leg, unregisterCoupons);
}

void simplifyNotificationGraph(Bond& bond, const bool unregisterCoupons) {
simplifyNotificationGraph(bond, bond.cashflows(), unregisterCoupons);
}


}
66 changes: 66 additions & 0 deletions ql/instruments/simplifynotificationgraph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2023 Peter Caspers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<[email protected]>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/

/*! \file simplifynotificationgraph.hpp
\brief utility functions to reduce number of notifications sent by observables
*/

#ifndef quantlib_simplify_notification_graph
#define quantlib_simplify_notification_graph

#include <ql/cashflow.hpp>
#include <ql/instrument.hpp>
#include <ql/instruments/bond.hpp>
#include <ql/instruments/swap.hpp>


namespace QuantLib {

//! Utility function to optimize the observability graph of an instrument
/*! This function unregisters the given instrument from the given cashflows and
instead registers with the observables of the cashflows. This is safe to do if
- the coupon pricers of the cashflows are set before the function is called and never
updated afterwards
- the cashflows are not themselves originating notifications, i.e. they only pass through
notifications from their observables (which is usually the case)
- the set of cashflows does not dynamically change (usually satisfied as well)
If unregisterCoupons is set to true, all given cashflows are in addition unregistered from
all their observables. This can be done
- if the coupons are not asked for results directly
- if deepUpdate() is called on the instrument before retrieving a result; to determine
whether the result might have changed, isCalculated() can be called on the instrument.
There are overloads of this function for specific instrument types like Swap, Bond.
*/
void simplifyNotificationGraph(Instrument& instrument,
const Leg& leg,
const bool unregisterCoupons = false);

//! Utility function to opimize the observability graph of a swap
void simplifyNotificationGraph(Swap& swap, const bool unregisterCoupons = false);

//! Utility function to opimize the observability graph of a bond
void simplifyNotificationGraph(Bond& bond, const bool unregisterCoupons = false);

}

#endif
2 changes: 2 additions & 0 deletions ql/instruments/swap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ namespace QuantLib {

Size Swap::numberOfLegs() const { return legs_.size(); }

const std::vector<Leg>& Swap::legs() const { return legs_; }

Date Swap::startDate() const {
QL_REQUIRE(!legs_.empty(), "no legs given");
Date d = CashFlows::startDate(legs_[0]);
Expand Down
1 change: 1 addition & 0 deletions ql/instruments/swap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace QuantLib {
//! \name Additional interface
//@{
Size numberOfLegs() const;
const std::vector<Leg>& legs() const;
virtual Date startDate() const;
virtual Date maturityDate() const;
Real legBPS(Size j) const {
Expand Down
6 changes: 6 additions & 0 deletions ql/patterns/lazyobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace QuantLib {
//@{
void update() override;
//@}
/*! Returns true if the instrument is calculated */
bool isCalculated() const;
/*! \name Calculations
These methods do not modify the structure of the object
and are therefore declared as <tt>const</tt>. Data members
Expand Down Expand Up @@ -260,6 +262,10 @@ namespace QuantLib {
}
}
}

inline bool LazyObject::isCalculated() const {
return calculated_;
}
}

#endif
3 changes: 3 additions & 0 deletions ql/termstructures/yield/oisratehelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include <ql/instruments/makeois.hpp>
#include <ql/instruments/simplifynotificationgraph.hpp>
#include <ql/pricingengines/swap/discountingswapengine.hpp>
#include <ql/termstructures/yield/oisratehelper.hpp>
#include <ql/utilities/null_deleter.hpp>
Expand Down Expand Up @@ -83,6 +84,8 @@ namespace QuantLib {
swap_ = tmp;
}

simplifyNotificationGraph(*swap_, true);

earliestDate_ = swap_->startDate();
maturityDate_ = swap_->maturityDate();

Expand Down
3 changes: 3 additions & 0 deletions ql/termstructures/yield/ratehelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <ql/currency.hpp>
#include <ql/indexes/swapindex.hpp>
#include <ql/instruments/makevanillaswap.hpp>
#include <ql/instruments/simplifynotificationgraph.hpp>
#include <ql/pricingengines/swap/discountingswapengine.hpp>
#include <ql/quote.hpp>
#include <ql/termstructures/yield/ratehelpers.hpp>
Expand Down Expand Up @@ -833,6 +834,8 @@ namespace QuantLib {
.withFloatingLegEndOfMonth(endOfMonth_)
.withIndexedCoupons(useIndexedCoupons_);

simplifyNotificationGraph(*swap_, true);

earliestDate_ = swap_->startDate();
maturityDate_ = swap_->maturityDate();

Expand Down

0 comments on commit 120de4a

Please sign in to comment.