Neural Network Dynamic Forecast

Questions and discussions on Time Series Analysis

Neural Network Dynamic Forecast

Postby TomDoan » Thu Mar 25, 2010 10:28 am

The following is an example of using a neural net model to do dynamic multiple step forecasts. The NNTEST instruction only does "static" forecasts, so dynamic forecasts require looping over the horizons and feeding the forecasts back in as the inputs. This is most conveniently done by making a copy of the endogenous variable and using the copy for the inputs.

Code: Select all
*
* NNDynFore.prg
* Example of dynamic forecasting with neural networks
*
cal(q) 1959:3
open data nnetdat.rat
data(format=rats,ver) * 1996:3
set logy = log(gdpq)
set trend = t
*
compute fstart=1990:1
*
* For comparison, do an OLS fit through 1989:4 and dynamic forecast from
* there through the end of sample.
*
linreg logy * fstart-1
# constant logy{1 2}
*
uforecast olsdynamic fstart 1996:3
*
* Now fit a neural net model, using data through 1989:4. We use 5 hidden
* nodes and train to an R^2 of .999 (it doesn't quite make that in
* 500000 iterations). We need to allow the range to expand quite a bit
* to accomodate the trending data, hence the pad=.9 option. Even with
* that, the multiple step forecasts tend to trail off below trend as a
* result of the "squashing" function. With a smaller value of pad, this
* will be even more pronounced.
*
nnlearn(pad=.90,save=mv, $
  hidden=5,rsquared=.999,iters=500000,trace) * fstart-1
# logy{1 2}
# logy
*
* Compute static forecasts during the hold-back period.
*
nntest fstart 1996:3 mv
# logy{1 2}
# nnstatic
*
* Compute dynamic forecasts. In order to do dynamic forecasts, we have
* to loop over a set of one-step forecasts, feeding the forecasts back
* into the calculation. We'll create a copy of logy and use that as the
* input for NNTEST. This allows us to update the input series after each
* step without changing the values of our original logy series.
*
clear nndynamic
set logydyn = logy
do time = fstart,1996:3
   nntest time time mv
   # logydyn{1 2}
   # logydyn
   *
   * Store the dynamic forecast
   *
   compute nndynamic(time) = logydyn(time)
end do time
*
graph(key=upleft) 4
# logy
# nnstatic
# nndynamic
# olsdynamic


Data file:
NNETDAT.RAT
(3.25 KiB) Downloaded 235 times
TomDoan
 
Posts: 2720
Joined: Wed Nov 01, 2006 5:36 pm

Re: Neural Network Dynamic Forecast

Postby basher » Tue Nov 20, 2012 7:43 am

I tried to run the program using your data, I get the following error message. FYI, I am using Rats 8.0.
## SR5. Writing Range -32766 to 149 of Series GCQ. (Incorrect start,end or SMPL)
basher
 
Posts: 12
Joined: Tue Mar 08, 2011 7:11 am

Re: Neural Network Dynamic Forecast

Postby TomDoan » Tue Nov 20, 2012 11:43 pm

change the data instruction to

data(format=rats,ver) 1959:3 1996:3
TomDoan
 
Posts: 2720
Joined: Wed Nov 01, 2006 5:36 pm


Return to Other Time Series Analysis

Who is online

Users browsing this forum: Bing [Bot] and 0 guests