## Basic Rolling Regression

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

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

### Re: Basic Rolling Regression

Change the line that does the simple regression to be the lines that do the rolling regression.

Posts: 15
Joined: Wed Dec 07, 2016 4:59 am

### Re: Basic Rolling Regression

Hello Gareth,

I have a basic question. I have 10 time series of firms stock return for each country (France, Belgium and UK). So in total i have 30 time series.
fr1 fr2 fr3....fr10 (fr1 is the time series stock return of firm 1 in France)
be1 be2 be3...be10
uk1 uk2 uk3...uk10

And i have 3 different market indices : France( Frmkt), Belgium(bemkt) and UK(ukmkt)

I would like to estimate a basic CAPM.
I know that i can the long way and write a code that way. (I'm wondering if you could have a simplier technique)

Code: Select all

`matrix(2,10) francecoefsmatrix(2,10) becoefsmatrix(2,10) ukcoefsfor !i=1 to 10equation eq1.ls fr{!i} c frmktnextcolplace(francecoefs, eq1.@coefs,!i)for !i=1 to 10equation eq2.ls be{!i} c bemktnextcolplace(beecoefs, eq2.@coefs,!i)for !i=1 to 10equation eq3.ls uk{!i} c ukmktnextcolplace(ukcoefs, eq3.@coefs,!i)`

Thank you !

Posts: 15
Joined: Wed Dec 07, 2016 4:59 am

### Re: Basic Rolling Regression

Hello,

Is it possible to perform Bai-Perron test with one maximum break , store the breakdate and then estimate rolling regression on the periode before the break date and the period after the breakdate? And then compare their means?

I know it sounds complicated but maybe someone before has done it

Thanks

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

### Re: Basic Rolling Regression

Yes it is possible (although why use the Bai-Perron if you only have one breakdate?).

Posts: 15
Joined: Wed Dec 07, 2016 4:59 am

### Re: Basic Rolling Regression

Hello Gareth,

Thank you for your help. Well i could use Chow test if i know my breakdate. But in my case, i don't know the break date.

What do you suggest as other structural break test?

Thank you!

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

### Re: Basic Rolling Regression

Quandt-Andrews

Posts: 15
Joined: Wed Dec 07, 2016 4:59 am

### Re: Basic Rolling Regression

Thank you Gareth

I guess that Quandt-Andrews test gives the same result as the bai-perron test when defining maximum breaks =1.

Code: Select all

`svector(230) anneeequation eqfor !i=1 to 230     equation eq.ls y{!i} c xfreeze(mytab) eq.ubreak 15%text=mytab(10,1)!dotpos=@instr(%text, "(")%obstext=@mid(%text, !dotpos+1)%obstext=@left(%obstext,@length(%obstext)-1)   annee(!i)=%obstextd mytabnextmatrix(230,1) pvalue!rowcounter=1equation eqfor !i=1 to 230          equation eq.ls y{!i} c x     freeze(mytab) eq.ubreak 15     pvalue(!rowcounter,1)= @val(mytab(10,4))     !rowcounter=!rowcounter +1               d mytabnext`

Here's my code for those who are interested

Posts: 15
Joined: Wed Dec 07, 2016 4:59 am

### Re: Basic Rolling Regression

Hello again,

So, i have 230 series y: y1, y2, y3...y230. I want to estimate the equation ls y{!i} c x. (each of y on a constant and a serie x)
Then i want to estimate a break date using Quandt Andrews test and store the break date of this test.
After having the break date, i want to estimate rolling regression: ls y c x on each sub-period (period before the break date and after the break date) to compute the mean of the coefficient of x for each subperiod and check if they are statistically different.
I was thinking about writing the code this way.

Any help is appreciated thank you

Code: Select all

`equation eqfor !k=1 to 230equation eq.ls y{!k} c xfreeze(mytab) eq.ubreak 15%text=mytab(10,1)!dotpos=@instr(%text, "(")%obstext=@mid(%text, !dotpos+1)%obstext=@left(%obstext,@length(%obstext)-1)   %obstextd mytab   statusline rolling series y{!k}   !window = 250   !step = 1   !length = @obsrange   equation eq{!k}   !nrolls = @round ((!length-!window)/!step)   !rowcounter = 1   !j= 0   for !i = 1 to %obstext '!length - !window +1-!step step !step      !j = !j+1      smpl @first+!i-1 @first+!i+ !window - 2       eq{!k}.ls y{!k} c x            next     for !t = %obstext to !length - !window +1-!step step !step      !j = !j+1      smpl @first+!t-1 @first+!t+ !window - 2       eq{!k}.ls y{!k} c x            nextnext`
Attachments
eviews.wf1
myworkfile

Econoforecast
Posts: 25
Joined: Sun Feb 19, 2017 1:25 pm

### Re: Basic Rolling Regression

The Yoyo wrote:For anyone still looking for a code that does a 1-period ahead recursive regression forecast (expanding window), I put together something from posts all over this forum. Most of the credits for these codes go to Esther and Gareth.
Try the code below. Note that I left out the coefficient matrix.

Code: Select all

`'create some datacreate u 100series y=nrndseries x1=nrndseries x2=nrndseries z=nrnd'run rolling regression' set window size!window = 20' set step size!step = 1' get size of workfile!length = @obsrange' declare equation for estimationequation eq1'calculate number of rolls!nrolls = @floor((!length-!window)/!step)'series to store forecast estimatesseries fcast'*EDITED: catching start and end points%start = "@first" '@otod(@ifirst(ser))   %end = "@last"   '@otod(@ilast(ser))   'variable keeping track of how many rolls we've done!j=0' move sample !step obs at a timefor !i = 1  to  !length-!window+1-!step step !step   !j=!j+1         ' set sample for estimation period            %first = @otod(@dtoo(%start))   %last = @otod(@dtoo(%start)+!i+!window-2)   smpl {%first} {%last}         ' estimate equation - where the equation is y =c(1) + c(2)*x1 +c(3)*x2   eq1.ls y c x1 x2   ' 1-period-ahead forecast%1pers = @otod(@dtoo(%start)+!i+!window-1) 'start point%1pere = @otod(@dtoo(%start)+!i+!window-1) 'end point      '*EDITED  if @dtoo(%end) < @dtoo(%1pere) then   'check whether the forecast end point is greater than the workfile end point      exitloop   endif      ' set smpl for forecasting period   smpl {%1pers} {%1pere}         ' forecast with command *forecast* (see also *fit*)   eq1.forecast(f=na) yf            ' set sampl to obtain the 4th period observation   smpl {%1pere} {%1pere}        ' store forecasts   fcast = yfnextsmpl @allshow fcast.lined(noerr) yf`

I have no real programming experience, so keep in mind that the code might not work 100% correctly. Any improvements are appreciated.

Thank you for posting this. Could anyone else (mod or user) who has used the above code for recursive forecasting OR has programming knowledge kindly confirm whether the above code works or has been written cleanly and should work?

I wish to use it, and modify it slightly and because I have no experience with programming, this could be convenient for me to pass through my data.

anne12
Posts: 1
Joined: Thu Mar 16, 2017 7:21 pm

### Re: Basic Rolling Regression

Hello,

I am trying to run a rolling regression with forecast using real-time data that has a series for every month whereby only one regression is estimated per series for the window and when the window steps forward to the next month, the regression is estimated on the subsequent month’s series. I have a rolling regression with forecast program that works for a single dependent variable series but I need help expanding it so the regression moves from series to series for each rolling window. Thanks in advance.

Charles_gre
Posts: 7
Joined: Sat Jan 21, 2017 3:30 am

### Re: Basic Rolling Regression

Hi I am trying to make a rolling regression with fixed window size of 36 and step of 1. Finally, I want to store the sixteen coefficients of the equation (lp40000151nwer).
The code is:
'run rolling regression
equation lp40000151nwer

' set window size
!window = 36

' set step size
!step = 1

' get size of workfile
!length = @obsrange

'calculate number of rolls
!nrolls = @round((!length-!window)/!step)

'matrix to store coefficient estimates
matrix(16,!nrolls) coefmat ' where 3 is the number of coefficients

'variable keeping track of how many rolls we've done
!j=0

' move sample !step obs at a time
for !i = 1 to @first+!i+!window-2
!j=!j+1

' set sample to estimation period
smpl @first+!i-1 @first+!i+!window-2

' estimate equation - where the equation is y=c(1) + c(2)*x1 + c(3)*x2
equation lp40000151nwer

'store coefficients
colplace(coefmat,lp40000151nwer.@coefs,!j)
next

show coefmat

The problem is that the sample is not shifting by the step of one. How can I make the sample to shift by one step?

The Yoyo
Posts: 9
Joined: Thu Apr 04, 2013 2:30 pm

### Re: Basic Rolling Regression

' move sample !step obs at a time
for !i = 1 to @first+!i+!window-2
!j=!j+1

Maybe try:

Code: Select all

`' move sample !step obs at a timefor !i = 1  to  !length-!window+1-!step step !step   !j=!j+1`

Not sure if that solves it.

Charles_gre
Posts: 7
Joined: Sat Jan 21, 2017 3:30 am

### Re: Basic Rolling Regression

Thank you, but unfortunately, it is not working.

How can someone extract the point estimates? Is there a code like @coef to extract the point estimates?

Charles_gre
Posts: 7
Joined: Sat Jan 21, 2017 3:30 am

### Re: Basic Rolling Regression

The problem is that it keeps regressing, the whole sample all at once and not performing a rolling regression of a window size of 36 and a step of 1.

For example, I want to be able to regress
observations 1 to 36 get the coefficients of the regression.
Regress 2-37 and get the coefficients of the regression.
Regress observations 3-38 coefficients of the regression.
Ideally the outputs will be placed in a matrix
Until the end of the observations which is 106.
I require this for my thesis... it is very urgent and important.

If you could help me please, I will be grateful as I do not know anybody who can help me.

The Yoyo
Posts: 9
Joined: Thu Apr 04, 2013 2:30 pm

### Re: Basic Rolling Regression

That sounds fairly simple. Not sure why it's not working. Try adjusting the following code (and ignore the forecasts it makes).

Code: Select all

`'create some datacreate u 100series y=nrndseries x1=nrndseries x2=nrndseries z=nrnd'run rolling regression' set window size!window = 36' set step size!step = 1' get size of workfile!length = @obsrange' declare equation for estimationequation eq1'calculate number of rolls!nrolls = @floor((!length-!window)/!step)'create matrix to store coefficients.  We'll be running !nrolls regressions (!nrolls rows) for 3 coefficients, so 3 columns.matrix(!nrolls,3) coefmat'series to store forecast estimatesseries fcast'*EDITED: catching start and end points%start = "@first" '@otod(@ifirst(ser))   %end = "@last"   '@otod(@ilast(ser))   'variable keeping track of how many rolls we've done!j=0' move sample !step obs at a timefor !i = 1  to  !length-!window+1-!step step !step   !j=!j+1         ' set sample for estimation period            %first = @otod(@dtoo(%start)+!i-1)   %last = @otod(@dtoo(%start)+!i+!window-2)   smpl {%first} {%last}         ' estimate equation - where the equation is y =c(1) + c(2)*x1 +c(3)*x2   eq1.ls y c x1 x2   'store the coefficient vector of this estimation in a matrix-row (transposing it first, for conveniency)  rowplace(coefmat,@transpose(eq1.@coefs),!i)' 1-period-ahead forecast%1pers = @otod(@dtoo(%start)+!i+!window-1) 'start point%1pere = @otod(@dtoo(%start)+!i+!window-1) 'end point      '*EDITED  if @dtoo(%end) < @dtoo(%1pere) then   'check whether the forecast end point is greater than the workfile end point      exitloop   endif      ' set smpl for forecasting period   smpl {%1pers} {%1pere}         ' forecast with command *forecast* (see also *fit*)   eq1.forecast(f=na) yf            ' set sampl to obtain the 4th period observation   smpl {%1pere} {%1pere}        ' store forecasts   fcast = yfnextsmpl @all'show fcast.lined(noerr) yf`