34 const QuantLib::ext::shared_ptr<XvaAnalytic>& xvaAnalytic) {
35 if (!
inputs_->xvaStressWriteCubes() || xvaAnalytic ==
nullptr) {
40 DLOG(
"Write raw cube under scenario " <<
label);
42 xvaAnalytic->reports()[
"XVA"][
"rawcube"]->toFile(
inputs_->resultsPath().string() +
"/rawcube_" +
label +
47 DLOG(
"Write raw cube under scenario " <<
label);
49 xvaAnalytic->reports()[
"XVA"][
"netcube"]->toFile(
inputs_->resultsPath().string() +
"/netcube_" +
label +
54 const auto& cubes = xvaAnalytic->npvCubes()[
"XVA"];
55 for (
const auto& [
name, cube] : cubes) {
56 DLOG(
"Write cube under scenario " <<
name <<
" for scenario" <<
label);
70 if (
inputs_->writeScenarios()) {
71 DLOG(
"Write scenario report under scenario " <<
label);
73 xvaAnalytic->reports()[
"XVA"][
"scenario"]->toFile(
inputs_->resultsPath().string() +
"/scenario" +
label +
84 const std::set<std::string>& runTypes) {
88 LOG(
"Running XVA Stress analytic.");
90 Settings::instance().evaluationDate() =
inputs_->asof();
92 QL_REQUIRE(
inputs_->portfolio(),
"XvaStressAnalytic::run: No portfolio loaded.");
94 Settings::instance().evaluationDate() =
inputs_->asof();
95 std::string marketConfig =
inputs_->marketConfig(
"pricing");
97 auto xvaAnalytic = dependentAnalytic<XvaAnalytic>(
"XVA");
101 CONSOLEW(
"XVA_STRESS: Build T0 and Sim Markets and Stress Scenario Generator");
105 QuantLib::ext::shared_ptr<StressTestScenarioData> scenarioData =
inputs_->xvaStressScenarioData();
106 if (scenarioData !=
nullptr && scenarioData->hasScenarioWithParShifts()) {
110 analytic()->configurations().simMarketParams,
analytic()->configurations().sensiScenarioData,
114 }
catch (
const std::exception& e) {
115 scenarioData =
inputs_->xvaStressScenarioData();
120 LOG(
"XVA Stress: Build SimMarket and StressTestScenarioGenerator")
121 auto simMarket = QuantLib::ext::make_shared<ScenarioSimMarket>(
122 analytic()->market(),
analytic()->configurations().simMarketParams, marketConfig,
123 *
analytic()->configurations().curveConfig, *
analytic()->configurations().todaysMarketParams,
124 inputs_->continueOnError(), scenarioData->useSpreadedTermStructures(),
false,
false,
125 *
inputs_->iborFallbackConfig(),
true);
127 auto baseScenario = simMarket->baseScenario();
128 auto scenarioFactory = QuantLib::ext::make_shared<CloneScenarioFactory>(baseScenario);
129 auto scenarioGenerator = QuantLib::ext::make_shared<StressScenarioGenerator>(
130 scenarioData, baseScenario,
analytic()->configurations().simMarketParams, simMarket, scenarioFactory,
131 simMarket->baseScenarioAbsolute());
132 simMarket->scenarioGenerator() = scenarioGenerator;
138 CONSOLE(
"XVA_STRESS: Running stress scenarios");
141 LOG(
"Run XVA Stresstest")
144 LOG(
"Running XVA Stress analytic finished.");
148 const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader) {
150 std::map<std::string, std::vector<QuantLib::ext::shared_ptr<ore::data::InMemoryReport>>> xvaReports;
151 for (
size_t i = 0; i < scenarioGenerator->samples(); ++i) {
152 auto scenario = scenarioGenerator->next(
inputs_->asof());
153 const std::string&
label = scenario !=
nullptr ? scenario->label() : std::string();
155 DLOG(
"Calculate XVA for scenario " <<
label);
157 auto newAnalytic = ext::make_shared<XvaAnalytic>(
159 (
label ==
"BASE" ?
nullptr :
analytic()->configurations().simMarketParams));
160 CONSOLE(
"XVA_STRESS: Calculate Exposure and XVA")
161 newAnalytic->runAnalytic(loader, {
"EXPOSURE",
"XVA"});
163 for (
auto& [
name, rpt] : newAnalytic->reports()[
"XVA"]) {
165 if (boost::starts_with(
name,
"exposure") || boost::starts_with(
name,
"xva")) {
166 DLOG(
"Save and extend report " <<
name);
171 }
catch (
const std::exception& e) {
173 "Error during XVA calc under scenario " +
label +
", got " + e.what() +
182 const std::map<std::string, std::vector<QuantLib::ext::shared_ptr<ore::data::InMemoryReport>>>& xvaReports) {
183 DLOG(
"Concat exposure and xva reports");
184 for (
auto& [
name, reports] : xvaReports) {
186 if (report !=
nullptr) {
200 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()
analytic_stresstests & stressTests()
virtual void buildMarket(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const bool marketRequired=true)
QuantLib::ext::shared_ptr< ore::analytics::StressTestScenarioData > convertStressScenarioData(const QuantLib::ext::shared_ptr< ore::analytics::StressTestScenarioData > &scenarioData) const
Convert all par shifts to zero shifts for all scenarios defined in the stresstest.
XvaStressAnalytic(const QuantLib::ext::shared_ptr< InputParameters > &inputs)
void runStressTest(const QuantLib::ext::shared_ptr< ore::analytics::StressScenarioGenerator > &scenarioGenerator, const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader)
void setUpConfigurations() override
void runAnalytic(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const std::set< std::string > &runTypes={}) override
void writeCubes(const std::string &label, const QuantLib::ext::shared_ptr< XvaAnalytic > &xvaAnalytic)
void concatReports(const std::map< std::string, std::vector< QuantLib::ext::shared_ptr< ore::data::InMemoryReport > > > &xvaReports)
XvaStressAnalyticImpl(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
void saveCube(const std::string &filename, const NPVCubeWithMetaData &cube, const bool doublePrecision)
QuantLib::ext::shared_ptr< ore::data::InMemoryReport > addColumnToExisitingReport(const std::string &columnName, const std::string &value, const QuantLib::ext::shared_ptr< ore::data::InMemoryReport > &report)
QuantLib::ext::shared_ptr< ore::data::InMemoryReport > concatenateReports(const std::vector< QuantLib::ext::shared_ptr< ore::data::InMemoryReport > > &reports)
Convert all par shifts in a stress test to a zero shifts.
A Market class that can be updated by Scenarios.
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.