Page 7 of 9

Re: Basic Rolling Regression

Posted: Tue Dec 13, 2016 8:50 am
by EViews Gareth
Change the line that does the simple regression to be the lines that do the rolling regression.

Re: Basic Rolling Regression

Posted: Wed Jan 25, 2017 2:47 am
by saadallah
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) 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

Posted: Wed Mar 01, 2017 9:07 am
by saadallah
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

Re: Basic Rolling Regression

Posted: Wed Mar 01, 2017 11:02 am
by EViews Gareth
Yes it is possible (although why use the Bai-Perron if you only have one breakdate?).

Re: Basic Rolling Regression

Posted: Tue Mar 07, 2017 3:37 am
by saadallah
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!

Re: Basic Rolling Regression

Posted: Tue Mar 07, 2017 8:30 am
by EViews Gareth
Quandt-Andrews

Re: Basic Rolling Regression

Posted: Wed Mar 08, 2017 2:38 am
by saadallah
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) 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

Posted: Wed Mar 08, 2017 4:15 pm
by saadallah
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 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 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+!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
            next
next



Re: Basic Rolling Regression

Posted: Thu Mar 16, 2017 10:43 am
by Econoforecast
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 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+!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 = 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

Posted: Sat Mar 18, 2017 6:47 am
by anne12
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.

Re: Basic Rolling Regression

Posted: Tue Mar 21, 2017 2:58 pm
by Charles_gre
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?

Thank you in advance.

Re: Basic Rolling Regression

Posted: Tue Mar 21, 2017 3:23 pm
by The Yoyo
Instead of
' 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 time
for !i = 1  to  !length-!window+1-!step step !step
   !j=!j+1


Not sure if that solves it.

Re: Basic Rolling Regression

Posted: Wed Mar 22, 2017 1:10 pm
by Charles_gre
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?

Re: Basic Rolling Regression

Posted: Wed Mar 22, 2017 2:59 pm
by Charles_gre
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.

Re: Basic Rolling Regression

Posted: Wed Mar 22, 2017 3:13 pm
by The Yoyo
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)+!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 = yf
next

smpl @all

'show fcast.line

d(noerr) yf