%此程序FGMC1N模型(已完成)
clc;
clear;
close all;
% Ts=[1931,1724,1517,1345,1207,1069,952,848,745,669]';
% Bhn=[555,477,444,415,363,302,277,255,223,201]';
Ts=[0,1931,1724,1517,1345,1207,1069,952,848,745,669]';
Bhn=[0,555,477,444,415,363,302,277,255,223,201]';
X0=[Ts,Bhn];
X0Train=X0(1:6,:); %训练集。用前面5组数据用于参数的计算,再对整体10组数据验证。
% [m,n]=size(X0Train);
% [mt,nt]=size(X0);
[mtrain,ntrain]=size(X0Train);
[m,n]=size(X0);
%%做1-AGO序列
% for i=1:n
% for j=1:m
% X1(j,i)=sum(X0Train((1:j),i));
% end
% end
for i=1:ntrain
for j=1:mtrain
X1Train(j,i)=sum(X0Train((1:j),i));
end
end
% for i=1:nt
% for j=1:mt
% X1T(j,i)=sum(X0((1:j),i));
% end
% end
for i=1:n
for j=1:m
X1(j,i)=sum(X0((1:j),i));
end
end
%%紧邻生成序列
for j=2:mtrain
for i=1:ntrain
Z(j,i)=(X1Train(j,i)+X1Train(j-1,i))*0.5;
end
end
%计算参数列
B(:,1)=-Z(:,1);
B(:,2:ntrain)=Z(:,2:ntrain);
B(:,ntrain+1)=ones(mtrain,1);
B=B(2:mtrain,:);
Y(:,1)=X0(2:mtrain,1);
b=inv(B'*B)*B'*Y;
a=b(1);
b(1)=[];
u=b(ntrain);
b(ntrain)=[];
% for t=2:m
% A1=0;
% for k=2:t-1
% A1=A1+f(k,X1(:,2:n),b,u)*exp(-a*(t-k));
% end
% X1yc(t)=X1(1,1)*exp(-a*(t-1))+ut(t-2)*...
% (0.5*exp(-a*(t-1))*f(1,X1(:,2:n),b,u)+0.5*f(t,X1(:,2:n),b,u)+A1);
% end
% X1yc(1)=X0(1,1);
% for t=2:m
% X0yc(t)=X1yc(t)-X1yc(t-1);
% end
% X0yc(1)=X0(1,1);
% %作残差svm
% Residuals=(X0(:,1)'-X0yc)';
% input=X1yc;
% output=Residuals';
%
% global p1
% p1=18;
% ker='rbf';
% C=100;
% loss='eInsensitive';
% e=0;
%
% [nsv beta bias] = svr(input',output',ker,C,loss,e);
% Y=svroutput(input',input',ker,beta,bias);
% Y=Y';
% %还原值
% for i=1:m
% X01yc(i)=X0yc(i)+Y(i);
% end
% figure(1)
% plot(X0(:,1)','-ko');hold on
% plot(X0yc,'--b*');hold on
% % plot(X01yc,'-.rv');
% % set(gca,'Ylim',[2,3])
% title('TRAIN')
% xlabel('Sample Number');
% ylabel('MI');
% % legend('Analysis','GMC(1,N)','GMC-SVM');
% legend('Analysis','GMC(1,N)');
% %计算训练集的误差
% disp('Train:')
% [GMC_MRE_Train,GMC_MXRE_Train] = MRE(X0(:,1),X0yc');
% [GMC_MAE_Train,GMC_MXAE_Train] = MAE(X0(:,1),X0yc');
% GMC_RMSE_Train=RMSE(X0(:,1),X0yc');
% GMC_TIC_Train = TIC(X0(:,1),X0yc');
% GMC_STD_Train = stda(X0(:,1),X0yc');
% disp('GMC_Train Error:')
% disp(' MRE MXRE MAE MXAE RMSE TIC STD')
% format = ' %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f';
% tab =sprintf(format,[GMC_MRE_Train,GMC_MXRE_Train,GMC_MAE_Train,GMC_MXAE_Train,GMC_RMSE_Train,GMC_TIC_Train,GMC_STD_Train]);
% disp(tab)
% [GMC_LSSVM_MRE_Train,GMC_LSSVM_MXRE_Train] = MRE(X0(:,1),X01yc');
% [GMC_LSSVM_MAE_Train,GMC_LSSVM_MXAE_Train] = MAE(X0(:,1),X01yc');
% GMC_LSSVM_RMSE_Train=RMSE(X0(:,1),X01yc');
% GMC_LSSVM_TIC_Train = TIC(X0(:,1),X01yc');
% GMC_LSSVM_STD_Train = stda(X0(:,1),X01yc');
% disp('GMC_LSSVM_Train Error:')
% disp(' MRE MXRE MAE MXAE RMSE TIC STD')
% format = ' %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f';
% tab =sprintf(format,[GMC_LSSVM_MRE_Train,GMC_LSSVM_MXRE_Train,GMC_LSSVM_MAE_Train,GMC_LSSVM_MXAE_Train,GMC_LSSVM_RMSE_Train,GMC_LSSVM_TIC_Train,GMC_LSSVM_STD_Train]);
% disp(tab)
% disp(' ')
%% test dataset
for t=2:m
% A1=0;
% for k=2:t-1
% A1=A1+f(k,X1(:,2:n),b,u)*exp(-a*(t-k));
% end
% X1Tyc(t)=X1(1,1)*exp(-a*(t-1))+ut(t-2)*...
% (0.5*exp(-a*(t-1))*f(1,X1(:,2:nt),b,u)+0.5*f(t,X1(:,2:n),b,u)+A1);
% X1yc(t)=X1(1,1)*exp(-a*(t-1))+ut(t-2)*...
% (0.5*exp(-a*(t-1))*f(1,X1(:,2:n),b,u)+0.5*f(t,X1(:,2:n),b,u)+A1);
A2=0;
for k=2:t
A2=A2+(f(k,X1(:,2:n),b,u)+f(k-1,X1(:,2:n),b,u))/2*exp(-a*(t-k+1/2));
end
X1yc(t)=X1(1,1)*exp(-a*(t-1))+ut(t-2)*A2;
end
X1yc(1)=X0(1,1);
for t=2:m
X0yc(t)=X1yc(t)-X1yc(t-1);
end
% X0Tyc(1)=X0T(1,1);
X0yc(1)=X0(1,1);
% inputT=X1Tyc;
% Y=svroutput(input',inputT',ker,beta,bias);
% Y=Y';
% for i=1:mt
% X01Tyc(i)=X0Tyc(i)+Y(i);
% end
figure(2);
% t=1:m;
% plot(t,X0T(:,1)','-ko',t,X0Tyc,'--b*',t,X01Tyc,'-.rv');
% plot(t,X0T(:,1)','-ko',t,X0Tyc,'--b*');
% plot(t,X0(:,1)','-ko',t,X0yc,'--b*');
X0(1,:)=[];
X0yc(1)=[];
plot(X0(:,1)','-ko');hold on
plot(X0yc,'--b*');
% set(gca,'Ylim',[2,3])
title('TEST')
xlabel('Sample Number');
ylabel('MI');
% legend('Analysis','GMC(1,N)','GMC-SVM');
legend('Analysis','FGMC(1,N)');
%计算测试集的误差
[GMC_MRE_Test,GMC_MXRE_Test] = MRE(X0(:,1),X0yc');
[GMC_MAE_Test,GMC_MXAE_Test] = MAE(X0(:,1),X0yc');
GMC_RMSE_Test=RMSE(X0(:,1),X0yc');
GMC_TIC_Test = TIC(X0(:,1),X0yc');
GMC_STD_Test = stda(X0(:,1),X0yc');
disp('Test:')
disp('GMC_Test Error:')
disp(' MRE MXRE MAE MXAE RMSE TIC STD')
format = ' %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f';
tab =sprintf(format,[GMC_MRE_Test,GMC_MXRE_Test,GMC_MAE_Test,GMC_MXAE_Test,GMC_RMSE_Test,GMC_TIC_Test,GMC_STD_Test]);
disp(tab)
% [GMC_LSSVM_MRE_Test,GMC_LSSVM_MXRE_Test] = MRE(X0T(:,1),X01Tyc');
% [GMC_LSSVM_MAE_Test,GMC_LSSVM_MXAE_Test] = MAE(X0T(:,1),X01Tyc');
% GMC_LSSVM_RMSE_Test=RMSE(X0T(:,1),X01Tyc');
% GMC_LSSVM_TIC_Test = TIC(X0T(:,1),X01Tyc');
% GMC_LSSVM_STD_Test = stda(X0T(:,1),X01Tyc');
% disp('GMC_LSSVM_Test Error:')
% disp(' MRE MXRE MAE MXAE RMSE TIC STD')
% format = ' %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f';
% tab =sprintf(format,[GMC_LSSVM_MRE_Test,GMC_LSSVM_MXRE_Test,GMC_LSSVM_MAE_Test,GMC_LSSVM_MXAE_Test,GMC_LSSVM_RMSE_Test,GMC_LSSVM_TIC_Test,GMC_LSSVM_STD_Test]);
% disp(tab)