QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
getcovariance.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2003, 2004, 2009 Ferdinando Ametrano
5 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file getcovariance.hpp
22 \brief Covariance matrix calculation
23*/
24
25#ifndef quantlib_montecarlo_get_covariance_h
26#define quantlib_montecarlo_get_covariance_h
27
28#include <ql/math/matrix.hpp>
31
32namespace QuantLib {
33
34 //! Calculation of covariance from correlation and standard deviations
35 /*! Combines the correlation matrix and the vector of standard deviations
36 to return the covariance matrix.
37
38 Note that only the symmetric part of the correlation matrix is
39 used. Also it is assumed that the diagonal member of the
40 correlation matrix equals one.
41
42 \pre The correlation matrix must be symmetric with the diagonal
43 members equal to one.
44
45 \test tested on know values and cross checked with
46 CovarianceDecomposition
47 */
48 template<class DataIterator>
49 Matrix getCovariance(DataIterator stdDevBegin,
50 DataIterator stdDevEnd,
51 const Matrix& corr,
52 Real tolerance = 1.0e-12){
53 Size size = std::distance(stdDevBegin, stdDevEnd);
54 QL_REQUIRE(corr.rows() == size,
55 "dimension mismatch between volatilities (" << size <<
56 ") and correlation rows (" << corr.rows() << ")");
57 QL_REQUIRE(corr.columns() == size,
58 "correlation matrix is not square: " << size <<
59 " rows and " << corr.columns() << " columns");
60
61 Matrix covariance(size,size);
62 Size i, j;
63 DataIterator iIt, jIt;
64 for (i=0, iIt=stdDevBegin; i<size; ++i, ++iIt){
65 for (j=0, jIt=stdDevBegin; j<i; ++j, ++jIt){
66 QL_REQUIRE(std::fabs(corr[i][j]-corr[j][i]) <= tolerance,
67 "correlation matrix not symmetric:"
68 << "\nc[" << i << "," << j << "] = " << corr[i][j]
69 << "\nc[" << j << "," << i << "] = " << corr[j][i]);
70 covariance[i][i] = (*iIt) * (*iIt);
71 covariance[i][j] = (*iIt) * (*jIt) *
72 0.5 * (corr[i][j] + corr[j][i]);
73 covariance[j][i] = covariance[i][j];
74 }
75 QL_REQUIRE(std::fabs(corr[i][i]-1.0) <= tolerance,
76 "invalid correlation matrix, "
77 << "diagonal element of the " << io::ordinal(i+1)
78 << " row is " << corr[i][i] << " instead of 1.0");
79 covariance[i][i] = (*iIt) * (*iIt);
80 }
81 return covariance;
82 }
83
84 //! Covariance decomposition into correlation and variances
85 /*! Extracts the correlation matrix and the vector of variances
86 out of the input covariance matrix.
87
88 Note that only the lower symmetric part of the covariance matrix is
89 used.
90
91 \pre The covariance matrix must be symmetric.
92
93 \test cross checked with getCovariance
94 */
96 public:
97 /*! \pre covarianceMatrix must be symmetric */
99 const Matrix& covarianceMatrix,
100 Real tolerance = 1.0e-12);
101 /*! returns the variances Array */
102 const Array& variances() const { return variances_; }
103 /*! returns the standard deviations Array */
104 const Array& standardDeviations() const {return stdDevs_; }
105 /*! returns the correlation matrix */
106 const Matrix& correlationMatrix() const { return correlationMatrix_; }
107 private:
110 };
111
112}
113
114
115#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Covariance decomposition into correlation and variances.
const Matrix & correlationMatrix() const
const Array & standardDeviations() const
Matrix used in linear algebra.
Definition: matrix.hpp:41
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
output manipulators
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
matrix used in linear algebra.
Definition: any.hpp:35
Matrix getCovariance(DataIterator stdDevBegin, DataIterator stdDevEnd, const Matrix &corr, Real tolerance=1.0e-12)
Calculation of covariance from correlation and standard deviations.
pseudo square root of a real symmetric matrix