how can i test an time series NAR NN forecasting tool?

6 views (last 30 days)
Aubai on 17 Apr 2014
Commented: Aubai on 25 Apr 2014
Dear all,
i would like to predicte the wind speed of a spesific location for the next 2 hours (12 points of 10min data) for that i am only using lagged input data of the time series (NAR AA tool).
i am trying to test the optimum number of delays (lagged inputs) needed to get a good prediction resutls using a for loop to increse the number of delays (for example between 1 and 10)
I am also trying to test the optimum number of inputs (input length) that need to be used for training the network and getting a better prediction. also by using another for loop (for example betweem number_of_delay_plus_one to length(input))
my code looks like the following:
if true
load('C:\Users\00037218\Desktop\Wind Speed prediction\E82_Wind_1_2013.mat');
% Solve an Autoregression Time-Series Problem with a NAR Neural Network
% Script generated by NTSTOOL
% Created Fri Mar 28 22:27:25 CET 2014
% This script assumes this variable is defined:
% Target_2 - feedback time series.
if 1 == 0
prediction_indx = 12;
time_input = Input_wind(:,1);
time_target = (time_input(end)+mean(diff(time_input)):mean(diff(time_input)):time_input(end)+(prediction_indx*mean(diff(time_input))))';
time = [time_input;time_target];
Final = [time Final_test];
i_end = length(Final_test);
j_end = 10;
for i = prediction_indx:prediction_indx:i_end
targetSeries = tonndata(Final_test(1:i,:),false,false);
targetSeriesVal = tonndata(Final_test(i+1:end,:),false,false);
for j = 1:i-1%j_end
feedbackDelays = 1:j;
hiddenLayerSize = prediction_indx;
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
% TAKE CARE THIS LINE WAS ACTIVE: 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
if 1 == 1
net.divideFcn = 'dividerand'; % Divide data randomly
net.divideMode = 'time'; % Divide up every value
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 10/100;
net.divideParam.testRatio = 20/100;
net.divideFcn = 'divideblock'; % Divide data in blocks
net.divideMode = 'time'; % Divide up every value
% 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
% 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.
% ----------------------------------------------------------------
delay=length(feedbackDelays); N=prediction_indx;
targetSeriesPred = [targetSeries(end-delay+1:end), con2seq(nan(1,N))];
% ----------------------------------------------------------------
netc = closeloop(net);
[xc,xic,aic,tc] = preparets(netc,{},{},targetSeriesPred);
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);
plot(cell2mat(inputs)); hold on;plot(cell2mat(outputs),'-.r');grid on; hold off;
legend('Original Targets (Inputs)','Network Predictions (output)')
plot(cell2mat(targets)); hold on;plot(cell2mat(yc),'-.r');grid on; hold off;
legend('Original Targets','Network Predictions')
plot(cell2mat(targetSeriesPred)); hold on;plot(cell2mat(yc),'-.r');grid on; hold off;
legend('Original Targets_pred.','Network Predictions')
my question is:
1- what i am doing wrong:
a- why my target data is changing even that i need it to be constant which mean i will always try to predicte the same next 2 hours with different delays used. (but i am not)
b- how can i use the same input data length for training with changable 'for loop' number of lags (delays)?
c- how to re-do the calcultion on a new input data length which is also changable 'for loop' in numaber
So simply speaking i am trying to do the following (only example):
if true
for i = 5:5: length(inputdata)
NN_input = inputdata(1:i);
NN_target = inputdata(i+1:12);% 12 is for the next 2 hours it can be changed and it is
% only used to compare
forj = 1:1:10 % for changing the delays in the delay layer
feedbackDelays = 1:j;
* _% in this part my input and target data should stay the same *They are no*_ *
% network code
% stuff for testing the resutls
% stuff for presenting the reustls
Thx in advance for you support

Accepted Answer

Greg Heath
Greg Heath on 20 Apr 2014
Find feedback delays from the significant delays of the target autocorrelation function.
Find input delays from the significant delays of the input/target cross-correlation function.
Search the NEWSGROUP and ANSWERS using
greg nncorr narnet
Hope this helps.
Thank you for formally accepting my answer
Aubai on 25 Apr 2014
hallo Greg,
thanks for your answer :)
I am trying to find the answer in those previous Answers, Newsgroup. but not fining it can you refer me to a spesific one?
another question: how the 'preparets' function works? i think my problem is there as iam getting random input data everytime the for-loop changes and this giving me not what i am expecting.
again i would like to: for the same available input data (for training) train the network for different delays number (changing with for-loop) and then predict the next 12 points in my time-series. so in my case i want the 'preparets' function to give me always the same set of input, validation and training data yet with different number of delays. how can i do this??
Best Regards

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!