Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
sensitivityrunner.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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
24
25using namespace std;
26using namespace ore::data;
27
28namespace {
29
30vector<string> getFilenames(const string& fileString, const string& path) {
31 vector<string> fileNames;
32 boost::split(fileNames, fileString, boost::is_any_of(",;"), boost::token_compress_on);
33 for (auto it = fileNames.begin(); it < fileNames.end(); it++) {
34 boost::trim(*it);
35 *it = path + "/" + *it;
36 }
37 return fileNames;
38}
39
40} // anonymous namespace
41
42namespace ore {
43namespace analytics {
44
45void SensitivityRunner::runSensitivityAnalysis(QuantLib::ext::shared_ptr<Market> market,
46 const QuantLib::ext::shared_ptr<CurveConfigurations>& curveConfigs,
47 const QuantLib::ext::shared_ptr<TodaysMarketParameters>& todaysMarketParams) {
48
49 MEM_LOG;
50 LOG("Running sensitivity analysis");
51
52 QuantLib::ext::shared_ptr<ScenarioSimMarketParameters> simMarketData(new ScenarioSimMarketParameters);
53 sensiData_ = QuantLib::ext::make_shared<SensitivityScenarioData>();
54 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
55 QuantLib::ext::shared_ptr<Portfolio> sensiPortfolio = QuantLib::ext::make_shared<Portfolio>();
56 string marketConfiguration = params_->get("markets", "sensitivity");
57
58 sensiInputInitialize(simMarketData, sensiData_, engineData, sensiPortfolio);
59
60 bool recalibrateModels =
61 params_->has("sensitivity", "recalibrateModels") && parseBool(params_->get("sensitivity", "recalibrateModels"));
62
63 bool analyticFxSensis = false;
64 if (params_->has("sensitivity", "analyticFxSensis")) {
65 analyticFxSensis = parseBool(params_->get("sensitivity", "analyticFxSensis"));
66 }
67
68 QuantLib::ext::shared_ptr<SensitivityAnalysis> sensiAnalysis = QuantLib::ext::make_shared<SensitivityAnalysis>(
69 sensiPortfolio, market, marketConfiguration, engineData, simMarketData, sensiData_, recalibrateModels,
71 analyticFxSensis);
72 sensiAnalysis->generateSensitivities();
73
74 simMarket_ = sensiAnalysis->simMarket();
75
76 sensiOutputReports(sensiAnalysis);
77
78 LOG("Sensitivity analysis completed");
79 MEM_LOG;
80}
81
82void SensitivityRunner::sensiInputInitialize(QuantLib::ext::shared_ptr<ScenarioSimMarketParameters>& simMarketData,
83 QuantLib::ext::shared_ptr<SensitivityScenarioData>& sensiData,
84 QuantLib::ext::shared_ptr<EngineData>& engineData,
85 QuantLib::ext::shared_ptr<Portfolio>& sensiPortfolio) {
86
87 DLOG("sensiInputInitialize called");
88
89 LOG("Get Simulation Market Parameters");
90 string inputPath = params_->get("setup", "inputPath");
91 string marketConfigFile = inputPath + "/" + params_->get("sensitivity", "marketConfigFile");
92 simMarketData->fromFile(marketConfigFile);
93
94 LOG("Get Sensitivity Parameters");
95 string sensitivityConfigFile = inputPath + "/" + params_->get("sensitivity", "sensitivityConfigFile");
96 sensiData->fromFile(sensitivityConfigFile);
97
98 LOG("Get Engine Data");
99 string sensiPricingEnginesFile = inputPath + "/" + params_->get("sensitivity", "pricingEnginesFile");
100 engineData->fromFile(sensiPricingEnginesFile);
101
102 LOG("Get Portfolio");
103 string portfoliosString = params_->get("setup", "portfolioFile");
104 vector<string> portfolioFiles = getFilenames(portfoliosString, inputPath);
105 // Just load here. We build the portfolio in SensitivityAnalysis, after building SimMarket.
106 for (auto portfolioFile : portfolioFiles) {
107 sensiPortfolio->fromFile(portfolioFile);
108 }
109
110 DLOG("sensiInputInitialize done");
111}
112
113void SensitivityRunner::sensiOutputReports(const QuantLib::ext::shared_ptr<SensitivityAnalysis>& sensiAnalysis) {
114
115 string outputPath = params_->get("setup", "outputPath");
116 Real sensiThreshold = parseReal(params_->get("sensitivity", "outputSensitivityThreshold"));
117
118 string outputFile = outputPath + "/" + params_->get("sensitivity", "scenarioOutputFile");
119 CSVFileReport scenReport(outputFile);
120 ReportWriter().writeScenarioReport(scenReport, sensiAnalysis->sensiCubes(), sensiThreshold);
121
122 // Create a stream from the sensitivity cube
123 auto baseCurrency = sensiAnalysis->simMarketData()->baseCcy();
124 auto ss = QuantLib::ext::make_shared<SensitivityCubeStream>(sensiAnalysis->sensiCube(), baseCurrency);
125
126 Size outputPrecision = 2;
127 if (params_->has("sensitivity", "outputPrecision")) {
128 outputPrecision = parseInteger(params_->get("sensitivity", "outputPrecision"));
129 }
130
131 outputFile = outputPath + "/" + params_->get("sensitivity", "sensitivityOutputFile");
132 CSVFileReport sensiReport(outputFile);
133 ReportWriter().writeSensitivityReport(sensiReport, ss, sensiThreshold, outputPrecision);
134
135 CSVFileReport pricingStatsReport(params_->get("setup", "outputPath") + "/pricingstats_sensi.csv");
136 ore::analytics::ReportWriter().writePricingStats(pricingStatsReport, sensiAnalysis->portfolio());
137}
138
139} // namespace analytics
140} // namespace ore
Write ORE outputs to reports.
virtual void writeSensitivityReport(ore::data::Report &report, const QuantLib::ext::shared_ptr< SensitivityStream > &ss, QuantLib::Real outputThreshold=0.0, QuantLib::Size outputPrecision=2)
virtual void writeScenarioReport(ore::data::Report &report, const std::vector< QuantLib::ext::shared_ptr< SensitivityCube > > &sensitivityCubes, QuantLib::Real outputThreshold=0.0)
virtual void writePricingStats(ore::data::Report &report, const QuantLib::ext::shared_ptr< Portfolio > &portfolio)
QuantLib::ext::shared_ptr< SensitivityScenarioData > sensiData_
Sensitivity configuration data used for the sensitivity run.
QuantLib::ext::shared_ptr< ScenarioSimMarket > simMarket_
Scenario simulation market that is bumped for the sensitivity run.
QuantLib::ext::shared_ptr< Parameters > params_
virtual void sensiInputInitialize(QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > &simMarketData, QuantLib::ext::shared_ptr< SensitivityScenarioData > &sensiData, QuantLib::ext::shared_ptr< EngineData > &engineData, QuantLib::ext::shared_ptr< Portfolio > &sensiPortfolio)
Initialize input parameters to the sensitivities analysis.
QuantLib::ext::shared_ptr< ore::data::ReferenceDataManager > referenceData_
virtual void runSensitivityAnalysis(QuantLib::ext::shared_ptr< ore::data::Market > market, const QuantLib::ext::shared_ptr< ore::data::CurveConfigurations > &curveConfigs, const QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > &todaysMarketParams)
virtual void sensiOutputReports(const QuantLib::ext::shared_ptr< SensitivityAnalysis > &sensiAnalysis)
Write out some standard sensitivities reports.
const QuantLib::ext::shared_ptr< SensitivityScenarioData > & sensiData() const
bool parseBool(const string &s)
Real parseReal(const string &s)
Integer parseInteger(const string &s)
#define MEM_LOG
#define LOG(text)
#define DLOG(text)
A Class to write ORE outputs to reports.
Class for streaming SensitivityRecords from a SensitivityCube.
A class to run the sensitivity analysis.
vector< string > curveConfigs