! Compute the eigenvalues/vectors of a covariance matrix.(EigenCovarMat3.lng)
Alternatively, do Principal Components Analysis.
If there is a single large eigenvalue for the covariance
matrix, then this suggests that there is a single factor,
e.g., "the market" that explains all the variability;
! Some things to note,
1)In general, given a square matrix A, then we try to find
an eigenvalue, lambda, and its associated eigenvector X,
to satisfy the matrix equation:
A*X = lambda*X.
2) the sum of the eigenvalues = sum of the terms on the diagonal
of the original matrix, the variances if it is a covariance
matrix.
3) the product of the eigenvalues = determinant of the original matrix.
4) A positive definite matrix has all positive eigenvalues;
! Keywords: Eigenvalue, PCA, Principal Component Analysis,
Singular value decomposition, Covariance;
SETS:
ASSET: lambda;
CMAT( ASSET, ASSET) : X, COVAR;
ENDSETS DATA:
! The investments available(Vanguard funds);
ASSET = VG040 VG102 VG058 VG079 VG072 VG533;
! Covariance matrix, based on June 2004 to Dec 2005;
COVAR=
! VG040 VG102 VG058 VG079 VG072 VG533;
!VG040; 0.6576337E-02 0.7255873E-02 -0.7277427E-03 0.6160186E-02 0.5015276E-02 0.1082129E-01
!VG102; 0.7255873E-02 0.8300280E-02 -0.1067516E-02 0.7007361E-02 0.5651304E-02 0.1208505E-01
!VG058; -0.7277427E-03 -0.1067516E-02 0.1366187E-02 -0.1281051E-02 -0.1113421E-02 -0.1179400E-02
!VG079; 0.6160186E-02 0.7007361E-02 -0.1281051E-02 0.1020367E-01 0.8663464E-02 0.1477201E-01
!VG072; 0.5015276E-02 0.5651304E-02 -0.1113421E-02 0.8663464E-02 0.1568187E-01 0.1510857E-01
!VG533; 0.1082129E-01 0.1208505E-01 -0.1179400E-02 0.1477201E-01 0.1510857E-01 0.3002400E-01
ENDDATA
CALC:
@SET( 'TERSEO', 2); ! Turn off default output;
@SET( 'LINLEN', 100); ! Terminal page width (0:none);
! Compute the eigenvalues and eigenvectors of the matrix COVAR;
LAMBDA, X = @EIGEN( COVAR);
! Get the sum of the variances;
TOTVAR = @SUM( ASSET(j): COVAR(j,j));
CUMEVAL= 0;
@WRITE(' Eigen- Frac- Associated Eigenvector',@NEWLINE(1),
' value tion ');
@FOR( ASSET(j):
@WRITE( @FORMAT( ASSET(j), '%7s'));
);
@WRITE( @NEWLINE(1));
@FOR( ASSET(j):
CUMEVAL = CUMEVAL + LAMBDA(j);
@WRITE( @FORMAT(lambda(j),'6.3f'), @FORMAT( CUMEVAL/TOTVAR,'7.3f'),': ');
@FOR( ASSET(i):
@WRITE(' ',@FORMAT(X(i,j),'6.3f'));
);
@WRITE( @NEWLINE(1));
);
@WRITE(' Sum of variances = ', @SUM(ASSET(j): COVAR(j,j)), @NEWLINE(1));
@WRITE(' Sum of eigenvalues= ', @SUM(ASSET(j): LAMBDA(j)), @NEWLINE(1));
ENDCALC
|