﻿// P82cMixLRDisc.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                 
rand('seed',0) 

nL=1000;                      // number of data for learning
nT=150;                       // number of data for testing

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

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

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

// 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)
