本人用到MATLAB神经网络NAR时间序列做预测,根据网上的程序和MATLAB说明,整理出了程序如下:
clc;close all;clear all;
load data1;
M=1080; %M为学习数据个数
N=9720; %N为预测数据个数
%get the data
data1=data(1:M);
data2=data(M+1:M+N);
%prepare training data
T=data1';
T=con2seq(T(1:M));%m=1*ones(1,M);T=mat2cell(T,1,m);
%prepare validation data
T2=data2';
T2=con2seq(T2(1:N));%m2=1*ones(1,N);T2=mat2cell(T2,1,m2);
%define nolinear autoregressive neural network
inputDelays=5;
hiddenSizes=10;
net=narnet(inputDelays,hiddenSizes);
%prepare target time series data for network traning
[Xs,Xi,Ai,Ts]=preparets(net,{},{},T);
%traning net
net=train(net,Xs,Ts,Xi,Ai);
view(net);
Y=net(Xs,Xi);
perf=perform(net,Ts,Y)
%prepare validation data for network simulation
yini=T(end-max(inputDelays)+1:end);%initial value from training data
%combine initial value and validation data T2
[Xs,Xi,Ai]=preparets(net,{},{},[yini,T2]);
%predict on validation data
predict=net(Xs,Xi,Ai);
%calculate error and RMSE
Yp=cell2mat(predict);
add=0;fch=0;
for i=1:N
error(i)=data2(i)-Yp(i);
add=add+data(i)*data(i);
fch=fch+error(i)*error(i);;
end
RMSE=sqrt(fch/length(data2))/sqrt(add/length(data2))
plot(Yp,'r');
hold on;
plot(data2,'g');
plot(error,'b');
kk
网上源代码
http://lab.fs.uni-lj.si/lasin/wp/IMIT_files/neural/nn05_narnet/