Recursive PCA?

For questions regarding the import, export and manipulation of data in EViews, including graphing and basic statistics.

Moderators: EViews Gareth, EViews Steve, EViews Moderator, EViews Jason

hansia
Posts: 6
Joined: Fri Apr 27, 2012 1:41 am

Recursive PCA?

Postby hansia » Fri Apr 27, 2012 1:58 am

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.

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: Recursive PCA?

Postby EViews Glenn » Fri Apr 27, 2012 9:28 am

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.

hansia
Posts: 6
Joined: Fri Apr 27, 2012 1:41 am

Re: Recursive PCA?

Postby hansia » Mon Apr 30, 2012 1:09 am

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

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Recursive PCA?

Postby EViews Gareth » Mon Apr 30, 2012 2:30 am

You might want to post your data too.

hansia
Posts: 6
Joined: Fri Apr 27, 2012 1:41 am

Re: Recursive PCA?

Postby hansia » Tue May 01, 2012 5:52 pm

You might want to post your data too.
Hi Gareth, data file is attached. All data in daily pct change. Thanks!
Attachments
pca test.wf1
PCA file without program
(401.18 KiB) Downloaded 263 times

hansia
Posts: 6
Joined: Fri Apr 27, 2012 1:41 am

Re: Recursive PCA?

Postby hansia » Tue May 08, 2012 7:31 pm

Bumped... any idea what went wrong with the program? Thanks!

hansia
Posts: 6
Joined: Fri Apr 27, 2012 1:41 am

Re: Recursive PCA?

Postby hansia » Sun May 20, 2012 11:43 pm

Hi Glenn/Gareth, any idea what's wrong with the program please? I still can't figure it out. Thanks!

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: Recursive PCA?

Postby EViews Gareth » Mon May 21, 2012 7:46 am

The error message is pretty explanatory - you have NAs in your data.

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: Recursive PCA?

Postby EViews Glenn » Mon May 21, 2012 10:09 am

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.

hansia
Posts: 6
Joined: Fri Apr 27, 2012 1:41 am

Re: Recursive PCA?

Postby hansia » Tue Jun 05, 2012 10:42 pm

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

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: Recursive PCA?

Postby EViews Glenn » Thu Jun 07, 2012 10:40 am

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.


Return to “Data Manipulation”

Who is online

Users browsing this forum: No registered users and 1 guest