Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
cubeinterpretation.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
21#include <ql/errors.hpp>
22
23namespace ore {
24namespace analytics {
25
26CubeInterpretation::CubeInterpretation(const bool storeFlows, const bool withCloseOutLag,
27 const QuantLib::Handle<AggregationScenarioData>& aggregationScenarioData,
28 const QuantLib::ext::shared_ptr<DateGrid>& dateGrid, const Size storeCreditStateNPVs,
29 const bool flipViewXVA)
30 : storeFlows_(storeFlows), withCloseOutLag_(withCloseOutLag), aggregationScenarioData_(aggregationScenarioData),
31 dateGrid_(dateGrid), storeCreditStateNPVs_(storeCreditStateNPVs), flipViewXVA_(flipViewXVA) {
32
33 // determine required cube depth and layout
34
37
38 if (withCloseOutLag_) {
40 QL_REQUIRE(dateGrid_ != nullptr, "CubeInterpretation: dateGrid is required when withCloseOutLag is true");
41 }
42
43 if (storeFlows_) {
46 }
47
48 if (storeCreditStateNPVs_ > 0) {
51 }
52}
53
55
57
59
60const QuantLib::Handle<AggregationScenarioData>& CubeInterpretation::aggregationScenarioData() const {
62}
63
64const QuantLib::ext::shared_ptr<DateGrid>& CubeInterpretation::dateGrid() const { return dateGrid_; }
65
67
69
74
75Real CubeInterpretation::getGenericValue(const QuantLib::ext::shared_ptr<NPVCube>& cube, Size tradeIdx, Size dateIdx,
76 Size sampleIdx, Size depth) const {
77 if (flipViewXVA_) {
78 return -cube->get(tradeIdx, dateIdx, sampleIdx, depth);
79 } else {
80 return cube->get(tradeIdx, dateIdx, sampleIdx, depth);
81 }
82}
83
84Real CubeInterpretation::getDefaultNpv(const QuantLib::ext::shared_ptr<NPVCube>& cube, Size tradeIdx, Size dateIdx,
85 Size sampleIdx) const {
86 return getGenericValue(cube, tradeIdx, dateIdx, sampleIdx, defaultDateNpvIndex_);
87}
88
89Real CubeInterpretation::getCloseOutNpv(const QuantLib::ext::shared_ptr<NPVCube>& cube, Size tradeIdx, Size dateIdx,
90 Size sampleIdx) const {
92 return getGenericValue(cube, tradeIdx, dateIdx, sampleIdx, closeOutDateNpvIndex_) /
94 else
95 return getGenericValue(cube, tradeIdx, dateIdx + 1, sampleIdx, defaultDateNpvIndex_);
96}
97
98Real CubeInterpretation::getMporPositiveFlows(const QuantLib::ext::shared_ptr<NPVCube>& cube, Size tradeIdx, Size dateIdx,
99 Size sampleIdx) const {
100 if (mporFlowsIndex_ == QuantLib::Null<Size>())
101 return 0.0;
102 Real aggMporFlowsVal = 0.0;
103 try {
104 aggMporFlowsVal = getGenericValue(cube, tradeIdx, dateIdx, sampleIdx, mporFlowsIndex_);
105 } catch (std::exception& e) {
106 DLOG("Unable to retrieve MPOR flows for trade " << tradeIdx << ", date " << dateIdx << ", sample " << sampleIdx
107 << "; " << e.what());
108 }
109 return aggMporFlowsVal;
110}
111
112Real CubeInterpretation::getMporNegativeFlows(const QuantLib::ext::shared_ptr<NPVCube>& cube, Size tradeIdx, Size dateIdx,
113 Size sampleIdx) const {
114 if (mporFlowsIndex_ == QuantLib::Null<Size>())
115 return 0.0;
116 Real aggMporFlowsVal = 0.0;
117 try {
118 aggMporFlowsVal = getGenericValue(cube, tradeIdx, dateIdx, sampleIdx, mporFlowsIndex_ + 1);
119 } catch (std::exception& e) {
120 DLOG("Unable to retrieve MPOR flows for trade " << tradeIdx << ", date " << dateIdx << ", sample " << sampleIdx
121 << "; " << e.what());
122 }
123 return aggMporFlowsVal;
124}
125
126Real CubeInterpretation::getMporFlows(const QuantLib::ext::shared_ptr<NPVCube>& cube, Size tradeIdx, Size dateIdx,
127 Size sampleIdx) const {
128 return getMporPositiveFlows(cube, tradeIdx, dateIdx, sampleIdx) + getMporNegativeFlows(cube, tradeIdx, dateIdx, sampleIdx) ;
129}
130
132 Size sampleIdx, const std::string& qualifier) const {
133 QL_REQUIRE(!aggregationScenarioData_.empty(),
134 "CubeInterpretation::getDefaultAggregationScenarioData(): no aggregation scenario data given");
135 return aggregationScenarioData_->get(dateIdx, sampleIdx, dataType, qualifier);
136}
137
139 Size sampleIdx, const std::string& qualifier) const {
140 if (withCloseOutLag_) {
141 QL_REQUIRE(dataType == AggregationScenarioDataType::Numeraire,
142 "close out aggr scen data only available for numeraire");
143 // this is an approximation
144 return getDefaultAggregationScenarioData(dataType, dateIdx, sampleIdx, qualifier);
145 } else {
146 QL_REQUIRE(!aggregationScenarioData_.empty(), "CubeInterpretation::getCloseOutAggregationScenarioData(): no "
147 "aggregation scenario data given");
148 return aggregationScenarioData_->get(dateIdx + 1, sampleIdx, dataType, qualifier);
149 }
150}
151
152Size CubeInterpretation::getMporCalendarDays(const QuantLib::ext::shared_ptr<NPVCube>& cube, Size dateIdx) const {
153 if (withCloseOutLag_) {
154 QuantLib::Date dd = dateGrid_->valuationDates()[dateIdx];
155 QuantLib::Date cd = dateGrid_->closeOutDates()[dateIdx];
156 QL_REQUIRE(cd > dd, "close-out date (" << cd << ") must be greater than default date (" << dd << ") at index "
157 << dateIdx);
158 return (cd - dd);
159 } else {
160 return cube->dates()[dateIdx + 1] - cube->dates()[dateIdx];
161 }
162}
163
164} // namespace analytics
165} // namespace ore
Real getMporFlows(const QuantLib::ext::shared_ptr< NPVCube > &cube, Size tradeIdx, Size dateIdx, Size sampleIdx) const
Retrieve the aggregate value of Margin Period of Risk cashflows from the Cube.
Size getMporCalendarDays(const QuantLib::ext::shared_ptr< NPVCube > &cube, Size dateIdx) const
Number of Calendar Days between a given default date and corresponding close-out date.
const QuantLib::ext::shared_ptr< DateGrid > & dateGrid() const
Size defaultDateNpvIndex() const
indices in depth direction, might be Null<Size>() if not applicable
const QuantLib::Handle< AggregationScenarioData > & aggregationScenarioData() const
Real getMporPositiveFlows(const QuantLib::ext::shared_ptr< NPVCube > &cube, Size tradeIdx, Size dateIdx, Size sampleIdx) const
Retrieve the aggregate value of Margin Period of Risk positive cashflows from the Cube.
Real getDefaultAggregationScenarioData(const AggregationScenarioDataType &dataType, Size dateIdx, Size sampleIdx, const std::string &qualifier="") const
Retrieve a (default date) simulated risk factor value from AggregationScenarioData.
Real getCloseOutAggregationScenarioData(const AggregationScenarioDataType &dataType, Size dateIdx, Size sampleIdx, const std::string &qualifier="") const
Retrieve a (default date) simulated risk factor value from AggregationScenarioData.
Real getDefaultNpv(const QuantLib::ext::shared_ptr< NPVCube > &cube, Size tradeIdx, Size dateIdx, Size sampleIdx) const
Retrieve the default date NPV from the Cube.
Real getMporNegativeFlows(const QuantLib::ext::shared_ptr< NPVCube > &cube, Size tradeIdx, Size dateIdx, Size sampleIdx) const
Retrieve the aggregate value of Margin Period of Risk negative cashflows from the Cube.
CubeInterpretation(const bool storeFlows, const bool withCloseOutLag, const QuantLib::Handle< AggregationScenarioData > &aggregationScenarioData=QuantLib::Handle< AggregationScenarioData >(), const QuantLib::ext::shared_ptr< DateGrid > &dateGrid=nullptr, const Size storeCreditStateNPVs=0, const bool flipViewXVA=false)
Real getCloseOutNpv(const QuantLib::ext::shared_ptr< NPVCube > &cube, Size tradeIdx, Size dateIdx, Size sampleIdx) const
Retrieve the close-out date NPV from the Cube.
QuantLib::ext::shared_ptr< DateGrid > dateGrid_
Size requiredNpvCubeDepth() const
npv cube depth that is at least required to work with this interpretation
QuantLib::Handle< AggregationScenarioData > aggregationScenarioData_
Real getGenericValue(const QuantLib::ext::shared_ptr< NPVCube > &cube, Size tradeIdx, Size dateIdx, Size sampleIdx, Size depth) const
Retrieve an arbitrary value from the Cube (user needs to know the precise location within depth axis)
class describing the layout of an npv cube and aggregation scenario data
#define DLOG(text)