# bp网络实现CPI指标预测

xiaoxiao2021-03-01  5

%% clc; clear; % 数据输入 % 某地的CPI指标，有数据如下： d1=[0.2356 0.2589 0.3000 0.2445 0.2589 0.2366 0.2897 0.2368 0.2569 0.2010 0.2111 0.3012]; d2=[0.2562 0.3023 0.2454 0.2465 0.2146 0.2037 0.2898 0.2969 0.2110 0.2111 0.2212 0.3213]; d3=[0.3000 0.2458 0.2315 0.2216 0.2317 0.2218 0.2039 0.2510 0.2111 0.2212 0.2313 0.3114 ]; f=[0.2515 0.2146 0.2147 0.2218 0.2169 0.2260 0.2111 0.2212 0.2313 0.2414 0.2115 0.3216 ]; k=1:1:12; % 使用12个月的数据训练得到网络仿真计算相对误差 p1=zeros(1,12); p2=zeros(1,12); p3=zeros(1,12); t=zeros(1,12); p1(1)=d1(1); for i=2:12 p1(i)=0.1*d1(i)+p1(i-1); end p2(1)=d2(1); for i=2:12 p2(i)=0.1*d2(i)+p2(i-1); end p3(1)=d3(1); for i=2:12 p3(i)=0.1*d3(i)+p3(i-1); end t(1)=f(1); for i=2:12 t(i)=0.1*f(i)+t(i-1); end %数据标准化处理,标准化为网络输入p,期望输出tt p=[(p1-mean(p1))./std(p1);(p2-mean(p2))./std(p2);(p3-mean(p3))./std(p3)]; tt=(t- mean(t))./std(t); %用newff建立bp网络并训练 net=newff(minmax(p),[4,1],{'tansig','purelin'},'trainlm'); net.iw{1,1}=zeros(size(net.iw{1,1}))+0.5; net.lw{2,1}=zeros(size(net.lw{2,1}))+0.75; net.b{1,1}=zeros(size(net.b{1,1}))+0.5; net.b{2,1}=zeros(size(net.b{2,1})); net.trainParam.epochs=3000; net.trainParam.goal =0.000005; % 训练网络 net=train(net,p,tt); %网络仿真得到网络输出，并计算误差 tt1=sim(net,p); %利用标准化的逆变换得到t1的近似值to % to为总增加率 to=tt1.*std(t)+mean(t); a=zeros(1,12); a(1)=to(1); for i=2:12 a(i)=(to(i)-to(i-1))*10; end %累减得到近似的实际产值a err_net=(tt1-tt)./tt;%网络相对误差 err_add=(to-t)./t;%总值累加数相对误差 err_real=(a-f)./f;%实际总值相对误差 %输出结果% r1=[err_net;err_add;err_real]'%网络相对误差/累加数相对误差/实际值相对误差 r=err_real./err_add %误差放大倍数 l=[to;t;to-t;a;f;a-f]'%累加数计算值/累加数/绝对误差//计算的实际值/实际值/绝对误差 plotyy(k,to,k,t);%仿真累加值(to)随时间变化曲线与实际累加值(t)随时间变化曲线 % Create graphs with y-axes on both left and right side % plotyy(X1,Y1,X2,Y2) plots X1 versus Y1 with y-axis labeling on the left % and plots X2 versus Y2 with y-axis labeling on the right. % % This example graphs two mathematical functions using plot as the plotting function. % The two y-axes enable you to display both sets of data on one graph % even though relative values of the data are quite different. x = 0:0.01:20; % y1 = 200*exp(-0.05*x).*sin(x); % y2 = 0.8*exp(-0.5*x).*sin(10*x); % [AX,H1,H2] = plotyy(x,y1,x,y2,'plot'); % % You can use the handles returned by plotyy to label the axes % and set the line styles used for plotting. % With the axes handles you can specify the YLabel properties of the left- % and right-side y-axis: set(get(AX(1),'Ylabel'),'String','Left Y-axis') % set(get(AX(2),'Ylabel'),'String','Right Y-axis') % % Use the xlabel and title commands to label the x-axis and add a title: % xlabel('Zero to 20 /musec.') % title('Labeling plotyy') % % Use the line handles to set the LineStyle properties of the left- % and right-side plots: set(H1,'LineStyle','--') % set(H2,'LineStyle',':') figure(2); plotyy(k,a,k,f);%仿真值(a)随时间变化曲线与实际值(f)随时间变化曲线