by GEReikard » Sat Apr 07, 2012 4:24 pm
The following code may be useful. This is a program that sets up a neural net model, and then forecasts two periods in advance, iteratively. The procedure is embedded inside a do loop, so that it will run forecasts over the entire span of observations, until the last observation is reached. The code also saves the forecast, and calculates the error.
The series being predicted is "flux", which is converted to logs ("lnf"), and a second copy of the same series is used to train the net. Then, the forecasting routine starts at observation 1000, and continues until last. The neural net includes three proximate lags, and one lag at 24 periods (this is hourly data, with a 24-hour cycle). The window is set at 54 hours (regend-54 regend). Interestingly enough, the neural net often works better with a shorter window than regression models, where much wider windows were needed. The window width is of course a matter of choice, and depends on the properties of the data. The net has one direct connection, and three hidden layers. The MAPE is the mean absolute percent error. Note that the forecast is for two periods ahead, so that the error is also computed for "regend+2 regend+2".
In this program, I tried combining neural nets with regressions. First the neural net is trained, generating the series called "static". Then, this is used as the input on the RHS of a regression model. The combined or hybrid model predicted more accurately than either the net or a regression alone.
log flux / lnf
set lnf2 = lnf
do regend =1000,last
Nnlearn(noprint,save=mvec,restart,mode=epoch,pad=0.9,iters=1000,direct,hidden=3,rsquared=0.999,theta=0.01,kappa=0.98,mu=0.1) regend-54 regend
#lnf{1 to 3} lnf{24}
#lnf2
Nntest regend-54 regend+2 mvec
#lnf{1 to 3} lnf{24}
#static2
Set mapenn regend+2 regend+2 = abs(lnf-static2)
Linreg(define=wfeqn6,noprint) lnf regend-270 regend
#constant static2{2} lnf{1 to 3} lnf{24}
forecast(skip=1) 1 2 regend+2
#wfeqn6 forlnf6
set mape6 regend+2 regend+2 = abs(forlnf6-lnf)
end do
tab