# How can predict multi step ahead using Narnet

조회 수: 12 (최근 30일)
coqui 2014년 7월 14일
댓글: MAT-Magic 2020년 3월 28일
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;
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);
how I can predict multistep ahead, for example the prediction of 10 days 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.
coqui 2014년 11월 14일
편집: Walter Roberson 2015년 8월 6일
Dear friend,
I found the feedbackdelays=1, I want to verify if my presented code is true.
To predict multistep ahead, for example the prediction of 10 days in advance:
% net = narnet(1:1,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(11,1);
% Ypred = tonndata(Ypred,false,false);
% Ypred(1:1) = T(end:end);
%[xc,xic,aic,tc] = preparets(net,{},{},Ypred);
% Ypred = fromnndata(net(xc,xic,aic),true,false,false);
Thank you very much.

댓글을 달려면 로그인하십시오.

### 채택된 답변

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이전 댓글 7개 표시이전 댓글 7개 숨기기
coqui 2014년 7월 19일
I found this code but for narxnet:
figure;
plot([cell2mat(targetSeries),nan(1,N);
nan(1,length(targetSeries)),cell2mat(yPred);
nan(1,length(targetSeries)),cell2mat(targetSeriesVal)]')
legend('Original Targets','Network Predictions','Expected Outputs')
I need your help to plot this figure for narnet.
Thanks.
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;
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);
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이전 댓글 6개 표시이전 댓글 6개 숨기기
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
MAT-Magic 2020년 3월 28일

댓글을 달려면 로그인하십시오.

coqui 2014년 7월 25일
Thank you Greg,
I find these expressions to compute Hub:
1) Hub = -1 + ceil( (Ntrneq-O) / (MXFD*O + O +1) )
2) Hub = -1 + ceil( ( Ntrneq-O)/ (O +1))
The right expression is 1 or 2?
##### 댓글 수: 26이전 댓글 24개 표시이전 댓글 24개 숨기기
Greg Heath 2015년 8월 7일
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 CenterFile Exchange에서 Signal Modeling에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by