?? question_a_bp.m
字號(hào):
%建立BP神經(jīng)網(wǎng)絡(luò)
%P為輸入矢量,即是股市點(diǎn)數(shù)的日期,以yyyymmdd形式作為一個(gè)數(shù)據(jù)
%T為目標(biāo)矢量,即是當(dāng)日的收盤(pán)點(diǎn)數(shù)
[pn,minp,maxp,tn,mint,maxt]=premnmx(P,T);
% 創(chuàng)建一個(gè)新的前向神經(jīng)網(wǎng)絡(luò)
net_1=newff([1,1429],[100,1],{'tansig','purelin'},'traingdm')
%設(shè)置訓(xùn)練參數(shù)
netbp.trainParam.lr=1.5; %設(shè)置學(xué)習(xí)率
net_1.trainParam.show=200;%每100步顯示一次
net_1.trainParam.goal=1e-7;%訓(xùn)練目標(biāo),精度
net_1.trainParam.epochs=3000;%訓(xùn)練次數(shù)
net_1.trainParam.min_grad=1e-3;%最小性能梯度
net_1.trainParam.lr_inc=1.1; %將學(xué)習(xí)率增量因子設(shè)置為1.1
net_1.trainParam.lr_dec=1.1; %將學(xué)習(xí)率減量因子設(shè)置為0.8
%調(diào)用train算法訓(xùn)練
[net_1,tr]=train(net_1,pn,tn);
%對(duì)BP網(wǎng)絡(luò)進(jìn)行仿真
anewn=sim(net_1,pn);
%數(shù)據(jù)還原
anew=postmnmx(anewn,mint,maxt);
%anew與tn做圖對(duì)比
subplot(1,2,1),plot(T),legend('真實(shí)數(shù)據(jù)',1)
hold,subplot(1,2,1),plot(anew,'r')
title('真實(shí)數(shù)據(jù)與模擬數(shù)據(jù)對(duì)比')
legend('真實(shí)數(shù)據(jù)','模擬數(shù)據(jù)',1)
%繪制誤差
subplot(1,2,2)
plot(T-anew)
title('誤差分析')
% 對(duì)新數(shù)據(jù)進(jìn)行分析,記為pnew
pnewn=tramnmx(pnew,minp,maxp);
anewn=sim(net_1,pnewn); %對(duì)BP網(wǎng)絡(luò)進(jìn)行仿真
anew=postmnmx(anewn,mint,maxt);
plot(pnew)
hold
plot(tar)
% %日期數(shù)據(jù)轉(zhuǎn)換
year=double(uint16(P/10000));
month=double(uint16((P-10000*year)/100));
day=P-10000*year-100*month;
P=(year-2005)*365+month*30+day-180;
%Change the order of P and T
[m,n]=size(P);
for i=1:n
Q(1,i)=P(1,n-i+1);
end
P=Q;
[m,n]=size(T);
for i=1:n
Q(1,i)=T(1,n-i+1);
end
T=Q;
%polyfit多項(xiàng)式擬合
A=polyfit(P,T,6);
Z=polyval(A,P);
plot(P,T,'k',P,Z,'r')
pp = polyder(A); % 對(duì)擬合得到的多項(xiàng)式求導(dǎo),得到導(dǎo)函數(shù)pp
dy2 = polyval(pp,P) % 計(jì)算導(dǎo)函數(shù)pp在x的導(dǎo)數(shù)值
plot(P,dy2,'r')
% 三次樣條插值方法
cs = csapi(P,T); % 生成三次樣條插值函數(shù)cs
pp = fnder(cs); % 計(jì)算三次樣條插值函數(shù)cs的導(dǎo)函數(shù)pp
dy3 = fnval(pp,P) % 計(jì)算導(dǎo)函數(shù)pp在x的導(dǎo)數(shù)值
plot(P,dy3,'k')
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -