Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
parametricvar.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
20
29
31
32#include <ql/math/matrixutilities/pseudosqrt.hpp>
33#include <ql/math/matrixutilities/symmetricschurdecomposition.hpp>
34
35using namespace QuantLib;
36using namespace ore::data;
37using namespace std;
38
39namespace ore {
40namespace analytics {
41
43 : method(parseParametricVarMethod(m)), samples(samp), seed(sd) {}
44
46 static map<string, ParametricVarCalculator::ParametricVarParams::Method> m = {
47 {"Delta", ParametricVarCalculator::ParametricVarParams::Method::Delta},
48 {"DeltaGammaNormal", ParametricVarCalculator::ParametricVarParams::Method::DeltaGammaNormal},
49 {"MonteCarlo", ParametricVarCalculator::ParametricVarParams::Method::MonteCarlo},
50 {"Cornish-Fisher", ParametricVarCalculator::ParametricVarParams::Method::CornishFisher},
51 {"Saddlepoint", ParametricVarCalculator::ParametricVarParams::Method::Saddlepoint}
52 };
53
54 auto it = m.find(s);
55 if (it != m.end())
56 return it->second;
57 else
58 QL_FAIL("ParametricVarParams Method \"" << s << "\" not recognized");
59}
60
62 switch (method) {
63 case ParametricVarCalculator::ParametricVarParams::Method::Delta:
64 return out << "Delta";
65 case ParametricVarCalculator::ParametricVarParams::Method::DeltaGammaNormal:
66 return out << "DeltaGammaNormal";
67 case ParametricVarCalculator::ParametricVarParams::Method::MonteCarlo:
68 return out << "MonteCarlo";
69 case ParametricVarCalculator::ParametricVarParams::Method::CornishFisher:
70 return out << "Cornish-Fisher";
71 case ParametricVarCalculator::ParametricVarParams::Method::Saddlepoint:
72 return out << "Saddlepoint";
73 default:
74 QL_FAIL("Invalid ParametricVarCalculator::ParametricVarParams::Method");
75 }
76}
77
78Real ParametricVarCalculator::var(Real confidence, const bool isCall,
79 const set<pair<string, Size>>& tradeIds) {
80 Real factor = isCall ? 1.0 : -1.0;
81
82 Array delta(deltas_.size(), 0.0);
83 Matrix gamma(deltas_.size(), deltas_.size(), 0.0);
84
85 if (includeDeltaMargin_) {
86 Size counter = 0;
87 for (auto it = deltas_.begin(); it != deltas_.end(); it++)
88 delta[counter++] = factor * it->second;
89 }
90
91 if (includeGammaMargin_) {
92 Size outerIdx = 0;
93 for (auto ito = deltas_.begin(); ito != deltas_.end(); ito++) {
94 Size innerIdx = 0;
95 // Error if no diagonal element
96 gamma[outerIdx][outerIdx] = factor * gammas_.at(std::make_pair(ito->first, ito->first));
97 for (auto iti = deltas_.begin(); iti != ito; iti++) {
98 auto it = gammas_.find(std::make_pair(iti->first, ito->first));
99 if (it != gammas_.end()) {
100 gamma[innerIdx][outerIdx] = factor * it->second;
101 gamma[outerIdx][innerIdx] = factor * it->second;
102 }
103 innerIdx++;
104 }
105 outerIdx++;
106 }
107 }
108
109 if (parametricVarParams_.method == ParametricVarCalculator::ParametricVarParams::Method::Delta)
110 return QuantExt::deltaVar(omega_, delta, confidence, *covarianceSalvage_);
111 else if (parametricVarParams_.method ==
112 ParametricVarCalculator::ParametricVarParams::Method::DeltaGammaNormal)
113 return QuantExt::deltaGammaVarNormal(omega_, delta, gamma, confidence, *covarianceSalvage_);
114 else if (parametricVarParams_.method == ParametricVarCalculator::ParametricVarParams::Method::MonteCarlo) {
115 QL_REQUIRE(parametricVarParams_.samples != Null<Size>(),
116 "ParametricVarCalculator::computeVar(): method MonteCarlo requires mcSamples");
117 QL_REQUIRE(parametricVarParams_.seed != Null<Size>(),
118 "ParametricVarCalculator::computeVar(): method MonteCarlo requires mcSamples");
119 return QuantExt::deltaGammaVarMc<PseudoRandom>(omega_, delta, gamma, confidence, parametricVarParams_.samples,
120 parametricVarParams_.seed, *covarianceSalvage_);
121 } else if (parametricVarParams_.method == ParametricVarCalculator::ParametricVarParams::Method::CornishFisher)
122 return QuantExt::deltaGammaVarCornishFisher(omega_, delta, gamma, confidence, *covarianceSalvage_);
123 else if (parametricVarParams_.method == ParametricVarCalculator::ParametricVarParams::Method::Saddlepoint) {
124 Real res;
125 try {
126 res = QuantExt::deltaGammaVarSaddlepoint(omega_, delta, gamma, confidence, *covarianceSalvage_);
127 } catch (const std::exception& e) {
128 ALOG("Saddlepoint VaR computation exited with an error: " << e.what()
129 << ", falling back on Monte-Carlo");
130 res = QuantExt::deltaGammaVarMc<PseudoRandom>(omega_, delta, gamma, confidence,
131 parametricVarParams_.samples, parametricVarParams_.seed, *covarianceSalvage_);
132 }
133 return res;
134 } else
135 QL_FAIL("ParametricVarCalculator::computeVar(): method " << parametricVarParams_.method << " not known.");
136}
137
138ParametricVarReport::ParametricVarReport(const std::string& baseCurrency, const ext::shared_ptr<Portfolio>& portfolio,
139 const string& portfolioFilter,
140 const vector<Real>& p,
141 const ParametricVarCalculator::ParametricVarParams& parametricVarParams,
142 const bool salvageCovarianceMatrix,
143 boost::optional<ore::data::TimePeriod> period,
144 std::unique_ptr<SensiRunArgs> sensiArgs,
145 const bool breakdown)
146 : VarReport(baseCurrency, portfolio, portfolioFilter, p, period, nullptr, std::move(sensiArgs), nullptr, breakdown),
147 parametricVarParams_(parametricVarParams), salvageCovarianceMatrix_(salvageCovarianceMatrix) {
148 sensiBased_ = true;
149}
150
152 const std::string& baseCurrency,
153 const ext::shared_ptr<Portfolio>& portfolio,
154 const string& portfolioFilter,
155 const ext::shared_ptr<HistoricalScenarioGenerator>& hisScenGen,
156 const vector<Real>& p,
157 const ParametricVarCalculator::ParametricVarParams& parametricVarParams,
158 const bool salvageCovarianceMatrix,
159 boost::optional<ore::data::TimePeriod> period,
160 std::unique_ptr<SensiRunArgs> sensiArgs,
161 const bool breakdown)
162 : VarReport(baseCurrency, portfolio, portfolioFilter, p, period, hisScenGen, std::move(sensiArgs), nullptr, breakdown),
163 parametricVarParams_(parametricVarParams), salvageCovarianceMatrix_(salvageCovarianceMatrix) {
164 sensiBased_ = true;
165}
166
168 varCalculator_ = QuantLib::ext::make_shared<ParametricVarCalculator>(
170}
171
172} // namespace analytics
173} // namespace ore
std::map< RiskFactorKey, QuantLib::Real > deltas_
std::map< std::pair< RiskFactorKey, RiskFactorKey >, QuantLib::Real > gammas_
QuantLib::ext::shared_ptr< QuantExt::CovarianceSalvage > salvage_
ParametricVarCalculator::ParametricVarParams parametricVarParams_
The parameters to use for calculating the parametric VAR benchmark.
ParametricVarReport(const std::string &baseCurrency, const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const std::string &portfolioFilter, const std::vector< QuantLib::Real > &p, const ParametricVarCalculator::ParametricVarParams &parametricVarParams, const bool salvageCovarianceMatrix, boost::optional< ore::data::TimePeriod > period, std::unique_ptr< SensiRunArgs > sensiArgs=nullptr, const bool breakdown=false)
QuantLib::ext::shared_ptr< VarCalculator > varCalculator_
Class for generating sensi pnl.
#define ALOG(text)
Real deltaGammaVarCornishFisher(const Matrix &omega, const Array &delta, const Matrix &gamma, const Real p, const CovarianceSalvage &sal)
Real deltaGammaVarNormal(const Matrix &omega, const Array &delta, const Matrix &gamma, const Real p, const CovarianceSalvage &sal=NoCovarianceSalvage())
Real deltaVar(const Matrix &omega, const Array &delta, const Real p, const CovarianceSalvage &sal=NoCovarianceSalvage())
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
Real deltaGammaVarSaddlepoint(const Matrix &omega, const Array &delta, const Matrix &gamma, const Real p, const CovarianceSalvage &sal)
ParametricVarCalculator::ParametricVarParams::Method parseParametricVarMethod(const string &s)
Perform parametric var calculation for a given portfolio.
risk class and type filter
Class for calculating the shift multiple between two scenarios for a given key.
Shift scenario generation.
A container for holding the parametric VAR parameters.