% DecBound   decision boundaries
%
% how to find decision boundary for data

clear all

%%%
% load trset(1:2).X and testset
%%%
load data
load clasidx

%%%
% statistical evaluation
%%%
for i=1:2,
  data(i).C	   = cov(trset(i).X);
  data(i).mean = mean(trset(i).X);
  data(i).P	   = size(trset(i).X,1)/( size(trset(1).X,1)+size(trset(2).X,1));
end

%%%
% plot of training set
%%%
data(1).color	 = 'b';
data(2).color	 = 'r';
figure(1)
clf
hold on
for i = 1:2,
  plot(trset(i).X(:,1),trset(i).X(:,2),sprintf('%s+',data(i).color),'MarkerSize',4);
end

%%%
% Plot the decision boundary
%%%
figure(1)
count = 0;
xaxis = 0:0.1:8;
for i = xaxis,
 count = count+1; 
 y1(count) = db1(i,data(1).P,data(2).P,data(1).C,data(2).C,data(1).mean,data(2).mean);
 % y2(count) = db2(i,data(1).P,data(2).P,data(1).C,data(2).C,data(1).mean,data(2).mean);
end
plot(xaxis,y1,'-g')
% plot(xaxis,y2,'-g')


%%%
% Classification
decs= []; % sign of the class decs > 0 belongs to the 2nd class (red)
for i = 1:size(testset,1),
  decs = [decs,db(testset(i,:),data(1).P,data(2).P,data(1).C,data(2).C,data(1).mean,data(2).mean)];
end

%%%
% find good and bad classified points from data set
%%%
idx     = find(clasidx==1);
idxgood = find(decs(idx)<0);
idxbad	= find(decs(idx)>0);
clas(1).idxXgood = idx(idxgood);
clas(1).idxXbad	 = idx(idxbad);

idx     = find(clasidx==2);
idxgood = find(decs(idx)>0);
idxbad	= find(decs(idx)<0);
clas(2).idxXgood = idx(idxgood);
clas(2).idxXbad  = idx(idxbad);

figure(1)
for i=1:2,
  plot(testset(clas(i).idxXbad,1),testset(clas(i).idxXbad,2),sprintf('%s*',data(i).color),'MarkerSize',10,'LineWidth',2);
  plot(testset(clas(i).idxXgood,1),testset(clas(i).idxXgood,2),sprintf('%so',data(i).color),'MarkerSize',4);
end

tosave = clas(1).idxXbad
save clas1bad.asc tosave -ascii
tosave = clas(2).idxXbad
save clas2bad.asc tosave -ascii



