28 static Real tolerance = 1.e-6;
31 "Input matrix is not square");
35 #if defined(QL_EXTRA_SAFETY_CHECKS)
37 for (
Size iRow=0; iRow<mtrx.
rows(); iRow++)
38 for (
Size iCol=0; iCol<iRow; iCol++)
39 QL_REQUIRE(mtrx[iRow][iCol] == mtrx[iCol][iRow],
40 "input matrix is not symmetric");
42 *std::min_element(mtrx.
begin(), mtrx.
end()) >=-1,
43 "input matrix data is not correlation data");
47 std::vector<Real> previousCorrels(
n, 0.);
48 for(
Size iCol=0; iCol<
n; iCol++) {
49 for(
Size iRow=0; iRow<
n; iRow++)
50 previousCorrels[iCol] +=
51 mtrx[iRow][iCol] * mtrx[iRow][iCol];
52 previousCorrels[iCol] =
53 std::sqrt((previousCorrels[iCol]-1.)/(
n-1.));
61 for(
Size iCol=0; iCol<
n; iCol++)
63 previousCorrels[iCol];
69 std::max_element(eigenVals.
begin(), eigenVals.
end());
72 Size iMax = std::distance(eigenVals.
begin(), itMaxEval);
73 std::vector<Real> newCorrels, distances;
74 for(
Size iCol=0; iCol<
n; iCol++) {
75 Real thisCorrel = mtrx[iMax][iCol];
76 newCorrels.push_back(thisCorrel);
80 std::abs(thisCorrel - previousCorrels[iCol])/
81 previousCorrels[iCol]);
83 previousCorrels = newCorrels;
84 distance = *std::max_element(distances.begin(), distances.end());
85 }
while(distance > tolerance && ++iteration <= maxIters );
90 "convergence not reached after " <<
91 iteration <<
" iterations");
93 #if defined(QL_EXTRA_SAFETY_CHECKS)
94 QL_REQUIRE(*std::max_element(previousCorrels.begin(),
95 previousCorrels.end()) <=1 &&
96 *std::min_element(previousCorrels.begin(),
97 previousCorrels.end()) >=-1,
98 "matrix can not be decomposed to a single factor dependence");
101 return previousCorrels;
1-D array used in linear algebra.
const Real * const_iterator
const_iterator end() const
const_iterator begin() const
Matrix used in linear algebra.
const_iterator begin() const
const_iterator end() const
symmetric threshold Jacobi algorithm.
const Array & eigenvalues() const
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Single factor correlation reduction.
std::size_t Size
size of a container
std::vector< Real > factorReduction(Matrix mtrx, Size maxIters)
Eigenvalues/eigenvectors of a real symmetric matrix.