% NOTE: If you run this script multiple times, you will can
%   visualize the effect that sampling has on the decision boundary.
%   You will see *different* decision boundaries generated for different runs,
%   even though the underlying p(x | w_i ) (posterior) distributions do not change.

% A simple example of a linear discriminant for two classes
% with equal covariance matrices and prior probabilities.

N = 100;
m1 = [0 0];
m2 = [5 0];
cv = [ 1 0 ; 0 1 ];

% Randomly sample N each from two Gaussians with means m1 and m2,
% and covariance matrix cv (identical covariance),  using the PRTools 
% gendatgauss function (see 'help gendatgauss')
%
% NOTE: cat(3, .... ) adds a new dimension so that each covariance
%   matrix may be accessed by an 'outer' list (3rd dimension at the top level).
D = gendatgauss([ N N ], [ m1 ; m2 ], cat(3, cv, cv ));

% Randomly split the data into training and testing, using 1/2 the data for each.
% This uses the PRTools 'gendat' function.
[ Train, Test ] = gendat(D, [ N/2 N/2 ])

% Generate the linear discriminant using the training data, report the
% error rate (using PRTools built-in function 'testc')
C = ldc(Train);

% In PRTools, use the syntax [dataset] * [classifier/mapping] to produce
% the classification results ('maps' input (features) to output (classes))

Classification = Test * C;

% disp is 'print' in MATLAB. Note that the error shown is the error rate
% weighted by (averaged by) the class priors (see 'help testc')
disp(' ')
disp('Classification result for Linear Discriminant:')
testc(Classification)


% Plot the result.
figure;
scatterd(D);
plotc(C);
hold off;

