Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
BalanceGuaranteedSwap Class Reference

Balance Guaranteed Swap. More...

#include <qle/instruments/balanceguaranteedswap.hpp>

+ Inheritance diagram for BalanceGuaranteedSwap:
+ Collaboration diagram for BalanceGuaranteedSwap:

Classes

class  arguments
 Arguments for Balance Guaranteed Swap More...
 
class  engine
 Base class for Balance Guaranteed Swap engines. More...
 
class  results
 Results for Balance Guaranteed Swap More...
 

Public Member Functions

 BalanceGuaranteedSwap (const VanillaSwap::Type type, const std::vector< std::vector< Real > > &trancheNominals, const Schedule &nominalSchedule, const Size referencedTranche, const Schedule &fixedSchedule, const std::vector< Real > &fixedRate, const DayCounter &fixedDayCount, const Schedule &floatingSchedule, const QuantLib::ext::shared_ptr< IborIndex > &iborIndex, const std::vector< Real > &gearing, const std::vector< Real > &spread, const std::vector< Real > &cappedRate, const std::vector< Real > &flooredRate, const DayCounter &floatingDayCount, boost::optional< BusinessDayConvention > paymentConvention=boost::none)
 

Inspectors

const VanillaSwap::Type type_
 
const std::vector< std::vector< Real > > trancheNominals_
 
const Schedule nominalSchedule_
 
const Size referencedTranche_
 
const Schedule fixedSchedule_
 
const std::vector< Real > fixedRate_
 
const DayCounter fixedDayCount_
 
const Schedule floatingSchedule_
 
const QuantLib::ext::shared_ptr< IborIndex > iborIndex_
 
const std::vector< Real > gearing_
 
const std::vector< Real > spread_
 
const std::vector< Real > cappedRate_
 
const std::vector< Real > flooredRate_
 
const DayCounter floatingDayCount_
 
BusinessDayConvention paymentConvention_
 
VanillaSwap::Type type () const
 
const std::vector< std::vector< Real > > & trancheNominal () const
 
const Schedule & nominalSchedule () const
 
const Size referencedTranche () const
 
const Schedule & fixedSchedule () const
 
const std::vector< Real > & fixedRate () const
 
const DayCounter & fixedDayCount () const
 
const Schedule & floatingSchedule () const
 
const QuantLib::ext::shared_ptr< IborIndex > & iborIndex () const
 
const std::vector< Real > & gearing () const
 
const std::vector< Real > & spread () const
 
const std::vector< Real > & cappedRate () const
 
const std::vector< Real > & flooredRate () const
 
const DayCounter & floatingDayCount () const
 
BusinessDayConvention paymentConvention () const
 
const Leg & fixedLeg () const
 
const Leg & floatingLeg () const
 
Real trancheNominal (const Size trancheIndex, const Date &d)
 
void setupArguments (QuantLib::PricingEngine::arguments *) const override
 
void setupExpired () const override
 
void fetchResults (const QuantLib::PricingEngine::results *) const override
 

Detailed Description

Balance Guaranteed Swap.

Notice the comment in the NumericLgmBgsFlexiSwapEngine concerning the start of the prepayments, this means that the tranche notionals for periods with a start date in the past or on the evaluation date should include actual (known) prepayments. For future periods the notionals should correspond to a zero CPR assumption on the other hand.

Definition at line 49 of file balanceguaranteedswap.hpp.

Constructor & Destructor Documentation

◆ BalanceGuaranteedSwap()

BalanceGuaranteedSwap ( const VanillaSwap::Type  type,
const std::vector< std::vector< Real > > &  trancheNominals,
const Schedule &  nominalSchedule,
const Size  referencedTranche,
const Schedule &  fixedSchedule,
const std::vector< Real > &  fixedRate,
const DayCounter &  fixedDayCount,
const Schedule &  floatingSchedule,
const QuantLib::ext::shared_ptr< IborIndex > &  iborIndex,
const std::vector< Real > &  gearing,
const std::vector< Real > &  spread,
const std::vector< Real > &  cappedRate,
const std::vector< Real > &  flooredRate,
const DayCounter &  floatingDayCount,
boost::optional< BusinessDayConvention >  paymentConvention = boost::none 
)

Definition at line 31 of file balanceguaranteedswap.cpp.

42
43 // checks
44
47 else
48 paymentConvention_ = floatingSchedule_.businessDayConvention();
49
50 QL_REQUIRE(nominalSchedule.size() > 0, "Nominal schedule size is zero");
51 QL_REQUIRE(nominalSchedule.hasTenor(), "Nominal schedule needs a tenor");
52 QL_REQUIRE(nominalSchedule.tenor().frequency() != QuantLib::OtherFrequency,
53 "Nominal schedule tenor (" << nominalSchedule.tenor() << ") not allowed, corresponds to OtherFrequency");
54 QL_REQUIRE(fixedSchedule.size() > 0, "Fixed schedule size is zero");
55 QL_REQUIRE(floatingSchedule.size() > 0, "Floating schedule size is zero");
56 QL_REQUIRE(fixedSchedule_.size() - 1 == fixedRate_.size(),
57 "Fixed schedule size (" << fixedSchedule_.size() << ") does not match fixed rate size ("
58 << fixedRate_.size() << ")");
59
60 QL_REQUIRE(floatingSchedule_.size() - 1 == gearing_.size(),
61 "Floating schedule size (" << floatingSchedule_.size() << ") does not match gearing size ("
62 << gearing_.size() << ")");
63 QL_REQUIRE(floatingSchedule_.size() - 1 == spread_.size(),
64 "Floating schedule size (" << floatingSchedule_.size() << ") does not match spread size ("
65 << spread_.size() << ")");
66 QL_REQUIRE(floatingSchedule_.size() - 1 == cappedRate_.size(),
67 "Floating schedule size (" << floatingSchedule_.size() << ") does not match spread size ("
68 << cappedRate_.size() << ")");
69 QL_REQUIRE(floatingSchedule_.size() - 1 == flooredRate_.size(),
70 "Floating schedule size (" << floatingSchedule_.size() << ") does not match spread size ("
71 << flooredRate_.size() << ")");
72 QL_REQUIRE((floatingSchedule.size() - 1) % (fixedSchedule.size() - 1) == 0,
73 "fixed schedule size - 1 (" << fixedSchedule.size() - 1 << ") must divide floating schedule size - 1 ("
74 << floatingSchedule.size() - 1 << ")");
75
76 QL_REQUIRE(trancheNominals.size() > 0, "trancheNominals must be non-empty");
77 QL_REQUIRE(referencedTranche < trancheNominals.size(),
78 "referencedTranche (" << referencedTranche << ") out of range 0..." << (trancheNominals.size() - 1));
79 for (Size i = 0; i < trancheNominals.size(); ++i) {
80 QL_REQUIRE(trancheNominals[i].size() == nominalSchedule.size() - 1,
81 "Tranche nominals at " << i << " (" << trancheNominals.size() << ") do not match nominal schedule ("
82 << nominalSchedule.size() << ")");
83 }
84
85 // build the fixed and floating nominal for the swap
86 std::vector<Real> fixedNominal, floatingNominal;
87 for (Size i = 0; i < fixedSchedule.size() - 1; ++i) {
88 fixedNominal.push_back(trancheNominal(referencedTranche_, fixedSchedule[i]));
89 }
90 // derive floating nominal schedule from fixed to ensure they match
91 Size ratio = (floatingSchedule.size() - 1) / (fixedSchedule.size() - 1); // we know there is no remainder
92 floatingNominal.resize(fixedNominal.size() * ratio);
93 Size i = 0; // remove in C++14 and instead write [i=0, &fixedNotional] in the capture below
94 std::generate(floatingNominal.begin(), floatingNominal.end(),
95 [i, &fixedNominal, ratio]() mutable { return fixedNominal[i++ / ratio]; });
96
97 // if the gearing is zero then the ibor leg will be set up with fixed
98 // coupons which makes trouble here in this context. We therefore use
99 // a dirty trick and enforce the gearing to be non zero.
100
101 // TODO do we need this?
102
103 std::vector<Real> gearingTmp(gearing_);
104 for (Size i = 0; i < gearingTmp.size(); ++i) {
105 if (close(gearingTmp[i], 0.0))
106 gearingTmp[i] = QL_EPSILON;
107 }
108
109 legs_[0] = FixedRateLeg(fixedSchedule_)
110 .withNotionals(fixedNominal)
111 .withCouponRates(fixedRate_, fixedDayCount_)
112 .withPaymentAdjustment(paymentConvention_);
113
114 legs_[1] = IborLeg(floatingSchedule_, iborIndex_)
115 .withNotionals(floatingNominal)
116 .withPaymentDayCounter(floatingDayCount_)
117 .withPaymentAdjustment(paymentConvention_)
118 .withSpreads(spread_)
119 .withGearings(gearingTmp)
120 .withCaps(cappedRate_)
121 .withFloors(flooredRate_);
122
123 for (Leg::const_iterator i = legs_[1].begin(); i < legs_[1].end(); ++i)
124 registerWith(*i);
125
126 auto cpnPricer = QuantLib::ext::make_shared<BlackIborCouponPricer>();
127 setCouponPricer(legs_[1], cpnPricer);
128
129 switch (type_) {
130 case VanillaSwap::Payer:
131 payer_[0] = -1.0;
132 payer_[1] = +1.0;
133 break;
134 case VanillaSwap::Receiver:
135 payer_[0] = +1.0;
136 payer_[1] = -1.0;
137 break;
138 default:
139 QL_FAIL("Unknown vanilla swap type");
140 }
141}
const std::vector< Real > fixedRate_
const std::vector< Real > & flooredRate() const
const DayCounter & fixedDayCount() const
const std::vector< Real > & gearing() const
BusinessDayConvention paymentConvention() const
const Schedule & fixedSchedule() const
const std::vector< std::vector< Real > > trancheNominals_
const std::vector< Real > cappedRate_
const Schedule & floatingSchedule() const
const QuantLib::ext::shared_ptr< IborIndex > & iborIndex() const
const std::vector< Real > & fixedRate() const
const std::vector< Real > flooredRate_
const std::vector< Real > & spread() const
const DayCounter & floatingDayCount() const
const std::vector< Real > & cappedRate() const
const Schedule & nominalSchedule() const
const QuantLib::ext::shared_ptr< IborIndex > iborIndex_
const std::vector< std::vector< Real > > & trancheNominal() const
void setCouponPricer(const Leg &leg, const QuantLib::ext::shared_ptr< FloatingRateCouponPricer > &pricer)
Set Coupon Pricer.
+ Here is the call graph for this function:

Member Function Documentation

◆ type()

VanillaSwap::Type type ( ) const

Definition at line 65 of file balanceguaranteedswap.hpp.

65{ return type_; }

◆ trancheNominal() [1/2]

const std::vector< std::vector< Real > > & trancheNominal ( ) const

Definition at line 66 of file balanceguaranteedswap.hpp.

66{ return trancheNominals_; }
+ Here is the caller graph for this function:

◆ nominalSchedule()

const Schedule & nominalSchedule ( ) const

Definition at line 67 of file balanceguaranteedswap.hpp.

67{ return nominalSchedule_; }
+ Here is the caller graph for this function:

◆ referencedTranche()

const Size referencedTranche ( ) const

Definition at line 68 of file balanceguaranteedswap.hpp.

68{ return referencedTranche_; }
+ Here is the caller graph for this function:

◆ fixedSchedule()

const Schedule & fixedSchedule ( ) const

Definition at line 70 of file balanceguaranteedswap.hpp.

70{ return fixedSchedule_; }
+ Here is the caller graph for this function:

◆ fixedRate()

const std::vector< Real > & fixedRate ( ) const

Definition at line 71 of file balanceguaranteedswap.hpp.

71{ return fixedRate_; }

◆ fixedDayCount()

const DayCounter & fixedDayCount ( ) const

Definition at line 72 of file balanceguaranteedswap.hpp.

72{ return fixedDayCount_; }

◆ floatingSchedule()

const Schedule & floatingSchedule ( ) const

Definition at line 74 of file balanceguaranteedswap.hpp.

74{ return floatingSchedule_; }
+ Here is the caller graph for this function:

◆ iborIndex()

const QuantLib::ext::shared_ptr< IborIndex > & iborIndex ( ) const

Definition at line 75 of file balanceguaranteedswap.hpp.

75{ return iborIndex_; }
+ Here is the caller graph for this function:

◆ gearing()

const std::vector< Real > & gearing ( ) const

Definition at line 76 of file balanceguaranteedswap.hpp.

76{ return gearing_; }

◆ spread()

const std::vector< Real > & spread ( ) const

Definition at line 77 of file balanceguaranteedswap.hpp.

77{ return spread_; }

◆ cappedRate()

const std::vector< Real > & cappedRate ( ) const

Definition at line 78 of file balanceguaranteedswap.hpp.

78{ return cappedRate_; }

◆ flooredRate()

const std::vector< Real > & flooredRate ( ) const

Definition at line 79 of file balanceguaranteedswap.hpp.

79{ return flooredRate_; }

◆ floatingDayCount()

const DayCounter & floatingDayCount ( ) const

Definition at line 80 of file balanceguaranteedswap.hpp.

80{ return floatingDayCount_; }

◆ paymentConvention()

BusinessDayConvention paymentConvention ( ) const

Definition at line 82 of file balanceguaranteedswap.hpp.

82{ return paymentConvention_; }
+ Here is the caller graph for this function:

◆ fixedLeg()

const Leg & fixedLeg ( ) const

Definition at line 84 of file balanceguaranteedswap.hpp.

84{ return legs_[0]; }
+ Here is the caller graph for this function:

◆ floatingLeg()

const Leg & floatingLeg ( ) const

Definition at line 85 of file balanceguaranteedswap.hpp.

85{ return legs_[1]; }
+ Here is the caller graph for this function:

◆ trancheNominal() [2/2]

Real trancheNominal ( const Size  trancheIndex,
const Date &  d 
)

Definition at line 205 of file balanceguaranteedswap.cpp.

205 {
206 QL_REQUIRE(trancheIndex < trancheNominals_.size(), "BalanceGuaranteedSwap::trancheNominal(): tranceIndex ("
207 << trancheIndex << ") out of range 0..."
208 << trancheNominals_.size() - 1);
209 if (d < nominalSchedule_.dates().front() || d >= nominalSchedule_.dates().back())
210 return 0.0;
211 Size l = std::upper_bound(nominalSchedule_.dates().begin(), nominalSchedule_.dates().end(), d) -
212 nominalSchedule_.dates().begin();
213 return trancheNominals_[trancheIndex][l - 1];
214}

◆ setupArguments()

void setupArguments ( QuantLib::PricingEngine::arguments ) const
overrideprivate

Definition at line 143 of file balanceguaranteedswap.cpp.

143 {
144 Swap::setupArguments(args);
145 auto arguments = dynamic_cast<BalanceGuaranteedSwap::arguments*>(args);
146
147 // QL_REQUIRE(arguments != nullptr, "BalanceGuaranteedSwap::setupArguments(): wrong argument type");
148
149 // allow for swap engine
150 if (arguments == nullptr)
151 return;
152
153 arguments->type = type_;
154 arguments->trancheNominals = trancheNominals_;
155 arguments->trancheNominalDates = nominalSchedule_.dates();
156 arguments->trancheNominalFrequency = nominalSchedule_.tenor().frequency();
157 arguments->referencedTranche = referencedTranche_;
158 arguments->fixedRate = fixedRate_;
159 arguments->iborIndex = iborIndex();
160 arguments->cappedRate = cappedRate_;
161 arguments->flooredRate = flooredRate_;
162
163 const Leg& fixedCoupons = fixedLeg();
164
165 arguments->fixedResetDates = arguments->fixedPayDates = std::vector<Date>(fixedCoupons.size());
166 arguments->fixedCoupons = std::vector<Real>(fixedCoupons.size());
167
168 for (Size i = 0; i < fixedCoupons.size(); ++i) {
169 auto coupon = QuantLib::ext::dynamic_pointer_cast<FixedRateCoupon>(fixedCoupons[i]);
170 QL_REQUIRE(coupon != nullptr, "BalanceGuaranteedSwap::setupArguments(): expected fixed rate coupon");
171 arguments->fixedPayDates[i] = coupon->date();
172 arguments->fixedResetDates[i] = coupon->accrualStartDate();
173 arguments->fixedCoupons[i] = coupon->amount();
174 }
175
176 const Leg& floatingCoupons = floatingLeg();
177
178 arguments->floatingResetDates = arguments->floatingPayDates = arguments->floatingFixingDates =
179 std::vector<Date>(floatingCoupons.size());
180 arguments->floatingAccrualTimes = std::vector<Time>(floatingCoupons.size());
181 arguments->floatingSpreads = std::vector<Spread>(floatingCoupons.size());
182 arguments->floatingGearings = std::vector<Real>(floatingCoupons.size());
183 arguments->floatingCoupons = std::vector<Real>(floatingCoupons.size());
184
185 for (Size i = 0; i < floatingCoupons.size(); ++i) {
186 auto coupon = QuantLib::ext::dynamic_pointer_cast<FloatingRateCoupon>(floatingCoupons[i]);
187 QL_REQUIRE(coupon != nullptr, "BalanceGuaranteedSwap::setupArguments(): expected fixed rate coupon");
188 arguments->floatingResetDates[i] = coupon->accrualStartDate();
189 arguments->floatingPayDates[i] = coupon->date();
190 arguments->floatingFixingDates[i] = coupon->fixingDate();
191 arguments->floatingAccrualTimes[i] = coupon->accrualPeriod();
192 arguments->floatingSpreads[i] = coupon->spread();
193 arguments->floatingGearings[i] = coupon->gearing();
194 try {
195 arguments->floatingCoupons[i] = coupon->amount();
196 } catch (const std::exception&) {
197 arguments->floatingCoupons[i] = Null<Real>();
198 }
199 }
200
201 arguments->fixedLeg = legs_[0];
202 arguments->floatingLeg = legs_[1];
203}
+ Here is the call graph for this function:

◆ setupExpired()

void setupExpired ( ) const
overrideprivate

Definition at line 216 of file balanceguaranteedswap.cpp.

216{ Swap::setupExpired(); }

◆ fetchResults()

void fetchResults ( const QuantLib::PricingEngine::results *  ) const
overrideprivate

Definition at line 218 of file balanceguaranteedswap.cpp.

218 {
219 Swap::fetchResults(r);
220 auto results = dynamic_cast<const BalanceGuaranteedSwap::results*>(r);
221
222 // QL_REQUIRE(results != nullptr, "BalanceGuaranteedSwap::fetchResult(): wrong result type");
223
224 // allow for swap engine
225 if (results == nullptr)
226 return;
227}

Member Data Documentation

◆ type_

const VanillaSwap::Type type_
private

Definition at line 91 of file balanceguaranteedswap.hpp.

◆ trancheNominals_

const std::vector<std::vector<Real> > trancheNominals_
private

Definition at line 92 of file balanceguaranteedswap.hpp.

◆ nominalSchedule_

const Schedule nominalSchedule_
private

Definition at line 93 of file balanceguaranteedswap.hpp.

◆ referencedTranche_

const Size referencedTranche_
private

Definition at line 94 of file balanceguaranteedswap.hpp.

◆ fixedSchedule_

const Schedule fixedSchedule_
private

Definition at line 95 of file balanceguaranteedswap.hpp.

◆ fixedRate_

const std::vector<Real> fixedRate_
private

Definition at line 96 of file balanceguaranteedswap.hpp.

◆ fixedDayCount_

const DayCounter fixedDayCount_
private

Definition at line 97 of file balanceguaranteedswap.hpp.

◆ floatingSchedule_

const Schedule floatingSchedule_
private

Definition at line 98 of file balanceguaranteedswap.hpp.

◆ iborIndex_

const QuantLib::ext::shared_ptr<IborIndex> iborIndex_
private

Definition at line 99 of file balanceguaranteedswap.hpp.

◆ gearing_

const std::vector<Real> gearing_
private

Definition at line 100 of file balanceguaranteedswap.hpp.

◆ spread_

const std::vector<Real> spread_
private

Definition at line 101 of file balanceguaranteedswap.hpp.

◆ cappedRate_

const std::vector<Real> cappedRate_
private

Definition at line 102 of file balanceguaranteedswap.hpp.

◆ flooredRate_

const std::vector<Real> flooredRate_
private

Definition at line 103 of file balanceguaranteedswap.hpp.

◆ floatingDayCount_

const DayCounter floatingDayCount_
private

Definition at line 104 of file balanceguaranteedswap.hpp.

◆ paymentConvention_

BusinessDayConvention paymentConvention_
private

Definition at line 105 of file balanceguaranteedswap.hpp.