24#ifndef quantext_deltagammavar_hpp
25#define quantext_deltagammavar_hpp
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>
37#if BOOST_VERSION >= 106400
38#include <boost/serialization/array_wrapper.hpp>
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>
51Real
deltaVar(
const Matrix& omega,
const Array& delta,
const Real p,
52 const CovarianceSalvage& sal = NoCovarianceSalvage());
58Real
deltaGammaVarNormal(
const Matrix& omega,
const Array& delta,
const Matrix& gamma,
const Real p,
59 const CovarianceSalvage& sal = NoCovarianceSalvage());
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());
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());
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) {
84 BOOST_FOREACH (Real x, a) {
85 if (std::abs(x) > tmp)
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,
102 if (QuantLib::close_enough(num, 0.0)) {
103 std::vector<Real> res(p.size(), 0.0);
107 Matrix L = sal.
salvage(omega).second;
109 L = CholeskyDecomposition(omega,
true);
112 Real pmin = QL_MAX_REAL;
113 BOOST_FOREACH (Real q, p) { pmin = std::min(pmin, q); }
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);
120 typename RNG::rsg_type rng = RNG::make_sequence_generator(delta.size(), seed);
122 for (Size i = 0; i < paths; ++i) {
123 std::vector<Real> seq = rng.nextSequence().value;
124 Array z(seq.begin(), seq.end());
126 acc(DotProduct(u, delta) + 0.5 * DotProduct(u, gamma * u));
129 std::vector<Real> res;
130 BOOST_FOREACH (Real q, p) {
131 res.push_back(boost::accumulators::quantile(acc, boost::accumulators::quantile_probability = q));
138Real
deltaGammaVarMc(
const Matrix& omega,
const Array& delta,
const Matrix& gamma,
const Real p,
const Size paths,
141 std::vector<Real> pv(1, p);
142 return deltaGammaVarMc<RNG>(omega, delta, gamma, pv, paths, seed, sal).front();
147 const CovarianceSalvage& sal = NoCovarianceSalvage());
151 const CovarianceSalvage& sal = NoCovarianceSalvage());
methods to make a symmetric matrix positive semidefinite
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