Time series of rolling eigenvalue proportion (from PCA)

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt

talshaps
Posts: 4
Joined: Thu Jul 17, 2014 7:20 am

Time series of rolling eigenvalue proportion (from PCA)

Postby talshaps » Thu Jul 17, 2014 8:32 am

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

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

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby EViews Gareth » Thu Jul 17, 2014 8:39 am

You have a space between the pcomp proc and its options. Remove the space.

talshaps
Posts: 4
Joined: Thu Jul 17, 2014 7:20 am

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby talshaps » Thu Jul 17, 2014 8:54 am

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?

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

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby EViews Gareth » Thu Jul 17, 2014 9:02 am

Do a search for "rolling" on this forum - there are tons of examples.

talshaps
Posts: 4
Joined: Thu Jul 17, 2014 7:20 am

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby talshaps » Thu Jul 17, 2014 10:13 am

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

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

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby EViews Gareth » Thu Jul 17, 2014 10:40 am

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.

talshaps
Posts: 4
Joined: Thu Jul 17, 2014 7:20 am

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby talshaps » Thu Jul 17, 2014 10:52 am

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?

hedgie
Posts: 12
Joined: Fri Jul 25, 2014 2:29 am

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby hedgie » Fri Aug 22, 2014 10:12 am

I get the same error message - Gareth, any suggestions?

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

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby EViews Gareth » Fri Aug 22, 2014 10:23 am

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)

hedgie
Posts: 12
Joined: Fri Jul 25, 2014 2:29 am

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby hedgie » Fri Aug 22, 2014 10:43 am

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

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

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby EViews Gareth » Fri Aug 22, 2014 11:46 am

You have to put the !cumpc somewhete

hedgie
Posts: 12
Joined: Fri Jul 25, 2014 2:29 am

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby hedgie » Fri Aug 22, 2014 12:52 pm

meaning !cumpc needs to be stored in the vector evalpc1? Can't seem to get the data stored in evalpc1

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

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby EViews Gareth » Fri Aug 22, 2014 1:06 pm

Code: Select all

evalpc1(!i) = !cumpc1

hedgie
Posts: 12
Joined: Fri Jul 25, 2014 2:29 am

Re: Time series of rolling eigenvalue proportion (from PCA)

Postby hedgie » Fri Aug 22, 2014 1:11 pm

That worked, thank you.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 1 guest