Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
deltagammavar.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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
19/*! \file qle/math/deltagammavar.hpp
20 \brief functions to compute delta or delta-gamma VaR numbers
21 \ingroup math
22*/
23
24#ifndef quantext_deltagammavar_hpp
25#define quantext_deltagammavar_hpp
26
28
29#include <ql/math/comparison.hpp>
30#include <ql/math/array.hpp>
31#include <ql/math/matrix.hpp>
32#include <ql/math/matrixutilities/choleskydecomposition.hpp>
33#include <ql/math/matrixutilities/pseudosqrt.hpp>
34#include <ql/math/randomnumbers/rngtraits.hpp>
35
36// fix for boost 1.64, see https://lists.boost.org/Archives/boost/2016/11/231756.php
37#if BOOST_VERSION >= 106400
38#include <boost/serialization/array_wrapper.hpp>
39#endif
40#include <boost/accumulators/accumulators.hpp>
41#include <boost/accumulators/statistics/stats.hpp>
42#include <boost/accumulators/statistics/tail_quantile.hpp>
43#include <boost/foreach.hpp>
44
45namespace QuantExt {
46using namespace QuantLib;
47
48//! function that computes a delta VaR
49/*! For a given covariance matrix and a delta vector this function computes a parametric var w.r.t. a given
50 * confidence level for multivariate normal risk factors. */
51Real deltaVar(const Matrix& omega, const Array& delta, const Real p,
52 const CovarianceSalvage& sal = NoCovarianceSalvage());
53
54//! function that computes a delta-gamma normal VaR
55/*! For a given a covariance matrix, a delta vector and a gamma matrix this function computes a parametric var
56 * w.r.t. a given confidence level. The gamma matrix is taken into account when computing the variance of the PL
57 * distribution, but the PL distribution is still assumed to be normal. */
58Real deltaGammaVarNormal(const Matrix& omega, const Array& delta, const Matrix& gamma, const Real p,
59 const CovarianceSalvage& sal = NoCovarianceSalvage());
60
61//! function that computes a delta-gamma VaR using Monte Carlo (single quantile)
62/*! For a given a covariance matrix, a delta vector and a gamma matrix this function computes a parametric var
63 * w.r.t. a given confidence level. The var quantile is estimated from Monte-Carlo realisations of a second order
64 * sensitivity based PL. */
65template <class RNG>
66Real deltaGammaVarMc(const Matrix& omega, const Array& delta, const Matrix& gamma, const Real p, const Size paths,
67 const Size seed, const CovarianceSalvage& sal = NoCovarianceSalvage());
68
69//! function that computes a delta-gamma VaR using Monte Carlo (multiple quantiles)
70/*! For a given a covariance matrix, a delta vector and a gamma matrix this function computes a parametric var
71 * w.r.t. a vector of given confidence levels. The var quantile is estimated from Monte-Carlo realisations of a second
72 * order sensitivity based PL. */
73template <class RNG>
74std::vector<Real> deltaGammaVarMc(const Matrix& omega, const Array& delta, const Matrix& gamma,
75 const std::vector<Real>& p, const Size paths, const Size seed,
76 const CovarianceSalvage& sal = NoCovarianceSalvage());
77
78namespace detail {
79void check(const Real p);
80void check(const Matrix& omega, const Array& delta);
81void check(const Matrix& omega, const Array& delta, const Matrix& gamma);
82template <typename A> Real absMax(const A& a) {
83 Real tmp = 0.0;
84 BOOST_FOREACH (Real x, a) {
85 if (std::abs(x) > tmp)
86 tmp = std::abs(x);
87 }
88 return tmp;
89}
90} // namespace detail
91
92// implementation
93
94template <class RNG>
95std::vector<Real> deltaGammaVarMc(const Matrix& omega, const Array& delta, const Matrix& gamma,
96 const std::vector<Real>& p, const Size paths, const Size seed,
97 const CovarianceSalvage& sal) {
98 BOOST_FOREACH (Real q, p) { detail::check(q); }
99 detail::check(omega, delta, gamma);
100
101 Real num = std::max(detail::absMax(delta), detail::absMax(gamma));
102 if (QuantLib::close_enough(num, 0.0)) {
103 std::vector<Real> res(p.size(), 0.0);
104 return res;
105 }
106
107 Matrix L = sal.salvage(omega).second;
108 if (L.rows() == 0) {
109 L = CholeskyDecomposition(omega, true);
110 }
111
112 Real pmin = QL_MAX_REAL;
113 BOOST_FOREACH (Real q, p) { pmin = std::min(pmin, q); }
114
115 Size cache = Size(std::floor(static_cast<double>(paths) * (1.0 - pmin) + 0.5)) + 2;
116 boost::accumulators::accumulator_set<
117 double, boost::accumulators::stats<boost::accumulators::tag::tail_quantile<boost::accumulators::right> > >
118 acc(boost::accumulators::tag::tail<boost::accumulators::right>::cache_size = cache);
119
120 typename RNG::rsg_type rng = RNG::make_sequence_generator(delta.size(), seed);
121
122 for (Size i = 0; i < paths; ++i) {
123 std::vector<Real> seq = rng.nextSequence().value;
124 Array z(seq.begin(), seq.end());
125 Array u = L * z;
126 acc(DotProduct(u, delta) + 0.5 * DotProduct(u, gamma * u));
127 }
128
129 std::vector<Real> res;
130 BOOST_FOREACH (Real q, p) {
131 res.push_back(boost::accumulators::quantile(acc, boost::accumulators::quantile_probability = q));
132 }
133
134 return res;
135}
136
137template <class RNG>
138Real deltaGammaVarMc(const Matrix& omega, const Array& delta, const Matrix& gamma, const Real p, const Size paths,
139 const Size seed, const CovarianceSalvage& sal) {
140
141 std::vector<Real> pv(1, p);
142 return deltaGammaVarMc<RNG>(omega, delta, gamma, pv, paths, seed, sal).front();
143}
144
145/* delta-gamma VaR using Cornish-Fisher extrapolation (or normal delta-gamma VaR) */
146Real deltaGammaVarCornishFisher(const Matrix& omega, const Array& delta, const Matrix& gamma, const Real p,
147 const CovarianceSalvage& sal = NoCovarianceSalvage());
148
149/* delta-gamma VaR using Saddlepoint approximation */
150Real deltaGammaVarSaddlepoint(const Matrix& omega, const Array& delta, const Matrix& gamma, const Real p,
151 const CovarianceSalvage& sal = NoCovarianceSalvage());
152
153} // namespace QuantExt
154
155#endif
methods to make a symmetric matrix positive semidefinite
Real absMax(const A &a)
void check(const Real p)
Real deltaGammaVarCornishFisher(const Matrix &omega, const Array &delta, const Matrix &gamma, const Real p, const CovarianceSalvage &sal)
Real deltaGammaVarMc(const Matrix &omega, const Array &delta, const Matrix &gamma, const Real p, const Size paths, const Size seed, const CovarianceSalvage &sal=NoCovarianceSalvage())
function that computes a delta-gamma VaR using Monte Carlo (single quantile)
Real deltaGammaVarNormal(const Matrix &omega, const Array &delta, const Matrix &gamma, const Real p, const CovarianceSalvage &sal)
function that computes a delta-gamma normal VaR
Real deltaVar(const Matrix &omega, const Array &delta, const Real p, const CovarianceSalvage &sal)
function that computes a delta VaR
Real deltaGammaVarSaddlepoint(const Matrix &omega, const Array &delta, const Matrix &gamma, const Real p, const CovarianceSalvage &sal)
virtual std::pair< Matrix, Matrix > salvage(const Matrix &m) const =0