QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
getcovariance.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4Copyright (C) 2004, 2007, 2009 Ferdinando Ametrano
5
6This file is part of QuantLib, a free-software/open-source library
7for financial quantitative analysts and developers - http://quantlib.org/
8
9QuantLib is free software: you can redistribute it and/or modify it
10under the terms of the QuantLib license. You should have received a
11copy of the license along with this program; if not, please email
12<quantlib-dev@lists.sf.net>. The license is also available online at
13<http://quantlib.org/license.shtml>.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
21
22namespace QuantLib {
23
25 Real tolerance)
26 : variances_(cov.diagonal()), stdDevs_(Array(cov.rows())),
27 correlationMatrix_(Matrix(cov.rows(), cov.rows()))
28 {
29 Size size = cov.rows();
30 QL_REQUIRE(size==cov.columns(),
31 "input covariance matrix must be square, it is [" <<
32 size << "x" << cov.rows() << "]");
33
34 for (Size i=0; i<size; ++i)
35 {
36 stdDevs_[i] = std::sqrt(variances_[i]);
37 correlationMatrix_[i][i] = 1.0;
38 for (Size j=0; j<i; ++j)
39 {
40 QL_REQUIRE(std::fabs(cov[i][j]-cov[j][i]) <= tolerance,
41 "invalid covariance matrix:" <<
42 "\nc[" << i << ", " << j << "] = " <<
43 cov[i][j] << "\nc[" << j << ", " << i <<
44 "] = " << cov[j][i]);
46 cov[i][j]/(stdDevs_[i]*stdDevs_[j]);
47 }
48 }
49 }
50}
1-D array used in linear algebra.
Definition: array.hpp:52
CovarianceDecomposition(const Matrix &covarianceMatrix, Real tolerance=1.0e-12)
Matrix used in linear algebra.
Definition: matrix.hpp:41
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Covariance matrix calculation.
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