13

Background: I am trying to use MATLAB's Neural Network toolbox to predict future values of data. I run it from the GUI, but I have also included the output code below.

Problem: My predicted values lag behind the actual values by 2 time periods, and I do not know how to actually see a "t+1" (predicted) value.

Code:

% Solve an Autoregression Time-Series Problem with a NAR Neural Network
% Script generated by NTSTOOL
% Created Tue Mar 05 22:09:39 EST 2013
%
% This script assumes this variable is defined:
%
%   close_data - feedback time series.

targetSeries = tonndata(close_data_short,false,false);

% Create a Nonlinear Autoregressive Network
feedbackDelays = 1:3;
hiddenLayerSize = 10;
net = narnet(feedbackDelays,hiddenLayerSize);

% Choose Feedback Pre/Post-Processing Functions
% Settings for feedback input are automatically applied to feedback output
% For a list of all processing functions type: help nnprocess
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};

% Prepare the Data for Training and Simulation
% The function PREPARETS prepares timeseries data for a particular network,
% shifting time by the minimum amount to fill input states and layer states.
% Using PREPARETS allows you to keep your original time series data unchanged, while
% easily customizing it for networks with differing numbers of delays, with
% open loop or closed loop feedback modes.
[inputs,inputStates,layerStates,targets] = preparets(net,{},{},targetSeries);

% Setup Division of Data for Training, Validation, Testing
% For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand';  % Divide data randomly
net.divideMode = 'time';  % Divide up every value
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;

% Choose a Training Function
% For a list of all training functions type: help nntrain
net.trainFcn = 'trainlm';  % Levenberg-Marquardt

% Choose a Performance Function
% For a list of all performance functions type: help nnperformance
net.performFcn = 'mse';  % Mean squared error

% Choose Plot Functions
% For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','plotresponse', ...
  'ploterrcorr', 'plotinerrcorr'};


% 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)

% Recalculate Training, Validation and Test Performance
trainTargets = gmultiply(targets,tr.trainMask);
valTargets = gmultiply(targets,tr.valMask);
testTargets = gmultiply(targets,tr.testMask);
trainPerformance = perform(net,trainTargets,outputs)
valPerformance = perform(net,valTargets,outputs)
testPerformance = perform(net,testTargets,outputs)

% View the Network
view(net)

% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, plotresponse(targets,outputs)
%figure, ploterrcorr(errors)
%figure, plotinerrcorr(inputs,errors)

% Closed Loop Network
% Use this network to do multi-step prediction.
% The function CLOSELOOP replaces the feedback input with a direct
% connection from the outout layer.
netc = closeloop(net);
[xc,xic,aic,tc] = preparets(netc,{},{},targetSeries);
yc = netc(xc,xic,aic);
perfc = perform(net,tc,yc)

% Early Prediction Network
% For some applications it helps to get the prediction a timestep early.
% The original network returns predicted y(t+1) at the same time it is given y(t+1).
% For some applications such as decision making, it would help to have predicted
% y(t+1) once y(t) is available, but before the actual y(t+1) occurs.
% The network can be made to return its output a timestep early by removing one delay
% so that its minimal tap delay is now 0 instead of 1.  The new network returns the
% same outputs as the original network, but outputs are shifted left one timestep.
nets = removedelay(net);
[xs,xis,ais,ts] = preparets(nets,{},{},targetSeries);
ys = nets(xs,xis,ais);
closedLoopPerformance = perform(net,tc,yc)

Proposed Solution: I believe the answer lies in the last part of the code "Early Prediction Network". I'm just not sure how to remove 'one delay'.

Additional question: Is there a function that can be output from this so I can use it over and over? Or would I just have to keep retraining once I get the next time period of data?

user2039871
  • 153
  • 1
  • 1
  • 9
  • Are you sure that the problem is in the code? If your time series is non stationary, you may be having the impression the output is lagged! – DanielTheRocketMan Mar 07 '13 at 08:07
  • It is a dynamic time series, yes. I would like to predict the next value in the sequence. Can I do that with a nonlinear autoregressive (NAR) neural network? – user2039871 Mar 07 '13 at 13:35
  • The main assumption for a NAR is that the data is stationary - i.e. the mean and and varaince are constant over time. An example of stationary data would be a sine wave, yes? My data is random and it varies nonlinearly and nonstationary. What would you recommend for trying to predict this? – user2039871 Mar 07 '13 at 14:44
  • Possibly an ARIMA model, or some type of fuzzy logic? – user2039871 Mar 07 '13 at 14:57
  • 1
    I believe that you should work in steps: (1) see if the data is stationary; (2) if not, deal with it (for instance, differentiate the data); (3) test the most possible model, for instance, ar model; (4) try nonlinear model, for instance, nar; (5) go to a nn model. – DanielTheRocketMan Mar 07 '13 at 20:11
  • Ok I will try that logic. My problem is trying to use a NAR or NARX and change to the closed-loop. Then use this to predict N periods into the future. – user2039871 Mar 08 '13 at 01:59
  • @DanielTheRocketMan I am working with NAR time-series tool and I have the same code as the OP. I have stationary data. Also, I am using `feedbackDelays = 1:7` . I want to predict future 10 values , but I do not know how to do it, any help . – Nishant Jun 05 '14 at 05:37
  • 1
    Correct me if I am wrong but NAR net just has a single input which is to be predicted then what do we have to write in "inputs" and "targets"? – Areeha May 22 '17 at 13:14

2 Answers2

1

To ensure that this question does not remain open whilst the answer is already present I will post the comment that seems to address the issue:

Credits to @DanielTheRocketMan

I believe that you should work in steps:

  1. see if the data is stationary
  2. if not, deal with it (for instance, differentiate the data)
  3. test the most possible model, for instance, ar model
  4. try nonlinear model, for instance, nar
  5. go to a nn model.
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
0

Try a simpler version. I have tested this code and this code works fine for me.

inputs = X;  %define input and target
targets = y;

hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);

%   Set up Division of Data for Training, Validation, Testing

net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;

[net,tr] = train(net,inputs,targets);

outputss(x,:) = net(inputs);

errors = gsubtract(targets,outputss);
mse(errors)
saurabh agarwal
  • 2,124
  • 4
  • 24
  • 46