// P82bMicLRUnif.sce
// Logistic regression based on mixtures (uniform distribution)
// --------------------------------------------------------------
[u,t,n]=file();                         // find working directory
chdir(dirname(n(2)));                   // set working directory
clear("u","t","n")                      // clear auxiliary data
exec("ScIntro.sce",-1),mode(0)          // intro to sesion                 

nL=10000;
nT=30;
nd=nT+nL;
x=rand(nd,2,'n');
x1=[x ones(nd,1)];
ys=x1*[.1,-3,2]'+.001*rand(nd,1,'n');
mi=min(ys);
ma=max(ys);
mm=ma-mi;
m1=mi+9*mm/20;
m2=mi+11*mm/20;
y=zeros(nd,1);
s1=find(ys<m1);                y(s1)=1;  x(s1,:)=x(s1,:)+2; X1=x(s1,:);
s2=find((ys>=m1) & (ys<m2));   y(s2)=2;                     X2=x(s2,:);
s3=find(ys>=m2);               y(s3)=3;  x(s3,:)=x(s3,:)-2; X3=x(s3,:);

yL=y(1:nL); xL=x(1:nL,:);
[Est,al]=lrLearnU(yL,xL);

tT=(1:nT)+nL;
xT=x(tT,:);
[yT,wt]=lrTestU(xT,Est,al);

yR=y(tT)';
yP=[1 2 3]*wt;
s=1:nT;
plot(s,yP,'o',s,yT,'x','markersize',8)
set(gcf(),'position',[700 100 600 400])

printf('Wrong %d from %d\n',sum(yR~=yT),nT)

tx=['r';'b';'g';'m';'c';'y'];
set(figure(2),'position',[50 150 600 400])
set(gcf(),'background',8)
for i=1:3
  s=find(y==i);
  plot(x(s,1),x(s,2),'.'+tx(i))
end

