Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
DynamicInitialMarginCalculator Class Referenceabstract

Dynamic Initial Margin Calculator base class. More...

#include <orea/aggregation/dimcalculator.hpp>

+ Inheritance diagram for DynamicInitialMarginCalculator:
+ Collaboration diagram for DynamicInitialMarginCalculator:

Public Member Functions

 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 > &currentIM=std::map< std::string, Real >())
 
virtual ~DynamicInitialMarginCalculator ()
 
virtual map< string, Real > unscaledCurrentDIM ()=0
 Model implied t0 DIM by netting set, does not need a call to build() before. More...
 
const map< string, Real > & currentIM () const
 t0 IM by netting set, as provided as an arguments More...
 
virtual void build ()=0
 Compute dynamic initial margin along all paths and fill result structures. More...
 
virtual void exportDimEvolution (ore::data::Report &dimEvolutionReport) const
 DIM evolution report. More...
 
const QuantLib::ext::shared_ptr< NPVCube > & dimCube ()
 DIM by nettingSet, date, sample returned as a regular NPV cube. More...
 
const vector< vector< Real > > & dynamicIM (const string &nettingSet)
 DIM matrix by date and sample index for the specified netting set. More...
 
const vector< vector< Real > > & cashFlow (const string &nettingSet)
 Cash flow matrix by date and sample index for the specified netting set. More...
 
const vector< Real > & expectedIM (const string &nettingSet)
 Expected DIM vector by date for the specified netting set. More...
 
const std::map< std::string, Real > & getInitialMarginScaling ()
 Get the implied netting set specific scaling factors. More...
 

Protected Attributes

QuantLib::ext::shared_ptr< InputParametersinputs_
 
QuantLib::ext::shared_ptr< Portfolioportfolio_
 
QuantLib::ext::shared_ptr< NPVCubecube_
 
QuantLib::ext::shared_ptr< NPVCubedimCube_
 
QuantLib::ext::shared_ptr< CubeInterpretationcubeInterpretation_
 
QuantLib::ext::shared_ptr< AggregationScenarioDatascenarioData_
 
Real quantile_
 
Size horizonCalendarDays_
 
map< string, Real > currentIM_
 
bool cubeIsRegular_
 
Size datesLoopSize_
 
std::set< string > nettingSetIds_
 
map< string, Real > nettingSetScaling_
 
map< string, vector< vector< Real > > > nettingSetNPV_
 
map< string, vector< vector< Real > > > nettingSetCloseOutNPV_
 
map< string, vector< vector< Real > > > nettingSetFLOW_
 
map< string, vector< vector< Real > > > nettingSetDeltaNPV_
 
map< string, vector< vector< Real > > > nettingSetDIM_
 
map< string, vector< Real > > nettingSetExpectedDIM_
 

Detailed Description

Dynamic Initial Margin Calculator base class.

Derived classes implement a constructor with the relevant additional input data and a build function that performs the DIM calculations for all netting sets and along all paths.

Definition at line 53 of file dimcalculator.hpp.

Constructor & Destructor Documentation

◆ DynamicInitialMarginCalculator()

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 > &  currentIM = std::map<std::string, Real>() 
)
Parameters
inputsGlobal input parameters
portfolioDriving portfolio consistent with the cube below
cubeNPV cube resulting from the Monte Carlo simulation loop
cubeInterpretationInterpretation of the cube, regular NPV, MPoR grid etc
scenarioDataAdditional output of the MC simulation loop with numeraires, index fixings, FX spots etc
quantileVaR quantile, e.g. 0.99 for 99%
horizonCalendarDaysVaR holding period in calendar days
currentIMActual t0 IM by netting set used to scale the DIM evolution, no scaling if the argument is omitted

Definition at line 48 of file dimcalculator.cpp.

54 : inputs_(inputs), portfolio_(portfolio), cube_(cube), cubeInterpretation_(cubeInterpretation), scenarioData_(scenarioData),
55 quantile_(quantile), horizonCalendarDays_(horizonCalendarDays), currentIM_(currentIM) {
56
57 QL_REQUIRE(portfolio_, "portfolio is null");
58 QL_REQUIRE(cube_, "cube is null");
59 QL_REQUIRE(cubeInterpretation_, "cube interpretation is null");
60 QL_REQUIRE(scenarioData_, "aggregation scenario data is null");
61
62 cubeIsRegular_ = !cubeInterpretation_->withCloseOutLag();
63 datesLoopSize_ = cubeIsRegular_ ? cube_->dates().size() - 1 : cube_->dates().size();
64
65 Size dates = cube_->dates().size();
66 Size samples = cube_->samples();
67
68 if (!cubeInterpretation_->storeFlows()) {
69 WLOG("cube holds no mpor flows, will assume no flows in the dim calculation");
70 }
71
72 // initialise aggregate NPV and Flow by date and scenario
73 set<string> nettingSets;
74 size_t i = 0;
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));
82 nettingSetCloseOutNPV_[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));
86 nettingSetExpectedDIM_[nettingSetId] = vector<Real>(dates, 0.0);
87 }
88 for (Size j = 0; j < datesLoopSize_; ++j) {
89 for (Size k = 0; k < samples; ++k) {
90 Real defaultNpv = cubeInterpretation_->getDefaultNpv(cube_, i, j, k);
91 Real closeOutNpv = cubeInterpretation_->getCloseOutNpv(cube_, i, j, k);
92 Real mporFlow =
93 cubeInterpretation_->storeFlows() ? cubeInterpretation_->getMporFlows(cube_, i, j, k) : 0.0;
94 nettingSetNPV_[nettingSetId][j][k] += defaultNpv;
95 nettingSetCloseOutNPV_[nettingSetId][j][k] += closeOutNpv;
96 nettingSetFLOW_[nettingSetId][j][k] += mporFlow;
97 }
98 }
99 }
100
101
102 nettingSetIds_ = std::move(nettingSets);
103
104 dimCube_ = QuantLib::ext::make_shared<SinglePrecisionInMemoryCube>(cube_->asof(), nettingSetIds_, cube_->dates(),
105 cube_->samples());
106}
map< string, vector< vector< Real > > > nettingSetNPV_
const map< string, Real > & currentIM() const
t0 IM by netting set, as provided as an arguments
map< string, vector< vector< Real > > > nettingSetCloseOutNPV_
map< string, vector< vector< Real > > > nettingSetDeltaNPV_
map< string, vector< Real > > nettingSetExpectedDIM_
QuantLib::ext::shared_ptr< Portfolio > portfolio_
QuantLib::ext::shared_ptr< AggregationScenarioData > scenarioData_
QuantLib::ext::shared_ptr< CubeInterpretation > cubeInterpretation_
QuantLib::ext::shared_ptr< NPVCube > dimCube_
map< string, vector< vector< Real > > > nettingSetFLOW_
QuantLib::ext::shared_ptr< InputParameters > inputs_
map< string, vector< vector< Real > > > nettingSetDIM_
QuantLib::ext::shared_ptr< NPVCube > cube_
#define WLOG(text)

◆ ~DynamicInitialMarginCalculator()

virtual ~DynamicInitialMarginCalculator ( )
virtual

Definition at line 73 of file dimcalculator.hpp.

73{}

Member Function Documentation

◆ unscaledCurrentDIM()

virtual map< string, Real > unscaledCurrentDIM ( )
pure virtual

Model implied t0 DIM by netting set, does not need a call to build() before.

Implemented in FlatDynamicInitialMarginCalculator, and RegressionDynamicInitialMarginCalculator.

◆ currentIM()

const map< string, Real > & currentIM ( ) const

t0 IM by netting set, as provided as an arguments

Definition at line 79 of file dimcalculator.hpp.

79{ return currentIM_; }
+ Here is the caller graph for this function:

◆ build()

virtual void build ( )
pure virtual

Compute dynamic initial margin along all paths and fill result structures.

Implemented in FlatDynamicInitialMarginCalculator, and RegressionDynamicInitialMarginCalculator.

◆ exportDimEvolution()

void exportDimEvolution ( ore::data::Report dimEvolutionReport) const
virtual

DIM evolution report.

Reimplemented in FlatDynamicInitialMarginCalculator, and RegressionDynamicInitialMarginCalculator.

Definition at line 129 of file dimcalculator.cpp.

129 {
130
131 Size samples = dimCube_->samples();
132 Size stopDatesLoop = datesLoopSize_;
133 Date asof = cube_->asof();
134
135 dimEvolutionReport.addColumn("TimeStep", Size())
136 .addColumn("Date", Date())
137 .addColumn("DaysInPeriod", Size())
138 .addColumn("AverageDIM", Real(), 6)
139 .addColumn("AverageFLOW", Real(), 6)
140 .addColumn("NettingSet", string())
141 .addColumn("Time", Real(), 6);
142
143 for (auto [nettingSet, _] : dimCube_->idsAndIndexes()) {
144
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;
150 }
151
152 Date defaultDate = dimCube_->dates()[i];
153 Time t = ActualActual(ActualActual::ISDA).yearFraction(asof, defaultDate);
154 Size days = cubeInterpretation_->getMporCalendarDays(dimCube_, i);
155 dimEvolutionReport.next()
156 .add(i)
157 .add(defaultDate)
158 .add(days)
159 .add(nettingSetExpectedDIM_.find(nettingSet)->second[i])
160 .add(expectedFlow)
161 .add(nettingSet)
162 .add(t);
163 }
164 }
165 dimEvolutionReport.end();
166 LOG("Exporting expected DIM through time done");
167}
virtual Report & add(const ReportType &rt)=0
virtual Report & next()=0
virtual void end()=0
virtual Report & addColumn(const string &name, const ReportType &, Size precision=0)=0
#define LOG(text)
Date asof(14, Jun, 2018)
+ Here is the call graph for this function:

◆ dimCube()

const QuantLib::ext::shared_ptr< NPVCube > & dimCube ( )

DIM by nettingSet, date, sample returned as a regular NPV cube.

Definition at line 88 of file dimcalculator.hpp.

88{ return dimCube_; }

◆ dynamicIM()

const vector< vector< Real > > & dynamicIM ( const string &  nettingSet)

DIM matrix by date and sample index for the specified netting set.

Definition at line 108 of file dimcalculator.cpp.

108 {
109 if (nettingSetDIM_.find(nettingSet) != nettingSetDIM_.end())
110 return nettingSetDIM_[nettingSet];
111 else
112 QL_FAIL("netting set " << nettingSet << " not found in DIM results");
113}

◆ cashFlow()

const vector< vector< Real > > & cashFlow ( const string &  nettingSet)

Cash flow matrix by date and sample index for the specified netting set.

Definition at line 122 of file dimcalculator.cpp.

122 {
123 if (nettingSetFLOW_.find(nettingSet) != nettingSetFLOW_.end())
124 return nettingSetFLOW_[nettingSet];
125 else
126 QL_FAIL("netting set " << nettingSet << " not found in DIM results");
127}

◆ expectedIM()

const vector< Real > & expectedIM ( const string &  nettingSet)

Expected DIM vector by date for the specified netting set.

Definition at line 115 of file dimcalculator.cpp.

115 {
116 if (nettingSetExpectedDIM_.find(nettingSet) != nettingSetExpectedDIM_.end())
117 return nettingSetExpectedDIM_[nettingSet];
118 else
119 QL_FAIL("netting set " << nettingSet << " not found in expected DIM results");
120}

◆ getInitialMarginScaling()

const std::map< std::string, Real > & getInitialMarginScaling ( )

Get the implied netting set specific scaling factors.

Definition at line 100 of file dimcalculator.hpp.

Member Data Documentation

◆ inputs_

QuantLib::ext::shared_ptr<InputParameters> inputs_
protected

Definition at line 103 of file dimcalculator.hpp.

◆ portfolio_

QuantLib::ext::shared_ptr<Portfolio> portfolio_
protected

Definition at line 104 of file dimcalculator.hpp.

◆ cube_

QuantLib::ext::shared_ptr<NPVCube> cube_
protected

Definition at line 105 of file dimcalculator.hpp.

◆ dimCube_

QuantLib::ext::shared_ptr<NPVCube> dimCube_
protected

Definition at line 105 of file dimcalculator.hpp.

◆ cubeInterpretation_

QuantLib::ext::shared_ptr<CubeInterpretation> cubeInterpretation_
protected

Definition at line 106 of file dimcalculator.hpp.

◆ scenarioData_

QuantLib::ext::shared_ptr<AggregationScenarioData> scenarioData_
protected

Definition at line 107 of file dimcalculator.hpp.

◆ quantile_

Real quantile_
protected

Definition at line 108 of file dimcalculator.hpp.

◆ horizonCalendarDays_

Size horizonCalendarDays_
protected

Definition at line 109 of file dimcalculator.hpp.

◆ currentIM_

map<string, Real> currentIM_
protected

Definition at line 110 of file dimcalculator.hpp.

◆ cubeIsRegular_

bool cubeIsRegular_
protected

Definition at line 112 of file dimcalculator.hpp.

◆ datesLoopSize_

Size datesLoopSize_
protected

Definition at line 113 of file dimcalculator.hpp.

◆ nettingSetIds_

std::set<string> nettingSetIds_
protected

Definition at line 114 of file dimcalculator.hpp.

◆ nettingSetScaling_

map<string, Real> nettingSetScaling_
protected

Definition at line 115 of file dimcalculator.hpp.

◆ nettingSetNPV_

map<string, vector<vector<Real> > > nettingSetNPV_
protected

Definition at line 118 of file dimcalculator.hpp.

◆ nettingSetCloseOutNPV_

map<string, vector<vector<Real> > > nettingSetCloseOutNPV_
protected

Definition at line 119 of file dimcalculator.hpp.

◆ nettingSetFLOW_

map<string, vector<vector<Real> > > nettingSetFLOW_
protected

Definition at line 120 of file dimcalculator.hpp.

◆ nettingSetDeltaNPV_

map<string, vector<vector<Real> > > nettingSetDeltaNPV_
protected

Definition at line 121 of file dimcalculator.hpp.

◆ nettingSetDIM_

map<string, vector<vector<Real> > > nettingSetDIM_
protected

Definition at line 122 of file dimcalculator.hpp.

◆ nettingSetExpectedDIM_

map<string, vector<Real> > nettingSetExpectedDIM_
protected

Definition at line 125 of file dimcalculator.hpp.