Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scenario.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/scenario.hpp
20 \brief Scenario class
21 \ingroup scenario
22*/
23
24#pragma once
25
27
28#include <ql/shared_ptr.hpp>
29#include <ql/math/array.hpp>
30#include <ql/time/date.hpp>
31#include <ql/types.hpp>
32
33#include <boost/functional/hash.hpp>
34#include <map>
35#include <unordered_map>
36#include <vector>
37namespace ore {
38namespace analytics {
39using QuantLib::Array;
40using QuantLib::Date;
41using QuantLib::Real;
42using QuantLib::Size;
43using std::string;
44
45//! Data types stored in the scenario class
46/*! \ingroup scenario
47 */
49public:
50 //! Risk Factor types
51 enum class KeyType {
52 None,
59 FXSpot,
79 CPR
80 };
81
82 //! Constructor
84 //! Constructor
85 RiskFactorKey(const KeyType& iKeytype, const string& iName, const Size& iIndex = 0)
86 : keytype(iKeytype), name(iName), index(iIndex) {}
87
88 //! Key type
90 //! Key name.
91 /*! For FX this is a pair ("EURUSD") for Discount or swaption it's just a currency ("EUR")
92 * and for an index it's the index name
93 */
94 std::string name;
95 //! Index
96 Size index;
97
98private:
100 template <class Archive> void serialize(Archive& ar, const unsigned int) {
101 ar& keytype;
102 ar& name;
103 ar& index;
104 }
105};
106
107std::size_t hash_value(const RiskFactorKey& k);
108
109inline bool operator<(const RiskFactorKey& lhs, const RiskFactorKey& rhs) {
110 return std::tie(lhs.keytype, lhs.name, lhs.index) < std::tie(rhs.keytype, rhs.name, rhs.index);
111}
112
113inline bool operator==(const RiskFactorKey& lhs, const RiskFactorKey& rhs) {
114 return lhs.keytype == rhs.keytype && lhs.name == rhs.name && lhs.index == rhs.index;
115}
116
117inline bool operator>(const RiskFactorKey& lhs, const RiskFactorKey& rhs) { return rhs < lhs; }
118inline bool operator<=(const RiskFactorKey& lhs, const RiskFactorKey& rhs) { return !(lhs > rhs); }
119inline bool operator>=(const RiskFactorKey& lhs, const RiskFactorKey& rhs) { return !(lhs < rhs); }
120inline bool operator!=(const RiskFactorKey& lhs, const RiskFactorKey& rhs) { return !(lhs == rhs); }
121
122std::ostream& operator<<(std::ostream& out, const RiskFactorKey::KeyType& type);
123std::ostream& operator<<(std::ostream& out, const RiskFactorKey& key);
124
126RiskFactorKey parseRiskFactorKey(const string& str);
127
128//-----------------------------------------------------------------------------------------------
129//! Scenario Base Class
130/*! A scenario contains a single cross asset model sample in terms of
131 yield curves by currency, FX rates, etc.
132
133 This base class provides the interface to add and retrieve data to and from a scenario.
134 Concrete simple and memory optimized "compact" scenario classes are derived from this.
135
136 \ingroup scenario
137*/
138class Scenario {
139public:
140 //! Destructor
141 virtual ~Scenario() {}
142
143 //! Return the scenario asof date
144 virtual const Date& asof() const = 0;
145 //! Set the asof date
146 virtual void setAsof(const Date& d) = 0;
147
148 //! Get the scenario label
149 virtual const string& label() const = 0;
150 //! Set the scenario label
151 virtual void label(const string&) = 0;
152
153 //! Get Numeraire ratio n = N(t) / N(0) so that Price(0) = N(0) * E [Price(t) / N(t) ]
154 virtual Real getNumeraire() const = 0;
155 //! Set the Numeraire ratio n = N(t) / N(0) so that Price(0) = N(0) * E [Price(t) / N(t) ]
156 virtual void setNumeraire(Real n) = 0;
157
158 //! Check whether this scenario provides the data for the given key
159 virtual bool has(const RiskFactorKey& key) const = 0;
160 //! Risk factor keys for which this scenario provides data
161 virtual const std::vector<RiskFactorKey>& keys() const = 0;
162 //! Add an element to the scenario
163 virtual void add(const RiskFactorKey& key, Real value) = 0;
164 //! Get an element from the scenario
165 virtual Real get(const RiskFactorKey& key) const = 0;
166
167 //! Is this an absolute or difference scenario?
168 virtual bool isAbsolute() const = 0;
169 //! Set if this is an absolute scenario
170 virtual void setAbsolute(const bool b) = 0;
171 //! Get coordinates
172 virtual const std::map<std::pair<RiskFactorKey::KeyType, std::string>, std::vector<std::vector<Real>>>&
173 coordinates() const = 0;
174
175 //! clones a scenario and returns a pointer to the new object
176 virtual QuantLib::ext::shared_ptr<Scenario> clone() const = 0;
177
178 //! checks for equality up to numerical differences
179 virtual bool isCloseEnough(const QuantLib::ext::shared_ptr<Scenario>& s) const;
180
181 //! return fingerprint identifying the set of rf keys of the scenarios, or 0 if not provided by the implementation
182 virtual std::size_t keysHash() const { return 0; }
183
184private:
186 template <class Archive> void serialize(Archive&, const unsigned int) {}
187};
188
191
192ShiftScheme parseShiftScheme(const std::string& s);
193std::ostream& operator<<(std::ostream& out, const ShiftScheme& shiftScheme);
194
195ShiftType parseShiftType(const std::string& s);
196std::ostream& operator<<(std::ostream& out, const ShiftType& shiftType);
197
198} // namespace analytics
199} // namespace ore
200
201template <>
202struct std::hash<ore::analytics::RiskFactorKey> {
203 std::size_t operator()(const ore::analytics::RiskFactorKey& k) const { return hash_value(k); }
204};
Data types stored in the scenario class.
Definition: scenario.hpp:48
KeyType keytype
Key type.
Definition: scenario.hpp:89
RiskFactorKey(const KeyType &iKeytype, const string &iName, const Size &iIndex=0)
Constructor.
Definition: scenario.hpp:85
std::string name
Key name.
Definition: scenario.hpp:94
KeyType
Risk Factor types.
Definition: scenario.hpp:51
friend class boost::serialization::access
Definition: scenario.hpp:99
void serialize(Archive &ar, const unsigned int)
Definition: scenario.hpp:100
Scenario Base Class.
Definition: scenario.hpp:138
virtual const string & label() const =0
Get the scenario label.
virtual const std::map< std::pair< RiskFactorKey::KeyType, std::string >, std::vector< std::vector< Real > > > & coordinates() const =0
Get coordinates.
virtual bool isCloseEnough(const QuantLib::ext::shared_ptr< Scenario > &s) const
checks for equality up to numerical differences
Definition: scenario.cpp:38
virtual const std::vector< RiskFactorKey > & keys() const =0
Risk factor keys for which this scenario provides data.
virtual const Date & asof() const =0
Return the scenario asof date.
virtual void add(const RiskFactorKey &key, Real value)=0
Add an element to the scenario.
virtual Real get(const RiskFactorKey &key) const =0
Get an element from the scenario.
virtual bool has(const RiskFactorKey &key) const =0
Check whether this scenario provides the data for the given key.
virtual Real getNumeraire() const =0
Get Numeraire ratio n = N(t) / N(0) so that Price(0) = N(0) * E [Price(t) / N(t) ].
virtual void setAsof(const Date &d)=0
Set the asof date.
virtual bool isAbsolute() const =0
Is this an absolute or difference scenario?
virtual void setNumeraire(Real n)=0
Set the Numeraire ratio n = N(t) / N(0) so that Price(0) = N(0) * E [Price(t) / N(t) ].
virtual void label(const string &)=0
Set the scenario label.
virtual ~Scenario()
Destructor.
Definition: scenario.hpp:141
virtual void setAbsolute(const bool b)=0
Set if this is an absolute scenario.
void serialize(Archive &, const unsigned int)
Definition: scenario.hpp:186
virtual QuantLib::ext::shared_ptr< Scenario > clone() const =0
clones a scenario and returns a pointer to the new object
friend class boost::serialization::access
Definition: scenario.hpp:185
virtual std::size_t keysHash() const
return fingerprint identifying the set of rf keys of the scenarios, or 0 if not provided by the imple...
Definition: scenario.hpp:182
bool operator<(const Dividend &d1, const Dividend &d2)
bool operator!=(const Filter &a, const Filter &b)
Filter operator>=(const RandomVariable &x, const RandomVariable &y)
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
bool operator==(const Dividend &d1, const Dividend &d)
bool operator>(const Distributionpair &p1, const Distributionpair &p2)
Filter operator<=(const RandomVariable &x, const RandomVariable &y)
ShiftScheme parseShiftScheme(const std::string &s)
Definition: scenario.cpp:193
RiskFactorKey::KeyType parseRiskFactorKeyType(const string &str)
Definition: scenario.cpp:128
RiskFactorKey parseRiskFactorKey(const string &str)
Definition: scenario.cpp:183
std::size_t hash_value(const RiskFactorKey &k)
Definition: scenario.cpp:30
std::size_t operator()(const ore::analytics::RiskFactorKey &k) const
Definition: scenario.hpp:203