Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
zerotoparshiftanalytic.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
25
26using namespace ore::data;
27using namespace boost::filesystem;
28
29namespace ore {
30namespace analytics {
31
33 const auto stressData = inputs_->stressScenarioData();
36 analytic()->configurations().todaysMarketParams = inputs_->todaysMarketParams();
37 analytic()->configurations().simMarketParams = inputs_->zeroToParShiftSimMarketParams();
38 analytic()->configurations().sensiScenarioData = inputs_->zeroToParShiftSensitivityScenarioData();
40}
41
42void ZeroToParShiftAnalyticImpl::runAnalytic(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
43 const std::set<std::string>& runTypes) {
44 if (!analytic()->match(runTypes))
45 return;
46
47 LOG("StressTestAnalytic::runAnalytic called");
48
49 Settings::instance().evaluationDate() = inputs_->asof();
50 ObservationMode::instance().setMode(inputs_->observationModel());
51
52 CONSOLEW("StressTestAnalytic: Build Market");
53 analytic()->buildMarket(loader);
54 CONSOLE("OK");
55
56 auto market = analytic()->market();
57 auto stressData = inputs_->zeroToParShiftScenarioData();
58 auto simMarketData = analytic()->configurations().simMarketParams;
60 auto todaysMarketParam = analytic()->configurations().todaysMarketParams;
61 auto sensiScenarioData = analytic()->configurations().sensiScenarioData;
62 Date asof = market->asofDate();
63
64 // reuse the converter to generate the simmarket and the par instruments, dont need to run full par sensi analysis;
65 ParStressTestConverter converter(inputs_->asof(), todaysMarketParam, simMarketData, sensiScenarioData, curveConfigs,
66 market, inputs_->iborFallbackConfig());
67
68 std::set<RiskFactorKey::KeyType> disabled;
69 auto [simMarket, parAnalysis] = converter.computeParSensitivity(disabled);
70 auto instruments = parAnalysis->parInstruments();
71
72 DLOG("Build Stress Scenario Generator");
73
74 QuantLib::ext::shared_ptr<Scenario> baseScenario = simMarket->baseScenario();
75 auto scenarioFactory = QuantLib::ext::make_shared<CloneScenarioFactory>(baseScenario);
76 QuantLib::ext::shared_ptr<StressScenarioGenerator> scenarioGenerator =
77 QuantLib::ext::make_shared<StressScenarioGenerator>(stressData, baseScenario, simMarketData, simMarket,
78 scenarioFactory, simMarket->baseScenarioAbsolute());
79 simMarket->scenarioGenerator() = scenarioGenerator;
80
81 ZeroToParShiftConverter shiftConvert(instruments, simMarket);
82
83 QuantLib::ext::shared_ptr<InMemoryReport> report = QuantLib::ext::make_shared<InMemoryReport>();
84
85 report->addColumn("ScenarioLabel", string());
86 report->addColumn("ParKey", string());
87 report->addColumn("ParShift", double(), 6);
88
89 // Zero to par Converter
90 simMarket->reset();
91 scenarioGenerator->reset();
92 for (size_t i = 0; i < scenarioGenerator->samples(); i++) {
93 auto scenario = scenarioGenerator->next(asof);
94 auto label = scenario->label();
95 auto shifts = shiftConvert.parShifts(scenario);
96 for (const auto& [key, shift] : shifts) {
97 if (std::abs(shift) > 1e-6) {
98 report->next();
99 report->add(label);
100 report->add(ore::data::to_string(key));
101 report->add(shift);
102 }
103 }
104 }
105 analytic()->reports()[label()]["parshifts"] = report;
106 CONSOLE("OK");
107}
108
109ZeroToParShiftAnalytic::ZeroToParShiftAnalytic(const QuantLib::ext::shared_ptr<InputParameters>& inputs)
110 : Analytic(std::make_unique<ZeroToParShiftAnalyticImpl>(inputs), {"ZEROTOPARSHIFT"}, inputs, false, false, false,
111 false) {}
112} // namespace analytics
113} // 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
virtual void buildMarket(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const bool marketRequired=true)
Definition: analytic.cpp:178
const QuantLib::ext::shared_ptr< ore::data::Market > & market() const
Definition: analytic.hpp:117
std::pair< QuantLib::ext::shared_ptr< ScenarioSimMarket >, QuantLib::ext::shared_ptr< ParSensitivityAnalysis > > computeParSensitivity(const std::set< RiskFactorKey::KeyType > &typesDisabled) const
ZeroToParShiftAnalytic(const boost::shared_ptr< InputParameters > &inputs)
void runAnalytic(const boost::shared_ptr< ore::data::InMemoryLoader > &loader, const std::set< std::string > &runTypes={}) override
std::unordered_map< RiskFactorKey, double > parShifts(QuantLib::ext::shared_ptr< Scenario > scenario) const
factory class for cloning a cached scenario
#define LOG(text)
#define DLOG(text)
#define CONSOLEW(text)
#define CONSOLE(text)
std::string to_string(const LocationInfo &l)
Singleton class to hold global Observation Mode.
Convert all par shifts in a stress test to a zero shifts.
Stress scenario generation.
QuantLib::ext::shared_ptr< ore::analytics::SensitivityScenarioData > sensiScenarioData
Definition: analytic.hpp:70
QuantLib::ext::shared_ptr< CurveConfigurations > curveConfig
Definition: analytic.hpp:73
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
vector< string > curveConfigs
Date asof(14, Jun, 2018)
applies a zero scenario and return the par instrument shifts
ORE ZeroToPar Shift Analytic.