% fitcdiscr_demo.m
% 
% Demo of the built-in function for discriminant analysis classifier

% Dr. David Pan, UAH

N = 1000;

% Class 1
m1 = [3, 3]';   % Mean vector
cov1 = [2 1; 1 2]; % Covariance matrix
%rng default  % For reproducibility
r1 = mvnrnd(m1,cov1,N);

data_C1 = zeros(N, 2);
data_C1 = r1;
label_C1 = ones(N, 1);

% Generate data entries for Class 2
m2 = [9, 9]';
cov2 = [2,1; 1,2];
%rng default  % For reproducibility
r2 = mvnrnd(m2,cov2,N);

data_C2 = zeros(N, 2);
data_C2 = r2;
label_C2 = 2*ones(N, 1);

% Combine data of two classes
data = vertcat (data_C1, data_C2);
label = vertcat (label_C1, label_C2);

% Plot the data samples of the two classes
gscatter(data(:,1),data(:,2),label,'rb','ox')
grid

% Export the dataset to a text file (to be imported by sklearn)
data_label = horzcat(data, label);
writematrix(data_label, 'lda_data.csv');

Model = fitcdiscr(data, label);
Model.DiscrimType


% The case with varying covariances
N = 1000;

% Class 1
m1 = [3, 3]';   % Mean vector
cov1 = [2 1; 1 2]; % Covariance matrix
%rng default  % For reproducibility
r1 = mvnrnd(m1,cov1,N);

data_C1 = zeros(N, 2);
data_C1 = r1;
label_C1 = ones(N, 1);

% Generate data entries for Class 2
m2 = [9, 9]';
cov2 = [5,3; 3,5];
%rng default  % For reproducibility
r2 = mvnrnd(m2,cov2,N);

data_C2 = zeros(N, 2);
data_C2 = r2;
label_C2 = 2*ones(N, 1);

% Combine data of two classes
data = vertcat (data_C1, data_C2);
label = vertcat (label_C1, label_C2);

