Random walk model rolling forecasting & obtaining the MSPE

For questions regarding programming in the EViews programming language.

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

dongyadu
Posts: 12
Joined: Mon Sep 05, 2011 7:43 pm

Random walk model rolling forecasting & obtaining the MSPE

Postby dongyadu » Tue Sep 20, 2011 1:23 am

hi,

I want to estimate the random walk model y=y(-1)+e with 148 data. Then I want to obtain the mean squared forecasting error for each one step ahead forecasting.
The estimation window is from observation 1 to 96, and the forecasting window is from 97 to 148. So the window size is 96.

I have write the code according to Esther's work. But, the work of Esther is 4 period ahead and the below one is the 1 period ahead.

please Can anyone help me to check if the below code is all correct? And I am not sure how to get the mean squared prediction error. All I know is the equation

MSPE= (sum(y-fy)^2)/window size, here fy is the forecasting dependent variable
Here is the code. I use Eviews 7

Code: Select all

'get same data wfcreate u 148 'create the random walk series series y=nrnd series x1=y(-1) 'run rolling regression ' set window size !window = 96 ' 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) 'matrix to store coefficient estimates matrix(1,!nrolls) coefmat ' where 3 is the number of coefficients 'series to store forecast estimates series fcast 'redundant series for catching start and end points series ser = 1 %start = @otod(@ifirst(ser)) %end = @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) smpl {%first} {%last} ' estimate equation - where the equation is y=c(1)*x1 eq1.ls y x1 ' store coefficients colplace(coefmat,eq1.@coefs,!j) ' 4-period-ahead forecast %4pers = @otod(@dtoo(%start)+!i+!window-1) 'start point %4pere = @otod(@dtoo(%start)+!i+!window) 'end point: %4pere - %4pers +1 = 4 if {%end} < {%4pere} then 'check whether the forecast end point is greater than the workfile end point return endif ' set smpl for forecasting period smpl {%4pers} {%4pere} ' forecast with command *forecast* (see also *fit*) eq1.forecast(f=na) yf 'estimate the forecasting error eq1.resids ' set sampl to obtain the 4th period observation smpl {%4pere} {%4pere} ' store forecasts fcast = yf 'series to store forecasting error series ferror next smpl @all show coefmat show fcast.line d(noerr) ser
Many Thanks

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

Re: Random walk model rolling forecasting & obtaining the MS

Postby EViews Gareth » Tue Sep 20, 2011 7:49 am

Looks like you haven't changed her code to do one step forecasting.

dongyadu
Posts: 12
Joined: Mon Sep 05, 2011 7:43 pm

Re: Random walk model rolling forecasting & obtaining the MS

Postby dongyadu » Wed Sep 21, 2011 2:10 am

Hi, Gareth

Thanks for your reply.

Please can you show me what's the right way to do the one step ahead forecasting?

I have modified the code as below.

Code: Select all

'4-period-ahead forecast %4pers = @otod(@dtoo(%start)+!i+!window-1) 'start point %4pere = @otod(@dtoo(%start)+!i+!window) 'end point
compare with the original code

Code: Select all

' 4-period-ahead forecast %4pers = @otod(@dtoo(%start)+!i+!window-1) 'start point %4pere = @otod(@dtoo(%start)+!i+!window[color=#FF0000]+2[/color]) 'end point: %4pere - %4pers +1 = 4
is this correct?

also, I want to store the forecasting error instead of coefficient to obtain the Mean squared prediction error for the model. Please can you advise me how to embody the code in this Esther's work.

Thanks a lot

Dongya

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

Re: Random walk model rolling forecasting & obtaining the MS

Postby EViews Gareth » Wed Sep 21, 2011 7:49 am

The fact you didn't change the comments threw me off, I guess you did change it ok.

Once you have a forecast series, you can compute the MPSE as you defined it - subtract the forecast from the real values, then take the sum of squares.

dongyadu
Posts: 12
Joined: Mon Sep 05, 2011 7:43 pm

Re: Random walk model rolling forecasting & obtaining the MS

Postby dongyadu » Thu Sep 22, 2011 3:49 am

Hi, Gareth,
Thanks a lot.

I have got another question. suppose if I have data from 1999m1 to 2011m4. Then, the forecasting window is from 2007m1 to 2011m4. And, I want to obtain the mean squared error prediction error for 2007m1, 2007m2, 2007m3.......2011m4 each one month ahead. So. there will be 52 MSPEs.

So. for mspe of 2011m4 should equal to the sum (forecasting error(2007m1-2011m4))^2/50, then mspe of 2011m3 should equal to the sum (forecasting error (2006m12-2011m3))^2/50, and the mspe of 2011m2 should equal to the sum(forecasting error(2006m111-2011m2)^/50..........mspe of 2007m1 should equal to the sum (forecasting error(2002m10-2007m1)^50

Is this correct? how shall I program the code for this?

Many thanks

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

Re: Random walk model rolling forecasting & obtaining the MS

Postby EViews Gareth » Thu Sep 22, 2011 7:46 am

I'm not sure if I can tell if that is correct or not.

dongyadu
Posts: 12
Joined: Mon Sep 05, 2011 7:43 pm

Re: Random walk model rolling forecasting & obtaining the MS

Postby dongyadu » Mon Sep 26, 2011 8:41 am

I'm not sure if I can tell if that is correct or not.
Hi Gareth

Please could you advise how to write the code to calculate the mean squared prediction error depending on the rolling forecast?

I have problem to write code for a loop of calculation the mspe value. For example. firstly, using the first value in y series minus the first value in fcast series and squaring the result, and then repeating the process one step ahead up to the first forecasting point, eg.2007m1. Then sum up all the forecasting errors. Then, starting from the second value in both y and fcast series and ending with the second forecasting point.

I think there will be two loops in the whole process. But, I really don't know how to write the code. Please help!

Your reply will be much appreciated.

Dongya

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

Re: Random walk model rolling forecasting & obtaining the MS

Postby EViews Gareth » Mon Sep 26, 2011 8:49 am

Just use the @sumsq function on the difference between y and forecast_y.

dongyadu
Posts: 12
Joined: Mon Sep 05, 2011 7:43 pm

Re: Random walk model rolling forecasting & obtaining the MS

Postby dongyadu » Tue Sep 27, 2011 8:43 am

Hi,

Many Thanks

But, I am not sure how exactly I shall use the @sumsq in the loop. please can you explain how to write the code?

is it something like below.

Code: Select all

'create some data wfcreate u 100 'rolling window size window=20 'create some series series y =nrnd series yf =nrnd %actual=@elem(y,@otod) %fit=@elem(yf,@otod) 'calculate the MSPE for !i =1 to 100 step series MSPE(i) = @sumsq({%actual } - {%fit})/!window next
Dongya


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests