Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
dynamiccreditxvacalculator.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 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
20
21#include <boost/accumulators/accumulators.hpp>
22#include <boost/accumulators/statistics/error_of_mean.hpp>
23#include <boost/accumulators/statistics/mean.hpp>
24#include <boost/accumulators/statistics/stats.hpp>
25
26using namespace std;
27using namespace QuantLib;
28
29using namespace boost::accumulators;
30
31namespace ore {
32namespace analytics {
33
35 //! Driving portfolio consistent with the cube below
36 const QuantLib::ext::shared_ptr<Portfolio> portfolio, const QuantLib::ext::shared_ptr<Market> market,
37 const string& configuration, const string& baseCurrency, const string& dvaName,
38 const string& fvaBorrowingCurve, const string& fvaLendingCurve,
39 const bool applyDynamicInitialMargin,
40 const QuantLib::ext::shared_ptr<DynamicInitialMarginCalculator> dimCalculator,
41 const QuantLib::ext::shared_ptr<NPVCube> tradeExposureCube,
42 const QuantLib::ext::shared_ptr<NPVCube> nettingSetExposureCube,
43 const QuantLib::ext::shared_ptr<NPVCube>& cptyCube,
44 const Size tradeEpeIndex, const Size tradeEneIndex,
45 const Size nettingSetEpeIndex, const Size nettingSetEneIndex, const Size cptySpIndex,
46 const bool flipViewXVA, const string& flipViewBorrowingCurvePostfix, const string& flipViewLendingCurvePostfix)
47 : ValueAdjustmentCalculator(portfolio, market, configuration, baseCurrency, dvaName,
48 fvaBorrowingCurve, fvaLendingCurve, applyDynamicInitialMargin,
49 dimCalculator, tradeExposureCube, nettingSetExposureCube, tradeEpeIndex, tradeEneIndex,
50 nettingSetEpeIndex, nettingSetEneIndex,
51 flipViewXVA, flipViewBorrowingCurvePostfix, flipViewLendingCurvePostfix),
52 cptyCube_(cptyCube), cptySpIndex_(cptySpIndex) {
53 // check consistency of input
54
55 QL_REQUIRE(tradeExposureCube_->numDates() == cptyCube->numDates(),
56 "number of dates in tradeExposureCube and cptyCube mismatch ("
57 << tradeExposureCube_->numDates() << " vs " << cptyCube->numDates() << ")");
58
59 QL_REQUIRE(cptySpIndex < cptyCube->depth(), "cptySpIndex("
60 << cptySpIndex << ") exceeds depth of cptyCube("
61 << cptyCube->depth() << ")");
62
63 for (Size i = 0; i < tradeExposureCube_->numDates(); i++) {
64 QL_REQUIRE(tradeExposureCube_->dates()[i] == cptyCube->dates()[i],
65 "date at " << i << " in tradeExposureCube and cptyCube mismatch ("
66 << tradeExposureCube_->dates()[i] << " vs " << cptyCube->dates()[i] << ")");
67 }
68}
69
70
72 const string& tid, const string& cid, const Date& d0, const Date& d1, const Real& rr) {
73 Real increment = 0.0;
74 for (Size k = 0; k < tradeExposureCube_->samples(); ++k) {
75 Real s0 = d0 == asof() ? 1.0 : cptyCube_->get(cid, d0, k, cptySpIndex_);
76 Real s1 = cptyCube_->get(cid, d1, k, cptySpIndex_);
77 Real epe = tradeExposureCube_->get(tid, d1, k, tradeEpeIndex_);
78 increment += (s0 - s1) * epe;
79 }
80 return (1.0 - rr) * increment / tradeExposureCube_->samples();
81}
82
84 const string& tid, const Date& d0, const Date& d1, const Real& rr) {
85 Real increment = 0.0;
86 for (Size k = 0; k < tradeExposureCube_->samples(); ++k) {
87 Real s0 = d0 == asof() ? 1.0 : cptyCube_->get(dvaName_, d0, k, cptySpIndex_);
88 Real s1 = cptyCube_->get(dvaName_, d1, k, cptySpIndex_);
89 Real ene = tradeExposureCube_->get(tid, d1, k, tradeEneIndex_);
90 increment += (s0 - s1) * ene;
91 }
92 return (1.0 - rr) * increment / tradeExposureCube_->samples();
93}
94
96 const string& nid, const string& cid, const Date& d0, const Date& d1, const Real& rr) {
97 Real increment = 0.0;
98 for (Size k = 0; k < nettingSetExposureCube_->samples(); ++k) {
99 Real s0 = d0 == asof() ? 1.0 : cptyCube_->get(cid, d0, k, cptySpIndex_);
100 Real s1 = cptyCube_->get(cid, d1, k, cptySpIndex_);
101 Real epe = nettingSetExposureCube_->get(nid, d1, k, nettingSetEpeIndex_);
102 increment += (s0 - s1) * epe;
103 }
104 return (1.0 - rr) * increment / nettingSetExposureCube_->samples();
105}
106
108 const string& nid, const Date& d0, const Date& d1, const Real& rr) {
109 Real increment = 0.0;
110 for (Size k = 0; k < nettingSetExposureCube_->samples(); ++k) {
111 Real s0 = d0 == asof() ? 1.0 : cptyCube_->get(dvaName_, d0, k, cptySpIndex_);
112 Real s1 = cptyCube_->get(dvaName_, d1, k, cptySpIndex_);
113 Real ene = nettingSetExposureCube_->get(nid, d1, k, nettingSetEneIndex_);
114 increment += (s0 - s1) * ene;
115 }
116 return (1.0 - rr) * increment / nettingSetExposureCube_->samples();
117}
118
120 const string& tid, const string& cid, const string& dvaName,
121 const Date& d0, const Date& d1, const Real& dcf) {
122 Real increment = 0.0;
123 for (Size k = 0; k < tradeExposureCube_->samples(); ++k) {
124 Real s0 = (d0 == asof() || cid == "") ? 1.0 : cptyCube_->get(cid, d0, k, cptySpIndex_);
125 Real s1 = (d0 == asof() || dvaName == "") ? 1.0 : cptyCube_->get(dvaName_, d0, k, cptySpIndex_);
126 Real ene = tradeExposureCube_->get(tid, d1, k, tradeEneIndex_);
127 increment += s0 * s1 * ene;
128 }
129 return increment * dcf / tradeExposureCube_->samples();
130}
131
133 const string& tid, const string& cid, const string& dvaName,
134 const Date& d0, const Date& d1, const Real& dcf) {
135 Real increment = 0.0;
136 for (Size k = 0; k < tradeExposureCube_->samples(); ++k) {
137 Real s0 = (d0 == asof() || cid == "") ? 1.0 : cptyCube_->get(cid, d0, k, cptySpIndex_);
138 Real s1 = (d0 == asof() || dvaName == "") ? 1.0 : cptyCube_->get(dvaName_, d0, k, cptySpIndex_);
139 Real epe = tradeExposureCube_->get(tid, d1, k, tradeEpeIndex_);
140 increment += s0 * s1 * epe;
141 }
142 return increment * dcf / tradeExposureCube_->samples();
143}
144
146 const string& nid, const string& cid, const string& dvaName,
147 const Date& d0, const Date& d1, const Real& dcf) {
148 Real increment = 0.0;
149 for (Size k = 0; k < nettingSetExposureCube_->samples(); ++k) {
150 Real s0 = (d0 == asof() || cid == "") ? 1.0 : cptyCube_->get(cid, d0, k, cptySpIndex_);
151 Real s1 = (d0 == asof() || dvaName == "") ? 1.0 : cptyCube_->get(dvaName_, d0, k, cptySpIndex_);
152 Real ene = nettingSetExposureCube_->get(nid, d1, k, nettingSetEneIndex_);
153 increment += s0 * s1 * ene;
154 }
155 return increment * dcf / nettingSetExposureCube_->samples();
156}
157
159 const string& nid, const string& cid, const string& dvaName,
160 const Date& d0, const Date& d1, const Real& dcf) {
161 Real increment = 0.0;
162 for (Size k = 0; k < nettingSetExposureCube_->samples(); ++k) {
163 Real s0 = (d0 == asof() || cid == "") ? 1.0 : cptyCube_->get(cid, d0, k, cptySpIndex_);
164 Real s1 = (d0 == asof() || dvaName == "") ? 1.0 : cptyCube_->get(dvaName_, d0, k, cptySpIndex_);
165 Real epe = nettingSetExposureCube_->get(nid, d1, k, nettingSetEpeIndex_);
166 increment += s0 * s1 * epe;
167 }
168 return increment * dcf / nettingSetExposureCube_->samples();
169}
170
172 const string& nid, const string& cid, const Date& d0, const Date& d1, const Real& dcf) {
173
174 Real increment = 0.0;
175 for (Size k = 0; k < nettingSetExposureCube_->samples(); ++k) {
176 Real s0 = (d0 == asof() || cid == "") ? 1.0 : cptyCube_->get(cid, d0, k, cptySpIndex_);
177 Real s1 = (d0 == asof() || dvaName_ == "") ? 1.0 : cptyCube_->get(dvaName_, d0, k, cptySpIndex_);
178 Real im = dimCalculator_->dimCube()->get(nid, d1, k);
179 increment += s0 * s1 * im;
180 }
181 return increment * dcf / nettingSetExposureCube_->samples();
182}
183
184} // namespace analytics
185} // namespace ore
virtual const Real calculateNettingSetDvaIncrement(const string &nid, const Date &d0, const Date &d1, const Real &rr) override
const QuantLib::ext::shared_ptr< NPVCube > & cptyCube_
virtual const Real calculateNettingSetFcaIncrement(const string &nid, const string &cid, const string &dvaName, const Date &d0, const Date &d1, const Real &dcf) override
virtual const Real calculateFbaIncrement(const string &tid, const string &cid, const string &dvaName, const Date &d0, const Date &d1, const Real &dcf) override
virtual const Real calculateNettingSetMvaIncrement(const string &nid, const string &cid, const Date &d0, const Date &d1, const Real &dcf) override
virtual const Real calculateCvaIncrement(const string &tid, const string &cid, const Date &d0, const Date &d1, const Real &rr) override
DynamicCreditXvaCalculator(const QuantLib::ext::shared_ptr< Portfolio > portfolio, const QuantLib::ext::shared_ptr< Market > market, const string &configuration, const string &baseCurrency, const string &dvaName, const string &fvaBorrowingCurve, const string &fvaLendingCurve, const bool applyDynamicInitialMargin, const QuantLib::ext::shared_ptr< DynamicInitialMarginCalculator > dimCalculator, const QuantLib::ext::shared_ptr< NPVCube > tradeExposureCube, const QuantLib::ext::shared_ptr< NPVCube > nettingSetExposureCube, const QuantLib::ext::shared_ptr< NPVCube > &cptyCube, const Size tradeEpeIndex=0, const Size tradeEneIndex=1, const Size nettingSetEpeIndex=0, const Size nettingSetEneIndex=1, const Size cptySpIndex=0, const bool flipViewXVA=false, const string &flipViewBorrowingCurvePostfix="_BORROW", const string &flipViewLendingCurvePostfix="_LEND")
virtual const Real calculateDvaIncrement(const string &tid, const Date &d0, const Date &d1, const Real &rr) override
virtual const Real calculateNettingSetCvaIncrement(const string &nid, const string &cid, const Date &d0, const Date &d1, const Real &rr) override
virtual const Real calculateNettingSetFbaIncrement(const string &nid, const string &cid, const string &dvaName, const Date &d0, const Date &d1, const Real &dcf) override
virtual const Real calculateFcaIncrement(const string &tid, const string &cid, const string &dvaName, const Date &d0, const Date &d1, const Real &dcf) override
QuantLib::ext::shared_ptr< DynamicInitialMarginCalculator > dimCalculator_
const QuantLib::ext::shared_ptr< NPVCube > nettingSetExposureCube_
const QuantLib::ext::shared_ptr< NPVCube > tradeExposureCube_
XVA calculator with dynamic credit.