Skip to content

Commit 22b6f89

Browse files
committed
QPR-11772 refactor
1 parent fd256cb commit 22b6f89

File tree

7 files changed

+117
-34
lines changed

7 files changed

+117
-34
lines changed

ql/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ set(QL_SOURCES
318318
instruments/quantobarrieroption.cpp
319319
instruments/quantoforwardvanillaoption.cpp
320320
instruments/quantovanillaoption.cpp
321+
instruments/simplifynotificationgraph.cpp
321322
instruments/stickyratchet.cpp
322323
instruments/stock.cpp
323324
instruments/swap.cpp
@@ -1361,6 +1362,7 @@ set(QL_HEADERS
13611362
instruments/quantobarrieroption.hpp
13621363
instruments/quantoforwardvanillaoption.hpp
13631364
instruments/quantovanillaoption.hpp
1365+
instruments/simplifynotificationgraph.hpp
13641366
instruments/stickyratchet.hpp
13651367
instruments/stock.hpp
13661368
instruments/swap.hpp

ql/instrument.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,4 @@ namespace QuantLib {
4646
QL_FAIL("Instrument::setupArguments() not implemented");
4747
}
4848

49-
void passThroughNotifications(Instrument& instrument,
50-
const std::vector<Leg>& legs,
51-
bool unregisterCoupons) {
52-
for (const auto& leg : legs) {
53-
for (auto const& coupon : leg) {
54-
instrument.unregisterWith(coupon);
55-
instrument.registerWithObservables(coupon);
56-
if (unregisterCoupons) {
57-
coupon->unregisterWithAll();
58-
}
59-
}
60-
}
61-
}
62-
6349
}

ql/instrument.hpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,5 @@ namespace QuantLib {
200200
calculate();
201201
return additionalResults_;
202202
}
203-
204-
//! Utility function to optimize the observability graph of an instrument
205-
/*! This function unregisters the given instrument (e.g. a Swap) from the given cashflows and
206-
instead registers with the observables of the cashflows. This is safe to do if
207-
- the coupon pricers of the cashflows are set before the function is called and never
208-
updated afterwards
209-
- the cashflows are not themselves originating notifications, i.e. they only pass through
210-
notifications from their observables (which is usually the case)
211-
- the set of cashflows does not dynamically change (usually satisfied as well)
212-
If unregisterCoupons is set to true, all given cashflows are in addition unregistered from
213-
all their observables. This can be done
214-
- if the coupons are not asked for results directly
215-
- if deepUpdate() is called on the instrument before retrieving a result; to determine
216-
whether the result might have changed, isCalculated() can be called on the instrument.
217-
*/
218-
void passThroughNotifications(Instrument& instrument,
219-
const std::vector<Leg>& legs,
220-
bool unregisterCoupons = false);
221203
}
222204
#endif
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/*
4+
Copyright (C) 2023 Peter Caspers
5+
6+
This file is part of QuantLib, a free-software/open-source library
7+
for financial quantitative analysts and developers - http://quantlib.org/
8+
9+
QuantLib is free software: you can redistribute it and/or modify it
10+
under the terms of the QuantLib license. You should have received a
11+
copy of the license along with this program; if not, please email
12+
<quantlib-dev@lists.sf.net>. The license is also available online at
13+
<http://quantlib.org/license.shtml>.
14+
15+
This program is distributed in the hope that it will be useful, but WITHOUT
16+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17+
FOR A PARTICULAR PURPOSE. See the license for more details.
18+
*/
19+
20+
#include <ql/instruments/simplifynotificationgraph.hpp>
21+
22+
namespace QuantLib {
23+
24+
25+
void simplifyNotificationGraph(Instrument& instrument, const Leg& leg, bool unregisterCoupons) {
26+
for (auto const& coupon : leg) {
27+
instrument.unregisterWith(coupon);
28+
instrument.registerWithObservables(coupon);
29+
if (unregisterCoupons) {
30+
coupon->unregisterWithAll();
31+
}
32+
}
33+
}
34+
35+
void simplifyNotificationGraph(Swap& swap, const bool unregisterCoupons) {
36+
for (auto const& leg : swap.legs())
37+
simplifyNotificationGraph(swap, leg, unregisterCoupons);
38+
}
39+
40+
void simplifyNotificationGraph(Bond& bond, const bool unregisterCoupons) {
41+
simplifyNotificationGraph(bond, bond.cashflows(), unregisterCoupons);
42+
}
43+
44+
45+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/*
4+
Copyright (C) 2023 Peter Caspers
5+
6+
This file is part of QuantLib, a free-software/open-source library
7+
for financial quantitative analysts and developers - http://quantlib.org/
8+
9+
QuantLib is free software: you can redistribute it and/or modify it
10+
under the terms of the QuantLib license. You should have received a
11+
copy of the license along with this program; if not, please email
12+
<quantlib-dev@lists.sf.net>. The license is also available online at
13+
<http://quantlib.org/license.shtml>.
14+
15+
This program is distributed in the hope that it will be useful, but WITHOUT
16+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17+
FOR A PARTICULAR PURPOSE. See the license for more details.
18+
*/
19+
20+
/*! \file simplifynotificationgraph.hpp
21+
\brief utility functions to reduce number of notifications sent by observables
22+
*/
23+
24+
#ifndef quantlib_simplify_notification_graph
25+
#define quantlib_simplify_notification_graph
26+
27+
#include <ql/cashflow.hpp>
28+
#include <ql/instrument.hpp>
29+
#include <ql/instruments/bond.hpp>
30+
#include <ql/instruments/swap.hpp>
31+
32+
33+
namespace QuantLib {
34+
35+
//! Utility function to optimize the observability graph of an instrument
36+
/*! This function unregisters the given instrument from the given cashflows and
37+
instead registers with the observables of the cashflows. This is safe to do if
38+
39+
- the coupon pricers of the cashflows are set before the function is called and never
40+
updated afterwards
41+
- the cashflows are not themselves originating notifications, i.e. they only pass through
42+
notifications from their observables (which is usually the case)
43+
- the set of cashflows does not dynamically change (usually satisfied as well)
44+
45+
If unregisterCoupons is set to true, all given cashflows are in addition unregistered from
46+
all their observables. This can be done
47+
48+
- if the coupons are not asked for results directly
49+
- if deepUpdate() is called on the instrument before retrieving a result; to determine
50+
whether the result might have changed, isCalculated() can be called on the instrument.
51+
52+
There are overloads of this function for specific instrument types like Swap, Bond.
53+
*/
54+
void simplifyNotificationGraph(Instrument& instrument,
55+
const Leg& leg,
56+
const bool unregisterCoupons = false);
57+
58+
//! Utility function to opimize the observability graph of a swap
59+
void simplifyNotificationGraph(Swap& swap, const bool unregisterCoupons = false);
60+
61+
//! Utility function to opimize the observability graph of a bond
62+
void simplifyNotificationGraph(Bond& bond, const bool unregisterCoupons = false);
63+
64+
}
65+
66+
#endif

ql/termstructures/yield/oisratehelper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
#include <ql/instruments/makeois.hpp>
22+
#include <ql/instruments/simplifynotificationgraph.hpp>
2223
#include <ql/pricingengines/swap/discountingswapengine.hpp>
2324
#include <ql/termstructures/yield/oisratehelper.hpp>
2425
#include <ql/utilities/null_deleter.hpp>
@@ -83,7 +84,7 @@ namespace QuantLib {
8384
swap_ = tmp;
8485
}
8586

86-
passThroughNotifications(*swap_, swap_->legs(), true);
87+
simplifyNotificationGraph(*swap_, true);
8788

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

ql/termstructures/yield/ratehelpers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <ql/currency.hpp>
2828
#include <ql/indexes/swapindex.hpp>
2929
#include <ql/instruments/makevanillaswap.hpp>
30+
#include <ql/instruments/simplifynotificationgraph.hpp>
3031
#include <ql/pricingengines/swap/discountingswapengine.hpp>
3132
#include <ql/quote.hpp>
3233
#include <ql/termstructures/yield/ratehelpers.hpp>
@@ -833,7 +834,7 @@ namespace QuantLib {
833834
.withFloatingLegEndOfMonth(endOfMonth_)
834835
.withIndexedCoupons(useIndexedCoupons_);
835836

836-
passThroughNotifications(*swap_, swap_->legs(), true);
837+
simplifyNotificationGraph(*swap_, true);
837838

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

0 commit comments

Comments
 (0)