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;
}
About these ads

One thought on “Matrix decomposition with QuantLib

  1. Pingback: Generation of correlated random numbers: recommended article | Quant Corner

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s