How can predict multi step ahead using Narnet
5 次查看(过去 30 天)
显示 更早的评论
I want to predict the future prices, I have used only the daily historical prices as input. I can predict only one step ahead using this code:
clear all;
clear all;
load('prices.mat');
set_size = 1413;
targetSeries =prices(1:set_size);
targetSeries = targetSeries';
targetSeries_train = targetSeries(1:set_size * (4/5) );
targetSeries_test = targetSeries(set_size * (4/5): end);
targetSeries_train = num2cell(targetSeries_train);
targetSeries_test = num2cell(targetSeries_test);
feedbackDelays = 1:4;
hiddenLayerSize = 10;
net = narnet(feedbackDelays, hiddenLayerSize);
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
[inputs, inputStates, layerStates,targets] = preparets(net,{},{}, targetSeries_train);
[inputs_test,inputStates_test,layerStates_test,targets_test] = preparets(net,{},{},targetSeries_test);
net.trainFcn = 'trainrp'; % Levenberg-Marquardt
net.performFcn = 'mse'; % Mean squared error
net.plotFcns = {'plotperform','plottrainstate','plotresponse', ...
'ploterrcorr', 'plotinerrcorr'};
[net,tr] = train(net,inputs,targets,inputStates,layerStates);
outputs = net(inputs_test,inputStates_test,layerStates_test);
errors = gsubtract(targets_test,outputs);
performance = perform(net,targets_test,outputs)
view(net)
% netc = closeloop(net);
% [xc,xic,aic,tc] = preparets(netc,{},{},targetSeries_test);
% yc = netc(xc,xic,aic);
% perfc = perform(net,tc,yc)
% nets = removedelay(net);
% [xs,xis,ais,ts] = preparets(nets,{},{},targetSeries_test);
% ys = nets(xs,xis,ais);
% stepAheadPerformance = perform(net,ts,ys)
how I can predict multistep ahead, for example the prediction of 10 days in advance.
Thanks in advance.
2 个评论
John D'Errico
2014-7-14
Argh! Learn how to use the code formatting tool. I'll fix it this once. See that your code is unreadable as it WAS, but after one click of the mouse it is now readable.
采纳的回答
Shashank Prasanna
2014-7-14
编辑:Shashank Prasanna
2014-7-14
Take a look at the line where I define Ypred here I specify nans for the number of forecasts I want to make. I specify 4 more for pre-samples. Hope this helps. And ofcourse Y is your data Ypred are the forecasts
net = narnet(1:4,10);
net.trainParam.showWindow = false;
T = tonndata(Y,false,false);
[Xs,Xi,Ai,Ts] = preparets(net,{},{},T);
net = closeloop(train(net,Xs,Ts,Xi,Ai));
Ypred = nan(14,1);
Ypred = tonndata(Ypred,false,false);
Ypred(1:4) = T(end-3:end);
[xc,xic,aic,tc] = preparets(net,{},{},Ypred);
Ypred = fromnndata(net(xc,xic,aic),true,false,false);
9 个评论
omer triyo
2017-5-13
编辑:omer triyo
2017-5-13
hi, When i try to predict future values, i got same values by increasing number of forecast. for example, i try to predict future values for 365 days. I got same values after about 150 days. What can i do ? Thanks.
更多回答(2 个)
Greg Heath
2014-7-17
%0. a. I do not have MATLAB on this computer, so some of my code comments may need correcting.
% b. I will not complain if you decide to change your mind and accept my answer
% c. Did you mean to begin with close all instead of two clears?
clear all;
clear all;
load('prices.mat');
set_size = 1413;
targetSeries = prices(1:set_size);
targetSeries = targetSeries';
targetSeries_train = targetSeries(1:set_size * (4/5) );
targetSeries_test = targetSeries(set_size * (4/5): end);}
%1. setsize*4/5 is not an integer
% 2. Why are you trying to avoid the default validation set used to avoid overtraining an overfit net (i.e., more unknown weights than training equations)?
% 3. WARNING: You will have to override the default net.divideFcn = 'dividerand' and associated net.divide... trn/val/tst ratios 0.7/0.15/0.15. Use net.divideFcn = 'dividetrain' with the default ratios 100/0/0. I don't think any other option allows absense of a val set.
%4. HOWEVER, my recommendation is to use net.divideFcn = 'divideblock' and accept the default ratios 0.7/0.15/0.15 . Otherwise is more trouble than it's worth. You will automatically get all three results at once.
targetSeries_train = num2cell(targetSeries_train);
targetSeries_test = num2cell(targetSeries_test);
% 5. OK, but check out function tonndata
feedbackDelays = 1:4;
%6. Use the significant delays indicated by the autocorrelation function. For examples search greg narnet nncorr
hiddenLayerSize = 10;
% 7. Default value of 10 may not work well. However, it is good for preliminary investigation because Numweights = ( 4+1)*10+(10+1)*1= 61 << Numtrainequations = 1130
net = narnet(feedbackDelays, hiddenLayerSize);
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
% 8. Unnecessary this is a default
[inputs, inputStates, layerStates,targets] = preparets(net,{},{}, targetSeries_train);
[inputs_test,inputStates_test,layerStates_test,targets_test] = preparets(net,{},{}, targetSeries_test);
net.trainFcn = 'trainrp'; % Levenberg-Marquardt
%9. INCORRECT. 'trainlm' is L-M;
net.performFcn = 'mse'; % Mean squared error
net.plotFcns = {'plotperform','plottrainstate','plotresponse', 'ploterrcorr', 'plotinerrcorr'};
% 10. Last 3 statements are defaults. Can omit to simplify code
[net,tr] = train(net,inputs,targets,inputStates,layerStates);
% 11. INCORRECT . You did not change the defaults net.divideFcn = 'dividerand' and net.divideRatio = 0.7/0.15/0.15
outputs = net(inputs_test,inputStates_test,layerStates_test);
errors = gsubtract(targets_test,outputs);
performance = perform(net,targets_test,outputs)
view(net)
netc = closeloop(net);
[xc,xic,aic,tc] = preparets(netc,{},{},targetSeries_test);
%%yc = netc(xc,xic,aic);
perfc = perform(net,tc,yc)
%12. Often closing the loop so degrades performance that you have to train netc
%%nets = removedelay(net);
% %[xs,xis,ais,ts] = preparets(nets,{},{},targetSeries_test);
% %ys = nets(xs,xis,ais);
% %stepAheadPerformance = perform(net,ts,ys)
how I can predict multistep ahead, for example the prediction of 10 days in advance.
% 13. I DON'T UNDERSTAND the use of removedelay. The significant delays of the autocorrelation function indicate how far you can predict ahead with probabilistic certainty. So 1<= FD <= dmax and the values may be nonconsecutive. I think removedelay removes the lower values of FD with, I assume, a decrease in performance. When my computer is available I will investigate.
%Hope this helps.
%Thank you for formally accepting my answer
Greg
8 个评论
Greg Heath
2014-7-24
Search the NEWSGROUP and ANSWERS using combinations of
greg
timedelaynet, narnet or narxnet
nncorr
Hub, Hmin:dH:Hmax
Hope this helps.
Greg
coqui
2014-7-25
26 个评论
Greg Heath
2015-8-7
Start with a subset containing the first few. If that doesn't work, add more.
If you plot the autocorreltion function, the two threshold lines, and color the points outside of the lines red, you will get a much better idea of what is going on. Similarly for crosscorrelations.
EanX
2015-10-1
To obtain 10 steps-ahead forecast, I have to use an empty cell array as input to closed loop net or a cell array of NaN? I suppose that both methods will work so a tried this code:
clearvars; clc; close all;
% test with a simple dataset
Y=simplenar_dataset;
net = narnet(1:4,3);
net.trainParam.showWindow = false;
T = Y(1:80);%tonndata(Y,false,false);
% use only first 80 samples to train and the remaining 20
% to test predictions
Tpred=Y(81:end);
[Xs,Xi,Ai,Ts] = preparets(net,{},{},T);
% is necessary to add Xi and Ai to closeloop to have xic1 and aci1
[net, xic1, aic1] = closeloop(train(net,Xs,Ts,Xi,Ai),Xi,Ai);
% NaN method, require to set first 4 (equal to delay) values
Ypred = nan(14,1);
Ypred = tonndata(Ypred,false,false);
Ypred(1:4) = T(end-3:end);
% empty cells method
Xc1=cell(1,10);
[xc,xic,aic,tc] = preparets(net,{},{},Ypred);
% prediction resulting from the two methods does not agree
% empty cell does not work but perhaps I'm missing something?
% empty cells method
Ypred1 = fromnndata(net(Xc1,xic1,aic1),true,false,false);
Ypred = fromnndata(net(xc,xic,aic),true,false,false); % NaN method
% plot results
plot([Ypred Ypred1 cell2mat(Tpred(1:10))']);
legend('Pred-NaN','Pred-EmptyCells','Target');
But I obtain different results. What am I missing? Thanks.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Modeling and Prediction with NARX and Time-Delay Networks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!