25#ifndef quantlib_montecarlo_get_covariance_h
26#define quantlib_montecarlo_get_covariance_h
48 template<
class DataIterator>
50 DataIterator stdDevEnd,
52 Real tolerance = 1.0e-12){
53 Size size = std::distance(stdDevBegin, stdDevEnd);
55 "dimension mismatch between volatilities (" << size <<
56 ") and correlation rows (" << corr.
rows() <<
")");
58 "correlation matrix is not square: " << size <<
59 " rows and " << corr.
columns() <<
" columns");
61 Matrix covariance(size,size);
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];
75 QL_REQUIRE(std::fabs(corr[i][i]-1.0) <= tolerance,
76 "invalid correlation matrix, "
78 <<
" row is " << corr[i][i] <<
" instead of 1.0");
79 covariance[i][i] = (*iIt) * (*iIt);
99 const Matrix& covarianceMatrix,
100 Real tolerance = 1.0e-12);
1-D array used in linear algebra.
Covariance decomposition into correlation and variances.
const Matrix & correlationMatrix() const
const Array & standardDeviations() const
Matrix correlationMatrix_
const Array & variances() const
Matrix used in linear algebra.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
std::size_t Size
size of a container
matrix used in linear algebra.
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