Page 1 of 1

Recursive PCA?

Posted: Fri Apr 27, 2012 1:58 am
by hansia
Hi all, i'm currently using roll add-in for rolling regressions since i know little about eviews programming. The challenge for me is to do the same for PCA, which unfortunately there's no add-ins available currently.

* Objective: generate time series of proportion of total variance accounted for by each principal components using a rolling PCA with a fixed window (say 20 days), rolled forward 1 day / step.

* Data set: daily time series of 19 financial variables expressed in percentage change terms. From 03Jan2005 to 27Apr2012.

Any idea how this can be done? Many thanks in advance!

I'm using Eviews7.

Re: Recursive PCA?

Posted: Fri Apr 27, 2012 9:28 am
by EViews Glenn
In principle there's nothing different about this calculation from the other rolling regression examples in the examples directory. The only difference is that you'll be grabbing the total variance rather than, say, coefficients. So you should take a look at the programming examples.

But you are correct that you'll have to roll your own.

Re: Recursive PCA?

Posted: Mon Apr 30, 2012 1:09 am
by hansia
In principle there's nothing different about this calculation from the other rolling regression examples in the examples directory. The only difference is that you'll be grabbing the total variance rather than, say, coefficients. So you should take a look at the programming examples.

But you are correct that you'll have to roll your own.
Hi Glenn, I've adapted the rolling regression model to save the first eigenvalue in PC1, but facing two problems here...
1. series of positive and negative numbers in PC1, which shouldn't be the case right?
2. error message at the end of program "missing value found in covariance/correlation matrix in DO_PCA1.MAKEPCOMP PC1"

Am I missing something here? Thanks...

____________________

' run rolling pca

' set window size
!window = 30

' set step size
!step = 1

' get size of workfile
!length = @obsrange

' declare group for pca (19 in total)
group pca1 AUD BE500 BUNDS COPPER EUR FTSE GBP GILTS GOLD JPY KRW NKK NSQ OIL SGD SHC SILV SPX UST

'calculate number of rolls
!nrolls = @round((!length-!window)/!step)

'variable keeping track of how many rolls we've done
!j=0

' move sample !step obs at a time
for !i = 1 to !length-!window+1-!step step !step
!j=!j+1

' set sample to estimation period
smpl @first+!i-1 @first+!i+!window-2

' store first eigenvalue of PCA in PC1
pca1.makepcomp pc1

next

Recursive PCA?

Posted: Mon Apr 30, 2012 2:30 am
by EViews Gareth
You might want to post your data too.

Re: Recursive PCA?

Posted: Tue May 01, 2012 5:52 pm
by hansia
You might want to post your data too.
Hi Gareth, data file is attached. All data in daily pct change. Thanks!

Re: Recursive PCA?

Posted: Tue May 08, 2012 7:31 pm
by hansia
Bumped... any idea what went wrong with the program? Thanks!

Re: Recursive PCA?

Posted: Sun May 20, 2012 11:43 pm
by hansia
Hi Glenn/Gareth, any idea what's wrong with the program please? I still can't figure it out. Thanks!

Re: Recursive PCA?

Posted: Mon May 21, 2012 7:46 am
by EViews Gareth
The error message is pretty explanatory - you have NAs in your data.

Re: Recursive PCA?

Posted: Mon May 21, 2012 10:09 am
by EViews Glenn
If I had to bet, there's a variable that has zero variance [edit] or, as Gareth tells me, has NAs for one of the subsamples.

Re: Recursive PCA?

Posted: Tue Jun 05, 2012 10:42 pm
by hansia
If I had to bet, there's a variable that has zero variance [edit] or, as Gareth tells me, has NAs for one of the subsamples.
Changing the sample range fixed the error message... but i'm still getting +ve and -ve values in pc1 series. Is the command below the wrong one to use for extracting the first eigenvalue to a series?

' store first eigenvalue of PCA in PC1
pca1.makepcomp pc1

Re: Recursive PCA?

Posted: Thu Jun 07, 2012 10:40 am
by EViews Glenn
That grabs the scores, which are derived from the loadings and the data. I'm not sure you want eigenvalues, but if you do

Code: Select all

pca1.pcomp(eigval=evals)
or the same using makepcomp instead of pcomp will save the eigenvalues into a vector which you can extract elements of as you wish.