SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
MaxCohen
Topic Author
Posts: 511
Joined: June 13th, 2007, 2:44 pm

C++ PCA, eigen vectors and values

May 16th, 2009, 3:42 pm

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.
 
User avatar
quantmeh
Posts: 5974
Joined: April 6th, 2007, 1:39 pm

C++ PCA, eigen vectors and values

May 16th, 2009, 4:22 pm

gsl
 
User avatar
Cuchulainn
Posts: 62053
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ PCA, eigen vectors and values

May 16th, 2009, 4:57 pm

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.
 
User avatar
ACD
Posts: 107
Joined: April 19th, 2004, 8:09 am

C++ PCA, eigen vectors and values

May 17th, 2009, 9:07 am

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.
 
User avatar
MaxCohen
Topic Author
Posts: 511
Joined: June 13th, 2007, 2:44 pm

C++ PCA, eigen vectors and values

May 18th, 2009, 6:03 am

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.
 
User avatar
katastrofa
Posts: 9186
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

C++ PCA, eigen vectors and values

May 18th, 2009, 10:00 am

I would recommend SVD.
 
User avatar
MaxCohen
Topic Author
Posts: 511
Joined: June 13th, 2007, 2:44 pm

C++ PCA, eigen vectors and values

May 18th, 2009, 10:18 am

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.
 
User avatar
Yossarian22
Posts: 184
Joined: March 15th, 2007, 2:27 am

C++ PCA, eigen vectors and values

May 19th, 2009, 4:17 am

TRED and TQLI functions in numerical recipes should do it. Y
 
User avatar
MaxCohen
Topic Author
Posts: 511
Joined: June 13th, 2007, 2:44 pm

C++ PCA, eigen vectors and values

May 19th, 2009, 6:04 am

What do TRED and TQLI implement I was going to use the Jacob function?
 
User avatar
PaperCut
Posts: 1616
Joined: May 14th, 2004, 6:45 pm

C++ PCA, eigen vectors and values

May 19th, 2009, 12:31 pm

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."
 
User avatar
MaxCohen
Topic Author
Posts: 511
Joined: June 13th, 2007, 2:44 pm

C++ PCA, eigen vectors and values

May 19th, 2009, 12:59 pm

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.
 
User avatar
PaperCut
Posts: 1616
Joined: May 14th, 2004, 6:45 pm

C++ PCA, eigen vectors and values

May 19th, 2009, 2:02 pm

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."
 
User avatar
MaxCohen
Topic Author
Posts: 511
Joined: June 13th, 2007, 2:44 pm

C++ PCA, eigen vectors and values

May 19th, 2009, 2:10 pm

Cool, I already have this book. Haven't read though, but I will take a look now.Cheers, Sam.
 
User avatar
rmeenaks
Posts: 186
Joined: May 1st, 2006, 2:31 pm

C++ PCA, eigen vectors and values

May 19th, 2009, 4:34 pm

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
 
User avatar
skebanga
Posts: 11
Joined: January 12th, 2011, 5:28 pm

C++ PCA, eigen vectors and values

November 29th, 2012, 11:34 pm

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.
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On