Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
StressTest Class Reference

Stress Test Analysis. More...

#include <orea/engine/stresstest.hpp>

+ Collaboration diagram for StressTest:

Public Member Functions

 StressTest (const QuantLib::ext::shared_ptr< ore::data::Portfolio > &portfolio, const QuantLib::ext::shared_ptr< ore::data::Market > &market, const string &marketConfiguration, const QuantLib::ext::shared_ptr< ore::data::EngineData > &engineData, const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > &simMarketData, const QuantLib::ext::shared_ptr< StressTestScenarioData > &stressData, const ore::data::CurveConfigurations &curveConfigs=ore::data::CurveConfigurations(), const ore::data::TodaysMarketParameters &todaysMarketParams=ore::data::TodaysMarketParameters(), QuantLib::ext::shared_ptr< ScenarioFactory > scenarioFactory={}, const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceData=nullptr, const IborFallbackConfig &iborFallbackConfig=IborFallbackConfig::defaultConfig(), bool continueOnError=false)
 Constructor. More...
 
const std::set< std::string > & trades ()
 Return set of trades analysed. More...
 
const std::set< std::string > & stressTests ()
 Return unique set of factors shifted. More...
 
const std::map< std::string, Real > & baseNPV ()
 Return base NPV by trade, before shift. More...
 
const std::map< std::pair< std::string, std::string >, Real > & shiftedNPV ()
 Return shifted NPVs by trade and scenario. More...
 
const std::map< std::pair< std::string, std::string >, Real > & delta ()
 Return delta NPV by trade and scenario. More...
 
void writeReport (const QuantLib::ext::shared_ptr< ore::data::Report > &report, Real outputThreshold=0.0)
 Write NPV by trade/scenario to a file (base and shifted NPVs, delta) More...
 

Private Attributes

std::map< std::string, Real > baseNPV_
 
std::map< std::pair< string, string >, Real > shiftedNPV_
 
std::map< std::pair< string, string >, Real > delta_
 
std::set< std::string > labels_
 
std::set< std::string > trades_
 

Detailed Description

Stress Test Analysis.

This class wraps functionality to perform a stress testing analysis for a given portfolio. It comprises

Definition at line 55 of file stresstest.hpp.

Constructor & Destructor Documentation

◆ StressTest()

StressTest ( const QuantLib::ext::shared_ptr< ore::data::Portfolio > &  portfolio,
const QuantLib::ext::shared_ptr< ore::data::Market > &  market,
const string &  marketConfiguration,
const QuantLib::ext::shared_ptr< ore::data::EngineData > &  engineData,
const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > &  simMarketData,
const QuantLib::ext::shared_ptr< StressTestScenarioData > &  stressData,
const ore::data::CurveConfigurations curveConfigs = ore::data::CurveConfigurations(),
const ore::data::TodaysMarketParameters todaysMarketParams = ore::data::TodaysMarketParameters(),
QuantLib::ext::shared_ptr< ScenarioFactory scenarioFactory = {},
const QuantLib::ext::shared_ptr< ReferenceDataManager > &  referenceData = nullptr,
const IborFallbackConfig iborFallbackConfig = IborFallbackConfig::defaultConfig(),
bool  continueOnError = false 
)

Constructor.

Definition at line 39 of file stresstest.cpp.

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 * Collect results
90 */
91 baseNPV_.clear();
92 shiftedNPV_.clear();
93 delta_.clear();
94 labels_.clear();
95 trades_.clear();
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);
103 trades_.insert(tradeId);
104 baseNPV_[tradeId] = npv0;
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);
110 shiftedNPV_[p] = npv;
111 delta_[p] = npv - npv0;
112 labels_.insert(label);
113 }
114 }
115 LOG("Stress testing done");
116}
std::map< std::pair< string, string >, Real > delta_
Definition: stresstest.hpp:92
std::map< std::pair< string, string >, Real > shiftedNPV_
Definition: stresstest.hpp:92
std::set< std::string > labels_
Definition: stresstest.hpp:94
std::map< std::string, Real > baseNPV_
Definition: stresstest.hpp:90
std::set< std::string > trades_
Definition: stresstest.hpp:94
#define LOG(text)
#define DLOG(text)
#define ALOG(text)
#define TLOG(text)
Date asof(14, Jun, 2018)
+ Here is the call graph for this function:

Member Function Documentation

◆ trades()

const std::set< std::string > & trades ( )

Return set of trades analysed.

Definition at line 71 of file stresstest.hpp.

71{ return trades_; }

◆ stressTests()

const std::set< std::string > & stressTests ( )

Return unique set of factors shifted.

Definition at line 74 of file stresstest.hpp.

74{ return labels_; }

◆ baseNPV()

const std::map< std::string, Real > & baseNPV ( )

Return base NPV by trade, before shift.

Definition at line 77 of file stresstest.hpp.

77{ return baseNPV_; }
+ Here is the caller graph for this function:

◆ shiftedNPV()

const std::map< std::pair< std::string, std::string >, Real > & shiftedNPV ( )

Return shifted NPVs by trade and scenario.

Definition at line 80 of file stresstest.hpp.

80{ return shiftedNPV_; }
+ Here is the caller graph for this function:

◆ delta()

const std::map< std::pair< std::string, std::string >, Real > & delta ( )

Return delta NPV by trade and scenario.

Definition at line 83 of file stresstest.hpp.

83{ return delta_; }

◆ writeReport()

void writeReport ( const QuantLib::ext::shared_ptr< ore::data::Report > &  report,
Real  outputThreshold = 0.0 
)

Write NPV by trade/scenario to a file (base and shifted NPVs, delta)

Definition at line 118 of file stresstest.cpp.

118 {
119
120 report->addColumn("TradeId", string());
121 report->addColumn("ScenarioLabel", string());
122 report->addColumn("Base NPV", double(), 2);
123 report->addColumn("Scenario NPV", double(), 2);
124 report->addColumn("Sensitivity", double(), 2);
125
126 for (auto const& [id, npv] : shiftedNPV_) {
127 string tradeId = id.first;
128 string factor = id.second;
129 Real base = baseNPV_[tradeId];
130 Real sensi = npv - base;
131 TLOG("Adding stress report result for tradeId '" << tradeId << "' and scenario '" << factor << ": sensi = "
132 << sensi << ", threshold = " << outputThreshold);
133 if (fabs(sensi) > outputThreshold || QuantLib::close_enough(sensi, outputThreshold)) {
134 report->next();
135 report->add(tradeId);
136 report->add(factor);
137 report->add(base);
138 report->add(npv);
139 report->add(sensi);
140 }
141 }
142
143 report->end();
144}

Member Data Documentation

◆ baseNPV_

std::map<std::string, Real> baseNPV_
private

Definition at line 90 of file stresstest.hpp.

◆ shiftedNPV_

std::map<std::pair<string, string>, Real> shiftedNPV_
private

Definition at line 92 of file stresstest.hpp.

◆ delta_

std::map<std::pair<string, string>, Real> delta_
private

Definition at line 92 of file stresstest.hpp.

◆ labels_

std::set<std::string> labels_
private

Definition at line 94 of file stresstest.hpp.

◆ trades_

std::set<std::string> trades_
private

Definition at line 94 of file stresstest.hpp.