Constructor.
47 {
48
49 LOG(
"Run Stress Test");
50 DLOG(
"Build Simulation Market");
51 QuantLib::ext::shared_ptr<ScenarioSimMarket> simMarket = QuantLib::ext::make_shared<ScenarioSimMarket>(
52 market, simMarketData, marketConfiguration, curveConfigs, todaysMarketParams, continueOnError,
53 stressData->useSpreadedTermStructures(), false, false, iborFallbackConfig, true);
54
55 DLOG(
"Build Stress Scenario Generator");
56 Date
asof = market->asofDate();
57 QuantLib::ext::shared_ptr<Scenario> baseScenario = simMarket->baseScenario();
58 scenarioFactory = scenarioFactory ? scenarioFactory : QuantLib::ext::make_shared<CloneScenarioFactory>(baseScenario);
59 QuantLib::ext::shared_ptr<StressScenarioGenerator> scenarioGenerator = QuantLib::ext::make_shared<StressScenarioGenerator>(
60 stressData, baseScenario, simMarketData, simMarket, scenarioFactory, simMarket->baseScenarioAbsolute());
61 simMarket->scenarioGenerator() = scenarioGenerator;
62
63 DLOG(
"Build Engine Factory");
64 map<MarketContext, string> configurations;
65 configurations[MarketContext::pricing] = marketConfiguration;
66 auto ed = QuantLib::ext::make_shared<EngineData>(*engineData);
67 ed->globalParameters()["RunType"] = "Stress";
68 QuantLib::ext::shared_ptr<EngineFactory> factory =
69 QuantLib::ext::make_shared<EngineFactory>(ed, simMarket, configurations, referenceData, iborFallbackConfig);
70
71 DLOG(
"Reset and Build Portfolio");
72 portfolio->reset();
73 portfolio->build(factory, "stress analysis");
74
75 DLOG(
"Build the cube object to store sensitivities");
76 QuantLib::ext::shared_ptr<NPVCube> cube = QuantLib::ext::make_shared<DoublePrecisionInMemoryCube>(
77 asof, portfolio->ids(), vector<Date>(1,
asof), scenarioGenerator->samples());
78
79 DLOG(
"Run Stress Scenarios");
80 QuantLib::ext::shared_ptr<DateGrid> dg = QuantLib::ext::make_shared<DateGrid>("1,0W", NullCalendar());
81 vector<QuantLib::ext::shared_ptr<ValuationCalculator>> calculators;
82 calculators.push_back(QuantLib::ext::make_shared<NPVCalculator>(simMarketData->baseCcy()));
83 ValuationEngine engine(
asof, dg, simMarket, factory->modelBuilders());
84
85 engine.registerProgressIndicator(QuantLib::ext::make_shared<ProgressLog>("stress scenarios", 100, oreSeverity::notice));
86 engine.buildCube(portfolio, cube, calculators);
87
88
89
90
96 for (auto const& [tradeId, trade] : portfolio->trades()) {
97 auto index = cube->idsAndIndexes().find(tradeId);
98 if (index == cube->idsAndIndexes().end()) {
99 ALOG(
"cube does not contain tradeId '" << tradeId <<
"'");
100 continue;
101 }
102 Real npv0 = cube->getT0(index->second, 0);
105 for (Size j = 0; j < scenarioGenerator->samples(); ++j) {
106 const string& label = scenarioGenerator->scenarios()[j]->label();
107 TLOG(
"Adding stress test result for trade '" << tradeId <<
"' and scenario #" << j <<
" '" << label <<
"'");
108 Real npv = cube->get(index->second, 0, j, 0);
109 pair<string, string> p(tradeId, label);
113 }
114 }
115 LOG(
"Stress testing done");
116}
std::map< std::pair< string, string >, Real > delta_
std::map< std::pair< string, string >, Real > shiftedNPV_
std::set< std::string > labels_
std::map< std::string, Real > baseNPV_
std::set< std::string > trades_