24#include <ql/errors.hpp>
25#include <ql/time/calendars/weekendsonly.hpp>
26#include <ql/math/distributions/normaldistribution.hpp>
27#include <ql/math/generallinearleastsquares.hpp>
28#include <ql/math/kernelfunctions.hpp>
29#include <ql/methods/montecarlo/lsmbasissystem.hpp>
30#include <ql/time/daycounters/actualactual.hpp>
35#include <boost/accumulators/accumulators.hpp>
36#include <boost/accumulators/statistics/error_of_mean.hpp>
37#include <boost/accumulators/statistics/mean.hpp>
38#include <boost/accumulators/statistics/stats.hpp>
43using namespace boost::accumulators;
49 const QuantLib::ext::shared_ptr<InputParameters>& inputs,
50 const QuantLib::ext::shared_ptr<Portfolio>& portfolio,
const QuantLib::ext::shared_ptr<NPVCube>& cube,
51 const QuantLib::ext::shared_ptr<CubeInterpretation>& cubeInterpretation,
52 const QuantLib::ext::shared_ptr<AggregationScenarioData>& scenarioData, Real quantile, Size horizonCalendarDays,
53 const std::map<std::string, Real>& currentIM)
54 : inputs_(inputs), portfolio_(portfolio), cube_(cube), cubeInterpretation_(cubeInterpretation), scenarioData_(scenarioData),
55 quantile_(quantile), horizonCalendarDays_(horizonCalendarDays), currentIM_(currentIM) {
58 QL_REQUIRE(
cube_,
"cube is null");
60 QL_REQUIRE(
scenarioData_,
"aggregation scenario data is null");
65 Size dates =
cube_->dates().size();
66 Size samples =
cube_->samples();
69 WLOG(
"cube holds no mpor flows, will assume no flows in the dim calculation");
73 set<string> nettingSets;
75 for (
auto tradeIt =
portfolio_->trades().begin(); tradeIt !=
portfolio_->trades().end(); ++tradeIt, ++i) {
76 auto trade = tradeIt->second;
77 string tradeId = tradeIt->first;
78 string nettingSetId = trade->envelope().nettingSetId();
79 if (nettingSets.find(nettingSetId) == nettingSets.end()) {
80 nettingSets.insert(nettingSetId);
81 nettingSetNPV_[nettingSetId] = vector<vector<Real>>(dates, vector<Real>(samples, 0.0));
83 nettingSetFLOW_[nettingSetId] = vector<vector<Real>>(dates, vector<Real>(samples, 0.0));
84 nettingSetDeltaNPV_[nettingSetId] = vector<vector<Real>>(dates, vector<Real>(samples, 0.0));
85 nettingSetDIM_[nettingSetId] = vector<vector<Real>>(dates, vector<Real>(samples, 0.0));
89 for (Size k = 0; k < samples; ++k) {
112 QL_FAIL(
"netting set " << nettingSet <<
" not found in DIM results");
119 QL_FAIL(
"netting set " << nettingSet <<
" not found in expected DIM results");
126 QL_FAIL(
"netting set " << nettingSet <<
" not found in DIM results");
135 dimEvolutionReport.
addColumn(
"TimeStep", Size())
143 for (
auto [nettingSet, _] :
dimCube_->idsAndIndexes()) {
145 LOG(
"Export DIM evolution for netting set " << nettingSet);
146 for (Size i = 0; i < stopDatesLoop; ++i) {
147 Real expectedFlow = 0.0;
148 for (Size j = 0; j < samples; ++j) {
149 expectedFlow +=
nettingSetFLOW_.find(nettingSet)->second[i][j] / samples;
152 Date defaultDate =
dimCube_->dates()[i];
153 Time t = ActualActual(ActualActual::ISDA).yearFraction(
asof, defaultDate);
155 dimEvolutionReport.
next()
165 dimEvolutionReport.
end();
166 LOG(
"Exporting expected DIM through time done");
map< string, vector< vector< Real > > > nettingSetNPV_
const vector< vector< Real > > & dynamicIM(const string &nettingSet)
DIM matrix by date and sample index for the specified netting set.
DynamicInitialMarginCalculator(const QuantLib::ext::shared_ptr< InputParameters > &inputs, 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< AggregationScenarioData > &scenarioData, Real quantile=0.99, Size horizonCalendarDays=14, const std::map< std::string, Real > ¤tIM=std::map< std::string, Real >())
std::set< string > nettingSetIds_
map< string, vector< vector< Real > > > nettingSetCloseOutNPV_
map< string, vector< vector< Real > > > nettingSetDeltaNPV_
map< string, vector< Real > > nettingSetExpectedDIM_
QuantLib::ext::shared_ptr< Portfolio > portfolio_
virtual void exportDimEvolution(ore::data::Report &dimEvolutionReport) const
DIM evolution report.
QuantLib::ext::shared_ptr< AggregationScenarioData > scenarioData_
QuantLib::ext::shared_ptr< CubeInterpretation > cubeInterpretation_
QuantLib::ext::shared_ptr< NPVCube > dimCube_
map< string, vector< vector< Real > > > nettingSetFLOW_
const vector< Real > & expectedIM(const string &nettingSet)
Expected DIM vector by date for the specified netting set.
map< string, vector< vector< Real > > > nettingSetDIM_
QuantLib::ext::shared_ptr< NPVCube > cube_
const vector< vector< Real > > & cashFlow(const string &nettingSet)
Cash flow matrix by date and sample index for the specified netting set.
virtual Report & add(const ReportType &rt)=0
virtual Report & next()=0
virtual Report & addColumn(const string &name, const ReportType &, Size precision=0)=0
Dynamic Initial Margin calculator base class.