Basic Rolling Regression
Moderators: EViews Gareth, EViews Moderator

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11774
 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.
Follow us on Twitter @IHSEViews
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)
Thank you !
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) francecoefs
matrix(2,10) becoefs
matrix(2,10) ukcoefs
for !i=1 to 10
equation eq1.ls fr{!i} c frmkt
next
colplace(francecoefs, eq1.@coefs,!i)
for !i=1 to 10
equation eq2.ls be{!i} c bemkt
next
colplace(beecoefs, eq2.@coefs,!i)
for !i=1 to 10
equation eq3.ls uk{!i} c ukmkt
next
colplace(ukcoefs, eq3.@coefs,!i)
Thank you !
Re: Basic Rolling Regression
Hello,
Is it possible to perform BaiPerron 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
Is it possible to perform BaiPerron 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

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11774
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Basic Rolling Regression
Yes it is possible (although why use the BaiPerron if you only have one breakdate?).
Follow us on Twitter @IHSEViews
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!
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!

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11774
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Basic Rolling Regression
Thank you Gareth
I guess that QuandtAndrews test gives the same result as the baiperron test when defining maximum breaks =1.
Here's my code for those who are interested
I guess that QuandtAndrews test gives the same result as the baiperron test when defining maximum breaks =1.
Code: Select all
svector(230) annee
equation eq
for !i=1 to 230
equation eq.ls y{!i} c x
freeze(mytab) eq.ubreak 15
%text=mytab(10,1)
!dotpos=@instr(%text, "(")
%obstext=@mid(%text, !dotpos+1)
%obstext=@left(%obstext,@length(%obstext)1)
annee(!i)=%obstext
d mytab
next
matrix(230,1) pvalue
!rowcounter=1
equation eq
for !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 mytab
next
Here's my code for those who are interested
Re: Basic Rolling Regression
Hello again,
I'd really appreciate any advice.
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 subperiod (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
I'd really appreciate any advice.
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 subperiod (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 eq
for !k=1 to 230
equation eq.ls y{!k} c x
freeze(mytab) eq.ubreak 15
%text=mytab(10,1)
!dotpos=@instr(%text, "(")
%obstext=@mid(%text, !dotpos+1)
%obstext=@left(%obstext,@length(%obstext)1)
%obstext
d 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+!i1 @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+!t1 @first+!t+ !window  2
eq{!k}.ls y{!k} c x
next
next
 Attachments

 eviews.wf1
 myworkfile
 (12.16 MiB) Downloaded 32 times

 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 1period 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 data
create u 100
series y=nrnd
series x1=nrnd
series x2=nrnd
series z=nrnd
'run rolling regression
' set window size
!window = 20
' set step size
!step = 1
' get size of workfile
!length = @obsrange
' declare equation for estimation
equation eq1
'calculate number of rolls
!nrolls = @floor((!length!window)/!step)
'series to store forecast estimates
series 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 time
for !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+!window2)
smpl {%first} {%last}
' estimate equation  where the equation is y =c(1) + c(2)*x1 +c(3)*x2
eq1.ls y c x1 x2
' 1periodahead forecast
%1pers = @otod(@dtoo(%start)+!i+!window1) 'start point
%1pere = @otod(@dtoo(%start)+!i+!window1) '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 = yf
next
smpl @all
show fcast.line
d(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.
Re: Basic Rolling Regression
Hello,
I am trying to run a rolling regression with forecast using realtime 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.
I am trying to run a rolling regression with forecast using realtime 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.

 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+!window2
!j=!j+1
' set sample to estimation period
smpl @first+!i1 @first+!i+!window2
' 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?
Thank you in advance.
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+!window2
!j=!j+1
' set sample to estimation period
smpl @first+!i1 @first+!i+!window2
' 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?
Thank you in advance.
Re: Basic Rolling Regression
Instead of
Maybe try:
Not sure if that solves it.
' move sample !step obs at a time
for !i = 1 to @first+!i+!window2
!j=!j+1
Maybe try:
Code: Select all
' move sample !step obs at a time
for !i = 1 to !length!window+1!step step !step
!j=!j+1
Not sure if that solves it.

 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?
How can someone extract the point estimates? Is there a code like @coef to extract the point estimates?

 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 237 and get the coefficients of the regression.
Regress observations 338 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.
For example, I want to be able to regress
observations 1 to 36 get the coefficients of the regression.
Regress 237 and get the coefficients of the regression.
Regress observations 338 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.
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 data
create u 100
series y=nrnd
series x1=nrnd
series x2=nrnd
series z=nrnd
'run rolling regression
' set window size
!window = 36
' set step size
!step = 1
' get size of workfile
!length = @obsrange
' declare equation for estimation
equation 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 estimates
series 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 time
for !i = 1 to !length!window+1!step step !step
!j=!j+1
' set sample for estimation period
%first = @otod(@dtoo(%start)+!i1)
%last = @otod(@dtoo(%start)+!i+!window2)
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 matrixrow (transposing it first, for conveniency)
rowplace(coefmat,@transpose(eq1.@coefs),!i)
' 1periodahead forecast
%1pers = @otod(@dtoo(%start)+!i+!window1) 'start point
%1pere = @otod(@dtoo(%start)+!i+!window1) '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 = yf
next
smpl @all
'show fcast.line
d(noerr) yf
Return to “Program Repository”
Who is online
Users browsing this forum: No registered users and 0 guests