Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scenariosimmarket.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 scenario/scenariosimmarket.hpp
20 \brief A Market class that can be updated by Scenarios
21 \ingroup scenario
22*/
23
24#pragma once
25
33
34#include <map>
35
36namespace ore {
37namespace analytics {
38using namespace QuantLib;
39using std::map;
40using std::string;
41using std::vector;
42
43//! Map a yield curve type to a risk factor key type
45
46//! A scenario filter can exclude certain key from updating the scenario
47/*! Override this class with to provide custom filtering, by default all keys
48 * are allowed.
49 */
51public:
53 virtual ~ScenarioFilter() {}
54
55 //! Allow this key to be updated
56 virtual bool allow(const RiskFactorKey& key) const { return true; }
57};
58
59//! Simulation Market updated with discrete scenarios
60/*! If useSpreadedTermStructures is true, spreaded term structures over the initMarket for supported risk factors will
61 be generated. This is used by the SensitivityScenarioGenerator.
62
63 If cacheSimData is true, the scenario application is optimised. This requires that all scenarios are SimpleScenario
64 instances with identical key structure in their data.
65
66 If allowPartialScenarios is true, the check that all simData_ is touched by a scenario is disabled.
67 */
69public:
70 //! Constructor
72
73 ScenarioSimMarket(const QuantLib::ext::shared_ptr<Market>& initMarket,
74 const QuantLib::ext::shared_ptr<ScenarioSimMarketParameters>& parameters,
75 const std::string& configuration = Market::defaultConfiguration,
78 const bool continueOnError = false, const bool useSpreadedTermStructures = false,
79 const bool cacheSimData = false, const bool allowPartialScenarios = false,
80 const IborFallbackConfig& iborFallbackConfig = IborFallbackConfig::defaultConfig(),
81 const bool handlePseudoCurrencies = true,
82 const QuantLib::ext::shared_ptr<Scenario>& offSetScenario = nullptr);
83
84 ScenarioSimMarket(const QuantLib::ext::shared_ptr<Market>& initMarket,
85 const QuantLib::ext::shared_ptr<ScenarioSimMarketParameters>& parameters,
86 const QuantLib::ext::shared_ptr<FixingManager>& fixingManager,
87 const std::string& configuration = Market::defaultConfiguration,
90 const bool continueOnError = false, const bool useSpreadedTermStructures = false,
91 const bool cacheSimData = false, const bool allowPartialScenarios = false,
92 const IborFallbackConfig& iborFallbackConfig = IborFallbackConfig::defaultConfig(),
93 const bool handlePseudoCurrencies = true,
94 const QuantLib::ext::shared_ptr<Scenario>& offSetScenario = nullptr);
95
96 //! Set scenario generator
97 virtual QuantLib::ext::shared_ptr<ScenarioGenerator>& scenarioGenerator() { return scenarioGenerator_; }
98 //! Get scenario generator
99 virtual const QuantLib::ext::shared_ptr<ScenarioGenerator>& scenarioGenerator() const { return scenarioGenerator_; }
100
101 //! Set aggregation data
102 virtual QuantLib::ext::shared_ptr<AggregationScenarioData>& aggregationScenarioData() { return asd_; }
103 //! Get aggregation data
104 virtual const QuantLib::ext::shared_ptr<AggregationScenarioData>& aggregationScenarioData() const { return asd_; }
105
106 //! Set scenarioFilter
107 virtual QuantLib::ext::shared_ptr<ScenarioFilter>& filter() { return filter_; }
108 //! Get scenarioFilter
109 virtual const QuantLib::ext::shared_ptr<ScenarioFilter>& filter() const { return filter_; }
110
111 //! Update
112 // virtual void update(const Date&) override;
113 virtual void preUpdate() override;
114 virtual void updateScenario(const Date&) override;
115 virtual void updateDate(const Date&) override;
116 virtual void postUpdate(const Date& d, bool withFixings) override;
117 virtual void updateAsd(const Date&) override;
118
119 //! Reset sim market to initial state
120 virtual void reset() override;
121
122 /*! Scenario representing the initial state of the market. If useSpreadedTermStructures = false, this scenario
123 contains absolute values for all risk factor keys. If useSpreadedTermStructures = true, this scenario contains
124 spread values for all risk factor keys which support spreaded term structures and absolute values for the other
125 risk factor keys. The spread values will typically be zero (e.g. for vol risk factors) or 1 (e.g. for rate curve
126 risk factors, since we use discount factors there). */
127 virtual QuantLib::ext::shared_ptr<Scenario> baseScenario() const { return baseScenario_; }
128
129 /*! Scenario representing the initial state of the market. This scenario contains absolute values for all risk factor
130 types, no matter whether useSpreadedTermStructures is true or false. */
131 virtual QuantLib::ext::shared_ptr<Scenario> baseScenarioAbsolute() const { return baseScenarioAbsolute_; }
132
133 /*! Return true if this instance uses spreaded term structures */
135
136 //! Return the fixing manager
137 const QuantLib::ext::shared_ptr<FixingManager>& fixingManager() const override { return fixingManager_; }
138
139 //! is risk factor key simulated by this sim market instance?
140 virtual bool isSimulated(const RiskFactorKey::KeyType& factor) const;
141
142 void applyScenario(const QuantLib::ext::shared_ptr<Scenario>& scenario);
143
144protected:
145
146
147 void writeSimData(std::map<RiskFactorKey, QuantLib::ext::shared_ptr<SimpleQuote>>& simDataTmp,
148 std::map<RiskFactorKey, Real>& absoluteSimDataTmp, const RiskFactorKey::KeyType keyType,
149 const std::string& name, const std::vector<std::vector<Real>>& coordinates);
150
151 void addYieldCurve(const QuantLib::ext::shared_ptr<Market>& initMarket, const std::string& configuration,
152 const RiskFactorKey::KeyType rf, const string& key, const vector<Period>& tenors,
153 bool& simDataWritten, bool simulate = true, bool spreaded = false);
154
155 /*! Given a yield curve spec ID, \p yieldSpecId, return the corresponding yield term structure
156 from the \p market. If \p market is `nullptr`, then the yield term structure is taken from
157 this ScenarioSimMarket instance.
158 */
159 QuantLib::Handle<QuantLib::YieldTermStructure>
160 getYieldCurve(const std::string& yieldSpecId, const ore::data::TodaysMarketParameters& todaysMarketParams,
161 const std::string& configuration, const QuantLib::ext::shared_ptr<ore::data::Market>& market = nullptr) const;
162
163 /*! add a single swap index to the market, return true if successful */
164 bool addSwapIndexToSsm(const std::string& indexName, const bool continueOnError);
165
166 const QuantLib::ext::shared_ptr<ScenarioSimMarketParameters> parameters_;
167 QuantLib::ext::shared_ptr<ScenarioGenerator> scenarioGenerator_;
168 QuantLib::ext::shared_ptr<AggregationScenarioData> asd_;
169 QuantLib::ext::shared_ptr<FixingManager> fixingManager_;
170 QuantLib::ext::shared_ptr<ScenarioFilter> filter_;
171
172 std::map<RiskFactorKey, QuantLib::ext::shared_ptr<SimpleQuote>> simData_;
173 QuantLib::ext::shared_ptr<Scenario> baseScenario_;
174 QuantLib::ext::shared_ptr<Scenario> baseScenarioAbsolute_;
175
176 std::vector<QuantLib::ext::shared_ptr<SimpleQuote>> cachedSimData_;
177 std::vector<bool> cachedSimDataActive_;
178 std::size_t cachedSimDataKeysHash_ = 0;
179
180 std::set<RiskFactorKey::KeyType> nonSimulatedFactors_;
181
182 // if generate spread scenario values for keys, we store the absolute values in this map
183 // so that we can set up the base scenario with absolute values
185 std::map<RiskFactorKey, Real> absoluteSimData_;
186
187 // hold meta data for the scenarios stored in simData_, absoluteSimData_
188 std::set<std::tuple<RiskFactorKey::KeyType, std::string, std::vector<std::vector<Real>>>> coordinatesData_;
189
193
194 // for delta scenario application
195 std::set<ore::analytics::RiskFactorKey> diffToBaseKeys_;
196
197 mutable QuantLib::ext::shared_ptr<Scenario> currentScenario_;
198 QuantLib::ext::shared_ptr<Scenario> offsetScenario_;
199};
200} // namespace analytics
201} // namespace ore
Data types stored in the scenario class.
Definition: scenario.hpp:48
KeyType
Risk Factor types.
Definition: scenario.hpp:51
A scenario filter can exclude certain key from updating the scenario.
virtual bool allow(const RiskFactorKey &key) const
Allow this key to be updated.
Simulation Market updated with discrete scenarios.
std::set< RiskFactorKey::KeyType > nonSimulatedFactors_
std::map< RiskFactorKey, QuantLib::ext::shared_ptr< SimpleQuote > > simData_
virtual QuantLib::ext::shared_ptr< ScenarioGenerator > & scenarioGenerator()
Set scenario generator.
QuantLib::ext::shared_ptr< ScenarioFilter > filter_
bool addSwapIndexToSsm(const std::string &indexName, const bool continueOnError)
QuantLib::ext::shared_ptr< FixingManager > fixingManager_
QuantLib::Handle< QuantLib::YieldTermStructure > getYieldCurve(const std::string &yieldSpecId, const ore::data::TodaysMarketParameters &todaysMarketParams, const std::string &configuration, const QuantLib::ext::shared_ptr< ore::data::Market > &market=nullptr) const
void applyScenario(const QuantLib::ext::shared_ptr< Scenario > &scenario)
virtual const QuantLib::ext::shared_ptr< ScenarioFilter > & filter() const
Get scenarioFilter.
QuantLib::ext::shared_ptr< Scenario > currentScenario_
std::vector< QuantLib::ext::shared_ptr< SimpleQuote > > cachedSimData_
QuantLib::ext::shared_ptr< ScenarioGenerator > scenarioGenerator_
virtual const QuantLib::ext::shared_ptr< ScenarioGenerator > & scenarioGenerator() const
Get scenario generator.
std::set< ore::analytics::RiskFactorKey > diffToBaseKeys_
virtual void updateScenario(const Date &) override
Retrieve next market scenario and apply this, but don't update date.
QuantLib::ext::shared_ptr< Scenario > baseScenario_
const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > parameters_
QuantLib::ext::shared_ptr< Scenario > baseScenarioAbsolute_
virtual void postUpdate(const Date &d, bool withFixings) override
Observable reset depending on selected mode, instrument updates.
void addYieldCurve(const QuantLib::ext::shared_ptr< Market > &initMarket, const std::string &configuration, const RiskFactorKey::KeyType rf, const string &key, const vector< Period > &tenors, bool &simDataWritten, bool simulate=true, bool spreaded=false)
virtual QuantLib::ext::shared_ptr< Scenario > baseScenario() const
virtual bool isSimulated(const RiskFactorKey::KeyType &factor) const
is risk factor key simulated by this sim market instance?
virtual QuantLib::ext::shared_ptr< Scenario > baseScenarioAbsolute() const
virtual void preUpdate() override
Update.
virtual QuantLib::ext::shared_ptr< AggregationScenarioData > & aggregationScenarioData()
Set aggregation data.
ScenarioSimMarket(const bool handlePseudoCurrencies)
Constructor.
QuantLib::ext::shared_ptr< Scenario > offsetScenario_
const QuantLib::ext::shared_ptr< FixingManager > & fixingManager() const override
Return the fixing manager.
std::set< std::tuple< RiskFactorKey::KeyType, std::string, std::vector< std::vector< Real > > > > coordinatesData_
virtual void updateDate(const Date &) override
Update to the given date.
std::map< RiskFactorKey, Real > absoluteSimData_
virtual QuantLib::ext::shared_ptr< ScenarioFilter > & filter()
Set scenarioFilter.
virtual const QuantLib::ext::shared_ptr< AggregationScenarioData > & aggregationScenarioData() const
Get aggregation data.
virtual void reset() override
Reset sim market to initial state.
virtual void updateAsd(const Date &) override
Update aggregation scenario data.
QuantLib::ext::shared_ptr< AggregationScenarioData > asd_
void writeSimData(std::map< RiskFactorKey, QuantLib::ext::shared_ptr< SimpleQuote > > &simDataTmp, std::map< RiskFactorKey, Real > &absoluteSimDataTmp, const RiskFactorKey::KeyType keyType, const std::string &name, const std::vector< std::vector< Real > > &coordinates)
Simulation Market.
Definition: simmarket.hpp:44
static IborFallbackConfig defaultConfig()
static const string defaultConfiguration
bool handlePseudoCurrencies() const
RiskFactorKey::KeyType yieldCurveRiskFactor(const ore::data::YieldCurveType y)
Map a yield curve type to a risk factor key type.
Scenario class.
Scenario generator base classes.
A class to hold Scenario parameters for scenarioSimMarket.
A Market class that can be Simulated.