Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
dimflatcalculator.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2024 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
21#include <ql/errors.hpp>
22#include <ql/time/calendars/weekendsonly.hpp>
23#include <ql/time/daycounters/actualactual.hpp>
24#include <ql/version.hpp>
25
26using namespace std;
27using namespace QuantLib;
28
29namespace ore {
30namespace analytics {
31
33 const QuantLib::ext::shared_ptr<InputParameters>& inputs,
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<AggregationScenarioData>& scenarioData)
37 : DynamicInitialMarginCalculator(inputs, portfolio, cube, cubeInterpretation, scenarioData) {
38}
39
40
41const vector<Real>& FlatDynamicInitialMarginCalculator::dimResults(const std::string& nettingSet) const {
42 auto it = nettingSetExpectedDIM_.find(nettingSet);
43 if (it != nettingSetExpectedDIM_.end())
44 return it->second;
45 else
46 QL_FAIL("netting set " << nettingSet << " not found in expected DIM results");
47}
48
50 LOG("FlatDynamicInitialMarginCalculator:build() called");
51
52 Size samples = cube_->samples();
53
54 if (!inputs_->collateralBalances()) {
55 ALOG("collateral balances not set");
56 }
57
58 for (const auto& n : nettingSetIds_) {
59 LOG("Process netting set " << n);
60
61 Real currentIM = 0;
62 if (inputs_->collateralBalances() && inputs_->collateralBalances()->has(n)) {
63 currentIM = inputs_->collateralBalances()->get(n)->initialMargin();
64 LOG("Found initial margin balance " << currentIM << " for netting set " << n);
65 }
66
67 for (Size j = 0; j < cube_->dates().size(); ++j) {
69 for (Size k = 0; k < samples; ++k)
70 nettingSetDIM_[n][j][k] = currentIM;
71 }
72 }
73 LOG("DIM by flat extraplation of initial IM done");
74}
75
77
78 // Size samples = dimCube_->samples();
79 Size stopDatesLoop = datesLoopSize_;
80 Date asof = cube_->asof();
81
82 dimEvolutionReport.addColumn("TimeStep", Size())
83 .addColumn("Date", Date())
84 .addColumn("DaysInPeriod", Size())
85 .addColumn("ZeroOrderDIM", Real(), 6)
86 .addColumn("AverageDIM", Real(), 6)
87 .addColumn("AverageFLOW", Real(), 6)
88 .addColumn("SimpleDIM", Real(), 6)
89 .addColumn("NettingSet", string())
90 .addColumn("Time", Real(), 6);
91
92 for (const auto& [nettingSet, _] : dimCube_->idsAndIndexes()) {
93
94 LOG("Export DIM evolution for netting set " << nettingSet);
95 for (Size i = 0; i < stopDatesLoop; ++i) {
96 Date defaultDate = dimCube_->dates()[i];
97 Time t = ActualActual(ActualActual::ISDA).yearFraction(asof, defaultDate);
98 Size days = cubeInterpretation_->getMporCalendarDays(dimCube_, i);
99 auto it = nettingSetExpectedDIM_.find(nettingSet);
100 Real dim = it->second.at(i);
101 dimEvolutionReport.next()
102 .add(i)
103 .add(defaultDate)
104 .add(days)
105 .add(dim)
106 .add(dim)
107 .add(0.0)
108 .add(dim)
109 .add(nettingSet)
110 .add(t);
111 }
112 }
113 dimEvolutionReport.end();
114 LOG("Exporting expected DIM through time done");
115}
116
117} // namespace analytics
118} // namespace ore
Dynamic Initial Margin Calculator base class.
const map< string, Real > & currentIM() const
t0 IM by netting set, as provided as an arguments
map< string, vector< Real > > nettingSetExpectedDIM_
QuantLib::ext::shared_ptr< CubeInterpretation > cubeInterpretation_
QuantLib::ext::shared_ptr< NPVCube > dimCube_
QuantLib::ext::shared_ptr< InputParameters > inputs_
map< string, vector< vector< Real > > > nettingSetDIM_
QuantLib::ext::shared_ptr< NPVCube > cube_
FlatDynamicInitialMarginCalculator(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)
void exportDimEvolution(ore::data::Report &dimEvolutionReport) const override
DIM evolution report.
const vector< Real > & dimResults(const std::string &nettingSet) const
void build() override
Compute dynamic initial margin along all paths and fill result structures.
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)
#define ALOG(text)
Size size(const ValueType &v)
Date asof(14, Jun, 2018)