Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
riskfilter.cpp
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
21
22#include <ql/errors.hpp>
23#include <boost/algorithm/string.hpp>
24#include <boost/assign.hpp>
25#include <boost/bimap.hpp>
26
27namespace ore {
28namespace analytics {
29
30using std::string;
31using std::ostream;
32using boost::assign::list_of;
33
34// custom comparator for bimaps
35struct string_cmp {
36 bool operator()(const string& lhs, const string& rhs) const {
37 return ((boost::to_lower_copy(lhs)) < (boost::to_lower_copy(rhs)));
38 }
39};
40
41// Ease the notation below
42template <typename T> using bm = boost::bimap<T, boost::bimaps::set_of<string, string_cmp>>;
43
44// Initialise the bimaps
45const bm<MarketRiskConfiguration::RiskClass> riskClassMap = list_of<bm<MarketRiskConfiguration::RiskClass>::value_type>(
46 MarketRiskConfiguration::RiskClass::All, "All")
47 (MarketRiskConfiguration::RiskClass::InterestRate, "InterestRate")
48 (MarketRiskConfiguration::RiskClass::Inflation, "Inflation")
49 (MarketRiskConfiguration::RiskClass::Credit, "Credit")
50 (MarketRiskConfiguration::RiskClass::Equity, "Equity")
51 (MarketRiskConfiguration::RiskClass::FX, "FX");
52
53const bm<MarketRiskConfiguration::RiskType> riskTypeMap = list_of<bm<MarketRiskConfiguration::RiskType>::value_type>(
54 MarketRiskConfiguration::RiskType::All, "All")(
55 MarketRiskConfiguration::RiskType::DeltaGamma, "DeltaGamma")(MarketRiskConfiguration::RiskType::Vega, "Vega")(
56 MarketRiskConfiguration::RiskType::BaseCorrelation, "BaseCorrelation");
57
58ostream& operator<<(ostream& out, const MarketRiskConfiguration::RiskClass& rc) {
59 QL_REQUIRE(riskClassMap.left.count(rc) > 0,
60 "Risk class (" << static_cast<int>(rc) << ") not a valid MarketRiskConfiguration::RiskClass");
61 return out << riskClassMap.left.at(rc);
62}
63
64ostream& operator<<(ostream& out, const MarketRiskConfiguration::RiskType& mt) {
65 QL_REQUIRE(riskTypeMap.left.count(mt) > 0,
66 "Risk type (" << static_cast<int>(mt) << ") not a valid MarketRiskConfiguration::RiskType");
67 return out << riskTypeMap.left.at(mt);
68}
69
71 QL_REQUIRE(riskClassMap.right.count(rc) > 0,
72 "Risk class string " << rc << " does not correspond to a valid MarketRiskConfiguration::RiskClass");
73 return riskClassMap.right.at(rc);
74}
75
77 QL_REQUIRE(riskTypeMap.right.count(mt) > 0,
78 "Risk type string " << mt << " does not correspond to a valid MarketRiskConfiguration::RiskType");
79 return riskTypeMap.right.at(mt);
80}
81
82//! Give back a set containing the RiskClass values optionally excluding 'All'
83std::set<MarketRiskConfiguration::RiskClass> MarketRiskConfiguration::riskClasses(bool includeAll) {
84 Size numberOfRiskClasses = riskClassMap.size();
85
86 // Return the set of values
87 set<RiskClass> result;
88 for (Size i = includeAll ? 0 : 1; i < numberOfRiskClasses; ++i)
89 result.insert(RiskClass(i));
90
91 return result;
92}
93
94//! Give back a set containing the RiskType values optionally excluding 'All'
95std::set<MarketRiskConfiguration::RiskType> MarketRiskConfiguration::riskTypes(bool includeAll) {
96 Size numberOfRiskTypes = riskTypeMap.size();
97
98 // Return the set of values
99 set<RiskType> result;
100 for (Size i = includeAll ? 0 : 1; i < numberOfRiskTypes; ++i)
101 result.insert(RiskType(i));
102
103 return result;
104}
105
107
108 static const std::set<RiskFactorKey::KeyType> all = {RiskFactorKey::KeyType::DiscountCurve,
128
129 std::set<RiskFactorKey::KeyType> allowed_type, allowed;
130
131 if (riskType == MarketRiskConfiguration::RiskType::All) {
132 allowed_type = all;
133 } else {
134 switch (riskType) {
135 case MarketRiskConfiguration::RiskType::DeltaGamma:
149 break;
150 case MarketRiskConfiguration::RiskType::Vega:
160 break;
161 case MarketRiskConfiguration::RiskType::BaseCorrelation:
163 break;
164 default:
165 QL_FAIL("unexpected riskTypeIndex " << riskType);
166 }
167 }
168
169 if (riskClass == MarketRiskConfiguration::RiskClass::All) {
170 allowed = allowed_type;
171 } else {
172 std::set<RiskFactorKey::KeyType> allowed_class;
173 switch (riskClass) {
174 case MarketRiskConfiguration::RiskClass::InterestRate:
175 allowed_class = {
180 break;
181 case MarketRiskConfiguration::RiskClass::Inflation:
184 break;
185 case MarketRiskConfiguration::RiskClass::Credit:
188 break;
189 case MarketRiskConfiguration::RiskClass::Equity:
192 break;
193 case MarketRiskConfiguration::RiskClass::FX:
195 break;
196 default:
197 QL_FAIL("unexpected riskClassIndex " << riskClass);
198 }
199 std::set_intersection(allowed_type.begin(), allowed_type.end(), allowed_class.begin(), allowed_class.end(),
200 std::inserter(allowed, allowed.begin()));
201 }
202 // use the complement if smaller, the check is called frequently
203 if (allowed.size() > all.size() / 2) {
204 std::set_difference(all.begin(), all.end(), allowed.begin(), allowed.end(),
205 std::inserter(allowed_, allowed_.begin()));
206 neg_ = true;
207 } else {
208 allowed_ = allowed;
209 neg_ = false;
210 }
211}
212
213bool RiskFilter::allow(const RiskFactorKey& t) const {
214 bool found = std::find(allowed_.begin(), allowed_.end(), t.keytype) != allowed_.end();
215 return neg_ ? !found : found;
216}
217
218} // namespace analytics
219} // namespace ore
static std::set< RiskClass > riskClasses(bool includeAll=false)
Give back a set containing the RiskClass values optionally excluding 'All'.
Definition: riskfilter.cpp:83
static std::set< RiskType > riskTypes(bool includeAll=false)
Give back a set containing the RiskType values optionally excluding 'All'.
Definition: riskfilter.cpp:95
Data types stored in the scenario class.
Definition: scenario.hpp:48
KeyType keytype
Key type.
Definition: scenario.hpp:89
std::set< RiskFactorKey::KeyType > allowed_
Definition: riskfilter.hpp:78
RiskFilter(const MarketRiskConfiguration::RiskClass &riskClass, const MarketRiskConfiguration::RiskType &riskType)
Definition: riskfilter.cpp:106
bool allow(const RiskFactorKey &t) const override
Allow this key to be updated.
Definition: riskfilter.cpp:213
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
MarketRiskConfiguration::RiskType parseVarMarginType(const string &mt)
Definition: riskfilter.cpp:76
MarketRiskConfiguration::RiskClass parseVarRiskClass(const string &rc)
Definition: riskfilter.cpp:70
const bm< MarketRiskConfiguration::RiskClass > riskClassMap
Definition: riskfilter.cpp:45
boost::bimap< T, boost::bimaps::set_of< string, string_cmp > > bm
Definition: riskfilter.cpp:42
const bm< MarketRiskConfiguration::RiskType > riskTypeMap
Definition: riskfilter.cpp:53
risk class and type filter