Home > Codi > variance_reconstruction_error.m

variance_reconstruction_error

PURPOSE ^

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SYNOPSIS ^

function VRE_Struct = variance_reconstruction_error(X, xi)

DESCRIPTION ^

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% VRE_Struct = variance_reconstruction_error(X, xi) %%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTIONALITY: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function aim is to find the optimal number of principal %%%%%%%%%%%
% components to grant the minimum reconstruction error for a given set %%%
% of faults. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% X:    Scaled data set (zero mean and unit variance) to find its minimum %
%      reconstruction error. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% xi:    Initial set of faults to find its minimum reconstruction error. %%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OUTPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% VRE_Struct:  Structure that contains all output elements obtained %%%%%%
%              after computing the unvariance of the reconstruction %%%%%%
%              error, which are: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - X_rec:     Final scaled data that can be reconstructed for the given %
%              set of faults and sensors in the statistical model. %%%%%%%
% - l_rec:     Number of principal components that have an %%%%%%%%%%%%%%%
%              unreconstructed variance lower than the desired %%%%%%%%%%%
%              threshold. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - xi_rec:    Set of faults that can be correctly detected, %%%%%%%%%%%%%
%              reconstructed and diagnosed using l_rec number of %%%%%%%%%
%              principal components. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - U_local:    Matrix with the unreconstructed variance for each fault %%%
%              in xi_rec and every possible number of principal %%%%%%%%%%
%              components. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - U_global:    Vector with the unreconstructed variance for all the %%%%%%
%              faults at the same time with every possible number of %%%%%
%              principal components. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - sensors:    Vector that contains those sensors that can be %%%%%%%%%%%%
%              reconstruted at each iteration. %%%%%%%%%%%%%%%%%%%%%%%%%%%
% - faults:    Vector containing the indexs of those faults that can be %%
%              reconstructed. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - factors:    Vector containing the factors used to decide whether a %%%%
%              fault can be reconstruced or not. %%%%%%%%%%%%%%%%%%%%%%%%%
% - xi_pcs:    Projection of the matrix xi in the principal components %%%
%              subspace. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - xi_res:    Porjection of the matrix xi in the residual subspace. %%%%%
%              This matrix is required for fault diagnosis and %%%%%%%%%%%
%              reconstruction. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - n_loops:   Number of iterations the method required to find the %%%%%%
%              reconstructible sensors and the number of principal %%%%%%%
%              componentes to use. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 We initialise the output parameter to the worst case. In this case, the
 worst situation would be that we remove only one sensor at a time, so we
 will define the size of the output parameters to the number of sensors at
 the first iteration.
 - Data set to be used for the model generation

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0002 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0003 %% VRE_Struct = variance_reconstruction_error(X, xi) %%%%%%%%%%%%%%%%%%%%%%
0004 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %% FUNCTIONALITY: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0006 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0007 %% This function aim is to find the optimal number of principal %%%%%%%%%%%
0008 %% components to grant the minimum reconstruction error for a given set %%%
0009 %% of faults. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0010 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0011 %% INPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0012 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0013 %% X:    Scaled data set (zero mean and unit variance) to find its minimum %
0014 %%      reconstruction error. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0015 %% xi:    Initial set of faults to find its minimum reconstruction error. %%%
0016 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0017 %% OUTPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0018 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0019 %% VRE_Struct:  Structure that contains all output elements obtained %%%%%%
0020 %%              after computing the unvariance of the reconstruction %%%%%%
0021 %%              error, which are: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0022 %% - X_rec:     Final scaled data that can be reconstructed for the given %
0023 %%              set of faults and sensors in the statistical model. %%%%%%%
0024 %% - l_rec:     Number of principal components that have an %%%%%%%%%%%%%%%
0025 %%              unreconstructed variance lower than the desired %%%%%%%%%%%
0026 %%              threshold. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0027 %% - xi_rec:    Set of faults that can be correctly detected, %%%%%%%%%%%%%
0028 %%              reconstructed and diagnosed using l_rec number of %%%%%%%%%
0029 %%              principal components. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0030 %% - U_local:    Matrix with the unreconstructed variance for each fault %%%
0031 %%              in xi_rec and every possible number of principal %%%%%%%%%%
0032 %%              components. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0033 %% - U_global:    Vector with the unreconstructed variance for all the %%%%%%
0034 %%              faults at the same time with every possible number of %%%%%
0035 %%              principal components. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0036 %% - sensors:    Vector that contains those sensors that can be %%%%%%%%%%%%
0037 %%              reconstruted at each iteration. %%%%%%%%%%%%%%%%%%%%%%%%%%%
0038 %% - faults:    Vector containing the indexs of those faults that can be %%
0039 %%              reconstructed. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0040 %% - factors:    Vector containing the factors used to decide whether a %%%%
0041 %%              fault can be reconstruced or not. %%%%%%%%%%%%%%%%%%%%%%%%%
0042 %% - xi_pcs:    Projection of the matrix xi in the principal components %%%
0043 %%              subspace. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0044 %% - xi_res:    Porjection of the matrix xi in the residual subspace. %%%%%
0045 %%              This matrix is required for fault diagnosis and %%%%%%%%%%%
0046 %%              reconstruction. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0047 %% - n_loops:   Number of iterations the method required to find the %%%%%%
0048 %%              reconstructible sensors and the number of principal %%%%%%%
0049 %%              componentes to use. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0050 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0051 function VRE_Struct = variance_reconstruction_error(X, xi)
0052 % We initialise the output parameter to the worst case. In this case, the
0053 % worst situation would be that we remove only one sensor at a time, so we
0054 % will define the size of the output parameters to the number of sensors at
0055 % the first iteration.
0056 % - Data set to be used for the model generation
0057 VRE_Struct.X_rec = cell(1,size(X,2));
0058 % - Number of principal components that present that satisfies the
0059 % variance of reconstruction error condition (minimum or at least
0060 % rec_tolerance)
0061 VRE_Struct.l_rec = cell(1,size(X,2));
0062 % - Matrix relating faults and sensors faults that can be reconstructed
0063 % with the variance of reconstruction error condition
0064 VRE_Struct.xi_rec = cell(1,size(X,2));
0065 % - Matrix containing the relation of faults and the variance of
0066 % reconstruction error for every possible number of principal components
0067 VRE_Struct.U_global = cell(1,size(X,2));
0068 % - Vector containing the relation of the variance of reconstruction error
0069 % for all the matrix faults that will be considered in order to find the
0070 % global satisfaction condition optimal
0071 VRE_Struct.U_local = cell(1,size(X,2));
0072 % - Vector containing all the sensors that satisfy the variance of
0073 % reconstruction error condition
0074 VRE_Struct.sensors = cell(1,size(X,2));
0075 % - Vector containing all the faults that satisfy the variance of
0076 % reconstruction error condition
0077 VRE_Struct.faults = cell(1,size(xi,2));
0078 % - Vector containing the factors used to decide whether a fault can be
0079 % reconstructed or not
0080 VRE_Struct.factors = cell(1,size(X,2));
0081 % - Vector containing the projection of the fault matrix xi in the
0082 % principal components space
0083 VRE_Struct.xi_pcs = cell(1,size(X,2));
0084 % - Vector containing the projection of the fault matrix in the residual
0085 % subspace
0086 VRE_Struct.xi_res = cell(1,size(X,2));
0087 % By default we initialise the loop control variable to 1
0088 loop = 1;
0089 % We set the parameters to compute all the variables to the original ones
0090 X_loop = X;
0091 xi_loop = xi;
0092 sensors_loop = 1:size(xi,1);
0093 faults_loop = 1:size(xi,2);
0094 % We initialise the position to write the results of iteration i
0095 j = 1;
0096 % While there are changes...
0097 while loop == 1
0098     % We compute the covariance matrix
0099     R_loop = (X_loop'*X_loop) / (size(X_loop, 1) - 1);
0100     % We initialise the reconstruction error matrix for this loop
0101     U_local_loop = zeros(size(xi_loop,2),size(X_loop, 2));
0102     % We compute the full reconstruction error matrix for each fault and number
0103     % of principal components
0104     for i = 1:size(xi_loop,2)
0105         [U_local_loop(i,:),~,~] = compute_reconstruction_error_matrix(X_loop,xi_loop(:,i));
0106     end
0107     % We compute the reconstruction error matrix for the whole fault set
0108     [U_global_loop,xi_pcs_loop,xi_res_loop] = compute_reconstruction_error_matrix(X_loop,xi_loop);
0109     % We find the minimum value of unreconstructed variance in the whole data set that
0110     % will be the optimal of this loop
0111     [~,l_rec_loop] = min(U_global_loop(1:length(U_global_loop) - 1));
0112     % We compute the factor to decide wheter a fault can be reconstructed
0113     % enough
0114     factor = diag(xi_loop' * R_loop * xi_loop);
0115     % We give value to the output parameters
0116     VRE_Struct.X_rec{j} = X_loop;
0117     VRE_Struct.l_rec{j} = l_rec_loop;
0118     VRE_Struct.xi_rec{j} = xi_loop;
0119     VRE_Struct.sensors{j} = sensors_loop;
0120     VRE_Struct.U_local{j} = U_local_loop;
0121     VRE_Struct.U_global{j} = U_global_loop;
0122     VRE_Struct.faults{j} = faults_loop;
0123     VRE_Struct.factors{j} = factor;
0124     VRE_Struct.xi_pcs{j} = xi_pcs_loop;
0125     VRE_Struct.xi_res{j} = xi_res_loop;
0126     % With the l optimal number of principal components we find if there
0127     % are any faults that cannot be reconstructed using the l_opt_rec_loop
0128     % number of principal components
0129     reconstructibility = U_local_loop(:,l_rec_loop) > factor;
0130     % We find those indices that are greater than the factor
0131     % (non_reconstructible faults)
0132     non_reconstructible = find(reconstructibility);
0133     % We update the loop control variable
0134     loop = ~isempty(non_reconstructible) & length(sensors_loop) ~= ...
0135         length(non_reconstructible);
0136     % If there are non reconstructible faults we have to update the
0137     % parameters for the next loop, otherwise, the algorithm ends here
0138     if loop == 1
0139         % We find the sensors associated to the non reconstructable faults
0140         [rows,~] = find(xi_loop(:,non_reconstructible));
0141         % We erase those faults that cannot be reconstructed from the fault
0142         % set
0143         xi_loop(:,non_reconstructible) = [];
0144         % We remove from the fault data set those that are not
0145         % reconstructible
0146         faults_loop(:,non_reconstructible) = [];
0147         % We erase those sensors associated to non reconstructible sensors
0148         xi_loop(rows,:) = [];
0149         % We eliminate the non reconstructible faults associated sensors
0150         % from the data set
0151         X_loop(:,rows) = [];
0152         % We remove those sensors that cannot be reconstructed
0153         sensors_loop(rows) = [];
0154         % We increment the position to write the next result
0155         j = j + 1;
0156     end
0157 end
0158 % We look if there were any sensor that was reconstructible at the end
0159 if length(sensors_loop) == length(non_reconstructible)
0160     % We advise the user that all sensors would be removed that there are
0161     % no sensors to be reconstructed
0162     fprintf('At iteration %d all sensors cannot be reconstruced!!\n',j);
0163 end
0164 % Finally, we only have to remove those positions of each variable that has
0165 % not been used
0166 VRE_Struct.X_rec(j+1:size(X,2)) = [];
0167 VRE_Struct.l_rec(j+1:size(X,2)) = [];
0168 VRE_Struct.xi_rec(j+1:size(X,2)) = [];
0169 VRE_Struct.sensors(j+1:size(X,2)) = [];
0170 VRE_Struct.U_local(j+1:size(X,2)) = [];
0171 VRE_Struct.U_global(j+1:size(X,2)) = [];
0172 VRE_Struct.faults(j+1:size(xi,2)) = [];
0173 VRE_Struct.factors(j+1:size(X,2)) = [];
0174 VRE_Struct.xi_pcs(j+1:size(X,2)) = [];
0175 VRE_Struct.xi_res(j+1:size(X,2)) = [];
0176 VRE_Struct.n_loops = j;

Generated on Wed 12-Sep-2012 13:03:54 by m2html © 2005