﻿// P82aMixLRNorm.sce
// Logistic regression based on mixtures
// -------------------------------------
[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=1000;                      // number of data for learning
nT=150;                       // number of data for testing

// Simulation --------------------------
nd=nT+nL;
x=rand(2,nd,'n');
x1=[x; ones(1,nd)];
ys=[1,1,2]*x1+.001*rand(1,nd,'n');
y=zeros(1,nd);
s1=find(ys<0);                y(s1)=1;
s2=find((ys>=0) & (ys<3));    y(s2)=2;
s3=find(ys>=3);               y(s3)=3;

// POZOR - xL musí mít veličiny v řádcích
yL=y(1,1:nL); xL=x(:,1:nL); 
// Learning ----------------------------
[Est,al]=lrLearn(yL,xL);           

tT=(1:nT)+nL;
// POZOR - xT musí mít veličiny v řádcích
xT=x(:,tT);
// Testing -----------------------------
yT=lrTest(xT,Est,al);            

// Results -----------------------------
yR=y(tT)';
s=1:nT;
plot(s,yR,'bo',s,yT,'rx','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'];
//for i=1:3
//  s=find(y==i);
//  plot(x(s,1),x(s,2),'.'+tx(i))
//end

