Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
DiscountingCurrencySwapEngineDeltaGamma Class Reference

Discounting currency swap engine providing analytical deltas and gammas. More...

#include <qle/pricingengines/discountingcurrencyswapenginedeltagamma.hpp>

+ Inheritance diagram for DiscountingCurrencySwapEngineDeltaGamma:
+ Collaboration diagram for DiscountingCurrencySwapEngineDeltaGamma:

Public Types

typedef std::map< Currency, Matrix, CurrencyComparatorresult_type_matrix
 
typedef std::map< Currency, std::vector< Real >, CurrencyComparatorresult_type_vector
 
typedef std::map< Currency, Real, CurrencyComparatorresult_type_scalar
 

Public Member Functions

 DiscountingCurrencySwapEngineDeltaGamma (const std::vector< Handle< YieldTermStructure > > &discountCurves, const std::vector< Handle< Quote > > &fxQuotes, const std::vector< Currency > &currencies, const Currency &npvCurrency, const std::vector< Time > &bucketTimes=std::vector< Time >(), const bool computeDelta=false, const bool computeGamma=false, const bool linearInZero=true, const bool applySimmExemptions=false)
 
void calculate () const override
 
std::vector< Handle< YieldTermStructure > > discountCurves ()
 
std::vector< Currency > currencies ()
 
Currency npvCurrency ()
 

Private Member Functions

Handle< YieldTermStructure > fetchTS (Currency ccy) const
 
Handle< Quote > fetchFX (Currency ccy) const
 

Private Attributes

const std::vector< Handle< YieldTermStructure > > discountCurves_
 
const std::vector< Handle< Quote > > fxQuotes_
 
const std::vector< Currency > currencies_
 
const Currency npvCurrency_
 
const std::vector< Time > bucketTimes_
 
const bool computeDelta_
 
const bool computeGamma_
 
const bool linearInZero_
 
const bool applySimmExemptions_
 

Detailed Description

Discounting currency swap engine providing analytical deltas and gammas.

This class generalizes QuantLib's DiscountingSwapEngine. It takes leg currencies into account and converts into the provided "npv currency", which must be one of the leg currencies. The evaluation date is the reference date of either of the discounting curves (which must be equal).

The same comments as in discountingswapenginedeltagamma.hpp apply here, so read them first.

The engine processes FX linked flows from xccy resetting swaps, but only captures the additional FX Spot Delta risk coming from the reset feature. Interest Deltas and Gammas coming from the FX forwarding factor P_for / P_dom are neglected, this factor is treated as a constant for the purpose of sensitivity calculation.

Here, the additional results are:

deltaDiscount (map<Currency, vector<Real>> ): Delta on discount curve, rebucketed, values are in currency deltaForward (map<Currency, vector<Real>> ): Delta on forward curve, rebucketed, value are in currency deltaFxSpot (map<Currency, Real> ): Delta on FX Spot (for all leg currencies, even if = npv ccy)

gamma (map<Currency, Matrix> ): Gamma matrix per currency with blocks | dsc-dsc dsc-fwd | | dsc-fwd fwd-fwd | (note that the second derivatives including the FX Spot are zero for the pure second derivative w.r.t. the FX Spot or given by the in currency delta values provided as the additional result deltaDiscount, deltaForward, to be reinterpreted as values in domestic currency)

fxSpot (map<Currency, Real> ): FX Spot used for conversion to npvCurrency (for all leg ccys) bucketTimes (vector<Real> ): Bucketing grid for deltas and gammas

Warning:
: The assumption is that per currency we only have one discount and one forward curve. It is possible to have several, but then the computed deltas will be aggregated over all those curves.

Definition at line 69 of file discountingcurrencyswapenginedeltagamma.hpp.

Member Typedef Documentation

◆ result_type_matrix

typedef std::map<Currency, Matrix, CurrencyComparator> result_type_matrix

Definition at line 71 of file discountingcurrencyswapenginedeltagamma.hpp.

◆ result_type_vector

typedef std::map<Currency, std::vector<Real>, CurrencyComparator> result_type_vector

Definition at line 72 of file discountingcurrencyswapenginedeltagamma.hpp.

◆ result_type_scalar

typedef std::map<Currency, Real, CurrencyComparator> result_type_scalar

Definition at line 73 of file discountingcurrencyswapenginedeltagamma.hpp.

Constructor & Destructor Documentation

◆ DiscountingCurrencySwapEngineDeltaGamma()

DiscountingCurrencySwapEngineDeltaGamma ( const std::vector< Handle< YieldTermStructure > > &  discountCurves,
const std::vector< Handle< Quote > > &  fxQuotes,
const std::vector< Currency > &  currencies,
const Currency &  npvCurrency,
const std::vector< Time > &  bucketTimes = std::vector<Time>(),
const bool  computeDelta = false,
const bool  computeGamma = false,
const bool  linearInZero = true,
const bool  applySimmExemptions = false 
)

The FX spots must be given as units of npvCurrency per respective currency. The spots must be given w.r.t. a settlement date equal to the npv date (which is the reference date of the term structures).

If applySimmExemptions = true, simple cashflows will be excluded from the additional results listed above (but not from the npv / leg npv results) if

  • the underlying instrument is physically settled and
  • the underlying instrument is not a resettable swap Notice that the SIMM adjustments for resettable swaps are not applied though!

Definition at line 34 of file discountingcurrencyswapenginedeltagamma.cpp.

39 bucketTimes_(bucketTimes), computeDelta_(computeDelta), computeGamma_(computeGamma), linearInZero_(linearInZero),
40 applySimmExemptions_(applySimmExemptions) {
41
42 QL_REQUIRE(discountCurves_.size() == currencies_.size(), "Number of "
43 "currencies does not match number of discount curves.");
44 QL_REQUIRE(fxQuotes_.size() == currencies_.size(), "Number of "
45 "currencies does not match number of FX quotes.");
46
47 for (Size i = 0; i < discountCurves_.size(); i++) {
48 registerWith(discountCurves_[i]);
49 registerWith(fxQuotes_[i]);
50 }
51
52 QL_REQUIRE(!bucketTimes_.empty() || (!computeDelta && !computeGamma),
53 "bucket times are empty, although sensitivities have to be calculated");
54}
const std::vector< Handle< YieldTermStructure > > discountCurves_

Member Function Documentation

◆ calculate()

void calculate ( ) const
override

Definition at line 72 of file discountingcurrencyswapenginedeltagamma.cpp.

72 {
73
74 for (Size i = 0; i < arguments_.currency.size(); i++) {
75 Currency ccy = arguments_.currency[i];
76 Handle<YieldTermStructure> yts = fetchTS(ccy);
77 QL_REQUIRE(!yts.empty(), "Discounting term structure is "
78 "empty for "
79 << ccy.name());
80 Handle<Quote> fxQuote = fetchFX(ccy);
81 QL_REQUIRE(!fxQuote.empty(), "FX quote is empty "
82 "for "
83 << ccy.name());
84 }
85
86 // Prepare the results containers
87 Size numLegs = arguments_.legs.size();
88
89 // - Instrument::results
90 results_.value = 0.0;
91 results_.errorEstimate = Null<Real>();
92
93 // - CurrencySwap::results
94 results_.legNPV.resize(numLegs);
95 results_.inCcyLegNPV.resize(numLegs);
96
97 // compute npv and raw deltas
98
99 std::map<Currency, std::map<Date, Real>, CurrencyComparator> deltaDiscountRaw, deltaForwardRaw, gammaDiscountRaw;
100 std::map<Currency, std::map<std::pair<Date, Date>, Real>, CurrencyComparator> gammaForwardRaw, gammaDscFwdRaw;
101 std::map<Currency, Real, CurrencyComparator> fxLinkedForeignNpv; // foreign ccy => npv in foreign ccy
102 std::map<Currency, Real, CurrencyComparator> fxSpot, fxSpotDelta;
103 std::set<Currency, CurrencyComparator> currencies;
104 std::map<Date, Real> empty, fxLinkedDeltaEmpty;
105
106 for (Size i = 0; i < numLegs; ++i) {
107 try {
108 Currency ccy = arguments_.currency[i];
109
110 // look for second ccy, we need this for FX Linked Coupons - the assumption is then that there are
111 // exactly two currencies in the swap; if we don't find a second ccy, we do not collect contributions
112 // from fx linked coupons below
113 Currency ccy2;
114 Real fx2 = 1.0;
115 for (Size j = 0; j < numLegs; ++j) {
116 if (arguments_.currency[j] != ccy) {
117 ccy2 = arguments_.currency[j];
118 fx2 = fetchFX(ccy2)->value();
119 fxSpot[ccy2] = fx2;
120 }
121 }
122
123 currencies.insert(ccy);
124 Handle<YieldTermStructure> yts = fetchTS(ccy);
125 Real npv = 0.0, bps = 0.0, simpleCashFlowNpv = 0.0;
126 Real fxLinkedForeignNpvTmp = 0.0;
127 detail::NpvDeltaGammaCalculator calc(
128 yts, arguments_.payer[i], npv, bps, computeDelta_, computeGamma_, false, deltaDiscountRaw[ccy],
129 deltaForwardRaw[ccy], empty, gammaDiscountRaw[ccy], gammaForwardRaw[ccy], gammaDscFwdRaw[ccy], empty,
130 fxLinkedForeignNpvTmp,
131 applySimmExemptions_ && arguments_.isPhysicallySettled && !arguments_.isResettable, simpleCashFlowNpv);
132 Leg& leg = arguments_.legs[i];
133 for (Size ii = 0; ii < leg.size(); ++ii) {
134 CashFlow& cf = *leg[ii];
135 if (cf.date() <= yts->referenceDate()) {
136 continue;
137 }
138 cf.accept(calc);
139 }
140 Real fx = fetchFX(ccy)->value();
141 fxSpotDelta[ccy] += npv;
142 fxSpot[ccy] = fx;
143 results_.inCcyLegNPV[i] = npv + simpleCashFlowNpv;
144 results_.legNPV[i] = results_.inCcyLegNPV[i] * fx;
145 results_.value += results_.legNPV[i];
146 // handle contribution from FX Linked Coupon:
147 // - these should be subtracted from the fx spot delta in the converted ccy,
148 // - and added to the fx spot delta in the original ccy.
149 if (!ccy2.empty()) {
150 fxSpotDelta[ccy] -= fxLinkedForeignNpvTmp * fx2 / fx;
151 fxLinkedForeignNpv[ccy2] += fxLinkedForeignNpvTmp;
152 }
153 } catch (std::exception& e) {
154 QL_FAIL("DiscountingCurrencySwapEngineDeltaGamma, leg " << i << ": " << e.what());
155 }
156 } // i = 0, ... ,numlegs
157
158 // contributions from FX Linked Coupons to fx spot delta
159
160 for (auto const& f : fxLinkedForeignNpv) {
161 fxSpotDelta[f.first] += f.second;
162 }
163
164 // set results
165
166 results_.additionalResults["fxSpot"] = fxSpot;
167 results_.additionalResults["deltaFxSpot"] = fxSpotDelta;
168
169 // convert raw deltas to given bucketing structure
170
171 results_.additionalResults["bucketTimes"] = bucketTimes_;
172
173 if (computeDelta_) {
174 std::map<Currency, std::vector<Real>, CurrencyComparator> deltaDiscount, deltaForward;
175 for (std::map<Currency, std::map<Date, Real>, CurrencyComparator>::const_iterator i = deltaDiscountRaw.begin();
176 i != deltaDiscountRaw.end(); ++i) {
177 Handle<YieldTermStructure> yts = fetchTS(i->first);
178 deltaDiscount[i->first] =
179 detail::rebucketDeltas(bucketTimes_, i->second, yts->referenceDate(), yts->dayCounter(), linearInZero_);
180 }
181 results_.additionalResults["deltaDiscount"] = deltaDiscount;
182
183 for (std::map<Currency, std::map<Date, Real>, CurrencyComparator>::const_iterator i = deltaForwardRaw.begin();
184 i != deltaForwardRaw.end(); ++i) {
185 Handle<YieldTermStructure> yts = fetchTS(i->first);
186 deltaForward[i->first] =
187 detail::rebucketDeltas(bucketTimes_, i->second, yts->referenceDate(), yts->dayCounter(), linearInZero_);
188 }
189 results_.additionalResults["deltaForward"] = deltaForward;
190 }
191
192 // convert raw gammas to given bucketing structure
193
194 if (computeGamma_) {
195 std::map<Currency, Matrix, CurrencyComparator> gamma;
196 for (std::set<Currency>::const_iterator i = currencies.begin(); i != currencies.end(); ++i) {
197 Handle<YieldTermStructure> yts = fetchTS(*i);
198 Matrix tmp =
199 detail::rebucketGammas(bucketTimes_, gammaDiscountRaw[*i], gammaForwardRaw[*i], gammaDscFwdRaw[*i],
200 true, yts->referenceDate(), yts->dayCounter(), linearInZero_);
201 gamma[*i] = tmp;
202 }
203 results_.additionalResults["gamma"] = gamma;
204 }
205
206} // calculate
const Instrument::results * results_
Definition: cdsoption.cpp:81
Matrix rebucketGammas(const std::vector< Time > &gammaTimes, const std::map< Date, Real > &gammaDscRaw, std::map< std::pair< Date, Date >, Real > &gammaForward, std::map< std::pair< Date, Date >, Real > &gammaDscFwd, const bool forceFullMatrix, const Date &referenceDate, const DayCounter &dc, const bool linearInZero)
std::vector< Real > rebucketDeltas(const std::vector< Time > &deltaTimes, const std::map< Date, Real > &deltaRaw, const Date &referenceDate, const DayCounter &dc, const bool linearInZero)
Swap::arguments * arguments_
+ Here is the call graph for this function:

◆ discountCurves()

std::vector< Handle< YieldTermStructure > > discountCurves ( )

Definition at line 91 of file discountingcurrencyswapenginedeltagamma.hpp.

91{ return discountCurves_; }

◆ currencies()

std::vector< Currency > currencies ( )

Definition at line 92 of file discountingcurrencyswapenginedeltagamma.hpp.

92{ return currencies_; }
+ Here is the caller graph for this function:

◆ npvCurrency()

Currency npvCurrency ( )

Definition at line 93 of file discountingcurrencyswapenginedeltagamma.hpp.

93{ return npvCurrency_; }

◆ fetchTS()

Handle< YieldTermStructure > fetchTS ( Currency  ccy) const
private

Definition at line 56 of file discountingcurrencyswapenginedeltagamma.cpp.

56 {
57 std::vector<Currency>::const_iterator i = std::find(currencies_.begin(), currencies_.end(), ccy);
58 if (i == currencies_.end())
59 return Handle<YieldTermStructure>();
60 else
61 return discountCurves_[i - currencies_.begin()];
62}
+ Here is the caller graph for this function:

◆ fetchFX()

Handle< Quote > fetchFX ( Currency  ccy) const
private

Definition at line 64 of file discountingcurrencyswapenginedeltagamma.cpp.

64 {
65 std::vector<Currency>::const_iterator i = std::find(currencies_.begin(), currencies_.end(), ccy);
66 if (i == currencies_.end())
67 return Handle<Quote>();
68 else
69 return fxQuotes_[i - currencies_.begin()];
70}
+ Here is the caller graph for this function:

Member Data Documentation

◆ discountCurves_

const std::vector<Handle<YieldTermStructure> > discountCurves_
private

Definition at line 99 of file discountingcurrencyswapenginedeltagamma.hpp.

◆ fxQuotes_

const std::vector<Handle<Quote> > fxQuotes_
private

Definition at line 100 of file discountingcurrencyswapenginedeltagamma.hpp.

◆ currencies_

const std::vector<Currency> currencies_
private

Definition at line 101 of file discountingcurrencyswapenginedeltagamma.hpp.

◆ npvCurrency_

const Currency npvCurrency_
private

Definition at line 102 of file discountingcurrencyswapenginedeltagamma.hpp.

◆ bucketTimes_

const std::vector<Time> bucketTimes_
private

Definition at line 103 of file discountingcurrencyswapenginedeltagamma.hpp.

◆ computeDelta_

const bool computeDelta_
private

Definition at line 104 of file discountingcurrencyswapenginedeltagamma.hpp.

◆ computeGamma_

const bool computeGamma_
private

Definition at line 104 of file discountingcurrencyswapenginedeltagamma.hpp.

◆ linearInZero_

const bool linearInZero_
private

Definition at line 104 of file discountingcurrencyswapenginedeltagamma.hpp.

◆ applySimmExemptions_

const bool applySimmExemptions_
private

Definition at line 104 of file discountingcurrencyswapenginedeltagamma.hpp.