Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
staticcreditxvacalculator.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 const QuantLib::ext::shared_ptr<Portfolio> portfolio, const QuantLib::ext::shared_ptr<Market> market,
36 const string& configuration, const string& baseCurrency, const string& dvaName,
37 const string& fvaBorrowingCurve, const string& fvaLendingCurve,
38 const bool applyDynamicInitialMargin,
39 const QuantLib::ext::shared_ptr<DynamicInitialMarginCalculator> dimCalculator,
40 const QuantLib::ext::shared_ptr<NPVCube> tradeExposureCube,
41 const QuantLib::ext::shared_ptr<NPVCube> nettingSetExposureCube,
42 const Size tradeEpeIndex, const Size tradeEneIndex,
43 const Size nettingSetEpeIndex, const Size nettingSetEneIndex,
44 const bool flipViewXVA, const string& flipViewBorrowingCurvePostfix, const string& flipViewLendingCurvePostfix)
45 : ValueAdjustmentCalculator(portfolio, market, configuration, baseCurrency, dvaName,
46 fvaBorrowingCurve, fvaLendingCurve, applyDynamicInitialMargin,
47 dimCalculator, tradeExposureCube, nettingSetExposureCube, tradeEpeIndex, tradeEneIndex,
48 nettingSetEpeIndex, nettingSetEneIndex,
49 flipViewXVA, flipViewBorrowingCurvePostfix, flipViewLendingCurvePostfix) {
50 for (Size i = 0; i < dates().size(); i++) {
51 dateIndexMap_.emplace(dates()[i], i);
52 }
53}
54
55
57 const string& tid, const string& cid, const Date& d0, const Date& d1, const Real& rr) {
58 Handle<DefaultProbabilityTermStructure> dts = market_->defaultCurve(cid, configuration_)->curve();
59 QL_REQUIRE(!dts.empty(), "Default curve missing for counterparty " << cid);
60 Real increment = 0.0;
61 Real s0 = dts->survivalProbability(d0);
62 Real s1 = dts->survivalProbability(d1);
63 Real epe = tradeExposureCube_->get(tid, d1, 0, tradeEpeIndex_);
64 increment = (1.0 - rr) * (s0 - s1) * epe;
65 return increment;
66}
67
69 const string& tid, const Date& d0, const Date& d1, const Real& rr) {
70 Handle<DefaultProbabilityTermStructure> dts = market_->defaultCurve(dvaName_, configuration_)->curve();
71 QL_REQUIRE(!dts.empty(), "Default curve missing for dvaName " << dvaName_);
72 Real increment = 0.0;
73 Real s0 = dts->survivalProbability(d0);
74 Real s1 = dts->survivalProbability(d1);
75 Real ene = tradeExposureCube_->get(tid, d1, 0, tradeEneIndex_);
76 increment = (1.0 - rr) * (s0 - s1) * ene;
77 return increment;
78}
79
81 const string& nid, const string& cid, const Date& d0, const Date& d1, const Real& rr) {
82 Handle<DefaultProbabilityTermStructure> dts = market_->defaultCurve(cid, configuration_)->curve();
83 QL_REQUIRE(!dts.empty(), "Default curve missing for counterparty " << cid);
84 Real increment = 0.0;
85 Real s0 = dts->survivalProbability(d0);
86 Real s1 = dts->survivalProbability(d1);
87 Real epe = nettingSetExposureCube_->get(nid, d1, 0, nettingSetEpeIndex_);
88 increment = (1.0 - rr) * (s0 - s1) * epe;
89 return increment;
90}
91
93 const string& nid, const Date& d0, const Date& d1, const Real& rr) {
94 Handle<DefaultProbabilityTermStructure> dts = market_->defaultCurve(dvaName_, configuration_)->curve();
95 QL_REQUIRE(!dts.empty(), "Default curve missing for dvaName " << dvaName_);
96 Real increment = 0.0;
97 Real s0 = dts->survivalProbability(d0);
98 Real s1 = dts->survivalProbability(d1);
99 Real ene = nettingSetExposureCube_->get(nid, d1, 0, nettingSetEneIndex_);
100 increment = (1.0 - rr) * (s0 - s1) * ene;
101 return increment;
102}
103
105 const string& tid, const string& cid, const string& dvaName,
106 const Date& d0, const Date& d1, const Real& dcf) {
107 Handle<DefaultProbabilityTermStructure> dts_cid;
108 Handle<DefaultProbabilityTermStructure> dts_dvaName;
109 if (cid != "") {
110 dts_cid = market_->defaultCurve(cid, configuration_)->curve();
111 QL_REQUIRE(!dts_cid.empty(), "Default curve missing for counterparty " << cid);
112 }
113 if (dvaName != "") {
114 dts_dvaName = market_->defaultCurve(dvaName, configuration_)->curve();
115 QL_REQUIRE(!dts_dvaName.empty(), "Default curve missing for dvaName " << dvaName);
116 }
117
118 Real increment = 0.0;
119 Real s0 = cid == "" ? 1.0 : dts_cid->survivalProbability(d0);
120 Real s1 = dvaName == "" ? 1.0 : dts_dvaName->survivalProbability(d0);
121 Real ene = tradeExposureCube_->get(tid, d1, 0, tradeEneIndex_);
122 increment = s0 * s1 * ene * dcf;
123 return increment;
124}
125
127 const string& tid, const string& cid, const string& dvaName,
128 const Date& d0, const Date& d1, const Real& dcf) {
129 Handle<DefaultProbabilityTermStructure> dts_cid;
130 Handle<DefaultProbabilityTermStructure> dts_dvaName;
131 if (cid != "") {
132 dts_cid = market_->defaultCurve(cid, configuration_)->curve();
133 QL_REQUIRE(!dts_cid.empty(), "Default curve missing for counterparty " << cid);
134 }
135 if (dvaName != "") {
136 dts_dvaName = market_->defaultCurve(dvaName, configuration_)->curve();
137 QL_REQUIRE(!dts_dvaName.empty(), "Default curve missing for dvaName " << dvaName);
138 }
139
140 Real increment = 0.0;
141 Real s0 = cid == "" ? 1.0 : dts_cid->survivalProbability(d0);
142 Real s1 = dvaName == "" ? 1.0 : dts_dvaName->survivalProbability(d0);
143 Real epe = tradeExposureCube_->get(tid, d1, 0, tradeEpeIndex_);
144 increment = s0 * s1 * epe * dcf;
145 return increment;
146}
147
149 const string& nid, const string& cid, const string& dvaName,
150 const Date& d0, const Date& d1, const Real& dcf) {
151 Handle<DefaultProbabilityTermStructure> dts_cid;
152 Handle<DefaultProbabilityTermStructure> dts_dvaName;
153 if (cid != "") {
154 dts_cid = market_->defaultCurve(cid, configuration_)->curve();
155 QL_REQUIRE(!dts_cid.empty(), "Default curve missing for counterparty " << cid);
156 }
157 if (dvaName != "") {
158 dts_dvaName = market_->defaultCurve(dvaName, configuration_)->curve();
159 QL_REQUIRE(!dts_dvaName.empty(), "Default curve missing for dvaName " << dvaName);
160 }
161
162 Real increment = 0.0;
163 Real s0 = cid == "" ? 1.0 : dts_cid->survivalProbability(d0);
164 Real s1 = dvaName == "" ? 1.0 : dts_dvaName->survivalProbability(d0);
165 Real ene = nettingSetExposureCube_->get(nid, d1, 0, nettingSetEneIndex_);
166 increment = s0 * s1 * ene * dcf;
167 return increment;
168}
169
171 const string& nid, const string& cid, const string& dvaName,
172 const Date& d0, const Date& d1, const Real& dcf) {
173 Handle<DefaultProbabilityTermStructure> dts_cid;
174 Handle<DefaultProbabilityTermStructure> dts_dvaName;
175 if (cid != "") {
176 dts_cid = market_->defaultCurve(cid, configuration_)->curve();
177 QL_REQUIRE(!dts_cid.empty(), "Default curve missing for counterparty " << cid);
178 }
179 if (dvaName != "") {
180 dts_dvaName = market_->defaultCurve(dvaName, configuration_)->curve();
181 QL_REQUIRE(!dts_dvaName.empty(), "Default curve missing for dvaName " << dvaName);
182 }
183
184 Real increment = 0.0;
185 Real s0 = cid == "" ? 1.0 : dts_cid->survivalProbability(d0);
186 Real s1 = dvaName == "" ? 1.0 : dts_dvaName->survivalProbability(d0);
187 Real epe = nettingSetExposureCube_->get(nid, d1, 0, nettingSetEpeIndex_);
188 increment = s0 * s1 * epe * dcf;
189 return increment;
190}
191
193 const string& nid, const string& cid, const Date& d0, const Date& d1, const Real& dcf) {
194 Handle<DefaultProbabilityTermStructure> dts_cid = market_->defaultCurve(cid, configuration_)->curve();
195 QL_REQUIRE(cid == "" || !dts_cid.empty(), "Default curve missing for counterparty " << cid);
196 Handle<DefaultProbabilityTermStructure> dts_dvaName = market_->defaultCurve(dvaName_, configuration_)->curve();
197 QL_REQUIRE(dvaName_ == "" || !dts_dvaName.empty(), "Default curve missing for dvaName " << dvaName_);
198
199 Real increment = 0.0;
200 Real s0 = cid == "" ? 1.0 : dts_cid->survivalProbability(d0);
201 Real s1 = dvaName_ == "" ? 1.0 : dts_dvaName->survivalProbability(d0);
202 increment = s0 * s1 * dimCalculator_->expectedIM(nid)[dateIndexMap_[d1]] * dcf;
203 return increment;
204}
205
206} // namespace analytics
207} // namespace ore
virtual const Real calculateNettingSetDvaIncrement(const string &nid, const Date &d0, const Date &d1, const Real &rr) override
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
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
StaticCreditXvaCalculator(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 Size tradeEpeIndex=0, const Size tradeEneIndex=1, const Size nettingSetEpeIndex=1, const Size nettingSetEneIndex=2, const bool flipViewXVA=false, const string &flipViewBorrowingCurvePostfix="_BORROW", const string &flipViewLendingCurvePostfix="_LEND")
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< Market > market_
virtual const vector< Date > & dates()
QuantLib::ext::shared_ptr< DynamicInitialMarginCalculator > dimCalculator_
const QuantLib::ext::shared_ptr< NPVCube > nettingSetExposureCube_
const QuantLib::ext::shared_ptr< NPVCube > tradeExposureCube_
XVA calculator with static credit.