Page 1 of 1
Forecasting with ANN
Posted: Sun Feb 12, 2012 1:57 pm
by JohnV
Hi,
I want to forecast t+80 periods ahead using paramter estimates and data as of today, t, using ANN.
I wrote the below program,
dec ser dspO
nnlearn(noprint,save=mem,hidden 8,nodirect,squash=logistic,cvcrit=0.0120,iters=100000,notrace) 2500 2700
# dsp{1 to 6}
# dsp
do i=2700,2780-1
nntest i i+1 mem
# dsp{1 to 6}
# dspO
com dsp(i+1)=dspOP(i+1)
end do
I am estimating up to t=2700, forecasting one step ahead t=2701 and using that forecast as actual data inorder to forecast t=2702 and so on to t=2779.
Basiacally, I am only using data up to t=2700 to forecast t=2700-2779.
Is above code correct?
The part I am concerned with is nntest i i+1.
Thanks,
John
Re: Forecasting with ANN
Posted: Mon Feb 13, 2012 10:57 am
by moderator
JohnV wrote:
do i=2700,2780-1
nntest i i+1 mem
# dsp{1 to 6}
# dspO
com dsp(i+1)=dspOP(i+1)
end do
If you want to do the standard dynamic forecasting approach of computing a forecast at, say, i, and using that value as the first lag on the right side to compute i+1, you probably want to change this up a bit to something like:
do i=2700,2780-1
nntest i i mem
# dsp{1 to 6}
# dspO
com dsp(i)=dspOP(i)
end do
That way, the first forecast will be at 2700, then the forecast at 2701 will use the forecasted value for 2700 plus the actual values at 2699, 2698, etc.
However, based on this: "forecasting one step ahead t=2701", I'm guessing that what you really want is to shift everything forward one period:
do i=2701,2780
nntest i i mem
# dsp{1 to 6}
# dspO
com dsp(i)=dspOP(i)
end do
That way, actual data through 2700 is used to forecast 2701, then the forecast at 2701 and actual values at 2700 and earlier are used to forecast 2702, and so on.
Regards,
Tom Maycock
Re: Forecasting with ANN
Posted: Sat Apr 07, 2012 4:24 pm
by GEReikard
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
Re: Forecasting with ANN
Posted: Mon Mar 04, 2013 9:40 am
by IRJ
I am forecasting the changes in a series with an artificial neural network. I am interested in obtaining a forecast of the levels of the series. I know that I can add an identity equation to move from a forecast of changes to that of levels when dealing with linear model. This doesn't seem to be possible when dealing with ANNs, however. I therefore do the following:
Code: Select all
compute fstart = 2002:01
nnlearn(save=nnmodel,hidden=3,rsquared=0.14,iters=5000000,trace) 1990:04 fstart-1
# dlibor1mus{1 2 3}
# dlibor1mus
* Compute static forecasts during the hold-back period.
*
nntest fstart 2008:01 nnmodel
# dlibor1mus{1 2 3}
# dnnstatic
set fnnstatic fstart 2008:01 = dnnstatic+%if(t==fstart,libor1mus(fstart-1),fnnstatic{1})
The last line in the snippet of code is the one I use to obtain the level forecast from the changes. I am doing this correctly?
I would also be thankful if anyone canadditionally enlighten me on the following. I know that the forecasts I obtain from the ANN with the code above are static forecasts. Is there anything wrong with comparing these to one-step-ahead forecasts from linear models? Or would it be better to compare one-step-ahead forecasts to dynamic ANN forecasts?
In addition, is there anything wrong with training and forecasting out of an ANN in levels rather than differences? Given that the goal of an ANN is not to model the time series dyamics but rather to forecast, I suppose that even if the dependent variable is nonstationary, this should't make much difference (spurious results are of no conern). Am I right? In fact, it would make the training of the ANN easier but no paper seems to use levels when forecasting out of an ANN witha nonstationary variable and the literature uses changes or returns.
Re: Forecasting with ANN
Posted: Mon Mar 04, 2013 7:30 pm
by TomDoan
IRJ wrote:I am forecasting the changes in a series with an artificial neural network. I am interested in obtaining a forecast of the levels of the series. I know that I can add an identity equation to move from a forecast of changes to that of levels when dealing with linear model. This doesn't seem to be possible when dealing with ANNs, however. I therefore do the following:
Code: Select all
compute fstart = 2002:01
nnlearn(save=nnmodel,hidden=3,rsquared=0.14,iters=5000000,trace) 1990:04 fstart-1
# dlibor1mus{1 2 3}
# dlibor1mus
* Compute static forecasts during the hold-back period.
*
nntest fstart 2008:01 nnmodel
# dlibor1mus{1 2 3}
# dnnstatic
set fnnstatic fstart 2008:01 = dnnstatic+%if(t==fstart,libor1mus(fstart-1),fnnstatic{1})
The last line in the snippet of code is the one I use to obtain the level forecast from the changes. I am doing this correctly?
IRJ wrote:
I would also be thankful if anyone canadditionally enlighten me on the following. I know that the forecasts I obtain from the ANN with the code above are static forecasts. Is there anything wrong with comparing these to one-step-ahead forecasts from linear models? Or would it be better to compare one-step-ahead forecasts to dynamic ANN forecasts?
Compare static with static, dynamic with dynamic.
IRJ wrote:
In addition, is there anything wrong with training and forecasting out of an ANN in levels rather than differences? Given that the goal of an ANN is not to model the time series dyamics but rather to forecast, I suppose that even if the dependent variable is nonstationary, this should't make much difference (spurious results are of no conern). Am I right? In fact, it would make the training of the ANN easier but no paper seems to use levels when forecasting out of an ANN witha nonstationary variable and the literature uses changes or returns.
If you don't include lags, then only the model with differences would be able to handle the dynamics. If you do levels with lagged levels, then it should be able to pick up the dynamics as well as if done in differences.