38 const std::set<std::string>& runTypes) {
42 LOG(
"Running XVA_SENSITIVITY analytic.");
44 Settings::instance().evaluationDate() =
inputs_->asof();
46 QL_REQUIRE(
inputs_->portfolio(),
"XvaSensitivityAnalytic::run: No portfolio loaded.");
48 Settings::instance().evaluationDate() =
inputs_->asof();
49 std::string marketConfig =
inputs_->marketConfig(
"pricing");
51 auto xvaAnalytic = dependentAnalytic<XvaAnalytic>(
"XVA");
55 CONSOLEW(
"XVA_SENSI: Build T0 and Sim Markets and Stress Scenario Generator");
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);
66 LOG(
"XvaSensitivityAnalytic: Build SensitivityScenarioGenerator")
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;
79 CONSOLE(
"XVA_SENSI: Running sensi scenarios");
82 LOG(
"Run XVA Sensitivity")
85 LOG(
"Running XVA Sensitivity analytic finished.");
89 const QuantLib::ext::shared_ptr<SensitivityScenarioGenerator>& scenarioGenerator,
90 const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader) {
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>();
100 double shiftSize1 = 0.0;
101 auto itShiftSize1 = scenarioGenerator->shiftSizes().find(desc.key1());
102 if (itShiftSize1 != scenarioGenerator->shiftSizes().end()){
103 shiftSize1 = (itShiftSize1->second);
105 double shiftSize2 = 0.0;
106 auto itShiftSize2 = scenarioGenerator->shiftSizes().find(desc.key2());
107 if (itShiftSize2 != scenarioGenerator->shiftSizes().end()) {
108 shiftSize2 = (itShiftSize2->second);
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());
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());
128 DLOG(
"Calculate XVA for scenario " <<
label);
130 auto newAnalytic = ext::make_shared<XvaAnalytic>(
132 (
label ==
"BASE" ?
nullptr :
analytic()->configurations().simMarketParams));
133 CONSOLE(
"XVA_SENSITIVITY: Calculate Exposure and XVA")
134 newAnalytic->runAnalytic(loader, {
"EXPOSURE",
"XVA"});
136 for (
auto& [
name, rpt] : newAnalytic->reports()[
"XVA"]) {
138 if (boost::starts_with(
name,
"exposure") || boost::starts_with(
name,
"xva")) {
139 DLOG(
"Save and extend report " <<
name);
144 }
catch (
const std::exception& e) {
146 "Error during XVA calc under scenario " +
label +
", got " + e.what() +
151 for (
auto& [
name, reports] : xvaReports) {
153 if (report !=
nullptr) {
168 impl()->addDependentAnalytic(
"XVA", QuantLib::ext::make_shared<XvaAnalytic>(inputs));
Analytic * analytic() const
void setLabel(const string &label)
const std::string & label() const
QuantLib::ext::shared_ptr< InputParameters > inputs_
analytic_reports & reports()
Result reports.
Configurations & configurations()
virtual void buildMarket(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const bool marketRequired=true)
XvaSensitivityAnalytic(const QuantLib::ext::shared_ptr< InputParameters > &inputs)
void setUpConfigurations() override
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)
static constexpr const char * LABEL
factory class for cloning a cached scenario
load / save cubes and agg scen data from / to disk
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
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > todaysMarketParams
QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > simMarketParams
Structured analytics error.
Class for structured analytics warnings.