Archivos Mensuales: febrero 2011

Matrix decomposition with QuantLib

QuantLib includes several matrix decomposition as well as Eigen values/vectors finding tools. If your memory needs a simple refresh in this area of linear algebra, you should visit the Wikipedia’s page on matrix decomposition. For more advanced discussions and other matrix-related topics, Math World reads numerous valuable pages.

In the code below, we shall implement some of these tools. As you will see, it’s quite straightforward. The matrix we work with is the standard square matrix :

#include<ql\quantlib.hpp>
using namespace QuantLib;

using std::endl;
using std::cout;

int main(int, char*[]){

	// Matrix declaration and population
	Matrix A(3,3);
	A[0][0] = 1.0; A[0][1] = 0.5; A[0][2] = 0.2;
	A[1][0] = 0.5; A[1][1] = 1.0; A[1][2] = 0.3;
	A[2][0] = 0.2; A[2][1] = 0.3; A[2][2] = 1.0;

	SymmetricSchurDecomposition schurDec(A);
	SVD SVDDec(A);

	// Outputting
	cout << "Matrix A : " << endl << A << endl;
	cout << "Eigen values (Schur): " << endl;
	schurDec.eigenvalues() << endl;
	cout << "Eigen vector (Schur) : " << endl;
	schurDec.eigenvectors() << endl;
	cout << "Cholesky decomposition : " << endl;
	CholeskyDecomposition(A) << endl;
	cout << "SVD U-matrix : " << endl;
	SVDDec.U() << endl;
	cout << "SVD V-matrix : ";
	endl << SVDDec.V() << endl;

	return 0;
}
Anuncios