clear all
close all
clc
%%%% NN-tradicional
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% data generation %%%% 
N1=500;
mu = [1 -1];
Sigma = [.9 .4; .4 .3];
c1 = mvnrnd(mu, Sigma, N1);
N1test=100;
c1test = mvnrnd(mu, Sigma, N1test);
 plot(c1(:,1),c1(:,2),'ro');
 hold on
%%%%%%%%%%% 
 N2=500;
%mu = [-4 3];
%mu = [-1 1.5];
%mu = [0 1.5];
mu = [0 0];
Sigma = [1 0; 0 2];
c2 = mvnrnd(mu, Sigma, N2);
N2test=100;
c2test = mvnrnd(mu, Sigma, N2test);
 plot(c2(:,1),c2(:,2),'bo');
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 dist=@(z,x) sum(abs(x-z).^2);
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axis([-7 7 -3 8])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% calculaErrores
Ctot_test=[c1test; c2test];
Labels_Ctot=[ones(1,N1test) zeros(1,N2test)];

%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
Kvect=1:10:800;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for jj=1:length(Kvect)
     if mod(jj,10)==0
        jj
     end
    %%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%
      Kvecinos=Kvect(jj);
    %%%%%%%%%%%%%%%%%%%%%%
        Cont_Empate(jj)=0;
        Cont_Correcto1(jj)=0;
        Cont_Error1(jj)=0;
        Cont_Correcto2(jj)=0;
        Cont_Error2(jj)=0;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    
   for k=1:N1test+N2test
% %      if mod(k,10)==0
% %         k
% %      end
         
          xtest=Ctot_test(k,:);
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
            for i=1:N1
              d1(i)=dist(xtest,c1(i,:));
            end
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
            for i=1:N2
              d2(i)=dist(xtest,c2(i,:));
            end
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
           dtot=[d1 d2];
           labels=[ones(1,N1) zeros(1,N2)];
           dsort=sort(dtot);
         %%%%%%%%%%%%%%%%%  
           contneg=0;
           contpos=0;
         %%%%%%%%%%%%%%%%%
           for s=1:Kvecinos
               pos(s)=find(dsort(s)==dtot);
               Lab(s)=labels(pos(s));
               if Lab(s)==1
                   contpos=contpos+1;
               else
                   contneg=contneg+1;
               end
           end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
          if  contpos>contneg
              dec=1;
              if Labels_Ctot(k)==1
                 Cont_Correcto1(jj)=Cont_Correcto1(jj)+1;
              else
                  Cont_Error1(jj)=Cont_Error1(jj)+1;
           %       plot(xtest(1,1),xtest(1,2),'gx','LineWidth',2,'MarkerSize',10)
              end
          %%%%%%%%%%%%%%%%%%%%%%%%%%% 
          elseif contpos<contneg
          %%%%%%%%%%%%%%%%%%%%%%%%%%%    
              dec=0;
                if Labels_Ctot(k)==0
                 Cont_Correcto2(jj)=Cont_Correcto2(jj)+1;
              else
                  Cont_Error2(jj)=Cont_Error2(jj)+1;
            %      plot(xtest(1,1),xtest(1,2),'kx','LineWidth',2,'MarkerSize',10)
              end
         %%%%%%%%     
          else
         %%%%%%%%   
             Cont_Empate(jj)=Cont_Empate(jj)+1;
            % plot(xtest(1,1),xtest(1,2),'cx','LineWidth',2,'MarkerSize',10)
           %  pause 
          end
     end
   end

 %   Cont_Error1
 %   Cont_Error2
 %   Cont_Empate
 
 
figure
 plot(Kvect,Cont_Error1)
 hold on
 plot(Kvect,Cont_Error2)
 plot(Kvect,Cont_Empate)

 figure
 plot(Kvect,Cont_Error2+Cont_Error1,'bo-')