function varargout = DAP(varargin)
% DAP M-file for DAP.fig
% DAP, by itself, creates a new DAP or raises the existing
% singleton*.
%
% H = DAP returns the handle to a new DAP or the handle to
% the existing singleton*.
%
% DAP('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DAP.M with the given input arguments.
%
% DAP('Property','Value',...) creates a new DAP or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before DAP_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to DAP_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Last Modified by GUIDE v2.5 28-Jun-2008 00:31:33
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DAP_OpeningFcn, ...
'gui_OutputFcn', @DAP_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before DAP is made visible.
function DAP_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to DAP (see VARARGIN)
% Normalize DAP's existing singleton raise
set(handles.datasets_popupmenu, 'Value', 1)
set(handles.datasets_popupmenu, 'String', {'Training Sample only', 'Train. Samp. + Classify Sample'})
set(handles.covariance_popupmenu, 'Value', 1)
set(handles.covariance_popupmenu, 'String', {'S-estimator', 'Donoho-Stahel', 'Classical'})
set(handles.model_popupmenu, 'Value', 1)
set(handles.model_popupmenu, 'String', {'Common Principal Compon.', 'Proportional', 'Usual Quadratic', 'Linear'})
set(handles.importdata_pushbutton,'Enable','off')
set(handles.estimators_pushbutton,'Enable','off')
set(handles.modelfit_pushbutton,'Enable','off')
set(handles.classify_pushbutton,'Enable','off')
% Finds and deletes previous Data Classification plotting figure windows
h = findobj('-regexp','Name','Data Classification');
delete(h);
% Finds and deletes previous Data Discrimination plotting figure windows
h = findobj('-regexp','Name','Data Discrimination');
delete(h);
% Finds and deletes previous Normal Probability plotting figure windows
h = findobj('-regexp','Name','Normal Probability');
delete(h);
% Choose default command line output for DAP
handles.output = hObject;
% Warning supression
warning off MATLAB:divideByZero
% Creates DAP main data structure
handles.m_dap = struct('constants',[],'datasets',[]);
% Creates DAP predefined input info values
handles.m_preDefInputVal.numGrp = '2';
handles.m_preDefInputVal.numVar = '2';
handles.m_preDefInputVal.numElemPerGrp = ['50'];
handles.m_preDefInputVal.classifySampleSize = '100';
% Creates the main preconfigured constants
handles.m_dap.constants.maxrep = 1000; % maximum number of replications
handles.m_dap.constants.breakdwnp = 0.25; % breakdown point for the S-estimator method
% Imports the Donoho-Stahel Estimator's dimensional constants data file
handles.m_dap.DSdim = ImportDonohoStahelDimensions;
[maxdim, dummy] = size(handles.m_dap.DSdim);
handles.m_dap.constants.maxDSdim = maxdim; % current maximum optimal dimension for the S-estimator method
% Creates the input sample datasets
handles.m_samples = {'Training Sample', ''};
% Creates the input info to import data
handles.m_dap.constants.numGrp = 0;
handles.m_dap.constants.numVar = 0;
handles.m_dap.constants.numElemPerGrp = [];
handles.m_dap.constants.classifySampleSize = 0;
handles.m_dap.constants.aprioriProb = []; % Group membership a priori probabilities
% Creates the input classify sample data
handles.m_dap.datasets.trainingSample = [];
% Creates the input data per groups
handles.m_dap.datasets.dataPerGroups = {};
% Creates the input classify sample data
handles.m_dap.datasets.classifySample = [];
% Creates the model
handles.m_model = 'cpc';
% Creates the covariance and inverse matrices lists
handles.m_dap.covList = {}; % Usual Quadratic
handles.m_dap.invCovList = {};
% Creates the modeled covariance and inverse matrices lists
handles.m_dap.modeledCovList.cpc = {};
handles.m_dap.modeledInvCovList.cpc = {};
handles.m_dap.modeledCovList.proportional = {};
handles.m_dap.modeledInvCovList.proportional = {};
handles.m_dap.modeledCovList.linear = {};
handles.m_dap.modeledInvCovList.linear = {};
% Creates the mean vector list
handles.m_dap.meanList = {};
% Creates the robust covariance method
handles.m_method = 'S-estimator';
%Import logo image
logoist = imread('logoist.jpg','jpeg');
% Display logo image
image(logoist)
% Remove axis ticks and numbers
axis off
% Choose default command line output for Proto1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes DAP wait for user response (see UIRESUME)
% uiwait(handles.m_dap);
% --- Outputs from this function are returned to the command line.
function varargout = DAP_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in input_pushbutton.
function input_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to input_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[trainingSampleInfo, classifySampleSize, handles.m_preDefInputVal] = InputInfo(handles.m_samples, handles.m_preDefInputVal);
if trainingSampleInfo.numGrp == 0 | trainingSampleInfo.numVar == 0 | trainingSampleInfo.numElemPerGrp == 0 ...
| (~strcmp(handles.m_samples{2}, '') & classifySampleSize == 0)
set(handles.importdata_pushbutton,'Enable','off')
set(handles.estimators_pushbutton,'Enable','off')
set(handles.classify_pushbutton,'Enable','off')
return
end
handles.m_dap.constants.numGrp = trainingSampleInfo.numGrp;
disp(sprintf('Number of Groups'))
disp(handles.m_dap.constants.numGrp)
handles.m_dap.constants.numVar = trainingSampleInfo.numVar;
disp(sprintf('Number of Variables'))
disp(handles.m_dap.constants.numVar)
handles.m_dap.constants.numElemPerGrp = trainingSampleInfo.numElemPerGrp;
disp(sprintf('Number of Observations per Group'))
disp(handles.m_dap.constants.numElemPerGrp)
handles.m_dap.constants.classifySampleSize = classifySampleSize;
disp('Classify Sample size')
disp(handles.m_dap.constants.classifySampleSize)
if handles.m_dap.constants.classifySampleSize == 0
disp(sprintf('\b(no Classify Sample)\n'))
end
format rat
handles.m_dap.constants.aprioriProb = trainingSampleInfo.numElemPerGrp / sum(trainingSampleInfo.numElemPerGrp);
disp(sprintf('Group membership a priori probabilities'))
disp(handles.m_dap.constants.aprioriProb)
format
guidata(hObject,handles)
set(handles.importdata_pushbutton, 'Enable', 'on')
set(handles.estimators_pushbutton,'Enable','off')
set(handles.modelfit_pushbutton,'Enable','off')
set(handles.classify_pushbutton,'Enable','off')
% --- Executes on button press in importdata_pushbutton.
function importdata_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to importdata_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
validData.valid = [false false]; % boolean vector indicating if the training and classify samples are valid
validData.reInputDataInfo = false;
datasets = {};
while (~all(validData.valid) & ~validData.reInputDataInfo)
datasets = DataImport(handles.m_samples, validData.valid, datasets);
validData = validateData(datasets, handles.m_samples, handles.m_dap.constants, validData);
if any(~validData.valid)
set(handles.importdata_pushbutton,'Enable','off')
set(handles.estimators_pushbutton,'Enable','off')
set(handles.modelfit_pushbutton,'Enable','off')
set(handles.classify_pushbutton,'Enable','off')
end
end
if validData.reInputDataInfo
return
end
dataPerGroups = {};
rowInitial = 1;
for i = 1:handles.m_dap.constants.numGrp
rowFinal = rowInitial+handles.m_dap.constants.numElemPerGrp(i) - 1;
dataPerGroups{i} = datasets{1}(rowInitial:rowFinal,:);
dataPerGroups{i} = dataPerGroups{i};
rowInitial = rowFinal + 1;
end
handles.m_dap.datasets.trainingSample = datasets{1};
disp(sprintf('Training Observations'))
disp(handles.m_dap.datasets.trainingSample)
disp(sprintf('Training Observations Per Group'))
handles.m_dap.datasets.dataPerGroups = dataPerGroups;
displayList(handles.m_dap.datasets.dataPerGroups, 'Observations in Group')
disp(sprintf('Classify Observations'))
if handles.m_dap.constants.classifySampleSize == 0
disp(sprintf('\n\b(no Classify Sample)'))
else
handles.m_dap.datasets.classifySample = datasets{2};
disp(handles.m_dap.datasets.classifySample)
end
plotNormalProbability(handles.m_dap)
guidata(hObject,handles)
set(handles.estimators_pushbutton,'Enable','on')
set(handles.modelfit_pushbutton,'Enable','off')
set(handles.classify_pushbutton,'Enable','off')
% --- Executes on button press in estimators_pushbutton.
function estimators_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to estimators_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
switch handles.m_method
case 'S-estimator' % User selects S-estimator.
for i=1:handles.m_dap.constants.numGrp
S = SEst(handles.m_dap.datasets.dataPerGroups{i}, handles.m_dap.constants.maxrep, handles.m_dap.constants.breakdwnp);
handles.m_dap.covList{i} = S.covariance;
handles.m_dap.invCovList{i} = inv(S.covariance);
handles.m_dap.meanList{i} = S.mean;
end
case 'Donoho-Stahel' % User selects Donoho-Stahel estimation.
for i=1:handles.m_dap.constants.numGrp
[scatterMat, locationVec] = DSEst(handles.m_dap.datasets.dataPerGroups{i}, handles.m_dap.constants.maxrep, handles.m_dap.DSdim);
handles.m_dap.covList{i} = scatterMat;
handles.m_dap.invCovList{i} = inv(scatterMat);
handles.m_dap.meanList{i} = locationVec;
if handles.m_dap.constants.numVar > handles.m_dap.constants.maxDSdim
messagStr=['Number of variables (', num2str(handles.m_dap.constants.numVar), ') is bigger than optimal dimension for the Donoho-Stahel method (', ...
num2str(handles.m_dap.constants.maxDSdim), ').'];
warndlg(messagStr,'ATTENTION: Use of S-estimator method advised!')
end
end
case 'Classical' % User selects Classical estimation.
for i=1:handles.m_dap.constants.numGrp
handles.m_dap.covList{i} = cov(handles.m_dap.datasets.dataPerGroups{i});
handles.m_dap.invCovList{i} = inv(handles.m_dap.covList{i});
handles.m_dap.meanList{i} = mean(handles.m_dap.datasets.dataPerGroups{i});
end
end
messageStr = ['Scatter/Location estimation method: ', handles.m_method];
if (~strcmp(handles.m_method, 'Classical'))
messageStr = ['\nRobust ', messageStr];
elseif strcmp(handles.m_method, 'S-estimator')
messageStr = [messageStr, ' with breakdown point ', num2str(handles.m_dap.constants.breakdwnp)];
end
disp(sprintf(messageStr))
disp(sprintf('\nScatter Matrices'))
displayList(handles.m_dap.covList, 'Scatter Matrix of Group')
disp(sprintf('Location Vectors'))
displayList(handles.m_dap.meanList, 'Location Vector of Group')
set(handles.modelfit_pushbutton,'Enable','on')
set(handles.classify_pushbutton,'Enable','off')
%Save the handles structure.
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function model_popupmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to model_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- Executes on selection change in model_popupmenu.
function model_popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to model_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns model_popupmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from model_popupmenu
% Determine the selected data set.
str = get(hObject, 'String');
val = get(hObject, 'Value');
% Set current estimator to the selected estimator.
switch str{val}
case 'Common Principal Compon.' % User selects Common Principal Components covariance model.
handles.m_model = 'cpc';
case 'Proportional' % User selects Proportional covariance model.
handles.m_model = 'proportional';
case 'Usual Quadratic' % User selects Usual Quadratic covariance model.
handles.m_model = 'quadratic';
case 'Linear' % User selects Linear covariance model.
handles.m_model = 'linear';
end
set(handles.classify_pushbutton,'Enable','off')
%Save the handles structure.
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function covariance_popupmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to covariance_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- Executes on selection change in covariance_popupmenu.
function covariance_popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to covariance_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns covariance_popupmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from covariance_popupmenu
str = get(hObject,'String');
val = get(hObject,'Value');
% Set current robust covariance method to the selected robust covariance method.
switch str{val}
case 'S-estimator' % User selects S-estimator method.
handles.m_method = 'S-estimator';
case 'Donoho-Stahel' % User selects Donoho-Stahel estimation method.
handles.m_method = 'Donoho-Stahel';
case 'Classical' % User selects Classical estimation method.
handles.m_method = 'Classical';
end
%Save the handles structure.
set(handles.modelfit_pushbutton,'Enable','off')
set(handles.classify_pushbutton,'Enable','off')
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function datasets_popupmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to datasets_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- Executes on selection change in datasets_popupmenu.
function datasets_popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to datasets_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns datasets_popupmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from datasets_popupmenu
str = get(hObject, 'String');
val = get(hObject,'Value');
% Set current sample dataset(s) to the selected one(s).
switch str{val}
case 'Training Sample only' % User selects training sample dataset.
handles.m_samples = {'Training Sample', ''};
case 'Train. Samp. + Classify Sample' % User selects training sample and classify sample datasets.
handles.m_samples = {'Training Sample', 'Classify Sample'};
end
%Save the handles structure.
guidata(hObject,handles)
% --- Executes on button press in modelfit_pushbutton.
function modelfit_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to modelfit_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
messageStr = ['Covariance Structure Model: '];
if (strcmp(handles.m_method, 'Classical'))
messageStr = ['Classical ', messageStr];
else
messageStr = ['Robust ', messageStr];
end
switch handles.m_model
case 'cpc' % User selected Common Principal Components Covariance Model.
messageStr = [messageStr, 'Common Principal Components'];
disp(messageStr)
[cpcCovList, LList, B, FList] = computeCPCModel(handles.m_dap.covList, handles.m_dap.constants);
displayCPCModel(cpcCovList, LList, B, FList, handles.m_dap.constants.numGrp);
handles.m_dap.modeledCovList.cpc = cpcCovList;
handles.m_dap.modeledInvCovList.cpc = computeInvCovList(cpcCovList, handles.m_dap.constants.numGrp);
[pValue, testStatistic, df] = computeLikelihoodRatioTest(handles.m_dap.modeledCovList.cpc, handles.m_dap.covList, handles.m_dap.constants, handles.m_model);
displayLikelihoodRatioTest(pValue, testStatistic, df);
case 'proportional' % User selected Proportional Covariance Model.
messageStr = [messageStr, 'Proportional'];
disp(messageStr)
[proportionalCovList, LList, B, rhoVec] = computeProportionalModel(handles.m_dap.covList, handles.m_dap.constants);
displayProportionalModel(proportionalCovList, LList, B, rhoVec);
handles.m_dap.modeledCovList.proportional = proportionalCovList;
handles.m_dap.modeledInvCovList.proportional = computeInvCovList(proportionalCovList, handles.m_dap.constants.numGrp);
[pValue, testStatistic, df] = computeLikelihoodRatioTest(handles.m_dap.modeledCovList.proportional, handles.m_dap.covList, handles.m_dap.constants, handles.m_model);
displayLikelihoodRatioTest(pValue, testStatistic, df);
case 'quadratic'
messageStr = [messageStr, 'Usual Quadratic'];
disp(messageStr)
displayQuadraticModel(handles.m_dap.covList, handles.m_dap.constants.numGrp);
case 'linear' % User selected Linear Covariance Model.
messageStr = [messageStr, 'Linear'];
disp(messageStr)
pooledCov = computeLinearModel(handles.m_dap.covList, handles.m_dap.constants);
displayLinearModel(pooledCov, handles.m_dap.constants.numGrp);
handles.m_dap.modeledCovList.linear = {pooledCov};
handles.m_dap.modeledInvCovList.linear = {inv(pooledCov)};
[pValue, testStatistic, df] = computeLikelihoodRatioTest(handles.m_dap.modeledCovList.linear, handles.m_dap.covList, handles.m_dap.constants, handles.m_model);
displayLikelihoodRatioTest(pValue, testStatistic, df);
end
set(handles.classify_pushbutton,'Enable','on')
%Save the handles structure.
guidata(hObject,handles)
% --- Executes on button press in classify_pushbutton.
function classify_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to classify_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
dummyData = handles.m_dap.datasets.dataPerGroups{1}(1,:); % Dummy data needed as input for rule coefficients computation
classifyRuleCoefList = classifyRuleCoef(handles.m_model, handles.m_dap, dummyData);
[discrimRuleCoefList, groupToGroupDistances, numGroupToGroupDistances] = discrimRuleCoef(handles.m_model, handles.m_dap, classifyRuleCoefList);
outpuTitle = sprintf('Discrimination Rule Coefficients (Discriminant Regions)\n');
displayCoefCellList(handles.m_model, discrimRuleCoefList, numGroupToGroupDistances, groupToGroupDistances, outpuTitle);
outpuTitle = sprintf('Classification Rule Coefficients\n');
displayCoefCellList(handles.m_model, classifyRuleCoefList, handles.m_dap.constants.numGrp, [], outpuTitle);
malclassificationList = crossValidation(handles.m_model, handles.m_dap);
expectedAparentTotalErrorRate = 0;
for i = 1:handles.m_dap.constants.numGrp
aparentErrorRatesList{i} = length(malclassificationList{i}) / handles.m_dap.constants.numElemPerGrp(i);
expectedAparentTotalErrorRate = expectedAparentTotalErrorRate + length(malclassificationList{i});
end
expectedAparentTotalErrorRate = expectedAparentTotalErrorRate / sum(handles.m_dap.constants.numElemPerGrp);
disp(sprintf('\nGroup Aparent Error Rates from Training Sample'))
displayList(aparentErrorRatesList, 'Aparent Error Rate of Group')
disp('Expected Aparent Total Error Rate')
disp(expectedAparentTotalErrorRate)
disp(sprintf('Data Classification of Training Sample given by Input Order Index(es)'))
displayList(malclassificationList, 'Malclassified data of Group');
if handles.m_dap.constants.numVar == 2
plotMalclassification(handles.m_model, malclassificationList, discrimRuleCoefList, numGroupToGroupDistances, handles.m_dap, handles.m_method);
end
if handles.m_dap.constants.classifySampleSize == 0
disp(sprintf('\n\b(no Classify Sample)'))
else
if strcmp(handles.m_model, 'linear')
classificationList = linearSampleClassification(classifyRuleCoefList, handles.m_dap);
else
classificationList = quadraticSampleClassification(classifyRuleCoefList, handles.m_dap);
end
disp(sprintf('\nData Classification of Classify Sample given by Input Order Index(es)'))
displayList(classificationList, 'Data classified as of Group')
if handles.m_dap.constants.numVar == 2
plotClassification(handles.m_model, classificationList, classifyRuleCoefList, handles.m_dap, handles.m_method);
end
end
% --- Executes when user attempts to close figure1.
function DAP_figure_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
EndSession(hObject)