clear all
close all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%
N1=500;
mu=[1,-1];
sigma=[0.9 0.4;0.4 0.3];

s=chol(sigma);
for i=1:N1;
  v = s*randn(2,1);
  c1(i,:) = mu + v';
 end
N1test=20;
%c1test=mvrnd(mu,sigma,N1test);
s1test=chol(sigma);
for i=1:N1test;
  v = s1test*randn(2,1);
  c1test(i,:) = mu + v';
 end
plot(c1(:,1),c1(:,2),'rs')
hold on
plot(c1test(:,1),c1test(:,2),'gx','LineWidth',10,'MarkerSize',10)
 
%%%%%%%%%%%%%%%%%%%%%%%
N2=500;
%mu2=[1,-1];
%mu2=[-2,2];
mu2=[0,0];
sigma2=[1 0;0 2];

s2=chol(sigma2);
for i=1:N2;
  v = s2*randn(2,1);
  c2(i,:) = mu2 + v';
 end
 
N2test=N1test;
%c2test=mvrnd(mu, sigma, N2test);

s2test=chol(sigma2);
for i=1:N2test;
  v = s2test*randn(2,1);
  c2test(i,:) = mu2 + v';
 end
 
plot(c2(:,1),c2(:,2),'bo');
plot(c2test(:,1),c2test(:,2),'kx','LineWidth',10,'MarkerSize',10)
pause

%%%%%%%%%%%%%%%%%%%%%%%%%%%
dist=@(x,z) sum(abs(x-z).^2);
%%%%%%%%Cont_correcto2%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axis([-7 7 -3 8])
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%Calcular errrores

Ctot_test=[c1test; c2test];
Labels_Ctot=[ones(1,N1test) zeros(1,N2test)];

%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%

H1vect=0.1:0.5:20; %%%% de las rojas 
H2vect=0.1:0.5:20; %%%% de las azules 


  Cont_empate=zeros(length(H1vect),length(H2vect));
  Cont_correcto1=zeros(length(H1vect),length(H2vect));%%%% DIGO QUE ES ROJO (CLASE 1) Y ES ROJO ===> P(R|R)
  Cont_error1=zeros(length(H1vect),length(H2vect));%%%% DIGO QUE ES ROJO (CLASE 1) Y ES AZUL    ===> P(R|A)
  Cont_correcto2=zeros(length(H1vect),length(H2vect));%%%% DIGO QUE ES AZUL (CLASE 2 o 0) Y ES AZUL ===> P(A|A)
  Cont_error2=zeros(length(H1vect),length(H2vect));%%%% DIGO QUE ES AZUL (CLASE 2 o 0) Y ES ROJO   ===> P(A|R)

for jj=1:length(H1vect)
  
    disp([num2str(jj/length(H1vect)*100),'%'])
  
for kk=1:length(H2vect)
  
  %kk
  
  
  %%%%%%%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%%%%%
  H1 = H1vect(jj);
  H2 = H2vect(kk);
  %%%%%%%%%%%%%%%%%%%%%%%%
 
  %%%%%%%%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  for k=1:N1test+N2test
  
    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)];
    
    DIM=2;
    K1=(1/(sqrt(2*pi*H1).^2))^DIM*exp(-d1/(2*H1^2));
   K2=(1/(sqrt(2*pi*H2).^2))^DIM*exp(-d2/(2*H2^2));
   P1=mean(K1);
   P2=mean(K2);
  
   %ProbC1(k,j) = P1/(P1 + P2);
  % ProbC2(k,j) = P2/(P1 + P2);
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  

 %  pos=find(dtot<Radius);
  %  Lab=labels(pos);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   % contneg=0;
   % contpos=0;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
    if P1>P2  %%% decido es que ROJO (CLASE 1)
     dec=1;
      if Labels_Ctot(k)==1
            Cont_correcto1(jj,kk) =Cont_correcto1(jj,kk)+1;
      else
         Cont_error1(jj,kk)=Cont_error1(jj,kk)+1;
      end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   elseif P1<P2 %%% decido es que AZUL (CLASE 2 o 0)
      dec=0;
        if Labels_Ctot(k)==0
           Cont_correcto2(jj,kk) =Cont_correcto2(jj,kk)+1;
      else
           Cont_error2(jj,kk)=Cont_error2(jj,kk)+1;
        end
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    else %%% empate
        Cont_empate(jj,kk)=Cont_empate(jj,kk)+1;
    end 
   end
end
  
end
figure
 pcolor(H2vect,H1vect,Cont_error2 + Cont_error1 + Cont_empate)
 
% % % % % % ##    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;
% % % % % % ##     end
% % % % % % ##       %%%%%%%%%%%%%%%%%%%%%%%%%%%
% % % % % % ##    elseif contpos<contneg
% % % % % % ##    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % % % % % ##      dec=0;
% % % % % % ##      if Labels_Ctot(k)==0
% % % % % % ##        Cont_correcto2(jj)=Cont_correcto2(jj)+1;
% % % % % % ##      el=Cont_error2(jj)+1;se
% % % % % % ##        Cont_error2(jj)=Cont_error2(jj)+1;
% % % % % % ##        end
% % % % % % ##    %%%%%%%%%%%%
% % % % % % ##    else
% % % % % % ##    %%%%%%%%%%%%
% % % % % % ##      Cont_empate(jj)=Cont_empate(jj)+1;
% % % % % % ##     end
% % % % % % ##   end
% % % % % % end
% % % % % % 
% % % % % % ##figure
% % % % % % ##plot(Rvect,Cont_error1)
% % % % % % ##hold on
% % % % % % ##plot(Rvect, Cont_error2)
% % % % % % ##plot(Rvect, Cont_empate)
% % % % % % ##
% % % % % % ##figure
% % % % % % ##plot(Rvect, Cont_error2 + Cont_error1 + Cont_empate,'bo-')
% % % % % % ##xlabel('K(numero vecinos)')
% % % % % % ##ylabel('suma errores')