x=[5416755196563005748258796602666146562828646536599467207662076585967295691727049972538745427636878534806718299285229871778921190859924209371794974962599754298705100072101654103008104357105851107507109300111026112704114333115823117171118517119850121121122389123626124761125786126743127627128453129227129988130756131448132129132802134480135030135770136460137510]';[x,ps]=mapminmax(x,0,1);%该脚本用来做NAR神经网络预测%作者:Macer程lag=3;%自回归阶数iinput=x;%x为原始序列(行向量)n=length(iinput);%准备输入和输出数据inputs=zeros(n-lag,lag);fori=1:n-laginputs(i,:)=iinput(i:i+lag-1)';endtargets=x(lag+1:end)%创建网络P=inputs;P=P';T=targets;net=newff(minmax(P),[10,1],{'logsig','purelin'},'trainlm');inputWeights=net.IW{1,1};inputbias=net.b{1};layerWeights=net.LW{2,1};layerbias=net.b{2,1};%避免过拟合,划分训练,测试和验net.trainParam.show=50;net.trainParam.lr=0.1;net.trainParam.mc=0.04;net.trainParam.epochs=500;net.trainParam.goal=1e-5;%训练网络[net,tr]=train(net,P,T);%%根据图表判断拟合好坏yn=net(P);errors=T-yn;figure,ploterrcorr(errors)figure,parcorr(errors)%绘制偏相关情况%[h,pValue,stat,cValue]=lbqtest(errors)%Ljung-BoxQ检验(20lags)figure,plotresponse(con2seq(targets),con2seq(yn))%看预测的趋势与原趋势%figure,ploterrhist(errors)%误差直方图%figure,plotperform(tr)%误差下降线%%下面预测往后预测几个时间段fn=7;%预测步数为fn。f_in=iinput(n-lag+1:end)';f_out=zeros(1,fn);%预测输出%多步预测时,用下面的循环将网络fori=1:fnf_out(i)=net(f_in);f_in=[f_in(2:end);f_out(i)];end%画出预测图figure,plot(1949:2013,iinput,'b',2013:2020,[iinput(end),f_out],'r')