Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scenario.cpp
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#include <boost/algorithm/string/split.hpp>
22#include <ql/errors.hpp>
23#include <vector>
24
25using namespace ore::data;
26
27namespace ore {
28namespace analytics {
29
30std::size_t hash_value(const RiskFactorKey& k) {
31 std::size_t seed = 0;
32 boost::hash_combine(seed, k.keytype);
33 boost::hash_combine(seed, k.name);
34 boost::hash_combine(seed, k.index);
35 return seed;
36}
37
38bool Scenario::isCloseEnough(const QuantLib::ext::shared_ptr<Scenario>& s) const {
39 return asof() == s->asof() && label() == s->label() && QuantLib::close_enough(getNumeraire(), s->getNumeraire()) &&
40 keys() == s->keys() && std::all_of(keys().begin(), keys().end(), [this, s](const RiskFactorKey& k) {
41 return QuantLib::close_enough(this->get(k), s->get(k));
42 });
43}
44
45std::ostream& operator<<(std::ostream& out, const RiskFactorKey::KeyType& type) {
46 switch (type) {
48 return out << "DiscountCurve";
50 return out << "YieldCurve";
52 return out << "IndexCurve";
54 return out << "SwaptionVolatility";
56 return out << "YieldVolatility";
58 return out << "OptionletVolatility";
60 return out << "FXSpot";
62 return out << "FXVolatility";
64 return out << "EquitySpot";
66 return out << "EquityVolatility";
68 return out << "DividendYield";
70 return out << "SurvivalProbability";
72 return out << "SurvivalWeight";
74 return out << "RecoveryRate";
76 return out << "CrState";
78 return out << "CDSVolatility";
80 return out << "BaseCorrelation";
82 return out << "CPIIndex";
84 return out << "ZeroInflationCurve";
86 return out << "YoYInflationCurve";
88 return out << "YoYInflationCapFloorVolatility";
90 return out << "ZeroInflationCapFloorVolatility";
92 return out << "CommodityCurve";
94 return out << "CommodityVolatility";
96 return out << "SecuritySpread";
98 return out << "Correlation";
100 return out << "CPR";
101 default:
102 return out << "?";
103 }
104}
105
106std::ostream& operator<<(std::ostream& out, const RiskFactorKey& key) {
107 // If empty key just return empty string (not "?//0")
108 if (key == RiskFactorKey()) {
109 return out << "";
110 }
111
112 string keyStr = key.name;
113 size_t index = 0;
114 while (true) {
115 /* Locate the substring to replace. */
116 index = keyStr.find("/", index);
117 if (index == std::string::npos)
118 break;
119
120 keyStr.replace(index, 1, "\\/");
121 index += 2;
122 }
123
124 // If not empty key
125 return out << key.keytype << "/" << keyStr << "/" << key.index;
126}
127
129 if (str == "DiscountCurve")
131 else if (str == "YieldCurve")
133 else if (str == "IndexCurve")
135 else if (str == "SwaptionVolatility")
137 else if (str == "YieldVolatility")
139 else if (str == "OptionletVolatility")
141 else if (str == "FXSpot")
143 else if (str == "FXVolatility")
145 else if (str == "EquitySpot")
147 else if (str == "EquityVolatility")
149 else if (str == "DividendYield")
151 else if (str == "SurvivalProbability")
153 else if (str == "RecoveryRate")
155 else if (str == "CDSVolatility")
157 else if (str == "BaseCorrelation")
159 else if (str == "CPIIndex")
161 else if (str == "ZeroInflationCurve")
163 else if (str == "YoYInflationCurve")
165 else if (str == "YoYInflationCapFloorVolatility")
167 else if (str == "ZeroInflationCapFloorVolatility")
169 else if (str == "CommodityCurve")
171 else if (str == "CommodityVolatility")
173 else if (str == "SecuritySpread")
175 else if (str == "Correlation")
177 else if (str == "CPR")
179
180 QL_FAIL("RiskFactorKey " << str << " does not exist.");
181}
182
184 boost::escaped_list_separator<char> sep('\\', '/', '\"');
185 boost::tokenizer<boost::escaped_list_separator<char> > tokenSplit(str, sep);
186 vector<string> tokens(tokenSplit.begin(), tokenSplit.end());
187
188 QL_REQUIRE(tokens.size() == 3, "Could not parse key " << str);
189 RiskFactorKey rfk(parseRiskFactorKeyType(tokens[0]), tokens[1], parseInteger(tokens[2]));
190 return rfk;
191}
192
193ShiftScheme parseShiftScheme(const std::string& s) {
194 static map<string, ShiftScheme> m = {{"Forward", ShiftScheme::Forward},
195 {"Backward", ShiftScheme::Backward},
196 {"Central", ShiftScheme::Central}};
197 auto it = m.find(s);
198 if (it != m.end()) {
199 return it->second;
200 } else {
201 QL_FAIL("Cannot convert shift scheme \"" << s << "\" to ShiftScheme");
202 }
203}
204
205std::ostream& operator<<(std::ostream& out, const ShiftScheme& shiftScheme) {
206 if (shiftScheme == ShiftScheme::Forward)
207 return out << "Forward";
208 else if (shiftScheme == ShiftScheme::Backward)
209 return out << "Backward";
210 else if (shiftScheme == ShiftScheme::Central)
211 return out << "Central";
212 else
213 QL_FAIL("Invalid ShiftScheme " << static_cast<int>(shiftScheme));
214}
215
216ShiftType parseShiftType(const std::string& s) {
217 static map<string, ShiftType> m = {
218 {"Absolute", ShiftType::Absolute},
219 {"Relative", ShiftType::Relative}};
220 auto it = m.find(s);
221 if (it != m.end()) {
222 return it->second;
223 } else {
224 QL_FAIL("Cannot convert shift type \"" << s << "\" to ShiftType");
225 }
226}
227
228std::ostream& operator<<(std::ostream& out, const ShiftType& shiftType) {
229 if (shiftType == ShiftType::Absolute)
230 return out << "Absolute";
231 else if (shiftType == ShiftType::Relative)
232 return out << "Relative";
233 else
234 QL_FAIL("Invalid ShiftType " << shiftType);
235}
236
237} // namespace analytics
238} // namespace ore
Data types stored in the scenario class.
Definition: scenario.hpp:48
KeyType keytype
Key type.
Definition: scenario.hpp:89
std::string name
Key name.
Definition: scenario.hpp:94
KeyType
Risk Factor types.
Definition: scenario.hpp:51
virtual const string & label() const =0
Get the scenario label.
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 Real get(const RiskFactorKey &key) const =0
Get an element from the scenario.
virtual Real getNumeraire() const =0
Get Numeraire ratio n = N(t) / N(0) so that Price(0) = N(0) * E [Price(t) / N(t) ].
Integer parseInteger(const string &s)
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
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
ShiftType parseShiftType(const std::string &s)
Definition: scenario.cpp:216
Scenario class.