Hi,I have set-up a basic C++ matrix class and need a to run Principal Component Analysis to give me all the eigen vetcors and values of a given matrix.Any body have some source code, I can use to do this?I don't want to use an existing library like boost to do this, but create my own!Cheers, Sam.

- Cuchulainn
**Posts:**62053**Joined:****Location:**Amsterdam-
**Contact:**

QuoteOriginally posted by: samkhalilianHi,I have set-up a basic C++ matrix class and need a to run Principal Component Analysis to give me all the eigen vetcors and values of a given matrix.Any body have some source code, I can use to do this?I don't want to use an existing library like boost to do this, but create my own!Cheers, Sam.? If someone gives you code then you are not really creating your own code... Sneak preview?BTW boost does not have PCA.

Look up the power method if this is just for self education. That's very easy to implement and understand (although is slow when eigenvalues are close in numerical value). Will only have required you to have implemented matrix multiplication, addition and the L2 norm or a vector.

Last edited by ACD on May 16th, 2009, 10:00 pm, edited 1 time in total.

I found the Power method, Jacobian method, and SVD can be used for PCAThe later two are in numerical recipes where the Jacobion is simple to implement and is full proof method over the Power method, when dealing with "nice" matrices, so I will opt for this method, but probably implement them all if I have time.Thanks for the help.

- katastrofa
**Posts:**9186**Joined:****Location:**Alpha Centauri

I would recommend SVD.

I agree as a general tool, but it is perhaps a bit overkill for what I am doing since I have nice real and symmetric matrices.The Jacobian method is simple and shorter to program. SVD is something I'll code up if I have the time.

- Yossarian22
**Posts:**184**Joined:**

TRED and TQLI functions in numerical recipes should do it. Y

What do TRED and TQLI implement I was going to use the Jacob function?

QuoteOriginally posted by: samkhalilianHi,I have set-up a basic C++ matrix class and need a to run Principal Component Analysis to give me all the eigen vetcors and values of a given matrix.Any body have some source code, I can use to do this?I don't want to use an existing library like boost to do this, but create my own!Cheers, Sam.It seems like the right mix of "buy versus build" here would be to obtain Jackson & Staunton, take his code in VBA and "port" it to C++. It would be straightforward, it works, and you would have "done it yourself."

Hey PaperCut,Are you talking about the Jackson & Staunton book "Advanced Modelling in Finance using Excel and VBA" that has VBA PCA code in it?Cheers, Sam.

QuoteOriginally posted by: samkhalilianHey PaperCut,Are you talking about the Jackson & Staunton book "Advanced Modelling in Finance using Excel and VBA" that has VBA PCA code in it?Cheers, Sam.Bingo. It's a "must have."

Cool, I already have this book. Haven't read though, but I will take a look now.Cheers, Sam.

See Matrix 2.3. It is god-sent in terms of VBA and Matrix Manipulation:http://digilander.libero.it/foxes/Softw ... tmCreating PCA using these routines is very trivial...Cheers,Ram

Sam, I have C++ source code which I developed for the CQF final project - it is an implementation of Staunton's example. The code works through finding the variance/covariance matrix and then performing a Jacobian decomposition to calculate the eigenvectors and eigenvalues. PM me if you want me to send it to you.

GZIP: On