
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%  Title: StatisCLAS: A Package of Classification Methods   %%%%
%%%%  Author: David Casado de Lucas                            %%%%
%%%%  Version: 29th May 2013                                   %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% PURPOSE: This function plots the discriminant functions.

% INPUT VARIABLES:
%    groupY1: Matrix with the values of the discriminat variables for the
%       training elements of sample 1.
%    groupY2: Matrix with the values of the discriminat variables for the
%       training elements of sample 2.
%    centroids1: Proyections of the mean of the training sample 1.
%    centroids2: Proyections of the mean of the training sample 2.
%    figureTitle: Optional variable with the title of the figure.
%    percentMargin: Optional variable to control the distance from the more
%                   external data to the margin of the axes.
%    letter: Optional letter to label the discriminant variables.

% COMMENTS:
%   (1) This function uses training and testing information. If desired, the user can
%       call it with the same values for both of them.

function plotDiscrimFunctions(groupY1, groupY2, centroids1, centroids2, figureTitle, percentMargin, letter)

if ~exist('figureTitle','var')
  figureTitle = 'Discriminant Function';
end
if ~exist('percentMargin','var')
  percentMargin = 5;
end
if ~exist('letter','var')
  letter = '';
end

m1 = size(groupY1,1);
m2 = size(groupY2,1);

if ~exist('letter','var')
  letter = '';
end

xMin = 1;
xMax = max([m1 m2]);
xRange = xMax - xMin;
if isequal(xRange,0)
  xRange = 0.01;
end
yMin = min([min(groupY1), min(groupY2), centroids1, centroids2]);
yMax = max([max(groupY1), max(groupY2), centroids1, centroids2]);
yRange = yMax - yMin;
if isequal(yRange,0)
  yRange = 0.01;
end
figure
subplot(2,1,1)
  plot(groupY1,'bo','MarkerSize',3,'LineWidth',1)  % Elements of the testing sample of group 1
  hold on
  plot(groupY2,'rs','MarkerSize',3,'LineWidth',1)  % Elements of the testing sample of group 2
  line([1 xMax],[mean(groupY1) mean(groupY1)],'Color','b','LineStyle',':','LineWidth',1)  % Centroid of the testing sample of group 1
  line([1 xMax],[mean(groupY2) mean(groupY2)],'Color','r','LineStyle',':','LineWidth',1)  % Centroid of the testing sample of group 2
  line([1 xMax],[centroids1 centroids1],'Color','b','LineWidth',1)  % Centroid of the training sample of group 1
  line([1 xMax],[centroids2 centroids2],'Color','r','LineWidth',1)  % Centroid of the training sample of group 2
  hold off
  xlabel('Testing elements')
  ylabel(['Function Y1' letter])
  title(figureTitle)
  set(gca, 'XLim', [xMin-xRange*percentMargin/100 xMax+xRange*percentMargin/100])
  set(gca, 'YLim', [yMin-yRange*percentMargin/100 yMax+yRange*percentMargin/100])
subplot(2,1,2)  % Histogram
  hist([groupY1; groupY2])
  xlabel('Histogram');

end
