
%Three groups


load Fl15

cxyz=[];


nrep=28;

for i=1:nrep
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'];
    
end

%gru=[1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3];

gruY=zeros(28,3);
gruY(1:12,1)=1;
gruY(13:18,2)=1;
gruY(19:28,3)=1;




%%leave-one-out






% set parameters
lambda = 1e-6; % l2-norm, default
maxiter = 50; % max iter in SDCA alg., default:25

Yclass=[repmat(1,1,12) repmat(2,1,6) repmat(3,1,10)];


gruY=zeros(28,3);
gruY(1:12,1)=1;
gruY(13:18,2)=1;
gruY(19:28,3)=1;

ee=[];
for stop=-24:-1
e=0;

rand('state',1)

n=28;

for i=1:28
    
   indi=ones(28,1);
 indi(i)=0;
 indi=logical(indi);
 
 % normalize data
[Xtr,mx,vx] = normalize(cxyz(indi,:));
In = find(vx~=0);
Xtr = Xtr(:,In);
X = (cxyz(:,In)-ones(n,1)*mx(In))./sqrt(ones(n,1)*vx(In));

% perform SDA
[sl theta] = slda(Xtr, gruY(indi,:), lambda, stop, 2,maxiter, 1e-6,0);
% Project data onto the sparse directions (dim=2)
DC = X*sl;

[class,tsterr] = classify(DC(i,:),DC(indi,:),Yclass(indi),'linear','empirical');

e=e + double(class~=Yclass(i));


if(class~=Yclass(i))

[i,class]

end

 
end

ee=[ee e];
end


%van del 24 hacia abajo!!! from 24 to 1!!!!

ee =

     4     4     3     4     4     3     3     3     3     3     3     4     4     5     5     5     5     6     7     8     9    10    12    13


     stop=-13:
     ans =

    13     3


ans =

    14     1


ans =

    15     3


ans =

    18     1
     
   %selccion del número de componentes. No. features: double leave-one-out
   
   erl=zeros(28,24); %matriz de errores global

   lambda=1e-6;
   
   ee=[];
for stop=-24:-1
e=0;
stop
rand('state',1)

n=28;

for i=1:28
    
   indi=ones(28,1);
 indi(i)=0;
 indi=logical(indi);
 
 
 % normalize data
[Xtr,mx,vx] = normalize(cxyz(indi,:));
In = find(vx~=0);
Xtr = Xtr(:,In);
X = (cxyz(:,In)-ones(n,1)*mx(In))./sqrt(ones(n,1)*vx(In));

% perform SDA
[sl theta] = slda(Xtr, gruY(indi,:), lambda, stop, 2,maxiter, 1e-6,0);
% Project data onto the sparse directions (dim=2)
DC = X*sl;

 s=setdiff(1:28,i);
errores=0;
for j=s
    
    indi2=ones(28,1);
    indi2(i)=0;
    indi2(j)=0;
   indi2=logical(indi2);
   
   
[class,tsterr] = classify(DC(j,:),DC(indi2,:),Yclass(indi2),'linear','empirical');

errores=errores + double(class~=Yclass(j));



end

erl(i,abs(stop))=errores;

end

 
end


figure
plot(1:24,sum(erl,1)) % mínimo en 13 (4 errores), most parsimonious

sum(erl,1)

 
   
       
      149    94    65    37    31    15    11     3     2     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0

    %%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%CN-MCI
    
    
    %leave-one-out

     
     load Fl15

cxyz=[];

nrep=18;

for i=1:nrep
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'];
    
end

% set parameters
lambda = 1e-6; % l2-norm, default
maxiter = 50; % max iter in SDCA alg.
Yclass=[repmat(1,1,12) repmat(2,1,6)];


gruY=zeros(18,2);
gruY(1:12,1)=1;
gruY(13:18,2)=1;


ee=[];
for stop=-14:-1
e=0;

rand('state',1)

n=18;

for i=1:18
    
   indi=ones(18,1);
 indi(i)=0;
 indi=logical(indi);
 
 % normalize data
[Xtr,mx,vx] = normalize(cxyz(indi,:));
In = find(vx~=0);
Xtr = Xtr(:,In);
X = (cxyz(:,In)-ones(n,1)*mx(In))./sqrt(ones(n,1)*vx(In));

% perform SDA
[sl theta] = slda(Xtr, gruY(indi,:), lambda, stop, 1,maxiter, 1e-6,0);
% Project data onto the sparse directions (dim=2)
DC = X*sl;

[class,tsterr] = classify(DC(i,:),DC(indi,:),Yclass(indi),'linear','empirical');

e=e + double(class~=Yclass(i));


if(class~=Yclass(i))

[i,class]

end

 
end

ee=[ee e];
end

%va de 14 a 1. From 14 to 1

ee =

     4     4     4     5     5     5     5     5     5     5     5     5     5     5
    
    
     
     
   %  para stop=-11, errors:
     ans =

     6     2


ans =

    14     1


ans =

    15     1


ans =

    16     1


ans =

    18     1
     
     
       %selccion del número de componentes. No. features: double leave-one-out
   
   erl=zeros(18,14); %matriz de errores global

   lambda=1e-6;
   
   ee=[];
for stop=-14:-1
e=0;
stop
rand('state',1)

n=18;

for i=1:18
    
   indi=ones(18,1);
 indi(i)=0;
 indi=logical(indi);
 
 
 % normalize data
[Xtr,mx,vx] = normalize(cxyz(indi,:));
In = find(vx~=0);
Xtr = Xtr(:,In);
X = (cxyz(:,In)-ones(n,1)*mx(In))./sqrt(ones(n,1)*vx(In));

% perform SDA
[sl theta] = slda(Xtr, gruY(indi,:), lambda, stop, 2,maxiter, 1e-6,0);
% Project data onto the sparse directions (dim=2)
DC = X*sl;

 s=setdiff(1:18,i);
errores=0;
for j=s
    
    indi2=ones(18,1);
    indi2(i)=0;
    indi2(j)=0;
   indi2=logical(indi2);
   
   
[class,tsterr] = classify(DC(j,:),DC(indi2,:),Yclass(indi2),'linear','empirical');

errores=errores + double(class~=Yclass(j));



end

erl(i,abs(stop))=errores;

end

 
end


figure
plot(1:14,sum(erl,1)) % mínimo en 11 (5 errores) 

sum(erl,1)

46    41    33    23    14     6     2     2     2     1     0     0     0     0
  

     %%%%%%%%%%%%%%%%
     %%% CN-AD
     
     
     g4=[repmat(1,1,12) repmat(3,1,10)];
 
 cxyz=[];
 
 do=[1:12 19:28];
 
for i=do
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'];
    
end

% set parameters
lambda = 1e-6; % l2-norm, default parameter
maxiter = 50; % max iter in SDCA alg.

Yclass=[repmat(1,1,12) repmat(3,1,10)];


gruY=zeros(22,2);
gruY(1:12,1)=1;
gruY(13:22,2)=1;


ee=[];
for stop=-18:-1
e=0;

rand('state',1)

n=22;

for i=1:22
    
   indi=ones(22,1);
 indi(i)=0;
 indi=logical(indi);
 
 % normalize data
[Xtr,mx,vx] = normalize(cxyz(indi,:));
In = find(vx~=0);
Xtr = Xtr(:,In);
X = (cxyz(:,In)-ones(n,1)*mx(In))./sqrt(ones(n,1)*vx(In));

% perform SDA
[sl theta] = slda(Xtr, gruY(indi,:), lambda, stop, 1,maxiter, 1e-6,0);
% Project data onto the sparse directions (dim=2)
DC = X*sl;

[class,tsterr] = classify(DC(i,:),DC(indi,:),Yclass(indi),'linear','empirical');

e=e + double(class~=Yclass(i));

if(class~=Yclass(i))

[i,class]

end

 
end

ee=[ee e];
end


 ee =

     1     1     1     1     1     1     1     1     1     2     2     2     2     2     2     2     2     2

    
  %   stop=-7 errors:
     
     ans =

     8     3


ans =

    17     1-> correponde al 23
     
        %selccion del número de componentes. No. fearures: double leave-one-out
   
   erl=zeros(22,18); %matriz de errores global

   lambda=1e-6;
   
   ee=[];
for stop=-18:-1
e=0;
stop
rand('state',1)

n=22;

for i=1:22
    
   indi=ones(22,1);
 indi(i)=0;
 indi=logical(indi);
 
 
 % normalize data
[Xtr,mx,vx] = normalize(cxyz(indi,:));
In = find(vx~=0);
Xtr = Xtr(:,In);
X = (cxyz(:,In)-ones(n,1)*mx(In))./sqrt(ones(n,1)*vx(In));

% perform SDA
[sl theta] = slda(Xtr, gruY(indi,:), lambda, stop, 1,maxiter, 1e-6,0);
% Project data onto the sparse directions (dim=2)
DC = X*sl;

 s=setdiff(1:22,i);
errores=0;
for j=s
    
    indi2=ones(22,1);
    indi2(i)=0;
    indi2(j)=0;
   indi2=logical(indi2);
   
   
[class,tsterr] = classify(DC(j,:),DC(indi2,:),Yclass(indi2),'linear','empirical');

errores=errores + double(class~=Yclass(j));



end

erl(i,abs(stop))=errores;

end

 
end


figure
plot(1:18,sum(erl,1)) % mínimo en 7 (2 errores)

sum(erl,1)


20    18    18    12     8     4     0     0     0     0     0     0     0     0     0     0     0     0

 

%%%%%%%%%%%%%%%%%
%%%% MCI-AD

g4=[repmat(2,1,6) repmat(3,1,10)];
 
 cxyz=[];
 
 do=[13:28];
 
for i=do
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'];
    
end

% set parameters
lambda = 1e-6; % l2-norm, default
maxiter = 50; % max iter in SDCA alg.

Yclass=[repmat(2,1,6) repmat(3,1,10)];


gruY=zeros(16,2);
gruY(1:6,1)=1;
gruY(7:16,2)=1;


ee=[];
for stop=-12:-1
e=0;

rand('state',1)

n=16;

for i=1:16
    
   indi=ones(16,1);
 indi(i)=0;
 indi=logical(indi);
 
 % normalize data
[Xtr,mx,vx] = normalize(cxyz(indi,:));
In = find(vx~=0);
Xtr = Xtr(:,In);
X = (cxyz(:,In)-ones(n,1)*mx(In))./sqrt(ones(n,1)*vx(In));

% perform SDA
[sl theta] = slda(Xtr, gruY(indi,:), lambda, stop, 1,maxiter, 1e-6,0);
% Project data onto the sparse directions (dim=2)
DC = X*sl;

[class,tsterr] = classify(DC(i,:),DC(indi,:),Yclass(indi),'linear','empirical');

e=e + double(class~=Yclass(i));


if(class~=Yclass(i))

[i+12,class]

end

 
end

ee=[ee e];
end


%from 12 to 1

ee =

     6     7     6     6     6     6     6     6     6     6     8     7

     
   %errors for  stop=-6
     
     ans =

    13     3


ans =

    15     3


ans =

    17     3


ans =

    18     3


ans =

    20     2


ans =

    22     2
     
        %seleccion del número de componentes. No. features: double leave-one-out
   
   erl=zeros(16,12); %matriz de errores global

   lambda=1e-6;
   
   ee=[];
for stop=-12:-1
e=0;
stop
rand('state',1)

n=16;

for i=1:16
    
   indi=ones(16,1);
 indi(i)=0;
 indi=logical(indi);
 
 
 % normalize data
[Xtr,mx,vx] = normalize(cxyz(indi,:));
In = find(vx~=0);
Xtr = Xtr(:,In);
X = (cxyz(:,In)-ones(n,1)*mx(In))./sqrt(ones(n,1)*vx(In));

% perform SDA
[sl theta] = slda(Xtr, gruY(indi,:), lambda, stop, 1,maxiter, 1e-6,0);
% Project data onto the sparse directions (dim=2)
DC = X*sl;

 s=setdiff(1:16,i);
errores=0;
for j=s
    
    indi2=ones(16,1);
    indi2(i)=0;
    indi2(j)=0;
   indi2=logical(indi2);
   
   
[class,tsterr] = classify(DC(j,:),DC(indi2,:),Yclass(indi2),'linear','empirical');

errores=errores + double(class~=Yclass(j));



end

erl(i,abs(stop))=errores;

end

 
end


figure
plot(1:12,sum(erl,1)) % mínimo en 6  (6  errores), most parsimonious

sum(erl,1)

42    18     9     7     1     0     0     0     0     0     0     0
     
 
     
