
% Generated under NARX by Seda Basihos
%
% This script assumes these variables are defined:
%
%   input - input time series.
%   target - feedback time series.

inputSeries = tonndata(input,true,false);
targetSeries = tonndata(target,true,false);

% Create a Nonlinear Autoregressive Network with External Input
%% this is example, the number of hidden layers and neurons is up to model's performance
% for my data: yearly GDP series are stationary, and co-integrated with
% nightlights
inputDelays = 1:1;
feedbackDelays = 1:1;
hiddenLayerSize = 4;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);

% Prepare the Data for Training and Simulation
% open loop or closed loop feedback modes.
[inputs,inputStates,layerStates,targets] = preparets(net,inputSeries,{},targetSeries);

% Setup Division of Data for Training, Validation, Testing
% here you can find the solution for overfitting!
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;

% Train the Network
[net,tr] = train(net,inputs,targets,inputStates,layerStates);

% Test the Network
outputs = net(inputs,inputStates,layerStates);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs);

% View the Network
view(net)

% Closed Loop Network
% Use this network to do multi-step prediction.
% connection from the outout layer.
netc = closeloop(net);
netc.name = [net.name ' - Closed Loop'];
view(netc)
[xc,xic,aic,tc] = preparets(netc,inputSeries,{},targetSeries);
yc = netc(xc,xic,aic);
closedLoopPerformance = perform(netc,tc,yc)
%%
% Early Prediction Network if it is needed!
% same outputs as the original network, but outputs are shifted left one timestep.
nets = removedelay(net);
nets.name = [net.name ' - Predict One Step Ahead'];
view(nets)
[xs,xis,ais,ts] = preparets(nets,inputSeries,{},targetSeries);
ys = nets(xs,xis,ais);
earlyPredictPerformance = perform(nets,ts,ys);
