% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % [U,xi_pcs,xi_res] = compute_reconstruction_error_matrix(X,xi) %%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % FUNCTIONALITY: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This function purpose is to compute the reconstruction error matrix %%%% % for a given data set X for a certain fault set xi. %%%%%%%%%%%%%%%%%%%%% % INPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % X: Scaled data set used to compute the reconstruction error matrix. %% % xi: Fault data set used to compute the reconstruction error matrix. %%% % OUTPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % U: Unreconstructed variance matrix for the data set X and the %%%% % fault set xi. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % xi_pcs: Projection of the fault matrix xi in the principal components % % subspace. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % xi_res: Projection of the fault matrix xi in the residual subspace. %%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% We initialise the values of the reconstruction error vector (1 value for each possible number of principal components)
0001 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0002 %% [U,xi_pcs,xi_res] = compute_reconstruction_error_matrix(X,xi) %%%%%%%%%% 0003 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0004 %% FUNCTIONALITY: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0006 %% This function purpose is to compute the reconstruction error matrix %%%% 0007 %% for a given data set X for a certain fault set xi. %%%%%%%%%%%%%%%%%%%%% 0008 %% INPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0009 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0010 %% X: Scaled data set used to compute the reconstruction error matrix. %% 0011 %% xi: Fault data set used to compute the reconstruction error matrix. %%% 0012 %% OUTPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0013 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0014 %% U: Unreconstructed variance matrix for the data set X and the %%%% 0015 %% fault set xi. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0016 %% xi_pcs: Projection of the fault matrix xi in the principal components % 0017 %% subspace. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0018 %% xi_res: Projection of the fault matrix xi in the residual subspace. %%% 0019 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0020 function [U,xi_pcs,xi_res] = compute_reconstruction_error_matrix(X,xi) 0021 % We initialise the values of the reconstruction error vector (1 value for 0022 % each possible number of principal components) 0023 U = zeros(1,size(X,2)); 0024 % We compute the correlation matrix of the data set X 0025 corr_X = (X' * X) / (size(X,1) - 1); 0026 % We decompose the correlation matrix in eigenvectors and eigenvalues 0027 [eigenvectors_X, eigenvalues_X] = eig(corr_X); 0028 % Next, we sort in descending order the eigenvalues to obtain the PCA 0029 % decomposition 0030 [~, indices_sort_eigenvectors_X] = ... 0031 sort(diag(eigenvalues_X), 'descend'); 0032 % We sort the same way the eigenvectors, to obtain the loading vectors of 0033 % the PCA decomposition 0034 P = eigenvectors_X(:, indices_sort_eigenvectors_X); 0035 % We initialise the structure that will contain all the projections in the 0036 % residual and principal components subspaces for each number of principal 0037 % components 0038 % - Projection of fault directions in the principal components subspace 0039 xi_pcs = cell(1,size(xi,2)); 0040 % - Projection of fault direction in the residual subspace 0041 xi_res = cell(1,size(xi,2)); 0042 % We obtain the score matrix (T) and the loading vectors (P) of the pca 0043 % model 0044 T = X * P; 0045 % Next, we compute the covariance matrix for the observation matrix X. 0046 R = (X' * X) / (size(X, 1) - 1); 0047 % For every possible number of principal components 0048 for i = 1:size(T,2) 0049 % We compute the projection matrix of theta in the projection space 0050 xi_pcs{i} = (xi'*P(:,1:i)*P(:,1:i)')'; 0051 % We compute the projection matrix of theta in the residual space 0052 xi_res{i} = xi - xi_pcs{i}; 0053 % - Now, we can compute the variance of the reconstruction error for 0054 % the residual subspace projection of the faulty situations into a PCA 0055 % model which retains i principal components. 0056 % SIMPLIFIED METHOD TO COMPUTE THE VRE! 0057 U_i = diag(xi_res{i}' * R * xi_res{i}) ./ diag(xi_res{i}' * ... 0058 xi_res{i}).^2; 0059 % We initialise the weighting vector q (we assume all faults have the 0060 % same importance, so we set all of them to 1) 0061 q = ones(size(U_i,1),1); 0062 % We normalise the vector so its norm is 1 0063 q = q / norm(q); 0064 % We compute the reconstruction error vector with the i first principal components with the weighted sum 0065 % of the projection error and the residual error 0066 U(i) = q'*U_i; 0067 end