function discrnew(X1,X2,x0)
%function discrnew(X1,X2,x0)
% Allocazione
%
% X1  n1 x p
% X2  n2 x p
% x0  vettore colonna con osservazione da allocare
%

[n1,p]=size(X1);
[n2,p]=size(X2);
S1=cov(X1); S2=cov(X2);
m1=mean(X1); m2=mean(X2);
S= 1/(n1+n2-2)*((n1-1)*S1+(n2-1)*S2)

a= (m1-m2)/S

coef=.5*(m1+m2)*a';

[a*x0,coef]
if (a*x0>coef), 
fprintf('Allocate pop.1\n');
else
fprintf('Allocate pop.2\n');
end

% Plot only if p=2
if (p==2)
  xx1=min([X1(:,1);X2(:,1)]); xx2=max([X1(:,1);X2(:,1)]);
  yy1=(coef -a(1)*xx1)/a(2); yy2=(coef -a(1)*xx2)/a(2);

  plot(X1(:,1),X1(:,2),'r+');hold on
  plot(X2(:,1),X2(:,2),'bo')
  plot(x0(1),x0(2),'k*')
  plot([xx1;xx2],[yy1;yy2],'k')
  xlabel('variabile x_1')
  ylabel('variabile x_2')
  hold off
end

% APER
% misallocated X1 and X2
mis1=sum( X1*a'<coef);
mis2=sum( X2*a'>coef);
APER = (mis1+mis2)/(n1+n2)
