Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
simmresults.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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/utilities/null.hpp>
23
24using QuantLib::Null;
25using QuantLib::Real;
26using std::get;
27using std::make_tuple;
28using std::ostream;
29using std::make_pair;
30using std::string;
31
32namespace ore {
33namespace analytics {
34
36 const SimmConfiguration::MarginType& mt, const string& b, Real im, const string& resultCurrency,
37 const string& calculationCurrency, const bool overwrite) {
38 if (!(mt == SimmConfiguration::MarginType::AdditionalIM || mt == SimmConfiguration::MarginType::All))
39 QL_REQUIRE(im >= 0.0, "Cannot add negative IM " << im << " result to SimmResults for RiskClass=" << rc
40 << ", MarginType=" << mt << ", and Bucket=" << b);
41
42 const auto key = make_tuple(pc, rc, mt, b);
43 add(key, im, resultCurrency, calculationCurrency, overwrite);
44}
45
46
47void SimmResults::add(const SimmResults::Key& key, QuantLib::Real im, const std::string& resultCurrency,
48 const std::string& calculationCurrency, const bool overwrite) {
49 // Add the value as long as the currencies are matching. If the SimmResults container does not yet have
50 // a currency, we set it to be that of the incoming value
51 if (resultCcy_.empty())
53 else
54 QL_REQUIRE(resultCurrency == resultCcy_,
55 "Cannot add value to SimmResults with a different result currency ("
56 << resultCurrency << "). Expected " << resultCcy_ << ".");
57
58 if (calcCcy_.empty())
60 else
61 QL_REQUIRE(calculationCurrency == calcCcy_,
62 "Cannot add value to SimmResults in a different calculation currency ("
63 << calculationCurrency << "). Expected " << calcCcy_ << ".");
64
65 const bool hasResults = data_.find(key) != data_.end();
66 if (hasResults && !overwrite)
67 data_[key] += im;
68 else
69 data_[key] = im;
70 }
71
72
73void SimmResults::convert(const QuantLib::ext::shared_ptr<ore::data::Market>& market, const string& currency) {
74 // Get corresponding FX spot rate
75 Real fxSpot = market->fxRate(resultCcy_ + currency)->value();
76
77 convert(fxSpot, currency);
78}
79
80void SimmResults::convert(Real fxSpot, const string& currency) {
81 // Check that target currency is valid
82 QL_REQUIRE(ore::data::checkCurrency(currency), "Cannot convert SIMM results. The target currency ("
83 << currency << ") must be a valid ISO currency code");
84
85 // Skip if already in target currency
86 if (resultCcy_ == currency)
87 return;
88
89 // Convert SIMM results to target currency
90 for (auto& sr : data_)
91 sr.second *= fxSpot;
92
93 // Update currency
94 resultCcy_ = currency;
95}
96
98 const SimmConfiguration::MarginType& mt, const string b) const {
99 if (has(pc, rc, mt, b)) {
100 return data_.at(make_tuple(pc, rc, mt, b));
101 } else {
102 return Null<Real>();
103 }
104}
105
107 const SimmConfiguration::MarginType& mt, const string b) const {
108 return data_.count(make_tuple(pc, rc, mt, b)) > 0;
109}
110
111bool SimmResults::empty() const { return data_.empty(); }
112
113void SimmResults::clear() { data_.clear(); }
114
115ostream& operator<<(ostream& out, const SimmResults::Key& k) {
116 return out << "[" << get<0>(k) << ", " << get<1>(k) << ", " << get<2>(k) << ", " << get<3>(k) << "]";
117}
118
119} // namespace analytics
120} // namespace ore
std::string & calculationCurrency()
Definition: simmresults.hpp:98
bool empty() const
Return true if the container is empty, otherwise false.
void add(const CrifRecord::ProductClass &pc, const SimmConfiguration::RiskClass &rc, const SimmConfiguration::MarginType &mt, const std::string &b, QuantLib::Real im, const std::string &resultCurrency, const std::string &calculationCurrency, const bool overwrite)
std::tuple< ProductClass, RiskClass, MarginType, std::string > Key
Definition: simmresults.hpp:43
bool has(const CrifRecord::ProductClass &pc, const SimmConfiguration::RiskClass &rc, const SimmConfiguration::MarginType &mt, const std::string b) const
void convert(const QuantLib::ext::shared_ptr< ore::data::Market > &market, const std::string &currency)
Convert SIMM amounts to a different currency.
Definition: simmresults.cpp:73
QuantLib::Real get(const CrifRecord::ProductClass &pc, const SimmConfiguration::RiskClass &rc, const SimmConfiguration::MarginType &mt, const std::string b) const
Definition: simmresults.cpp:97
void clear()
Clear the results from the container.
std::string & resultCurrency()
Definition: simmresults.hpp:95
std::map< Key, QuantLib::Real > data_
bool checkCurrency(const string &code)
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
Class for holding SIMM results.