How can I do multi-step ahead prediction using NAR for a single timeseries data (Sensex) using colsed loop and removing delay? I tried to use the code generated by GUI.
1 view (last 30 days)
Show older comments
% Solve an Autoregression Time-Series Problem with a NAR Neural Network % Script generated by NTSTOOL % Created Fri Apr 19 17:00:24 IST 2013 % % This script assumes this variable is defined: % % Sensex - feedback time series.
targetSeries = tonndata(Sensex,false,false);
% Create a Nonlinear Autoregressive Network feedbackDelays = 1:2; hiddenLayerSize = 20; 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) view(netc)
% 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) figure, plot (cell2mat(ys)) grid on view(nets) %display(cell2mat(ys)) figure, plot (Sensex) grid on
1 Comment
Greg Heath
on 22 Apr 2013
1. Replace
perfc = perform(net,tc,yc) % netc?
with
closedLoopPerformance = perform(netc,tc,yc) ;
2. Replace
closedLoopPerformance = perform(net,tc,yc) ;
with
removeDelayPerformance = perform( nets, ts, ys )
Accepted Answer
Greg Heath
on 22 Apr 2013
Edited: Greg Heath
on 22 Apr 2013
1. Replace
perfc = perform(net,tc,yc) % netc?
with
MSEc = perform(netc,tc,yc) ;
2. Replace
closedLoopPerformance = perform(net,tc,yc) ;
with
MSErd = perform( nets, ts, ys )
3. Post results of
a. Replacing the Sensex data with the simplenar_dataset
b. insert the command rng(0) before creating the first net
c. Using the default H = 10 instead of H = 20
4. I get
a. Open Loop
MSEs = 2.14e-7
MSEstrn = 1.84e-7
MSEsval = 3.50e-7
MSEstst = 2.16e-7
b. Closed Loop
MSEc = 4.17e-6
c. Target and Output differences
maxtdiff = 0
maxydiff = 0.0080
d. Remove Delay
MSErd = 2.14e-7
Hope this helps
Thank you for formally accepting my answer
Greg
2 Comments
Greg Heath
on 25 Apr 2013
There are several versions of random number generator.
>> help rng
rng Control the random number generator used by RAND, RANDI, and RANDN. rng(SD) seeds the random number generator using the non-negative integer SD so that RAND, RANDI, and RANDN produce a predictable sequence of numbers.
rng('shuffle') seeds the random number generator based on the current time so that RAND, RANDI, and RANDN produce a different sequence of numbers after each time you call rng.
rng(SD,GENERATOR) and rng('shuffle',GENERATOR) additionally specify the type of the random number generator used by RAND, RANDI, and RANDN. GENERATOR is one of:
Generator Description
------------------------------------------------------------------
'twister' Mersenne Twister
'combRecursive' Combined Multiple Recursive
'multFibonacci' Multiplicative Lagged Fibonacci
'v5uniform' Legacy MATLAB 5.0 uniform generator
'v5normal' Legacy MATLAB 5.0 normal generator
'v4' Legacy MATLAB 4.0 generator
rng('default') puts the settings of the random number generator used by RAND, RANDI, and RANDN to their default values so that they produce the same random numbers as if you restarted MATLAB. In this release, the default settings are the Mersenne Twister with seed 0.
S = rng returns the current settings of the random number generator used by RAND, RANDI, and RANDN. The settings are returned in a structure S with fields 'Type', 'Seed', and 'State'.
rng(S) restores the settings of the random number generator used by RAND, RANDI, and RANDN back to the values captured previously by S = rng.
S = rng(...) additionally returns the previous settings of the random number generator used by RAND, RANDI, and RANDN before changing the seed, generator type or the settings.
Example 1:
s = rng % get the current generator settings
x = rand(1,5) % RAND generates some values
rng(s) % restore the generator settings
y = rand(1,5) % generate the same values so x and y are equal
Example 2:
oldS = rng(0,'v5uniform') % use legacy generator
x = rand % legacy startup value .9501
rng(oldS) % restore the old settings
See Updating Your Random Number Generator Syntax to use rng to replace RAND or RANDN with the 'seed', 'state', or 'twister' inputs.
See also rand, randi, randn, RandStream, now.
Reference page in Help browser
doc rng
More Answers (0)
See Also
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!