kpcak.m
上传用户:nxf103
上传日期:2017-12-04
资源大小:2690k
文件大小:2k
开发平台:

Matlab

  1. function [ctni,CSPE]=kpcak(test,train,aa,ss,ks)
  2. %输入的训练数据和测试数据的每一行代表一个样本
  3. %得出训练数据和测试数据所包含的样本数
  4. %我们假设径向基核函数的度为1,即分母为2
  5. n1=size(train,1);
  6. n2=size(test,1);
  7. n3=size(train,2);
  8. %计算A的大小
  9. A=0;
  10. for i=1:n1
  11.     for j=1:n1
  12.         A=A+exp(-norm(train(i,:)-train(j,:))^2/ks);
  13.     end
  14. end
  15. A=(1/n1^2)*A;
  16. %计算ap的大小
  17. ap=zeros(1,n1);
  18. for i=1:n1
  19.     for j=1:n1
  20.         ap(i)=ap(i)+exp(-norm(train(i,:)-train(j,:))^2/ks);
  21.     end
  22.     ap(i)=(1/n1)*ap(i);
  23. end
  24. %计算对原始输入变量的第k采样做偏导
  25. ctni=zeros(n2,n3);
  26. CSPE=zeros(n2,n3);
  27. add_ctni=zeros(n2,1);
  28. add_CSPE=zeros(n2,1);
  29. for new=1:n2
  30.     kv=zeros(n3,1);
  31.     for i=1:n3
  32.     %第i个偏导的矩阵记为KKPQ
  33.     KKPQ=zeros(n1);
  34.             for p=1:n1
  35.             for q=1:n1
  36.                 KKPQ(p,q)=-1/ks*...
  37.                 (((train(p,i)-test(new,i))^2+(train(q,i)-test(new,i))^2)*onekpca(train(p,:),test(new,:))*onekpca(train(q,:),test(new,:))...
  38.                 +(A-ap(q))*(train(p,i)-test(new,i))^2*onekpca(train(p,:),test(new,:))...
  39.                 +(A-ap(p))*(train(q,i)-test(new,i))^2*onekpca(train(q,:),test(new,:))...
  40.                 -twokpca(test,train,i,p,new)-twokpca(test,train,i,q,new)...
  41.                 +(1/n1)*(ap(p)+ap(q)-2*A)*threekpca(test,train,new,i)...
  42.                 +fourkpca(test,train,i,new));
  43.             end
  44.              kv(i)=kv(i)+(train(p,i)-test(new,i))^2*exp(-norm(train(p,:)-test(new,:))^2/ks);
  45.         end
  46.         
  47.     %计算第new个测试值的第i个观测变量的偏导T平方值ctni
  48.     ctni(new,i)=abs(trace(aa'*KKPQ*aa*ss)); 
  49.     CSPE(new,i)=abs(1/ks*(-2/(ks*n1)*kv(i)+trace(aa'*KKPQ*aa)));
  50.     add_ctni(new)=add_ctni(new)+ctni(new,i);
  51.     add_CSPE(new)=add_CSPE(new)+CSPE(new,i);
  52.     end
  53.     for i=1:n3
  54.     ctni(new,i)=ctni(new,i)/add_ctni(new);
  55.     CSPE(new,i)=CSPE(new,i)/add_CSPE(new);
  56.     end
  57.  end