|
| 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 |
0 commit comments