Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
sensitivityanalysis.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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
19/*! \file engine/sensitivityanalysis.hpp
20 \brief Perform sensitivity analysis for a given portfolio
21 \ingroup simulation
22*/
23
24#pragma once
25
32
39
40#include <map>
41#include <set>
42#include <tuple>
43
44namespace ore {
45namespace analytics {
46
47class ScenarioFactory;
48class ValuationCalculator;
49
50//! Sensitivity Analysis
51/*!
52 This class wraps functionality to perform a sensitivity analysis for a given portfolio.
53 It comprises
54 - building the "simulation" market to which sensitivity scenarios are applied
55 - building the portfolio linked to this simulation market
56 - generating sensitivity scenarios
57 - running the scenario "engine" to apply these and compute the NPV impacts of all required shifts
58 - compile first and second order sensitivities for all factors and all trades
59 - fill result structures that can be queried
60
61 \ingroup simulation
62*/
63
65public:
66 //! Constructor using single-threaded engine
67 SensitivityAnalysis(const QuantLib::ext::shared_ptr<ore::data::Portfolio>& portfolio,
68 const QuantLib::ext::shared_ptr<ore::data::Market>& market, const string& marketConfiguration,
69 const QuantLib::ext::shared_ptr<ore::data::EngineData>& engineData,
70 const QuantLib::ext::shared_ptr<ScenarioSimMarketParameters>& simMarketData,
71 const QuantLib::ext::shared_ptr<SensitivityScenarioData>& sensitivityData, const bool recalibrateModels,
72 const QuantLib::ext::shared_ptr<ore::data::CurveConfigurations>& curveConfigs = nullptr,
73 const QuantLib::ext::shared_ptr<ore::data::TodaysMarketParameters>& todaysMarketParams = nullptr,
74 const bool nonShiftedBaseCurrencyConversion = false,
75 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData = nullptr,
76 const IborFallbackConfig& iborFallbackConfig = IborFallbackConfig::defaultConfig(),
77 const bool continueOnError = false, bool dryRun = false);
78
79 //! Constructor using multi-threaded engine
80 SensitivityAnalysis(const Size nThreads, const Date& asof, const QuantLib::ext::shared_ptr<ore::data::Loader>& loader,
81 const QuantLib::ext::shared_ptr<ore::data::Portfolio>& portfolio, const string& marketConfiguration,
82 const QuantLib::ext::shared_ptr<ore::data::EngineData>& engineData,
83 const QuantLib::ext::shared_ptr<ore::analytics::ScenarioSimMarketParameters>& simMarketData,
84 const QuantLib::ext::shared_ptr<ore::analytics::SensitivityScenarioData>& sensitivityData,
85 const bool recalibrateModels,
86 const QuantLib::ext::shared_ptr<ore::data::CurveConfigurations>& curveConfigs,
87 const QuantLib::ext::shared_ptr<ore::data::TodaysMarketParameters>& todaysMarketParams,
88 const bool nonShiftedBaseCurrencyConversion = false,
89 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData = nullptr,
90 const IborFallbackConfig& iborFallbackConfig = IborFallbackConfig::defaultConfig(),
91 const bool continueOnError = false, bool dryRun = false,
92 const std::string& context = "sensi analysis");
93
95
96 //! Generate the Sensitivities
98
99 //! The ASOF date for the sensitivity analysis
100 const QuantLib::Date asof() const { return asof_; }
101
102 //! The market configuration string
103 const std::string marketConfiguration() const { return marketConfiguration_; }
104
105 //! A getter for the sim market
106 const QuantLib::ext::shared_ptr<ScenarioSimMarket> simMarket() const { return simMarket_; }
107
108 //! A getter for SensitivityScenarioGenerator (the main one, without possibly customized shifts)
109 const QuantLib::ext::shared_ptr<SensitivityScenarioGenerator> scenarioGenerator() const { return scenarioGenerator_; }
110
111 //! A getter for ScenarioSimMarketParameters
112 const QuantLib::ext::shared_ptr<ScenarioSimMarketParameters> simMarketData() const { return simMarketData_; }
113
114 //! A getter for SensitivityScenarioData
115 const QuantLib::ext::shared_ptr<SensitivityScenarioData> sensitivityData() const { return sensitivityData_; }
116
117 //! override shift tenors with sim market tenors
118 void overrideTenors(const bool b) { overrideTenors_ = b; }
119
120 //! the portfolio of trades
121 QuantLib::ext::shared_ptr<Portfolio> portfolio() const { return portfolio_; }
122
123 //! a wrapper for the sensitivity results cubes (one per shift configuration)
124 std::vector<QuantLib::ext::shared_ptr<SensitivityCube>> sensiCubes() const { return sensiCubes_; }
125
126 //! a wrapper for the first sensitivity result cube (if that is unique, otherwise throws, for bwd compatibility)
127 QuantLib::ext::shared_ptr<SensitivityCube> sensiCube() const {
128 QL_REQUIRE(sensiCubes_.size() == 1, "SensitivityAnalysis: sensiCube() called, but got "
129 << sensiCubes_.size() << " sensi cubes. Check the calling code.");
130 return sensiCubes_.front();
131 }
132
133private:
134 QuantLib::ext::shared_ptr<ore::data::Market> market_;
136 Date asof_;
137 QuantLib::ext::shared_ptr<SensitivityScenarioGenerator> scenarioGenerator_;
138 QuantLib::ext::shared_ptr<ScenarioSimMarket> simMarket_;
139 QuantLib::ext::shared_ptr<ScenarioSimMarketParameters> simMarketData_;
140 QuantLib::ext::shared_ptr<SensitivityScenarioData> sensitivityData_;
142 //! Optional curve configurations. Used in building the scenario sim market.
143 QuantLib::ext::shared_ptr<ore::data::CurveConfigurations> curveConfigs_;
144 //! Optional todays market parameters. Used in building the scenario sim market.
145 QuantLib::ext::shared_ptr<ore::data::TodaysMarketParameters> todaysMarketParams_;
147
148 // if true, convert sensis to base currency using the original (non-shifted) FX rate
150 QuantLib::ext::shared_ptr<ore::data::ReferenceDataManager> referenceData_;
152 // if true, the processing is continued even on build errors
154 //! the engine data (provided as input, needed to construct the engine factory)
155 QuantLib::ext::shared_ptr<EngineData> engineData_;
156 //! the portfolio (provided as input)
157 QuantLib::ext::shared_ptr<Portfolio> portfolio_;
158 //! do dry run
160
161 //! model builders
162 std::set<std::pair<string, QuantLib::ext::shared_ptr<QuantExt::ModelBuilder>>> modelBuilders_;
163 //! sensitivityCube
164 std::vector<QuantLib::ext::shared_ptr<SensitivityCube>> sensiCubes_;
165
167 // additional members needed for multihreaded constructor
169 QuantLib::ext::shared_ptr<ore::data::Loader> loader_;
170 std::string context_;
171};
172
173/*! Returns the absolute shift size corresponding to a particular risk factor \p key
174 given sensitivity parameters \p sensiParams and a simulation market \p simMarket
175*/
176Real getShiftSize(const RiskFactorKey& key, const SensitivityScenarioData& sensiParams,
177 const QuantLib::ext::shared_ptr<ScenarioSimMarket>& simMarket, const std::string& marketConfiguration = "");
178
179} // namespace analytics
180} // namespace ore
Data types stored in the scenario class.
Definition: scenario.hpp:48
QuantLib::ext::shared_ptr< ore::data::CurveConfigurations > curveConfigs_
Optional curve configurations. Used in building the scenario sim market.
QuantLib::ext::shared_ptr< SensitivityScenarioData > sensitivityData_
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > todaysMarketParams_
Optional todays market parameters. Used in building the scenario sim market.
const std::string marketConfiguration() const
The market configuration string.
const QuantLib::Date asof() const
The ASOF date for the sensitivity analysis.
const QuantLib::ext::shared_ptr< SensitivityScenarioData > sensitivityData() const
A getter for SensitivityScenarioData.
void generateSensitivities()
Generate the Sensitivities.
QuantLib::ext::shared_ptr< EngineData > engineData_
the engine data (provided as input, needed to construct the engine factory)
QuantLib::ext::shared_ptr< ScenarioSimMarket > simMarket_
std::vector< QuantLib::ext::shared_ptr< SensitivityCube > > sensiCubes_
sensitivityCube
QuantLib::ext::shared_ptr< Portfolio > portfolio() const
the portfolio of trades
QuantLib::ext::shared_ptr< SensitivityCube > sensiCube() const
a wrapper for the first sensitivity result cube (if that is unique, otherwise throws,...
QuantLib::ext::shared_ptr< Portfolio > portfolio_
the portfolio (provided as input)
const QuantLib::ext::shared_ptr< ScenarioSimMarket > simMarket() const
A getter for the sim market.
std::set< std::pair< string, QuantLib::ext::shared_ptr< QuantExt::ModelBuilder > > > modelBuilders_
model builders
const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > simMarketData() const
A getter for ScenarioSimMarketParameters.
QuantLib::ext::shared_ptr< ore::data::ReferenceDataManager > referenceData_
QuantLib::ext::shared_ptr< ore::data::Market > market_
const QuantLib::ext::shared_ptr< SensitivityScenarioGenerator > scenarioGenerator() const
A getter for SensitivityScenarioGenerator (the main one, without possibly customized shifts)
std::vector< QuantLib::ext::shared_ptr< SensitivityCube > > sensiCubes() const
a wrapper for the sensitivity results cubes (one per shift configuration)
QuantLib::ext::shared_ptr< SensitivityScenarioGenerator > scenarioGenerator_
void overrideTenors(const bool b)
override shift tenors with sim market tenors
QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > simMarketData_
QuantLib::ext::shared_ptr< ore::data::Loader > loader_
Description of sensitivity shift scenarios.
static IborFallbackConfig defaultConfig()
Real getShiftSize(const RiskFactorKey &key, const SensitivityScenarioData &sensiParams, const QuantLib::ext::shared_ptr< ScenarioSimMarket > &simMarket, const string &marketConfiguration)
An NPV cube for storing NPVs resulting from risk factor shifts.
A Market class that can be updated by Scenarios.
A class to hold Scenario parameters for scenarioSimMarket.
holds a grid of NPVs for a list of trades under various scenarios
A class to hold the parametrisation for building sensitivity scenarios.
Sensitivity scenario generation.
vector< string > curveConfigs