### g02aa demo

Given a set of n random variables
*
x _{1}, x_{2}, ... x_{n}
*,
an

*n*by

*n*correlation matrix is the matrix with

*(i,j)*element set equal to the correlation coefficient between the

^{th}*i*and

^{th}*j*variables.

^{th}
In financial applications, a correlation matrix can be constructed
to show the relationships between several stocks. However, because of
imperfect knowledge, the constructed matrix may not be a true
correlation matrix, which by definition must be symmetric
positive semi-definite and have diagonal elements which are all
equal to *1*.
(Positive semi-definite means that the matrix has non-negative eigenvalues).

In order to be able to use the constructed matrix in contexts which
require a true correlation matrix, it may be convenient to find the
*nearest correlation matrix*

The nag routine g02aa aims to accomplish this task, and this demo shows
it in operation. On the left hand side of the pane in the figure above,
a *4 by 4* matrix is shown.
Underneath the matrix is a histogram plot of the
eigenvalues of the matrix. Since eigenvalues may be real or complex,
the magnitudes of the real parts of the eigenvalues are plotted in blue,
and any imaginary part is plotted in red. Any eigenvalue
which is complex or negative prevents the original matrix from
being symmetric positive-definite.

The original matrix is passed to routine g02aa, which returns the nearest correlation matrix, displayed on the right side of the pane. Once again the eigenvalues are plotted. You should be able to see that the eigenvalues are now all real, with no imaginary parts, and no negative real parts.

The demo repeatedly modifies the original matrix one element at a time, and each time computes the nearest correlation matrix.

To stop the demo, press any key while the demo window has focus. Then select "stop" from the pop-up menu.