Page 1 of 1
Time series of rolling eigenvalue proportion (from PCA)
Posted: Thu Jul 17, 2014 8:32 am
by talshaps
Hi,
I would like to run a rolling PCA of weekly returns of a basket of risky assets. To understand the evolution over time of the co-movement of the different assets in the basket, what I need as output is a time series of the proportion of the eigenvalue of the 1st principal component from each 2-year window sample, rolled over a week forward at the time (from 1/1/1995- 7/6/2014). two questions: 1) I want the computation to use ordinary covariances, so included 'cov' in the options, but I am still getting ordinary correlations in the output. what am I doing wrong? 2) I realize I need to use the following commands, but not sure how to build the loop, and generate and save a time series of the proportion of the first eigenvalue.
group gbasket brent copper alum nickel plat cotton spx dax hk_eq bra_eq Chn_eq Ind_eq spxmatr consdis aud nzd adxy sek cad
gbasket.pcomp (cov, eigval=eval)
Can you please help me with this?
Thanks very much in advance,
Tal
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Thu Jul 17, 2014 8:39 am
by EViews Gareth
You have a space between the pcomp proc and its options. Remove the space.
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Thu Jul 17, 2014 8:54 am
by talshaps
Ok great, so that answers my first question... Thank you very much. Can you kindly also explain how I should construct the loop to get a weekly rolling eigenvalue proportion of 2-year window?
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Thu Jul 17, 2014 9:02 am
by EViews Gareth
Do a search for "rolling" on this forum - there are tons of examples.
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Thu Jul 17, 2014 10:13 am
by talshaps
Thank you Gareth. I looked it up, and also looked at the 'roll' examples you provide in the sample programs. I think it should be something like the below, however I get an error on the final command, when I try to store the output (the proportion of the first PC eigenvalues) in a vector... Can you please tell me what is the problem?
Thanks again very much for your help
group gbasket brent copper alum nickel plat cotton spx dax hk_eq bra_eq Chn_eq Ind_eq spxmatr consdis aud nzd adxy sek cad
' set window size
!window = 104
' set step size
!step = 1
' get size of workfile
!length = @obsrange
'calculate number of rolls
!nrolls = @floor((!length-!window)/!step)
'vector to store eigenvalue proportions
vector(!nrolls) evalpc1
' 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 @first+!i+!window-2
' principle component estimation
gbasket.pcomp(cov, eigval=eval, cproport) pc1
' store eigenvalue proportions
evalpc1(!1) = @cproport(gbasket)
next
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Thu Jul 17, 2014 10:40 am
by EViews Gareth
The pcomp group proc doesn't have a "cproport" option. I'm not sure what you're trying to do with the lines:
Code: Select all
gbasket.pcomp(cov, eigval=eval, cproport) pc1
evalpc1(!1) = @cproport(gbasket)
Edit: Sorry, now I see that it does.
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Thu Jul 17, 2014 10:52 am
by talshaps
The 'cproport' (as defined in the Eviews 7 help section) is what generates the cumulative proportion of eigenvalues. This is the closest I could find to what I am looking for. The first line you quoted is supposed to run the principal component analysis and generate the eigenvalues and proportion of the first PC (1st eigenvalue/total sum of eigenvalues) in a rolling manner, ie generate one value (=proportion) for each time window, on a weekly rolling basis, until I get a full time series. Then the second line is just trying to store this time series in the vector I created...
Is there no function that will allow me to perform this in one direct way? Do you think perhaps I need to just request for a time series of the rolling 1st principal component eigenvalue, and then divide the series myself by the sum of eigenvalues?
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Fri Aug 22, 2014 10:12 am
by hedgie
I get the same error message - Gareth, any suggestions?
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Fri Aug 22, 2014 10:23 am
by EViews Gareth
Store the eigenvalues using the eigval= option in pcomp. Then if you want the proportion of a single eigenvalue, divide it by the sum of them:
Code: Select all
gbasket.pcomp(cov, eigval=eval) pc1
' store eigenvalue proportions
!cumpc = eval(1)/@sum(eval)
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Fri Aug 22, 2014 10:43 am
by hedgie
Thank you. That seemed to work. However, I'm getting all zeros.
The entire code is below: can you suggest any changes?
' set window size
!window = 104
' set step size
!step = 1
' get size of workfile
!length = @obsrange
'calculate number of rolls
!nrolls = @floor((!length-!window)/!step)
'vector to store eigenvalue proportions
vector(!nrolls) evalpc1
' 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 @first+!i+!window-2
' principle component estimation
gbasket.pcomp(cor, eigval=eval) pc1
' store eigenvalue proportions
!cumpc = eval(1)/@sum(eval)
next
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Fri Aug 22, 2014 11:46 am
by EViews Gareth
You have to put the !cumpc somewhete
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Fri Aug 22, 2014 12:52 pm
by hedgie
meaning !cumpc needs to be stored in the vector evalpc1? Can't seem to get the data stored in evalpc1
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Fri Aug 22, 2014 1:06 pm
by EViews Gareth
Re: Time series of rolling eigenvalue proportion (from PCA)
Posted: Fri Aug 22, 2014 1:11 pm
by hedgie
That worked, thank you.