QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
getcovariance.hpp
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
25#ifndef quantlib_montecarlo_get_covariance_h
26#define quantlib_montecarlo_get_covariance_h
27
28#include <ql/math/matrix.hpp>
29#include <ql/utilities/dataformatters.hpp>
30#include <ql/math/matrixutilities/pseudosqrt.hpp>
31
32namespace QuantLib {
33
35
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
85
96 public:
99 const Matrix& covarianceMatrix,
100 Real tolerance = 1.0e-12);
102 const Array& variances() const { return variances_; }
104 const Array& standardDeviations() const {return stdDevs_; }
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
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
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.