Matlab中的切片逆回归误差

时间:2018-06-20 22:01:43

标签: matlab regression

我正在尝试根据李克超的论文(https://cloudfront.escholarship.org/dist/prd/content/qt3mw1p0mb/qt3mw1p0mb.pdf)编写切片逆回归代码。当我使用内置函数eigs时,遇到的错误是下面代码的第2步,但我看不出逻辑有什么问题。

function [PCAMean] = SIR(X, NumH, Y_noisy)
% X is the the input data, which is a m x n matrix
% NumH is the number of slices
% Y_noisy is the function with the error
%% Step 1 - slicing and computing means
%number of slices
%NumH   = 20;
n = size(X,1); %length of the matrix
% 1 indicates the row you are summing and taking the mean
xbar = mean(X, 1);
Xbar = repmat(xbar,n,1);
X = (X - Xbar);
cov_XX = (1/n) * X' * X;
%normalize part
[U, sigma]= eigs(cov_XX);
sigma = diag(sigma);
sigma = 1./sqrt(sigma).*(sigma>0)+0.*(sigma==0);
sigma = diag(sigma);
cov_XX = U * sigma * U';
X_norm = cov_XX * X';
X_norm = X_norm';
%X_norm = X_norm'; %to make it correct size
%X_norm=X;
ymesh  = linspace(min(Y_noisy),max(Y_noisy),NumH+1);
XIdx   = cell(NumH,1);
Xmean  = zeros(NumH,5);
Vhat   = zeros(size(X,2),size(X,2));
for iter = 1:NumH
    Idx        = find((Y_noisy>=ymesh(iter)).*(Y_noisy<ymesh(iter+1)));
    XIdx{iter} = Idx;
    Xmean(iter,:) = sum(X_norm(Idx,:),1)/length(Idx);   
    Vhat          = Vhat + length(Idx)/n * (Xmean(iter,:)).' * Xmean(iter,:);
end
clearvars Idx
%% Step 2 - PCA
%[PCAMean] = pca(Xmean);
%[PCAMean, ~]= eigs(Vhat,2);
PCAMean = (PCAMean.'*cov_XX).';

我使用下面的脚本和前面提到的错误运行了SIR代码。

clear all
close all

%initalize number of data points
NumData = 500;
%randomalize data and determine number of dimensions
dim = 5;
X = rand(NumData,dim);

%number of slices
NumH = [5,10,20,50,100,500,1000];

%Case 1: Linear
%initalize Coefficient matrix (Beta)
Co1 = [1 1 1 1 0]';
STD=[0,0.5,1];

for i=1:length(NumH)
    for j=1:length(STD)
        %Create 'normalized' noise with mean = 0 STD=[0,0.5,1], size: NumData x 1
        E = normrnd(0,STD(j),NumData,1);

        %Multiply random data with Beta
        Y = (X * Co1);

        %Add noise
        Y_noisy_linear = Y+E;

        %Perform SIR
        PCAMean_linear{i,j} =  SIR(X,NumH(i), Y_noisy_linear);
        [m,n]= size(PCAMean_linear);

        %calculate angle

            angle_measure_linear{i,j}=subspace(Co1,PCAMean_linear{i,j}(:,1));



    end
end

0 个答案:

没有答案