Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
zerotoparcube.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
20
23
24#include <boost/numeric/ublas/vector.hpp>
25
26using namespace QuantLib;
27using namespace ore::analytics;
28
30using std::map;
31using std::set;
32using std::string;
33
34namespace ore {
35namespace analytics {
36
37ZeroToParCube::ZeroToParCube(const QuantLib::ext::shared_ptr<SensitivityCube>& zeroCube,
38 const QuantLib::ext::shared_ptr<ParSensitivityConverter>& parConverter,
39 const set<RiskFactorKey::KeyType>& typesDisabled, const bool continueOnError)
40 : ZeroToParCube(std::vector<QuantLib::ext::shared_ptr<SensitivityCube>>{zeroCube}, parConverter, typesDisabled,
41 continueOnError) {}
42
43ZeroToParCube::ZeroToParCube(const std::vector<QuantLib::ext::shared_ptr<SensitivityCube>>& zeroCubes,
44 const QuantLib::ext::shared_ptr<ParSensitivityConverter>& parConverter,
45 const set<RiskFactorKey::KeyType>& typesDisabled, const bool continueOnError)
46 : zeroCubes_(zeroCubes), parConverter_(parConverter), typesDisabled_(typesDisabled),
47 continueOnError_(continueOnError) {
48
49 Size counter = 0;
50 for (auto const& k : parConverter_->rawKeys()) {
51 factorToIndex_[k] = counter++;
52 }
53}
54
55map<RiskFactorKey, Real> ZeroToParCube::parDeltas(QuantLib::Size cubeIdx, QuantLib::Size tradeIdx) const {
56
57 DLOG("Calculating par deltas for trade index " << tradeIdx);
58
59 map<RiskFactorKey, Real> result;
60
61 // Get the "par-convertible" zero deltas
62 boost::numeric::ublas::vector<Real> zeroDeltas(parConverter_->rawKeys().size(), 0.0);
63
64 QL_REQUIRE(cubeIdx < zeroCubes_.size(),
65 "ZeroToParCube::parDeltas(): cubeIdx (" << cubeIdx << ") out of range 0..." << (zeroCubes_.size() - 1));
66
67 const QuantLib::ext::shared_ptr<SensitivityCube>& zeroCube = zeroCubes_[cubeIdx];
68 const QuantLib::ext::shared_ptr<NPVSensiCube>& sensiCube = zeroCube->npvCube();
69
70 std::set<RiskFactorKey> rkeys;
71 for (auto const& kv : sensiCube->getTradeNPVs(tradeIdx)) {
72 if (auto k = zeroCube->upDownFactor(kv.first); k.keytype != RiskFactorKey::KeyType::None)
73 rkeys.insert(k);
74 }
75
76 for (auto const& rk : rkeys) {
77 auto it = factorToIndex_.find(rk);
78 if (it == factorToIndex_.end()) {
79 if (ParSensitivityAnalysis::isParType(rk.keytype) && typesDisabled_.count(rk.keytype) != 1) {
80 if (continueOnError_) {
81 StructuredAnalyticsErrorMessage("Par conversion", "",
82 "Par factor " + ore::data::to_string(rk) +
83 " not found in factorToIndex map")
84 .log();
85 } else {
86 QL_REQUIRE(!ParSensitivityAnalysis::isParType(rk.keytype) || typesDisabled_.count(rk.keytype) == 1,
87 "ZeroToParCube::parDeltas(): par factor " << rk << " not found in factorToIndex map");
88 }
89 }
90 } else {
91 zeroDeltas[it->second] = zeroCube->delta(tradeIdx, rk);
92 }
93 }
94
95 // Convert the zero deltas to par deltas
96 boost::numeric::ublas::vector<Real> parDeltas = parConverter_->convertSensitivity(zeroDeltas);
97 Size counter = 0;
98 for (const auto& key : parConverter_->parKeys()) {
99 if (!close(parDeltas[counter], 0.0)) {
100 result[key] = parDeltas[counter];
101 }
102 counter++;
103 }
104
105 // Add non-zero deltas that do not need to be converted from underlying zero cube
106 for (const auto& f : rkeys) {
107 if (!ParSensitivityAnalysis::isParType(f.keytype) || typesDisabled_.count(f.keytype) == 1) {
108 Real delta = zeroCube->delta(tradeIdx, f);
109 if (!close(delta, 0.0)) {
110 result[f] = delta;
111 }
112 }
113 }
114
115 DLOG("Finished calculating par deltas for cube index " << cubeIdx << ", trade index " << tradeIdx);
116
117 return result;
118}
119
120map<RiskFactorKey, Real> ZeroToParCube::parDeltas(const string& tradeId) const {
121
122 DLOG("Calculating par deltas for trade " << tradeId);
123 map<RiskFactorKey, Real> result;
124
125 Size cubeIdx;
126 Size tradeIdx = Null<Size>();
127 for (cubeIdx = 0; cubeIdx < zeroCubes_.size() && tradeIdx == Null<Size>(); ++cubeIdx) {
128 try {
129 tradeIdx = zeroCubes_[cubeIdx]->npvCube()->getTradeIndex(tradeId);
130 break;
131 } catch (...) {
132 }
133 }
134 QL_REQUIRE(tradeIdx != Null<Size>(), "ZeroToParCube::parDeltas(): tradeId '"
135 << tradeId << "' not found in " << zeroCubes_.size() << " zero cubes.");
136
137 result = parDeltas(cubeIdx, tradeIdx);
138
139 DLOG("Finished calculating par deltas for trade " << tradeId);
140
141 return result;
142}
143
144} // namespace analytics
145} // namespace ore
static bool isParType(ore::analytics::RiskFactorKey::KeyType type)
Returns true if risk factor type is applicable for par conversion.
SensitivityCube is a wrapper for an npvCube that gives easier access to the underlying cube elements.
std::map< ore::analytics::RiskFactorKey, Size > factorToIndex_
std::set< ore::analytics::RiskFactorKey::KeyType > typesDisabled_
Set of risk factor types available for par conversion but that are disabled for this instance of Zero...
ZeroToParCube(const QuantLib::ext::shared_ptr< ore::analytics::SensitivityCube > &zeroCube, const QuantLib::ext::shared_ptr< ParSensitivityConverter > &parConverter, const std::set< ore::analytics::RiskFactorKey::KeyType > &typesDisabled={}, const bool continueOnError=false)
Constructor.
std::map< ore::analytics::RiskFactorKey, QuantLib::Real > parDeltas(const std::string &tradeId) const
Return the non-zero par deltas for the given tradeId.
std::vector< QuantLib::ext::shared_ptr< ore::analytics::SensitivityCube > > zeroCubes_
QuantLib::ext::shared_ptr< ParSensitivityConverter > parConverter_
#define DLOG(text)
bool close(const Real &t_1, const Real &t_2)
std::string to_string(const LocationInfo &l)
Structured analytics error.
Class for converting zero sensitivities to par sensitivities.