資源描述:
《BP神經(jīng)網(wǎng)絡(luò)預(yù)測代碼》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、x=[54167???55196???56300???57482???58796???60266???61465???62828???64653???65994???67207???66207???65859???67295???69172???70499???72538???74542???76368???78534???80671???82992???85229???87177???89211????90859????92420????93717????94974????96259????97542????98705???100072???101654
2、???103008???104357???105851???107507???109300???111026???112704???114333???115823???117171???118517???119850???121121???122389???123626???124761???125786???126743???127627???128453???129227???129988???130756???131448???132129???132802???134480???135030???135770???136460???137510]'
3、;%該腳本用來做NAR神經(jīng)網(wǎng)絡(luò)預(yù)測%作者:Macer程lag=3;???%自回歸階數(shù)iinput=x;%x為原始序列(行向量)n=length(iinput);%準(zhǔn)備輸入和輸出數(shù)據(jù)inputs=zeros(lag,n-lag);fori=1:n-lag???inputs(:,i)=iinput(i:i+lag-1)';endtargets=x(lag+1:end);%創(chuàng)建網(wǎng)絡(luò)hiddenLayerSize=10;%隱藏層神經(jīng)元個數(shù)net=fitnet(hiddenLayerSize);%避免過擬合,劃分訓(xùn)練,測試和驗證數(shù)據(jù)的比例net.dividePara
4、m.trainRatio=70/100;net.divideParam.valRatio=15/100;net.divideParam.testRatio=15/100;%訓(xùn)練網(wǎng)絡(luò)[net,tr]=train(net,inputs,targets);%%根據(jù)圖表判斷擬合好壞yn=net(inputs);errors=targets-yn;figure,ploterrcorr(errors)?????????????????????%繪制誤差的自相關(guān)情況(20lags)figure,parcorr(errors)???????????????????????
5、??%繪制偏相關(guān)情況%[h,pValue,stat,cValue]=lbqtest(errors)????????%Ljung-BoxQ檢驗(20lags)figure,plotresponse(con2seq(targets),con2seq(yn))%看預(yù)測的趨勢與原趨勢%figure,ploterrhist(errors)?????????????????????%誤差直方圖%figure,plotperform(tr)?????????????????????????%誤差下降線%%下面預(yù)測往后預(yù)測幾個時間段fn=7;?%預(yù)測步數(shù)為fn。f_in=
6、iinput(n-lag+1:end)';f_out=zeros(1,fn);?%預(yù)測輸出%多步預(yù)測時,用下面的循環(huán)將網(wǎng)絡(luò)輸出重新輸入fori=1:fn???f_out(i)=net(f_in);???f_in=[f_in(2:end);f_out(i)];end%畫出預(yù)測圖figure,plot(1949:2013,iinput,'b',2013:2020,[iinput(end),f_out],'r')