Rolling Beta Estimates

For questions regarding programming in the EViews programming language.

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

MAK
Posts: 15
Joined: Tue Oct 11, 2016 4:30 pm

Rolling Beta Estimates

Postby MAK » Tue Oct 11, 2016 4:41 pm

Hi,

I have to estimate Betas for 1200 companies over 20 years. I know how to estimate rolling beta estimates for a single stock but can I estimate the rolling beta coefficients for all the 1200 stocks in Eviews. I earlier used the below code to estimate rolling betas for individual stocks.

!window = 60
!step = 1
!length = @obsrange
equation eq1
!nrolls = @round((!length-!window)/!step)
matrix(6,!nrolls) coefmat
!j=0
for !i = 1 to !length-!window+1-!step step !step
!j=!j+1
smpl @first+!i-1 @first+!i+!window-2
eq1.arch(1,1,options) sr269 c mrp
colplace(coefmat,eq1.@coefs,!j)
next
show coefmat

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

Re: Rolling Beta Estimates

Postby EViews Gareth » Tue Oct 11, 2016 7:32 pm

Put your code inside a for loop that loops through the 1200

MAK
Posts: 15
Joined: Tue Oct 11, 2016 4:30 pm

Re: Rolling Beta Estimates

Postby MAK » Wed Oct 12, 2016 8:07 am

Thanks. for the reply. I am new to Eviews programming and don't know how to use the for loops. Can you please help me with a demo?

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

Re: Rolling Beta Estimates

Postby EViews Gareth » Wed Oct 12, 2016 8:10 am

Sure, here's an example of a for loop:

Code: Select all

for !i = 1 to !length-!window+1-!step step !step !j=!j+1 smpl @first+!i-1 @first+!i+!window-2 eq1.arch(1,1,options) sr269 c mrp colplace(coefmat,eq1.@coefs,!j) next

You might also want to read through the programming guide:
http://forums.eviews.com/viewtopic.php?f=5&t=1638

MAK
Posts: 15
Joined: Tue Oct 11, 2016 4:30 pm

Re: Rolling Beta Estimates

Postby MAK » Wed Oct 12, 2016 5:12 pm

I tried the following but is only giving me the coefficient estimates for the first company.

for !i=1 to 1200 //declaring the series i=1 to 1200, I have named stock returns as SR1 to SR1200
!window = 36
!step = 1
!length = @obsrange
equation eq1 //
!nrolls = @round((!length-!window)/!step)
matrix(2,!nrolls) coefmat
!j=0
for !i = 1 to !length-!window+1-!step step !step
!j=!j+1
smpl @first+!i-1 @first+!i+!window-2
equation eq1.ls(options) sr1 c mrp //Problem with this
colplace(coefmat,eq1.@coefs,!j) //should I be using equation eq1 or eq(!1)
next
show coefmat
next

I am stuck with this code. Please help me out with this.

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

Re: Rolling Beta Estimates

Postby EViews Gareth » Wed Oct 12, 2016 6:42 pm

One obvious problem is that you're using !i for two different things.

Also in your equation you probably want Sr!i (or whatever your looping variable is), not sr1

MAK
Posts: 15
Joined: Tue Oct 11, 2016 4:30 pm

Re: Rolling Beta Estimates

Postby MAK » Thu Oct 13, 2016 10:46 am

I have tried the following code but it is still not working. I have attached a test file with stock returns of 3 stocks and the market risk premium. Can you please help me with making the required corrections to the code?

for !i=1 to 3
!window = 36
!step = 1
!length = @obsrange
equation eq1
!nrolls = @round((!length-!window)/!step)
matrix(2,!nrolls) coefmat
!j=0
for !j = 1 to !length-!window+1-!step step !step
!j=!j+1
smpl @first+!j-1 @first+!j+!window-2
equation eq1.ls(options) sr!i c mrp
colplace(coefmat,eq1.@coefs,!j)
next
show coefmat
next
Test RollReg.xlsx
(19.74 KiB) Downloaded 354 times

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

Re: Rolling Beta Estimates

Postby EViews Gareth » Thu Oct 13, 2016 11:13 am

You're storing the estimates into the same place each time, writing over themselves.

Try this:

Code: Select all

for !i=1 to 3 !window = 36 !step = 1 !length = @obsrange equation eq1 !nrolls = @round((!length-!window)/!step) matrix(2,!nrolls) coefmat!i !j=0 for !j = 1 to !length-!window+1-!step step !step !j=!j+1 smpl @first+!j-1 @first+!j+!window-2 equation eq1.ls(options) sr!i c mrp colplace(coefmat!i,eq1.@coefs,!j) next next

MAK
Posts: 15
Joined: Tue Oct 11, 2016 4:30 pm

Re: Rolling Beta Estimates

Postby MAK » Thu Oct 13, 2016 5:10 pm

When I estimate the rolling regression using this code, it only estimates the coefficient at one roll and gives zero values for estimates of the coefficients at the second roll and so on over the different windows for the period. Please help me with the solution to the issue. Also can you please guide me on how to copy/view all the coefficients for the different stocks in one window?

for !i=1 to 3
!window = 36
!step = 1
!length = @obsrange
equation eq1
!nrolls = @round((!length-!window)/!step)
matrix(2,!nrolls) coefmat!i
!j=0
for !j = 1 to !length-!window+1-!step step !step
!j=!j+1
smpl @first+!j-1 @first+!j+!window-2
equation eq1.ls(options) sr!i c mrp
colplace(coefmat!i,eq1.@coefs,!j)
next
next

MAK
Posts: 15
Joined: Tue Oct 11, 2016 4:30 pm

Re: Rolling Beta Estimates

Postby MAK » Mon Oct 17, 2016 3:53 pm

Dear Gareth,

Thanks for your valuable help. I have corrected the model as below and now it is estimating coefficients as required. I need your help with one more thing and that is how can I retrieve estimated coefficients for all the stocks in one group view or can directly store them into one output file such as excel?

for !i=1 to 3
!window = 36
!step = 1
!length = @obsrange
equation eq1
!nrolls = @round((!length-!window)/!step)
matrix(2,!nrolls) coefmat!i
!j=0
for !j = 1 to !length-!window+1-!step step !step
smpl @first+!j-1 @first+!j+!window-2
equation eq1.ls(options) sr!i c mrp
colplace(coefmat!i,eq1.@coefs,!j)
next
next

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

Re: Rolling Beta Estimates

Postby EViews Gareth » Tue Oct 18, 2016 8:16 am

You'll have to create a big master matrix then insert each individual matrix into the big one (in whatever way you want). You can use matplace to place the individual ones inside the master.

MAK
Posts: 15
Joined: Tue Oct 11, 2016 4:30 pm

Re: Rolling Beta Estimates

Postby MAK » Tue Oct 18, 2016 3:12 pm

Thanks Gareth. Can you please share the code structure for the Master Matrix as my skills are quite limited in creating such a matrix?

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

Re: Rolling Beta Estimates

Postby EViews Gareth » Tue Oct 18, 2016 3:50 pm

Same idea as when you're building up the coefmats.


Declare it outside the loop (right at the top of your program):

Code: Select all

matrix(rows,columns) mastermat
where rows, columns are the size of the master matrix (determined by the number of coefficients, number of rolls and number of series).

The exact shape of the matrix, and exactly where you put each coefficient matrix inside it is up to you.

MAK
Posts: 15
Joined: Tue Oct 11, 2016 4:30 pm

Re: Rolling Beta Estimates

Postby MAK » Wed Oct 19, 2016 3:14 pm

Thanks Gareth. can you please help me do it for the following three stocks. do i have to do this for all the 1200 stocks individually or can I declare it in advance?

for !i=1 to 3
!window = 36
!step = 1
!length = @obsrange
equation eq1
!nrolls = @round((!length-!window)/!step)
matrix(6,!nrolls) mastermat
matrix(6,!nrolls) coefmat!i

!j=0
for !j = 1 to !length-!window+1-!step step !step
smpl @first+!j @first+!j+!window-1
equation eq1.ls(options) r!i c mrp(-2) mrp(-1) mrp mrp(1) mrp(2)
colplace(coefmat!i,eq1.@coefs,!j)
matplace(mastermat, coefmat1, 1,1)
matplace(mastermat, coefmat2, 7, 1)
matplace(mastermat, coefmat3, 13, 1)
next
next
Attachments
rollregcorrectprog.prg
(328 Bytes) Downloaded 351 times
3 stock sample.wf1
(17.32 KiB) Downloaded 259 times


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests