Page 1 of 2

C++ PCA, eigen vectors and values

Posted: May 16th, 2009, 3:42 pm
by MaxCohen
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.

C++ PCA, eigen vectors and values

Posted: May 16th, 2009, 4:22 pm
by quantmeh
gsl

C++ PCA, eigen vectors and values

Posted: May 16th, 2009, 4:57 pm
by Cuchulainn
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.

C++ PCA, eigen vectors and values

Posted: May 17th, 2009, 9:07 am
by ACD
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.

C++ PCA, eigen vectors and values

Posted: May 18th, 2009, 6:03 am
by MaxCohen
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.

C++ PCA, eigen vectors and values

Posted: May 18th, 2009, 10:00 am
by katastrofa
I would recommend SVD.

C++ PCA, eigen vectors and values

Posted: May 18th, 2009, 10:18 am
by MaxCohen
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.

C++ PCA, eigen vectors and values

Posted: May 19th, 2009, 4:17 am
by Yossarian22
TRED and TQLI functions in numerical recipes should do it. Y

C++ PCA, eigen vectors and values

Posted: May 19th, 2009, 6:04 am
by MaxCohen
What do TRED and TQLI implement I was going to use the Jacob function?

C++ PCA, eigen vectors and values

Posted: May 19th, 2009, 12:31 pm
by PaperCut
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."

C++ PCA, eigen vectors and values

Posted: May 19th, 2009, 12:59 pm
by MaxCohen
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.

C++ PCA, eigen vectors and values

Posted: May 19th, 2009, 2:02 pm
by PaperCut
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."

C++ PCA, eigen vectors and values

Posted: May 19th, 2009, 2:10 pm
by MaxCohen
Cool, I already have this book. Haven't read though, but I will take a look now.Cheers, Sam.

C++ PCA, eigen vectors and values

Posted: May 19th, 2009, 4:34 pm
by rmeenaks
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

C++ PCA, eigen vectors and values

Posted: November 29th, 2012, 11:34 pm
by skebanga
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.