32#include <ql/math/matrixutilities/pseudosqrt.hpp>
33#include <ql/math/matrixutilities/symmetricschurdecomposition.hpp>
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}
58 QL_FAIL(
"ParametricVarParams Method \"" << s <<
"\" not recognized");
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";
74 QL_FAIL(
"Invalid ParametricVarCalculator::ParametricVarParams::Method");
78Real ParametricVarCalculator::var(Real confidence,
const bool isCall,
79 const set<pair<string, Size>>& tradeIds) {
80 Real factor = isCall ? 1.0 : -1.0;
82 Array delta(deltas_.size(), 0.0);
83 Matrix gamma(deltas_.size(), deltas_.size(), 0.0);
85 if (includeDeltaMargin_) {
87 for (
auto it = deltas_.begin(); it != deltas_.end(); it++)
88 delta[counter++] = factor * it->second;
91 if (includeGammaMargin_) {
93 for (
auto ito = deltas_.begin(); ito != deltas_.end(); ito++) {
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;
109 if (parametricVarParams_.method == ParametricVarCalculator::ParametricVarParams::Method::Delta)
111 else if (parametricVarParams_.method ==
112 ParametricVarCalculator::ParametricVarParams::Method::DeltaGammaNormal)
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)
123 else if (parametricVarParams_.method == ParametricVarCalculator::ParametricVarParams::Method::Saddlepoint) {
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_);
135 QL_FAIL(
"ParametricVarCalculator::computeVar(): method " << parametricVarParams_.method <<
" not known.");
138ParametricVarReport::ParametricVarReport(
const std::string& baseCurrency,
const ext::shared_ptr<Portfolio>& portfolio,
139 const string& portfolioFilter,
140 const vector<Real>& p,
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) {
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) {
168 varCalculator_ = QuantLib::ext::make_shared<ParametricVarCalculator>(
QuantLib::Matrix covarianceMatrix_
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.
void createVarCalculator() override
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 ¶metricVarParams, 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.
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.