資源描述:
《因子分析MATLAB程序源代碼1.doc》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫。
1、clearall;DATA=load('D:0.m');DATA=double(DATA);DATA=DATA';TESTDATA=load('D:14f.m');TESTDATA=double(TESTDATA);%DATA=load('D:正常.txt');%DATA=double(DATA);%DATA=DATA(:,3:12);%TESTDATA=load('D:異常.txt');%TESTDATA=double(TESTDATA);%TESTDATA=TESTDATA(:,3:12);[Kp,T2]=
2、tztq(DATA,TESTDATA);function[contribution,T2,SPE,t2cl,s_cl]=PCA_model(Xtrain,Xtest)X_mean=mean(Xtrain);X_std=std(Xtrain);[X_row,X_col]=size(Xtrain);fori=1:X_colXtrain(:,i)=(Xtrain(:,i)-X_mean(i))./X_std(i);Xtest(:,i)=(Xtest(:,i)-X_mean(i))./X_std(i);end[U,S,
3、V]=svd(Xtrain./sqrt(size(Xtrain,1)-1),0);D=S^2;lamda=diag(D);num_pc=1;whilesum(lamda(1:num_pc))/sum(lamda)<0.9num_pc=num_pc+1;endD=diag(lamda);P=V(:,1:num_pc);[a,b]=size(Xtest);[r,y]=size(P*P');I=eye(r,y);e=Xtest*(I-P*P');fori=1:aT2(i)=Xtest(i,:)*P*inv(D(1:n
4、um_pc,1:num_pc))*P'*Xtest(i,:)';endforl=1:aSPE(l)=e(l,:)*e(l,:)';endforj=1:bcontribution(j)=(norm(e(:,j)))^2;endt2cl=num_pc*(X_row-1)*(X_row+1)*icdf('f',0.99,num_pc,X_row-num_pc)/(X_row*(X_row-num_pc));fori=1:3theta(i)=trace((D(num_pc+1:X_col,num_pc+1:X_col)
5、)^i);end%另一種SPE控制線算法%h=(theta(1)^2)/theta(2);%g=theta(2)/theta(1);%conf=0.95;%df=round(h);%delta2a1=g*pinv(df,2);h0=1-2*theta(1)*theta(3)/(3*theta(2)^2);ca=icdf('norm',0.99,0,1);s_cl=theta(1)*(ca*sqrt(2*theta(2)*h0^2)/theta(1)+1+theta(2)*h0*(h0-1)/theta(1)^2
6、)^(1/h0);clearall;X1=load('D:0.m');Xtrain=X1';Xtrain=double(Xtrain);X2=load('D:14f.m');Xtest=X2;Xtest=double(Xtest);%X1=load('D:正常br.txt');%Xtrain=X1(:,3:62);%Xtrain=double(Xtrain);%X2=load('D:異常br.txt');%Xtest=X2(:,3:62);%Xtest=double(Xtest);[contribution,T
7、2,SPE,t2cl,s_cl]=PCA_model(Xtrain,Xtest);figurex=size(Xtest,1);plot(1:x,SPE,'k');holdon;plot(1:x,s_cl,'r-');title('SPE');holdoff;figureplot(1:x,T2,'K');title('T2');holdon;plot(1:x,t2cl,'r-');holdoff;figurebar(contribution,'group')title('貢獻圖');function[Kp,T2]
8、=tztq(ax,ay)[Nx]=size(ax);mean_X=mean(ax);axb=ax;std_X=std(ax);ax=ax-mean_X(ones(Nx,1),:);std_X(find(std_X==0))=1;%數(shù)據(jù)預(yù)處理ax=ax./std_X(ones(Nx,1),:);c=10000;%gama=0.05;%ni=1;%F1=ax(1,:);%F=F1';%fo