Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
sensitivitycube.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 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 orea/cube/sensitivitycube.hpp
20 \brief holds a grid of NPVs for a list of trades under various scenarios
21 \ingroup Cube
22*/
23
24#pragma once
25
28
29#include <ql/errors.hpp>
30#include <ql/time/date.hpp>
31#include <ql/types.hpp>
32
33#include <vector>
34
35#include <boost/bimap.hpp>
36#include <ql/shared_ptr.hpp>
37
38namespace ore {
39namespace analytics {
40
41//! SensitivityCube is a wrapper for an npvCube that gives easier access to the underlying cube elements
43
44public:
45 typedef std::pair<RiskFactorKey, RiskFactorKey> crossPair;
47
48 struct FactorData {
49 QuantLib::Size index = 0;
50 QuantLib::Real targetShiftSize = 0.0;
51 QuantLib::Real actualShiftSize = 0.0;
53 string factorDesc;
54 bool operator<(const FactorData& fd) const { return index < fd.index; }
55 };
56
57 //! Constructor using a vector of scenario descriptions
58 SensitivityCube(const QuantLib::ext::shared_ptr<NPVSensiCube>& cube,
59 const std::vector<ShiftScenarioDescription>& scenarioDescriptions,
60 const std::map<RiskFactorKey, QuantLib::Real>& targetShiftSizes,
61 const std::map<RiskFactorKey, QuantLib::Real>& actualShiftSizes,
62 const std::map<RiskFactorKey, ShiftScheme>& shiftSchemes);
63
64 //! Constructor using a vector of scenario description strings
65 SensitivityCube(const QuantLib::ext::shared_ptr<NPVSensiCube>& cube, const std::vector<std::string>& scenarioDescriptions,
66 const std::map<RiskFactorKey, QuantLib::Real>& targetShiftSizes,
67 const std::map<RiskFactorKey, QuantLib::Real>& actualshiftSizes,
68 const std::map<RiskFactorKey, ShiftScheme>& shiftSchemes);
69
70 //! \name Inspectors
71 //@{
72 const QuantLib::ext::shared_ptr<NPVSensiCube>& npvCube() const { return cube_; }
73 const std::vector<ShiftScenarioDescription>& scenarioDescriptions() const { return scenarioDescriptions_; }
74 //@}
75
76 //! Check if the cube has scenario NPVs for trade with ID \p tradeId
77 bool hasTrade(const std::string& tradeId) const;
78
79 //! Return the map of up trade id's to index in cube
80 const std::map<std::string, QuantLib::Size>& tradeIdx() const { return cube_->idsAndIndexes(); };
81
82 /*! Return factor for given up or down scenario index or None if given index is not an up or down scenario (to be reviewed) */
83 RiskFactorKey upDownFactor(const Size index) const;
84
85 /*! Return factor for given cross scenario index or None if given index is not a cross scenario (to be reviewed) */
86 crossPair crossFactor(const Size crossIndex) const;
87
88 //! Check if the cube has scenario NPVs for scenario with description \p scenarioDescription
89 bool hasScenario(const ShiftScenarioDescription& scenarioDescription) const;
90
91 //! Get the description for the risk factor key \p riskFactorKey
92 //! Returns the result of ShiftScenarioGenerator::ScenarioDescription::factor1()
93 std::string factorDescription(const RiskFactorKey& riskFactorKey) const;
94
95 //! Returns the set of risk factor keys for which a delta and gamma can be calculated
96 const std::set<RiskFactorKey>& factors() const;
97
98 //! Return the map of up risk factors to its factor data
99 const std::map<RiskFactorKey, SensitivityCube::FactorData>& upFactors() const { return upFactors_; };
100
101 //! Return the map of down risk factors to its factor data
102 const std::map<RiskFactorKey, SensitivityCube::FactorData>& downFactors() const { return downFactors_; };
103
104 //! Returns the set of pairs of risk factor keys for which a cross gamma is available
105 const std::map<crossPair, std::tuple<SensitivityCube::FactorData, SensitivityCube::FactorData, QuantLib::Size>>&
106 crossFactors() const;
107
108 //! Returns the absolute target shift size for given risk factor \p key
109 QuantLib::Real targetShiftSize(const RiskFactorKey& riskFactorKey) const;
110
111 //! Returns the absolute actual shift size for given risk factor \p key
112 QuantLib::Real actualShiftSize(const RiskFactorKey& riskFactorKey) const;
113
114 //! Returns the shift scheme for given risk factor \p key
115 ShiftScheme shiftScheme(const RiskFactorKey& riskFactorKey) const;
116
117 //! Get the base NPV for trade with ID \p tradeId
118 QuantLib::Real npv(const std::string& tradeId) const;
119
120 //! Get the NPV for trade given the index of trade in the cube
121 QuantLib::Real npv(QuantLib::Size id) const;
122
123 //! Get the trade delta for trade with index \p tradeIdx and for the given risk factor key \p riskFactorKey
124 QuantLib::Real delta(const Size tradeIdx, const RiskFactorKey& riskFactorKey) const;
125
126 //! Get the trade delta for trade with ID \p tradeId and for the given risk factor key \p riskFactorKey
127 QuantLib::Real delta(const std::string& tradeId, const RiskFactorKey& riskFactorKey) const;
128
129 //! Get the trade gamma for trade with index \p tradeIdx and for the given risk factor key \p riskFactorKey
130 QuantLib::Real gamma(const Size tradeIdx, const RiskFactorKey& riskFactorKey) const;
131
132 //! Get the trade gamma for trade with ID \p tradeId and for the given risk factor key \p riskFactorKey
133 QuantLib::Real gamma(const std::string& tradeId, const RiskFactorKey& riskFactorKey) const;
134
135 //! Get the trade cross gamma for trade with ID \p tradeId and for the given risk factor key pair \p
136 //! riskFactorKeyPair
137 QuantLib::Real crossGamma(const Size tradeIdx, const crossPair& riskFactorKeyPair) const;
138
139 //! Get the trade cross gamma for trade with ID \p tradeId and for the given risk factor key pair \p
140 //! riskFactorKeyPair
141 QuantLib::Real crossGamma(const std::string& tradeId, const crossPair& riskFactorKeyPair) const;
142
143 //! Get the trade cross gamma for trade given the index of trade and risk factors in the cube
144 QuantLib::Real crossGamma(QuantLib::Size tradeIdx, QuantLib::Size upIdx_1, QuantLib::Size upIdx_2,
145 QuantLib::Size crossId, QuantLib::Real scaling1, QuantLib::Real scaling2) const;
146
147 //! Get the relevant risk factors
148 std::set<RiskFactorKey> relevantRiskFactors() const;
149
150private:
151 //! Initialise method used by the constructors
152 void initialise();
153
154 QuantLib::ext::shared_ptr<NPVSensiCube> cube_;
155 std::vector<ShiftScenarioDescription> scenarioDescriptions_;
156 std::map<RiskFactorKey, QuantLib::Real> targetShiftSizes_;
157 std::map<RiskFactorKey, QuantLib::Real> actualShiftSizes_;
158 std::map<RiskFactorKey, ShiftScheme> shiftSchemes_;
159
160 // Duplication between map keys below and these sets but trade-off
161 // Means that we can return by reference in public inspector methods
162 std::set<RiskFactorKey> factors_;
163
164 // Maps for faster lookup of cube entries. They are populated in the constructor
165 // TODO: Review this i.e. could it be done better / using less memory
166 std::map<std::string, QuantLib::Size> tradeIdx_;
167 std::map<ShiftScenarioDescription, QuantLib::Size> scenarioIdx_;
168
169 std::map<RiskFactorKey, FactorData> upFactors_, downFactors_;
170 // map of crossPair to tuple of (data of first \p RiskFactorKey, data of second \p RiskFactorKey, index of
171 // crossFactor)
172 std::map<crossPair, std::tuple<FactorData, FactorData, QuantLib::Size>> crossFactors_;
173
174 // map of up / down / cross factor index to risk factor key
175 std::map<QuantLib::Size, RiskFactorKey> upIndexToKey_;
176 std::map<QuantLib::Size, RiskFactorKey> downIndexToKey_;
177 std::map<QuantLib::Size, crossPair> crossIndexToKey_;
178
179};
180
181std::ostream& operator<<(std::ostream& out, const SensitivityCube::crossPair& cp);
182
183} // namespace analytics
184} // namespace ore
185
Data types stored in the scenario class.
Definition: scenario.hpp:48
SensitivityCube is a wrapper for an npvCube that gives easier access to the underlying cube elements.
std::map< RiskFactorKey, ShiftScheme > shiftSchemes_
std::string factorDescription(const RiskFactorKey &riskFactorKey) const
void initialise()
Initialise method used by the constructors.
std::vector< ShiftScenarioDescription > scenarioDescriptions_
std::map< std::string, QuantLib::Size > tradeIdx_
std::map< QuantLib::Size, RiskFactorKey > downIndexToKey_
const QuantLib::ext::shared_ptr< NPVSensiCube > & npvCube() const
bool hasTrade(const std::string &tradeId) const
Check if the cube has scenario NPVs for trade with ID tradeId.
std::map< RiskFactorKey, QuantLib::Real > targetShiftSizes_
QuantLib::Real actualShiftSize(const RiskFactorKey &riskFactorKey) const
Returns the absolute actual shift size for given risk factor key.
const std::map< std::string, QuantLib::Size > & tradeIdx() const
Return the map of up trade id's to index in cube.
std::map< ShiftScenarioDescription, QuantLib::Size > scenarioIdx_
QuantLib::Real targetShiftSize(const RiskFactorKey &riskFactorKey) const
Returns the absolute target shift size for given risk factor key.
std::map< RiskFactorKey, FactorData > upFactors_
const std::map< RiskFactorKey, SensitivityCube::FactorData > & upFactors() const
Return the map of up risk factors to its factor data.
const std::map< RiskFactorKey, SensitivityCube::FactorData > & downFactors() const
Return the map of down risk factors to its factor data.
std::map< crossPair, std::tuple< FactorData, FactorData, QuantLib::Size > > crossFactors_
QuantLib::ext::shared_ptr< NPVSensiCube > cube_
QuantLib::Real gamma(const Size tradeIdx, const RiskFactorKey &riskFactorKey) const
Get the trade gamma for trade with index tradeIdx and for the given risk factor key riskFactorKey.
bool hasScenario(const ShiftScenarioDescription &scenarioDescription) const
Check if the cube has scenario NPVs for scenario with description scenarioDescription.
std::map< QuantLib::Size, crossPair > crossIndexToKey_
std::map< QuantLib::Size, RiskFactorKey > upIndexToKey_
QuantLib::Real npv(QuantLib::Size id) const
Get the NPV for trade given the index of trade in the cube.
QuantLib::Real delta(const Size tradeIdx, const RiskFactorKey &riskFactorKey) const
Get the trade delta for trade with index tradeIdx and for the given risk factor key riskFactorKey.
std::set< RiskFactorKey > factors_
RiskFactorKey upDownFactor(const Size index) const
const std::vector< ShiftScenarioDescription > & scenarioDescriptions() const
QuantLib::Real crossGamma(const Size tradeIdx, const crossPair &riskFactorKeyPair) const
std::map< RiskFactorKey, QuantLib::Real > actualShiftSizes_
const std::set< RiskFactorKey > & factors() const
Returns the set of risk factor keys for which a delta and gamma can be calculated.
const std::map< crossPair, std::tuple< SensitivityCube::FactorData, SensitivityCube::FactorData, QuantLib::Size > > & crossFactors() const
Returns the set of pairs of risk factor keys for which a cross gamma is available.
ShiftScheme shiftScheme(const RiskFactorKey &riskFactorKey) const
Returns the shift scheme for given risk factor key.
crossPair crossFactor(const Size crossIndex) const
QuantLib::Real npv(const std::string &tradeId) const
Get the base NPV for trade with ID tradeId.
std::map< RiskFactorKey, FactorData > downFactors_
std::pair< RiskFactorKey, RiskFactorKey > crossPair
std::set< RiskFactorKey > relevantRiskFactors() const
Get the relevant risk factors.
ShiftScenarioGenerator::ScenarioDescription ShiftScenarioDescription
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
SensitivityCube::crossPair crossPair
An NPV cube for storing NPVs resulting from risk factor shifts.
Shift scenario generation.
bool operator<(const FactorData &fd) const