Multi-Step Neural Network - Crude Oil Price Forecasting Model

1 view (last 30 days)
Hi, I am trying to create a predictive crude oil forecasting model with 2 step ahead prediction.
Following is the code I made but I running into errors. Will really appreciate if you could resolve them. I made facing the error in the final ypred variable.
----------------------------------------------------------------------------------- % Solve an Autoregression Problem with External Input with a NARX Neural Network % Script generated by NTSTOOL % Created Tue May 19 13:44:22 EDT 2015 % % This script assumes these variables are defined: % % Inputs - input time series. % Output - feedback time series.
inputSeries = tonndata(Inputs,true,false); targetSeries = tonndata(Output,true,false);
% Create a Nonlinear Autoregressive Network with External Input
inputDelays = 1:2; feedbackDelays = 1:2; hiddenLayerSize = 20; net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);
% Choose Input and 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 % Customize input parameters at: net.inputs{i}.processParam % Customize output parameters at: net.outputs{i}.processParam
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'}; net.inputs{2}.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,inputSeries,{},targetSeries);
% Setup Division of Data for Training, Validation, Testing % The function DIVIDERAND randomly assigns target values to training, % validation and test sets during training. % For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand'; % Divide data randomly
% The property DIVIDEMODE set to TIMESTEP means that targets are divided % into training, validation and test sets according to timesteps. % For a list of data division modes type: help nntype_data_division_mode
net.divideMode = 'value'; % Divide up every value net.divideParam.trainRatio = 60/100; net.divideParam.valRatio = 25/100; net.divideParam.testRatio = 15/100;
% Choose a Training Function % For a list of all training functions type: help nntrain % Customize training parameters at: net.trainParam
net.trainFcn = 'trainbr'; % Levenberg-Marquardt
% Choose a Performance Function % For a list of all performance functions type: help nnperformance % Customize performance parameters at: net.performParam
net.performFcn = 'mse'; % Mean squared error
% Choose Plot Functions % For a list of all plot functions type: help nnplot % Customize plot parameters at: net.plotParam
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, plotregression(targets,outputs) 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); 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 % 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.
net = narnet(1:2,1); net.trainParam.showWindow = false;
%T = tonndata(targets, false, false);
[xs,xis,ais,ts] = preparets(nets,inputSeries,{},targetSeries); %net = closeloop(train(net,xs,ts,xis,ais));
ypred = net(xs,xis,ais); ypred = nan(3,1); ypred = tonndata(ypred,false,false);
figure() subplot(3,1,1); plot(cell2mat(outputs),'b','DisplayName','Expected Outputs') hold on plot(cell2mat(targetSeries),'r','DisplayName','Original Targets') title({'Prediction'}) legend('show') hold off
subplot(3,1,2) plot(cell2mat(yc),'b','DisplayName','Expected Outputs with Close Loop') hold on plot(cell2mat(targetSeries),'r','DisplayName','Original Targets') title({'Prediction'}) legend('show') hold off
subplot(3,1,3); plot(cell2mat(ypred),'b','DisplayName','Expected Outputs with Step Ahead Prediction') hold on plot(cell2mat(targetSeries),'r','DisplayName','Original Targets') title({'Prediction'}) legend('show') hold off -----------------------------------------------------------------------------------
Thanks & Regards, Tapas
  3 Comments
Tapas
Tapas on 31 May 2015
Hi Greg,
I didn't get what exactly you want me to change?
Regards, Tapas
Greg Heath
Greg Heath on 31 May 2015
The format:
No more than 1 executable statement per line so that the program will run when cut and pasted into the command line.

Sign in to comment.

Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!