Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
covariancesalvage.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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/covariancesalvage.hpp
20 \brief methods to make a symmetric matrix positive semidefinite
21 \ingroup math
22*/
23
24#pragma once
25
26#include <ql/math/matrixutilities/pseudosqrt.hpp>
27
28namespace QuantExt {
29
30using QuantLib::Matrix;
31using QuantLib::SalvagingAlgorithm;
32
33/*! Interface, salvage(m) should return (p,s) with
34 p = the salvaged (i.e. positive semidefinite) version of m
35 s = a square root of p, if provided, otherwise an empty Matrix
36
37 An implementation of this class represents a method to make a given covariance matrix m positive
38 semidefinite. This includes an implementation that just returns the input matrix unchanged, e.g. for
39 cases where it is known in advance / for theoretical reasons that the matrix m is positive semidefinite.
40
41 If the method produces a square root of the output matrix as a side product, this should be returned
42 in addition since many use cases that require a salvaged covariance matrix also require a square root
43 of this matrix e.g. for generating correlated normal random variates. A repeated computation of the
44 square root can be avoided this way. The returned square root may but is not required to be the
45 unique symmetric positive semidefinite square root of the salvaged covariance matrix p.
46
47 If the method does not provide a square root, an empty matrix Matrix() should be returned instead,
48 in which case the caller is responsible to compute a square root if required. */
50 virtual ~CovarianceSalvage() {}
51 virtual std::pair<Matrix, Matrix> salvage(const Matrix& m) const = 0;
52};
53
54//! Implementation that does not change the input matrix
56 std::pair<Matrix, Matrix> salvage(const Matrix& m) const override { return std::make_pair(m, Matrix()); }
57};
58
59//! Implementation that uses the spectral method
61 std::pair<Matrix, Matrix> salvage(const Matrix& m) const override {
62 auto L = pseudoSqrt(m, SalvagingAlgorithm::Spectral);
63 return std::make_pair(L * transpose(L), L);
64 }
65};
66
67} // namespace QuantExt
virtual std::pair< Matrix, Matrix > salvage(const Matrix &m) const =0
Implementation that does not change the input matrix.
std::pair< Matrix, Matrix > salvage(const Matrix &m) const override
Implementation that uses the spectral method.
std::pair< Matrix, Matrix > salvage(const Matrix &m) const override