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.
Recursive PCA?
Moderators: EViews Gareth, EViews Steve, EViews Moderator, EViews Jason
-
EViews Glenn
- EViews Developer
- Posts: 2682
- Joined: Wed Oct 15, 2008 9:17 am
Re: Recursive PCA?
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.
But you are correct that you'll have to roll your own.
Re: Recursive PCA?
Hi Glenn, I've adapted the rolling regression model to save the first eigenvalue in PC1, but facing two problems here...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.
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?
You might want to post your data too.
Re: Recursive PCA?
Hi Gareth, data file is attached. All data in daily pct change. Thanks!You might want to post your data too.
- Attachments
-
- pca test.wf1
- PCA file without program
- (401.18 KiB) Downloaded 264 times
Re: Recursive PCA?
Bumped... any idea what went wrong with the program? Thanks!
Re: Recursive PCA?
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?
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?
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?
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?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.
' 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?
That grabs the scores, which are derived from the loadings and the data. I'm not sure you want eigenvalues, but if you do
or the same using makepcomp instead of pcomp will save the eigenvalues into a vector which you can extract elements of as you wish.
Code: Select all
pca1.pcomp(eigval=evals)Who is online
Users browsing this forum: No registered users and 2 guests
