24#include <ql/time/date.hpp>
25#include <ql/time/calendars/weekendsonly.hpp>
34 const QuantLib::ext::shared_ptr<Portfolio>& portfolio,
const QuantLib::ext::shared_ptr<NPVCube>& cube,
35 const QuantLib::ext::shared_ptr<CubeInterpretation> cubeInterpretation,
36 const QuantLib::ext::shared_ptr<Market>& market,
37 bool exerciseNextBreak,
const string& baseCurrency,
const string& configuration,
39 const bool flipViewXVA)
40 : portfolio_(portfolio), cube_(cube), cubeInterpretation_(cubeInterpretation),
41 market_(market), exerciseNextBreak_(exerciseNextBreak),
42 baseCurrency_(baseCurrency), configuration_(configuration),
43 quantile_(quantile), calcType_(calcType),
44 multiPath_(multiPath), dates_(cube->dates()),
45 today_(market_->asofDate()), dc_(ActualActual(ActualActual::ISDA)), flipViewXVA_(flipViewXVA) {
50 exposureCube_ = QuantLib::ext::make_shared<SinglePrecisionInMemoryCubeN>(
54 exposureCube_ = QuantLib::ext::make_shared<DoublePrecisionInMemoryCubeN>(
59 set<string> nettingSetIdsSet;
60 for (
const auto& [tradeId, trade] :
portfolio->trades())
61 nettingSetIdsSet.insert(trade->envelope().nettingSetId());
62 nettingSetIds_= vector<string>(nettingSetIdsSet.begin(), nettingSetIdsSet.end());
65 for (Size i = 0; i <
dates_.size(); i++)
72 LOG(
"Compute trade exposure profiles, " << (
flipViewXVA_ ?
"inverted (flipViewXVA = Y)" :
"regular (flipViewXVA = N)"));
74 for (
auto tradeIt =
portfolio_->trades().begin(); tradeIt !=
portfolio_->trades().end(); ++tradeIt, ++i) {
75 auto trade = tradeIt->second;
76 string tradeId = tradeIt->first;
77 string nettingSetId = trade->envelope().nettingSetId();
78 LOG(
"Aggregate exposure for trade " << tradeId);
87 Date nextBreakDate = trade->maturity();
91 vector<TradeAction> actions = ta.
actions();
92 for (Size j = 0; j < actions.size(); ++j) {
93 DLOG(
"TradeAction for " << tradeId <<
", actionType " << actions[j].type() <<
", actionOwner "
94 << actions[j].owner());
96 if (actions[j].type() ==
"Break" && actions[j].owner() ==
"Mutual") {
98 vector<Date>
dates = schedule.dates();
100 Date
today = Settings::instance().evaluationDate();
101 for (Size k = 0; k <
dates.size(); ++k) {
103 nextBreakDate =
dates[k];
104 DLOG(
"Next break date for trade " << tradeId <<
": "
105 << QuantLib::io::iso_date(nextBreakDate));
116 npv0 = -
cube_->getT0(i);
118 npv0 =
cube_->getT0(i);
120 vector<Real>
epe(
dates_.size() + 1, 0.0);
121 vector<Real>
ene(
dates_.size() + 1, 0.0);
124 vector<Real>
pfe(
dates_.size() + 1, 0.0);
125 epe[0] = std::max(npv0, 0.0);
126 ene[0] = std::max(-npv0, 0.0);
129 pfe[0] = std::max(npv0, 0.0);
132 for (Size j = 0; j <
dates_.size(); ++j) {
133 Date d =
cube_->dates()[j];
134 vector<Real> distribution(
cube_->samples(), 0.0);
135 for (Size k = 0; k <
cube_->samples(); ++k) {
147 closeOutValue = defaultValue;
156 Real npv = defaultValue;
163 distribution[k] = npv;
173 ee_b[j + 1] =
epe[j + 1] / curve->discount(
cube_->dates()[j]);
175 std::sort(distribution.begin(), distribution.end());
176 Size index = Size(floor(
quantile_ * (
cube_->samples() - 1) + 0.5));
177 pfe[j + 1] = std::max(distribution[index], 0.0);
187 Calendar cal = WeekendsOnly();
193 Date
maturity = std::min(cal.adjust(
today_ + 1 * Years + 4 * Days), trade->maturity());
200 vector<double> weights(t);
202 for (Size k = 1; k < t; k++)
204 double totalWeights = std::accumulate(weights.begin(), weights.end(), 0.0);
205 for (Size k = 0; k < t; k++)
206 weights[k] /= totalWeights;
208 for (Size k = 0; k < t; k++) {
219 vector<Real>
exp(
dates_.size() + 1, 0.0);
221 for (Size i = 0; i <
dates_.size(); i++) {
map< string, std::vector< Real > > pfe_
ExposureCalculator(const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const QuantLib::ext::shared_ptr< NPVCube > &cube, const QuantLib::ext::shared_ptr< CubeInterpretation > cubeInterpretation, const QuantLib::ext::shared_ptr< Market > &market, const bool exerciseNextBreak, const string &baseCurrency, const string &configuration, const Real quantile, const CollateralExposureHelper::CalculationType calcType, const bool multiPath, const bool flipViewXVA)
const bool exerciseNextBreak_
const QuantLib::ext::shared_ptr< Portfolio > portfolio_
map< string, vector< vector< Real > > > nettingSetDefaultValue_
map< string, vector< vector< Real > > > nettingSetMporPositiveFlow_
const QuantLib::ext::shared_ptr< NPVCube > cube_
vector< Real > epe(const string &tid)
vector< Real > getMeanExposure(const string &tid, ExposureIndex index)
QuantLib::ext::shared_ptr< Market > market()
const string configuration_
map< string, Real > eepe_b_
map< string, Real > epe_b_
QuantLib::ext::shared_ptr< NPVCube > exposureCube_
virtual void build()
Compute exposures along all paths and fill result structures.
const Size EXPOSURE_CUBE_DEPTH
bool isRegularCubeStorage_
const string baseCurrency_
vector< Real > & pfe(const string &tid)
map< string, vector< vector< Real > > > nettingSetMporNegativeFlow_
vector< Real > & eee_b(const string &tid)
map< string, std::vector< Real > > eee_b_
Real & eepe_b(const string &tid)
Real & epe_b(const string &tid)
map< string, vector< vector< Real > > > nettingSetCloseOutValue_
QuantLib::ext::shared_ptr< Portfolio > portfolio()
const QuantLib::ext::shared_ptr< Market > market_
map< string, std::vector< Real > > ee_b_
vector< Real > & ee_b(const string &tid)
vector< string > nettingSetIds_
const QuantLib::ext::shared_ptr< CubeInterpretation > cubeInterpretation_
vector< Real > ene(const string &tid)
const vector< TradeAction > & actions() const
A cube implementation that stores the cube in memory.
RandomVariable max(RandomVariable x, const RandomVariable &y)
RandomVariable exp(RandomVariable x)
Schedule makeSchedule(const ScheduleDates &data)