Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
stresstestanalytic.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
27
28using namespace ore::data;
29using namespace boost::filesystem;
30
31namespace ore {
32namespace analytics {
33
35 const auto stressData = inputs_->stressScenarioData();
37 if (stressData != nullptr) {
38 analytic()->configurations().sensitivityConfigRequired = stressData->hasScenarioWithParShifts();
39 } else {
41 }
42 analytic()->configurations().todaysMarketParams = inputs_->todaysMarketParams();
43 analytic()->configurations().simMarketParams = inputs_->stressSimMarketParams();
44 analytic()->configurations().sensiScenarioData = inputs_->stressSensitivityScenarioData();
46}
47
48void StressTestAnalyticImpl::runAnalytic(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
49 const std::set<std::string>& runTypes) {
50 if (!analytic()->match(runTypes))
51 return;
52
53 LOG("StressTestAnalytic::runAnalytic called");
54
55 Settings::instance().evaluationDate() = inputs_->asof();
56 ObservationMode::instance().setMode(inputs_->observationModel());
57 QL_REQUIRE(inputs_->portfolio(), "StressTestAnalytic::run: No portfolio loaded.");
58
59 CONSOLEW("StressTestAnalytic: Build Market");
60 analytic()->buildMarket(loader);
61 CONSOLE("OK");
62
63 CONSOLEW("StressTestAnalytic: Build Portfolio");
65 CONSOLE("OK");
66 QuantLib::ext::shared_ptr<InMemoryReport> report = QuantLib::ext::make_shared<InMemoryReport>();
67 // This hook allows modifying the portfolio in derived classes before running the analytics below,
68 // e.g. to apply SIMM exemptions.
70 CONSOLEW("Risk: Stress Test Report");
71 LOG("Stress Test Analysis called");
72 QuantLib::ext::shared_ptr<StressTestScenarioData> scenarioData = inputs_->stressScenarioData();
73 if (scenarioData != nullptr && scenarioData->hasScenarioWithParShifts()) {
74 try{
75 ParStressTestConverter converter(
76 inputs_->asof(), analytic()->configurations().todaysMarketParams,
77 analytic()->configurations().simMarketParams, analytic()->configurations().sensiScenarioData,
78 analytic()->configurations().curveConfig, analytic()->market(), inputs_->iborFallbackConfig());
79 scenarioData = converter.convertStressScenarioData(scenarioData);
80 analytic()->stressTests()[label()]["stress_ZeroStressData"] = scenarioData;
81 } catch(const std::exception& e){
82 StructuredAnalyticsErrorMessage(label(), "ParConversionFailed", e.what()).log();
83 }
84 }
85
86 Settings::instance().evaluationDate() = inputs_->asof();
87
88 std::string marketConfig = inputs_->marketConfig("pricing");
89 std::vector<QuantLib::ext::shared_ptr<ore::data::EngineBuilder>> extraEngineBuilders;
90 std::vector<QuantLib::ext::shared_ptr<ore::data::LegBuilder>> extraLegBuilders;
91 QuantLib::ext::shared_ptr<StressTest> stressTest = QuantLib::ext::make_shared<StressTest>(
92 analytic()->portfolio(), analytic()->market(), marketConfig, inputs_->pricingEngine(),
93 analytic()->configurations().simMarketParams, scenarioData, *analytic()->configurations().curveConfig,
94 *analytic()->configurations().todaysMarketParams, nullptr, inputs_->refDataManager(),
95 *inputs_->iborFallbackConfig(), inputs_->continueOnError());
96 stressTest->writeReport(report, inputs_->stressThreshold());
97 analytic()->reports()[label()]["stress"] = report;
98 CONSOLE("OK");
99}
100
101StressTestAnalytic::StressTestAnalytic(const QuantLib::ext::shared_ptr<InputParameters>& inputs)
102 : Analytic(std::make_unique<StressTestAnalyticImpl>(inputs), {"STRESS"}, inputs, false, false, false, false) {}
103} // namespace analytics
104} // namespace ore
Analytic * analytic() const
Definition: analytic.hpp:193
const std::string & label() const
Definition: analytic.hpp:190
void setGenerateAdditionalResults(const bool generateAdditionalResults)
Definition: analytic.hpp:197
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
analytic_stresstests & stressTests()
Definition: analytic.hpp:134
virtual void modifyPortfolio()
Definition: analytic.hpp:110
virtual void buildMarket(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const bool marketRequired=true)
Definition: analytic.cpp:178
virtual void buildPortfolio()
Definition: analytic.cpp:222
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.
StressTestAnalytic(const boost::shared_ptr< InputParameters > &inputs)
void runAnalytic(const boost::shared_ptr< ore::data::InMemoryLoader > &loader, const std::set< std::string > &runTypes={}) override
#define LOG(text)
#define CONSOLEW(text)
#define CONSOLE(text)
Singleton class to hold global Observation Mode.
Perfrom sensitivity analysis for a given portfolio.
Convert all par shifts in a stress test to a zero shifts.
A Class to write ORE outputs to reports.
A Market class that can be updated by Scenarios.
perform a stress testing analysis for a given portfolio.
ORE Stresstest Analytic.
QuantLib::ext::shared_ptr< ore::analytics::SensitivityScenarioData > sensiScenarioData
Definition: analytic.hpp:70
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > todaysMarketParams
Definition: analytic.hpp:68
bool simulationConfigRequired
Booleans to determine if these configs are needed.
Definition: analytic.hpp:64
QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > simMarketParams
Definition: analytic.hpp:69
Structured analytics error.