Step-by-Step PCA algorithm (checking correctness without math packages)

I would appreciate if someone could correct me if i am wrong in my suggestion.

I am using PCA to :

• find measure of cointegration between selected assets
• find the eigenvector and its portfolio with a market-neutral position (min variance)

Unfortunately i am not sure whether i am doing it the right way. Here is my algorithm :

• getting the base matrix N x M : N - number of assets, M - number of history samples
• getting simple covariance matrix : Cov = E[(X - E[X])(Y - E[Y])]
• solving eigenproblem using Jacobi's Rotation method : [example]
• finding the index of the biggest eigenvalue by module : MinVariance = Min(Abs(eigenvalue))
• eigenvector can be found as a column in the rotation matrix by index of eigenvalue

The question is : did i miss something in this list of actions according to my initial purposes mentioned above?

I am asking because i already calculated these weights for selected currencies but they look odd to me because e.g. EURUSD and GBPUSD seem to be opposite to each other when everyone knows that they are highly correlated and moves together most of the time

http://c.mql5cdn.com/3/28/USDCHFM1.png

Here is my implementation of PCA on a C++ similar language called MQL

http://www.mql5.com/ru/forum/16512/page3#comment_732844 (see attachment)

• You ask too much in one question. To answer a part: no: minimal variance is not related to the largest eigenvalue but rather too the smallest. And: inverting a matrix is not an estimator - what do you mean? Please rephrase the question. It starts with PCA and then goes to shrinkage. There is too much going on in this question – Richard Jan 22 '14 at 8:12
• Simplified my question - it is only about correctness of PCA algorithm now – Anonymous Jan 22 '14 at 10:55
• And what about this answer - i presume it says that minimum eigenvalue points to minimum variance, i mean this quotation : "As a side note, you could form a eigenportfolio that has minimum variance by identifying a principal component with a low eigenvalue" – Anonymous Jan 22 '14 at 11:04
• @Richard: in addition i found this answer saying that biggest eigenvalue adds the most variance to the portfolio so i presume that opposite suggestion about min variance is also true - quant.stackexchange.com/questions/4607/… – Anonymous Jan 22 '14 at 13:25
• Yes .. as far as I rememeber you had something like "largest eigenvalue" and "small variance". If you know that large eigenvalue measn large variance and vice-versa then it is fine. – Richard Jan 22 '14 at 14:32

To close this question.

Steps used, in short :

• get matrix N x M where N - number of assets, M - number of history samples
• normalize all samples using logarithms and mean to have returns instead of some asset specific values
• obtain covariance matrix, or correlation, if you want to avoid influence of volatility
• solve eigenproblem using SVD and Jacobi's rotation on covariance matrices
• Jacobi rotation returns two matrices - eigenvectors and eigenvalues

Understanding results :

• eigenvalue stands for variance of the portfolio's spread
• choosing max eigenvalue means selecting direction of entire portfolio
• choosing min eigenvalue means selecting periodic, noise or error component of portfolio with min variance
• each value in eigenvector is weighting coefficient for relevant asset in portfolio which asset needs to be multiplied by to get back to initial course
• eigenvector is a column in rotation matrix taken by index of selected eigenvalue

Resources :