Skip to content

Commit 24b4a04

Browse files
pcaspersjenkins
authored andcommitted
QPR-12173: Decomposed Sensitivity Stream
1 parent 15548ea commit 24b4a04

12 files changed

Lines changed: 2459 additions & 23 deletions

OREAnalytics/orea/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ cube/sparsenpvcube.cpp
4646
engine/amcvaluationengine.cpp
4747
engine/bufferedsensitivitystream.cpp
4848
engine/cptycalculator.cpp
49+
engine/decomposedsensitivitystream.cpp
4950
engine/filteredsensitivitystream.cpp
5051
engine/historicalpnlgenerator.cpp
5152
engine/historicalsensipnlcalculator.cpp
@@ -187,6 +188,7 @@ cube/sparsenpvcube.hpp
187188
engine/amcvaluationengine.hpp
188189
engine/bufferedsensitivitystream.hpp
189190
engine/cptycalculator.hpp
191+
engine/decomposedsensitivitystream.hpp
190192
engine/filteredsensitivitystream.hpp
191193
engine/historicalpnlgenerator.hpp
192194
engine/historicalsensipnlcalculator.hpp

OREAnalytics/orea/engine/decomposedsensitivitystream.cpp

Lines changed: 335 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Copyright (C) 2023 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/engine/Decomposedsensitivitystream.hpp
20+
\brief Class that wraps a sensitivity stream and decomposes equity/commodity and default risk records
21+
*/
22+
23+
#pragma once
24+
#include <orea/engine/sensitivitystream.hpp>
25+
#include <orea/scenario/sensitivityscenariodata.hpp>
26+
#include <ored/configuration/curveconfigurations.hpp>
27+
#include <ored/marketdata/market.hpp>
28+
#include <ored/portfolio/referencedata.hpp>
29+
30+
#include <fstream>
31+
#include <set>
32+
#include <string>
33+
34+
namespace ore {
35+
namespace analytics {
36+
37+
//! Class that wraps a sensitivity stream and decompose default, equity and commodity risk records given weights
38+
class DecomposedSensitivityStream : public SensitivityStream {
39+
public:
40+
/*! Constructor providing the weights for the credit index decomposition and the ids and reference data used for
41+
*/
42+
DecomposedSensitivityStream(
43+
const boost::shared_ptr<SensitivityStream>& ss, const std::string& baseCurrency,
44+
std::map<std::string, std::map<std::string, double>> defaultRiskDecompositionWeights = {},
45+
const std::set<std::string>& eqComDecompositionTradeIds = {},
46+
const std::map<std::string, std::map<std::string, double>>& currencyHedgedIndexQuantities = {},
47+
const boost::shared_ptr<ore::data::ReferenceDataManager>& refDataManager = nullptr,
48+
const boost::shared_ptr<ore::data::CurveConfigurations>& curveConfigs = nullptr,
49+
const boost::shared_ptr<SensitivityScenarioData>& scenarioData = nullptr,
50+
const boost::shared_ptr<ore::data::Market>& todaysMarket = nullptr);
51+
//! Returns the next SensitivityRecord in the stream after filtering
52+
SensitivityRecord next() override;
53+
//! Resets the stream so that SensitivityRecord objects can be streamed again
54+
void reset() override;
55+
56+
private:
57+
// Result of a equity / commodity index decomposition
58+
struct IndexDecompositionResult {
59+
std::map<std::string, double> spotRisk;
60+
std::map<std::string, double> fxRisk;
61+
std::string indexCurrency;
62+
};
63+
64+
65+
//! Decompose a equity/commodity spot sensitivity into the constituent spot sensistivities
66+
std::map<std::string, double>
67+
constituentSpotRiskFromDecomposition(const double spotDelta, const std::map<std::string, double>& indexWeights) const;
68+
69+
//! Compute the resulting fx risks from a given equity/commodity decomposition
70+
std::map<std::string, double>
71+
fxRiskFromDecomposition(const std::map<std::string, double>& spotRisk,
72+
const std::map<std::string, std::vector<std::string>>& constituentCurrencies,
73+
const std::map<std::string, double>& fxSpotShiftSize, const double eqShiftSize) const;
74+
75+
//! Return the sensi shift size for the shifting the ccy-baseCurrency spot quote
76+
double fxRiskShiftSize(const std::string ccy) const;
77+
//! Returns the shift sizes for all currencies in the map
78+
std::map<std::string, double> fxRiskShiftSizes(const std::map<std::string, std::vector<std::string>>& constituentCurrencies) const;
79+
80+
//! Return the asset spot shift size
81+
double assetSpotShiftSize(const std::string name) const;
82+
83+
std::map<std::string, std::vector<std::string>> getConstituentCurrencies(const std::map<std::string, double>& constituents,
84+
const std::string& indexCurrency,
85+
const ore::data::CurveSpec::CurveType curveType) const;
86+
87+
//! Decompose the record and add it to the internal storage;
88+
std::vector<SensitivityRecord> decompose(const SensitivityRecord& record) const;
89+
std::vector<SensitivityRecord> decomposeSurvivalProbability(const SensitivityRecord& record) const;
90+
std::vector<SensitivityRecord> decomposeCurrencyHedgedIndexRisk(const SensitivityRecord& record) const;
91+
92+
IndexDecompositionResult indexDecomposition(double delta, const std::string& indexName,
93+
const ore::data::CurveSpec::CurveType curveType) const;
94+
95+
std::vector<SensitivityRecord> sensitivityRecords(const std::map<std::string, double>& eqDeltas,
96+
const std::map<std::string, double>& fxDeltas,
97+
const std::string indexCurrency,
98+
const SensitivityRecord& orginialRecord) const;
99+
100+
// get the curve currency for name, fallback to check equity curves
101+
std::string curveCurrency(const std::string& name, ore::data::CurveSpec::CurveType curveType) const;
102+
// Scale the fx risk entries from the index decomposition
103+
std::vector<SensitivityRecord> decomposedRecords_;
104+
std::vector<SensitivityRecord>::iterator itCurrent_;
105+
106+
//! The underlying sensitivity stream that has been wrapped
107+
boost::shared_ptr<SensitivityStream> ss_;
108+
std::string baseCurrency_;
109+
//! map of trade ids to the basket consituents with their resp. weights
110+
std::map<std::string, std::map<std::string, double>> defaultRiskDecompositionWeights_;
111+
//! list of trade id, for which a equity index decomposition should be applied
112+
std::set<std::string> eqComDecompositionTradeIds_;
113+
//! list of trade id, for which a commodity index decomposition should be applied
114+
std::map<std::string, std::map<std::string, double>> currencyHedgedIndexQuantities_;
115+
//! refDataManager holding the equity and commodity index decomposition weights
116+
boost::shared_ptr<ore::data::ReferenceDataManager> refDataManager_;
117+
boost::shared_ptr<ore::data::CurveConfigurations> curveConfigs_;
118+
boost::shared_ptr<SensitivityScenarioData> ssd_;
119+
// needed for currency hedged index decomposition
120+
boost::shared_ptr<ore::data::Market> todaysMarket_;
121+
// flag if decompose is possible
122+
bool decompose_;
123+
};
124+
125+
} // namespace analytics
126+
} // namespace ore

OREAnalytics/orea/orea.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <orea/engine/amcvaluationengine.hpp>
5959
#include <orea/engine/bufferedsensitivitystream.hpp>
6060
#include <orea/engine/cptycalculator.hpp>
61+
#include <orea/engine/decomposedsensitivitystream.hpp>
6162
#include <orea/engine/filteredsensitivitystream.hpp>
6263
#include <orea/engine/historicalpnlgenerator.hpp>
6364
#include <orea/engine/historicalsensipnlcalculator.hpp>

0 commit comments

Comments
 (0)