Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
shiftscenariogenerator.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 scenario/shiftscenariogenerator.hpp
20 \brief Shift scenario generation
21 \ingroup scenario
22*/
23
24#pragma once
25
31
32#include <tuple>
33
34namespace ore {
35namespace analytics {
36using namespace data;
37
38//! Shift Scenario Generator
39/*!
40 Base class for sensitivity and stress scenario generators
41 \ingroup scenario
42 */
44public:
46 public:
47 enum class Type { Base, Up, Down, Cross };
48 //! Constructor
54 : type_(Type::Cross), key1_(d1.key1()), indexDesc1_(d1.indexDesc1()), key2_(d2.key1()),
55 indexDesc2_(d2.indexDesc1()) {}
56 //! Construct from string
57 ScenarioDescription(const std::string& description);
58
59 //! Inspectors
60 //@{
61 const Type& type() const { return type_; }
62 const RiskFactorKey& key1() const { return key1_; }
63 const RiskFactorKey& key2() const { return key2_; }
64 const string& indexDesc1() const { return indexDesc1_; }
65 const string& indexDesc2() const { return indexDesc2_; }
66 string keyName1() const { return keyName(key1_); }
67 string keyName2() const { return keyName(key2_); }
68 //@}
69 //! Return type as string
70 string typeString() const;
71 //! Return key1 as string with text1 appended as key index description
72 string factor1() const;
73 //! Return key2 as string with text2 appended as key index description
74 string factor2() const;
75 //! Return "factor1" and append ":factor2" if factor2 is not empty
76 string factors() const;
77
78 private:
79 string keyName(RiskFactorKey key) const;
80
86 };
87
88 //! Constructor
89 ShiftScenarioGenerator(const QuantLib::ext::shared_ptr<Scenario>& baseScenario,
90 const QuantLib::ext::shared_ptr<ScenarioSimMarketParameters>& simMarketData,
91 const QuantLib::ext::weak_ptr<ScenarioSimMarket>& simMarket);
92 //! Default destructor
94
95 //! Scenario Generator interface
96 //@{
97 QuantLib::ext::shared_ptr<Scenario> next(const Date& d) override;
98 void reset() override { counter_ = 0; }
99 //@}
100
101 //! Inspectors
102 //@{
103 //! Number of shift scenarios
104 Size samples() { return scenarios_.size(); }
105 //! Return the base scenario, i.e. cached initial values of all relevant market points
106 const QuantLib::ext::shared_ptr<Scenario>& baseScenario() { return scenarios_.front(); }
107 //! Return vector of sensitivity scenarios, scenario 0 is the base scenario
108 const std::vector<QuantLib::ext::shared_ptr<Scenario>>& scenarios() { return scenarios_; }
109 //! Return vector of scenario descriptions
110 std::vector<ScenarioDescription> scenarioDescriptions() { return scenarioDescriptions_; }
111 // ! Return map of RiskFactorKeys to factors, i.e. human readable text representations
112 const std::map<RiskFactorKey, std::string>& keyToFactor() { return keyToFactor_; }
113 //! Return revers map of factors to RiskFactorKeys
114 const std::map<std::string, RiskFactorKey>& factorToKey() { return factorToKey_; }
115 //@}
116
117 //! Apply 1d triangular shift to 1d data such as yield curves, public to allow test suite access
118 /*!
119 Apply triangular shaped shifts to the underlying curve where the triangle
120 reaches from the previous to the next shift tenor point with peak at the
121 current shift tenor point.
122 At the initial and final shift tenor the shape is replaced such that the
123 full shift is applied to all curve grid points to the left of the first
124 shift point and to the right of the last shift point, respectively.
125 The procedure guarantees that no sensitivity to original curve points is
126 "missed" when the shift curve is less granular, e.g.
127 original curve |...|...|...|...|...|...|...|...|...|
128 shift curve ......|...........|...........|......
129 */
131 //! Number of the shift curve tenor point to be shifted here
132 Size j,
133 //! Shift size interpreted as either absolute or relative shift
134 Real shiftSize,
135 //! Upwards shift if true, otherwise downwards
136 bool up,
137 //! Absolute: newValue = oldValue + shiftSize. Relative: newValue = oldValue * (1 + shiftSize)
138 ShiftType type,
139 //! Shift tenors expressed as times
140 const vector<Time>& shiftTimes,
141 //! Input curve values such as zero rates
142 const vector<Real>& values,
143 //! Tenor points of the input curve, expressed as times
144 const vector<Time>& times,
145 //! Resulting shifted curve with same tenor structure as the input curve
146 vector<Real>& shiftedValues,
147 //! Initialise shiftedValues vector before applying this shift j (yes for sensitivity, no for stress)
148 bool initialise);
149
150 //! Apply 2d shift to 2d matrix such as swaption volatilities, public to allow test suite access
151 /*! This is the 2d generalisation of the 1d version of applyShift()
152 */
153 void applyShift(
154 //! Index of the shift tenor in "expiry" direction
155 Size j,
156 //! Index of the shift tenor in "term" (Swaptions) or "strike" (Caps) direction
157 Size k,
158 //! Shift size interpreted as either absolute or relative shift
159 Real shiftSize,
160 //! Upwards shift if true, otherwise downwards
161 bool up,
162 //! Absolute: newValue = oldValue + shiftSize. Relative: newValue = oldValue * (1 + shiftSize)
163 ShiftType type,
164 //! Coordinate time in "expiry" direction of the shift curve
165 const vector<Time>& shiftX,
166 //! Coordinate time in "term" or "strike" direction of the shift curve
167 const vector<Time>& shiftY,
168 //! Coordinate time in "expiry" direction of the underlying data
169 const vector<Time>& dataX,
170 //! Coordinate time in "term" or "strike" direction of the underlying data
171 const vector<Time>& dataY,
172 //! Matrix of input data
173 const vector<vector<Real>>& data,
174 //! Matrix of shifted result data
175 vector<vector<Real>>& shiftedData,
176 //! Initialise shiftedData vector before applying this shift j/k (yes for sensitivity, no for stress)
177 bool initialise);
178
179 //! return the base scenario
180 QuantLib::ext::shared_ptr<Scenario> baseScenario() const { return scenarios_.front(); }
181
182protected:
183 const QuantLib::ext::shared_ptr<Scenario> baseScenario_;
184 const QuantLib::ext::shared_ptr<ScenarioSimMarketParameters> simMarketData_;
185 const QuantLib::ext::weak_ptr<ScenarioSimMarket> simMarket_;
186 std::vector<QuantLib::ext::shared_ptr<Scenario>> scenarios_;
188 std::vector<ScenarioDescription> scenarioDescriptions_;
189 // map risk factor key to "factor", i.e. human readable text representation
190 std::map<RiskFactorKey, std::string> keyToFactor_;
191 // reverse map of factors to risk factor keys
192 std::map<std::string, RiskFactorKey> factorToKey_;
193};
194
195std::ostream& operator<<(std::ostream& out, const ShiftScenarioGenerator::ScenarioDescription& scenarioDescription);
196
197//! Retrieve the RiskFactorKey and index description from the result of ScenarioDescription::factor1() or
198//! ScenarioDescription::factor2()
199std::pair<RiskFactorKey, std::string> deconstructFactor(const std::string& factor);
200
201//! Reconstruct the string description from a risk factor \p key and its index description \p desc
202std::string reconstructFactor(const RiskFactorKey& key, const std::string& desc);
203
204//! risk factor key parser that takes into account additional tokens occurring in sensitivity risk factor keys
205QuantLib::ext::shared_ptr<RiskFactorKey> parseRiskFactorKey(const std::string& str, std::vector<std::string>& addTokens);
206
209 return std::tie(lhs.type(), lhs.key1(), lhs.key2()) < std::tie(rhs.type(), rhs.key1(), rhs.key2());
210}
211
214 return lhs.type() == rhs.type() && lhs.key1() == rhs.key1() && lhs.indexDesc1() == rhs.indexDesc1() &&
215 lhs.key2() == rhs.key2() && lhs.indexDesc2() == rhs.indexDesc2();
216}
217
218} // namespace analytics
219} // namespace ore
Data types stored in the scenario class.
Definition: scenario.hpp:48
Scenario generator base class.
string factor2() const
Return key2 as string with text2 appended as key index description.
string factor1() const
Return key1 as string with text1 appended as key index description.
string factors() const
Return "factor1" and append ":factor2" if factor2 is not empty.
ScenarioDescription(ScenarioDescription d1, ScenarioDescription d2)
ScenarioDescription(Type type, RiskFactorKey key1, string indexDesc1)
std::vector< ScenarioDescription > scenarioDescriptions_
const QuantLib::ext::shared_ptr< Scenario > & baseScenario()
Return the base scenario, i.e. cached initial values of all relevant market points.
QuantLib::ext::shared_ptr< Scenario > baseScenario() const
return the base scenario
std::map< RiskFactorKey, std::string > keyToFactor_
const std::vector< QuantLib::ext::shared_ptr< Scenario > > & scenarios()
Return vector of sensitivity scenarios, scenario 0 is the base scenario.
std::vector< QuantLib::ext::shared_ptr< Scenario > > scenarios_
const std::map< std::string, RiskFactorKey > & factorToKey()
Return revers map of factors to RiskFactorKeys.
std::vector< ScenarioDescription > scenarioDescriptions()
Return vector of scenario descriptions.
QuantLib::ext::shared_ptr< Scenario > next(const Date &d) override
Scenario Generator interface.
const QuantLib::ext::shared_ptr< Scenario > baseScenario_
void applyShift(Size j, Real shiftSize, bool up, ShiftType type, const vector< Time > &shiftTimes, const vector< Real > &values, const vector< Time > &times, vector< Real > &shiftedValues, bool initialise)
Apply 1d triangular shift to 1d data such as yield curves, public to allow test suite access.
const std::map< RiskFactorKey, std::string > & keyToFactor()
std::map< std::string, RiskFactorKey > factorToKey_
void reset() override
Reset the generator so calls to next() return the first scenario.
const QuantLib::ext::weak_ptr< ScenarioSimMarket > simMarket_
const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > simMarketData_
data
bool operator<(const Dividend &d1, const Dividend &d2)
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
bool operator==(const Dividend &d1, const Dividend &d)
string reconstructFactor(const RiskFactorKey &key, const string &desc)
Reconstruct the string description from a risk factor key and its index description desc.
RiskFactorKey parseRiskFactorKey(const string &str)
Definition: scenario.cpp:183
pair< RiskFactorKey, string > deconstructFactor(const string &factor)
factory classes for scenarios
Scenario generator base classes.
A Market class that can be updated by Scenarios.
A class to hold the parametrisation for building sensitivity scenarios.