30 const vector<Real>& epe,
const vector<Date>& dates,
31 const Handle<DefaultProbabilityTermStructure>& dts,
33 const Handle<YieldTermStructure>& yts,
34 const vector<Period>& shiftTenors, Real shiftSize)
35 : key_(key), asof_(
asof), epe_(epe), dates_(dates), dts_(dts), recovery_(recovery), yts_(yts),
36 shiftTenors_(shiftTenors), shiftSize_(shiftSize) {
46 Real cvaShifted =
cva(
true, i);
50 DLOG(
"CVA Calculator key=" <<
key_ <<
" cvaBase=" << cvaBase);
55 DLOG(
"CVA Calculator key=" <<
key_ <<
" fairSpread[" << i <<
"]=" << cdsSpreadBase);
57 for (Size j = 0; j <= i; ++j) {
61 DLOG(
"CVA Calculator key=" <<
key_ <<
" jacobi[" << j <<
"][" << i <<
"]=" <<
jacobi_[j][i]);
63 DLOG(
"CVA Calculator key=" <<
key_ <<
" jacobi column[" << i <<
"]=" << row);
73 return dts_->survivalProbability(t);
75 QL_REQUIRE(index <
shiftTimes_.size(),
"index " << index <<
" out of range");
77 Real t1 = index == 0 ? 0.0 :
shiftTimes_[index - 1];
78 bool lastBucket = index ==
shiftTimes_.size() - 1 ? true :
false;
80 return dts_->survivalProbability(t);
92 Real t =
dts_->timeFromReference(d);
98 for (Size j = 0; j <
dates_.size(); ++j) {
103 Real increment = (1.0 -
recovery_) * (s0 - s1) *
epe_[j + 1];
106 DLOG(
"CVA Calculator key=" <<
key_ <<
" shift=" << shift <<
" index=" << index <<
" cva=" <<
sum);
112 QL_REQUIRE(term <
shiftTimes_.size(),
"term " << term <<
" out of range");
115 Size n = Size(floor(T / dt + 0.5));
116 QL_REQUIRE(fabs(T - dt * n) < 0.1 * dt,
"shift term is not a multiple of 6M");
117 Real enumerator = 0.0, denominator = 0.0;
118 for (Size i = 1; i <= n; ++i) {
119 Real t0 = dt * (i - 1);
123 Real dis =
yts_->discount(t1);
124 enumerator += (s0 - s1) * dis;
125 denominator += dt * s1 * dis;
127 return (1.0 -
recovery_) * enumerator / denominator;
const vector< Period > shiftTenors()
Handle< YieldTermStructure > yts_
Real cva(bool shift=false, Size index=0)
vector< Real > cdsSpreadSensitivities_
Real survivalProbability(Date d, bool shift, Size index)
CVASpreadSensitivityCalculator(const std::string &key, const Date &asof, const vector< Real > &epe, const vector< Date > &dates, const Handle< DefaultProbabilityTermStructure > &dts, const Real &recovery, const Handle< YieldTermStructure > &yts, const vector< Period > &shiftTenors, Real shiftSize=0.0001)
vector< Period > shiftTenors_
vector< Real > hazardRateSensitivities_
Handle< DefaultProbabilityTermStructure > dts_
Real fairCdsSpread(Size term, bool shift=false, Size index=0)
Fair CDS Spread calculation with and without shifted hazard rates.
vector< Real > shiftTimes_
RandomVariable exp(RandomVariable x)
QuantLib::SparseMatrix inverse(QuantLib::SparseMatrix m)
Real sum(const Cash &c, const Cash &d)