Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
xvasensitivityanalytic.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
23#include <orea/cube/cube_io.hpp>
29namespace ore {
30namespace analytics {
31
32XvaSensitivityAnalyticImpl::XvaSensitivityAnalyticImpl(const QuantLib::ext::shared_ptr<InputParameters>& inputs)
33 : Analytic::Impl(inputs) {
35}
36
37void XvaSensitivityAnalyticImpl::runAnalytic(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
38 const std::set<std::string>& runTypes) {
39
40 // basic setup
41
42 LOG("Running XVA_SENSITIVITY analytic.");
43
44 Settings::instance().evaluationDate() = inputs_->asof();
45
46 QL_REQUIRE(inputs_->portfolio(), "XvaSensitivityAnalytic::run: No portfolio loaded.");
47
48 Settings::instance().evaluationDate() = inputs_->asof();
49 std::string marketConfig = inputs_->marketConfig("pricing"); // FIXME
50
51 auto xvaAnalytic = dependentAnalytic<XvaAnalytic>("XVA");
52
53 // build t0, sim market, stress scenario generator
54
55 CONSOLEW("XVA_SENSI: Build T0 and Sim Markets and Stress Scenario Generator");
56
57 analytic()->buildMarket(loader);
58
59 LOG("XvaSensitivityAnalytic: Build SimMarket")
60 auto simMarket = QuantLib::ext::make_shared<ScenarioSimMarket>(
61 analytic()->market(), analytic()->configurations().simMarketParams, marketConfig,
62 *analytic()->configurations().curveConfig, *analytic()->configurations().todaysMarketParams,
63 inputs_->continueOnError(), analytic()->configurations().sensiScenarioData->useSpreadedTermStructures(), false,
64 false, *inputs_->iborFallbackConfig(), true);
65
66 LOG("XvaSensitivityAnalytic: Build SensitivityScenarioGenerator")
67
68 auto baseScenario = simMarket->baseScenario();
69 auto scenarioFactory = QuantLib::ext::make_shared<CloneScenarioFactory>(baseScenario);
70 auto scenarioGenerator = QuantLib::ext::make_shared<SensitivityScenarioGenerator>(
71 analytic()->configurations().sensiScenarioData, baseScenario, analytic()->configurations().simMarketParams,
72 simMarket, scenarioFactory, false);
73 simMarket->scenarioGenerator() = scenarioGenerator;
74
75 CONSOLE("OK");
76
77 // generate the stress scenarios and run dependent xva analytic under each of them
78
79 CONSOLE("XVA_SENSI: Running sensi scenarios");
80
81 // run stress test
82 LOG("Run XVA Sensitivity")
83 runSensitivity(scenarioGenerator, loader);
84
85 LOG("Running XVA Sensitivity analytic finished.");
86}
87
89 const QuantLib::ext::shared_ptr<SensitivityScenarioGenerator>& scenarioGenerator,
90 const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader) {
91
92 std::map<std::string, std::vector<QuantLib::ext::shared_ptr<ore::data::InMemoryReport>>> xvaReports;
93 for (size_t i = 0; i < scenarioGenerator->samples(); ++i) {
94 auto scenario = scenarioGenerator->next(inputs_->asof());
95 auto desc = scenarioGenerator->scenarioDescriptions()[i];
96 const auto label = scenario->label();
97 QuantLib::ext::shared_ptr<ore::data::InMemoryReport> descReport =
98 QuantLib::ext::make_shared<ore::data::InMemoryReport>();
99
100 double shiftSize1 = 0.0;
101 auto itShiftSize1 = scenarioGenerator->shiftSizes().find(desc.key1());
102 if (itShiftSize1 != scenarioGenerator->shiftSizes().end()){
103 shiftSize1 = (itShiftSize1->second);
104 }
105 double shiftSize2 = 0.0;
106 auto itShiftSize2 = scenarioGenerator->shiftSizes().find(desc.key2());
107 if (itShiftSize2 != scenarioGenerator->shiftSizes().end()) {
108 shiftSize2 = (itShiftSize2->second);
109 }
110 descReport->addColumn("Type", string());
111 descReport->addColumn("IsPar", string());
112 descReport->addColumn("Factor_1", string());
113 descReport->addColumn("ShiftSize_1", double(), 8);
114 descReport->addColumn("Factor_2", string());
115 descReport->addColumn("ShiftSize_2", double(), 8);
116 descReport->addColumn("Currency", string());
117 descReport->next();
118 descReport->add(ore::data::to_string(desc.type()));
119 descReport->add("false");
120 descReport->add(desc.factor1());
121 descReport->add(shiftSize1);
122 descReport->add(desc.factor2());
123 descReport->add(shiftSize2);
124 descReport->add(inputs_->baseCurrency());
125 descReport->end();
126
127 try {
128 DLOG("Calculate XVA for scenario " << label);
129 CONSOLE("XVA_SENSITIVITY: Apply scenario " << label);
130 auto newAnalytic = ext::make_shared<XvaAnalytic>(
131 inputs_, (label == "BASE" ? nullptr : scenario),
132 (label == "BASE" ? nullptr : analytic()->configurations().simMarketParams));
133 CONSOLE("XVA_SENSITIVITY: Calculate Exposure and XVA")
134 newAnalytic->runAnalytic(loader, {"EXPOSURE", "XVA"});
135 // Collect exposure and xva reports
136 for (auto& [name, rpt] : newAnalytic->reports()["XVA"]) {
137 // add scenario column to report and copy it, concat it later
138 if (boost::starts_with(name, "exposure") || boost::starts_with(name, "xva")) {
139 DLOG("Save and extend report " << name);
140 xvaReports[name].push_back(addColumnsToExisitingReport(descReport, rpt));
141 }
142 }
143 // writeCubes(label, newAnalytic);
144 } catch (const std::exception& e) {
145 StructuredAnalyticsErrorMessage("XvaSensitivity", "XVACalc",
146 "Error during XVA calc under scenario " + label + ", got " + e.what() +
147 ". Skip it")
148 .log();
149 }
150 }
151 for (auto& [name, reports] : xvaReports) {
152 auto report = concatenateReports(reports);
153 if (report != nullptr) {
154 analytic()->reports()[label()][name] = report;
155 }
156 }
157}
158
160 analytic()->configurations().todaysMarketParams = inputs_->todaysMarketParams();
161 analytic()->configurations().simMarketParams = inputs_->xvaSensiSimMarketParams();
162 analytic()->configurations().sensiScenarioData = inputs_->xvaSensiScenarioData();
163}
164
165XvaSensitivityAnalytic::XvaSensitivityAnalytic(const QuantLib::ext::shared_ptr<InputParameters>& inputs)
166 : Analytic(std::make_unique<XvaSensitivityAnalyticImpl>(inputs), {"XVA_SENSITIVITY"}, inputs, true, false, false,
167 false) {
168 impl()->addDependentAnalytic("XVA", QuantLib::ext::make_shared<XvaAnalytic>(inputs));
169}
170
171} // namespace analytics
172} // namespace ore
Analytic * analytic() const
Definition: analytic.hpp:193
void setLabel(const string &label)
Definition: analytic.hpp:189
const std::string & label() const
Definition: analytic.hpp:190
QuantLib::ext::shared_ptr< InputParameters > inputs_
Definition: analytic.hpp:216
analytic_reports & reports()
Result reports.
Definition: analytic.hpp:131
Configurations & configurations()
Definition: analytic.hpp:128
virtual void buildMarket(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const bool marketRequired=true)
Definition: analytic.cpp:178
XvaSensitivityAnalytic(const QuantLib::ext::shared_ptr< InputParameters > &inputs)
void runAnalytic(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const std::set< std::string > &runTypes={}) override
void runSensitivity(const QuantLib::ext::shared_ptr< SensitivityScenarioGenerator > &scenarioGenerator, const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader)
XvaSensitivityAnalyticImpl(const QuantLib::ext::shared_ptr< InputParameters > &inputs)
factory class for cloning a cached scenario
load / save cubes and agg scen data from / to disk
#define LOG(text)
#define DLOG(text)
#define CONSOLEW(text)
#define CONSOLE(text)
std::string to_string(const LocationInfo &l)
QuantLib::ext::shared_ptr< ore::data::InMemoryReport > concatenateReports(const std::vector< QuantLib::ext::shared_ptr< ore::data::InMemoryReport > > &reports)
QuantLib::ext::shared_ptr< ore::data::InMemoryReport > addColumnsToExisitingReport(const QuantLib::ext::shared_ptr< ore::data::InMemoryReport > &newColsReport, const QuantLib::ext::shared_ptr< ore::data::InMemoryReport > &report)
A Market class that can be updated by Scenarios.
Sensitivity scenario generation.
Stress scenario generation.
QuantLib::ext::shared_ptr< ore::analytics::SensitivityScenarioData > sensiScenarioData
Definition: analytic.hpp:70
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > todaysMarketParams
Definition: analytic.hpp:68
QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > simMarketParams
Definition: analytic.hpp:69
Structured analytics error.
Class for structured analytics warnings.
string name
xva sensitivity analytic