Sample objects in a rolling regression

For questions regarding programming in the EViews programming language.

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

Student15
Posts: 19
Joined: Fri Apr 17, 2015 2:23 pm

Sample objects in a rolling regression

Postby Student15 » Fri May 29, 2015 5:29 am

Hi Guys

I am trying to estimate a rolling regression with a moving window of 126 observations and a step size of 20.6. I have tried the code below and it is giving me results. However, when storing the coefficients, for each coefficient, it gives me the same value for all the 115 iterations. I think that the error is related to the way I have used my sample objects in a loop but I am failing to correct that. May you please help me on how to solve this problem. The code is as follows:

Code: Select all

'run rolling regression ' set window size !window = 126 ' set step size !step = 20.6 ' get size of workfile !length = @obsrange ' declare equation for estimation equation eq1 'calculate number of rolls !nrolls = @round((!length-!window)/!step) 'matrix to store coefficient estimates matrix(9,!nrolls) coefmat ' where 9 is the number of coefficients matrix(9,!nrolls) tmat 'variable keeping track of how many rolls we've done !j=0 ' move sample !step obs at a time for !i = 1 to !window !j=!j+1 ' set sample to estimation period smpl @first+!i-1 @first+!i-1+!window ' estimate equation sample s0 @first+1 @first+!window sample s1 @first+!window+!step @last smpl @all ' declare coef vectors to use in ARCH likelihood coef(4) beta = 0 coef(3) alpha = 0 coef(1) dof = 0 coef(1) mu = 0 ' get starting values equation eq1.arch(1,1,ged,archm=sd,backcast=1) y c y(-1) eq1.makegarch garchm equation eq1.arch(1,1,ged,archm=sd,backcast=1) y c garchm*y(-1) y(-1) beta(1) = eq1.c(1) beta(2) = eq1.c(2) beta(3) = eq1.c(3) beta(4) = eq1.c(4) alpha(1) = eq1.c(5) alpha(2) = eq1.c(6) alpha(3) = eq1.c(7) dof(1) = eq1.c(8) mu(1) = eq1.@se^2 ' set presample values of expressions in logl series sig2 = mu(1) series resid = resid ' set up GARCH likelihood logl ll1 ll1.append @logl logl ll1.append @byeqn ll1.append resid = y - beta(2) - beta(1)*sig2 - beta(3)*sig2*y(-1)- beta(4)*y(-1) ll1.append sig2 = alpha(1)+ alpha(2)*resid(-1)^2 + alpha(3)*sig2(-1) ll1.append z = resid/@sqrt(sig2) ll1.append logl = log(@dged(z,dof(1))) - log(sig2)/2 ' declare coef vectors to use in ARCH likelihood coef(4) beta = 0 coef(1) thresh = 0 coef(3) alpha = 0 coef(1) dof = 0 coef(1) mu = 0 ' get starting values eq1.arch(1,1,thrsh=1,archm=sd,ged,backcast=1) y c y(-1) eq1.makegarch garchm eq1.arch(1,1,thrsh=1,archm=sd,ged,backcast=1) y c garchm*y(-1) y(-1) beta(1) = eq1.c(1) beta(2) = eq1.c(2) beta(3) = eq1.c(3) beta(4) = eq1.c(4) alpha(1) = eq1.c(5) alpha(2) = eq1.c(6) thresh(1) = eq1.c(7) alpha(3) = eq1.c(8) dof(1) = eq1.c(9) mu(1) = eq1.@se^2 ' set presample values of expressions in logl series sig2 = mu(1) series resid = resid ' set up GARCH likelihood logl ll1 ll1.append @logl logl ll1.append @byeqn ll1.append resid = y - beta(2) - beta(1)*sig2 - beta(3)*sig2*y(-1)- beta(4)*y(-1) ll1.append sig2 = alpha(1)+ alpha(2)*resid(-1)^2 + alpha(3)*sig2(-1) + thresh(1)*resid(-1)^2*(resid(-1)<0) ll1.append z = resid/@sqrt(sig2) ll1.append logl = log(@dged(z,dof(1))) - log(sig2)/2 ' estimate and display results smpl s1 ll1.ml(showopts, m=1000, c=1e-5) show ll1.output delete s0 s1 'store coefficients colplace(coefmat,eq1.@coefs,!j) colplace(tmat,eq1.@tstats,!j) next show coefmat show tmat
I have also attached the work file.Thank you.
Attachments
daily returns.wf1
Daily returns for Company A
(69.86 KiB) Downloaded 295 times

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

Re: Sample objects in a rolling regression

Postby EViews Gareth » Fri May 29, 2015 6:44 am

You're storing the estimates from the equation EQ1, not from the LogL estimation. EQ1 is estimated over the entire sample each time, so the estimates never change.

Student15
Posts: 19
Joined: Fri Apr 17, 2015 2:23 pm

Re: Sample objects in a rolling regression

Postby Student15 » Fri May 29, 2015 7:42 am

Thank you Gareth for the quick response, greatly appreciated. How do I go about changing that so that I store all the coefficients correctly?

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

Re: Sample objects in a rolling regression

Postby EViews Gareth » Fri May 29, 2015 8:27 am

Rather than using eq1.@coefs use ll1.@coefs.

Student15
Posts: 19
Joined: Fri Apr 17, 2015 2:23 pm

Re: Sample objects in a rolling regression

Postby Student15 » Fri May 29, 2015 2:00 pm

Thank you Gareth, let me do so.

Student15
Posts: 19
Joined: Fri Apr 17, 2015 2:23 pm

Re: Sample objects in a rolling regression

Postby Student15 » Fri May 29, 2015 2:20 pm

Hi Gareth,the code is now assigning different values for each coefficient, thank you. However, I am getting the following error message during the estimation process: Division by zero in "colplace(tmat,ll1.@tstats,4)".

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

Re: Sample objects in a rolling regression

Postby EViews Gareth » Sun May 31, 2015 5:37 pm

You've probably sized the matrices to the wrong size.

Student15
Posts: 19
Joined: Fri Apr 17, 2015 2:23 pm

Re: Sample objects in a rolling regression

Postby Student15 » Sun May 31, 2015 10:51 pm

Thank you Gareth. Do you have any idea of how I can solve this error given my current code. I have tried to change some aspects of the code using the information I got on the forum regarding the division by zero error but I don't seem to get it right

Code: Select all

'run rolling regression ' set window size !window = 126 ' set step size !step = 20.6 ' get size of workfile !length = @obsrange ' declare equation for estimation equation eq1 'calculate number of rolls !nrolls = @round((!length-!window)/!step) 'matrix to store coefficient estimates matrix(9,!nrolls) coefmat ' where 9 is the number of coefficients matrix(9,!nrolls) tmat 'variable keeping track of how many rolls we've done !j=0 ' move sample !step obs at a time for !i = 1 to !window !j=!j+1 ' set sample to estimation period smpl @first+!i-1 @first+!i-1+!window ' estimate equation sample s0 @first+1 @first+!window sample s1 @first+!window+!step @last smpl @all ' declare coef vectors to use in ARCH likelihood coef(4) beta = 0 coef(3) alpha = 0 coef(1) dof = 0 coef(1) mu = 0 ' get starting values equation eq1.arch(1,1,ged,archm=sd,backcast=1) y c y(-1) eq1.makegarch garchm equation eq1.arch(1,1,ged,archm=sd,backcast=1) y c garchm*y(-1) y(-1) beta(1) = eq1.c(1) beta(2) = eq1.c(2) beta(3) = eq1.c(3) beta(4) = eq1.c(4) alpha(1) = eq1.c(5) alpha(2) = eq1.c(6) alpha(3) = eq1.c(7) dof(1) = eq1.c(8) mu(1) = eq1.@se^2 ' set presample values of expressions in logl series sig2 = mu(1) series resid = resid ' set up GARCH likelihood logl ll1 ll1.append @logl logl ll1.append @byeqn ll1.append resid = y - beta(2) - beta(1)*sig2 - beta(3)*sig2*y(-1)- beta(4)*y(-1) ll1.append sig2 = alpha(1)+ alpha(2)*resid(-1)^2 + alpha(3)*sig2(-1) ll1.append z = resid/@sqrt(sig2) ll1.append logl = log(@dged(z,dof(1))) - log(sig2)/2 ' declare coef vectors to use in ARCH likelihood coef(4) beta = 0 coef(1) thresh = 0 coef(3) alpha = 0 coef(1) dof = 0 coef(1) mu = 0 ' get starting values eq1.arch(1,1,thrsh=1,archm=sd,ged,backcast=1) y c y(-1) eq1.makegarch garchm eq1.arch(1,1,thrsh=1,archm=sd,ged,backcast=1) y c garchm*y(-1) y(-1) beta(1) = eq1.c(1) beta(2) = eq1.c(2) beta(3) = eq1.c(3) beta(4) = eq1.c(4) alpha(1) = eq1.c(5) alpha(2) = eq1.c(6) thresh(1) = eq1.c(7) alpha(3) = eq1.c(8) dof(1) = eq1.c(9) mu(1) = eq1.@se^2 ' set presample values of expressions in logl series sig2 = mu(1) series resid = resid ' set up GARCH likelihood logl ll1 ll1.append @logl logl ll1.append @byeqn ll1.append resid = y - beta(2) - beta(1)*sig2 - beta(3)*sig2*y(-1)- beta(4)*y(-1) ll1.append sig2 = alpha(1)+ alpha(2)*resid(-1)^2 + alpha(3)*sig2(-1) + thresh(1)*resid(-1)^2*(resid(-1)<0) ll1.append z = resid/@sqrt(sig2) ll1.append logl = log(@dged(z,dof(1))) - log(sig2)/2 ' estimate and display results smpl s1 ll1.ml(showopts, m=1000, c=1e-5) show ll1.output delete s0 s1 'store coefficients colplace(coefmat,ll1.@coefs,!j) colplace(tmat,ll1.@tstats,!j) next show coefmat show tmat
Thank you.

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

Re: Sample objects in a rolling regression

Postby EViews Gareth » Sun May 31, 2015 11:18 pm

How many iterations are you doing?

Student15
Posts: 19
Joined: Fri Apr 17, 2015 2:23 pm

Re: Sample objects in a rolling regression

Postby Student15 » Sun May 31, 2015 11:29 pm

115

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

Re: Sample objects in a rolling regression

Postby EViews Gareth » Sun May 31, 2015 11:53 pm

Your for loop runs from 1 to !window, which is 126. Therefore you're actually doing 126 iterations.

Student15
Posts: 19
Joined: Fri Apr 17, 2015 2:23 pm

Re: Sample objects in a rolling regression

Postby Student15 » Mon Jun 01, 2015 12:38 am

Finally, the code is now storing all the coefficients correctly. Thank you so much Gareth for all the help, greatly appreciated :D :D .


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests