wine_data;
%In this case, we want to normalize the data so that the window width can be the same
%for different dimensions
normalize_flag = 1;
if normalize_flag ~= 0,
   for ccol=2:size(wine_ins,2),
      minV=min(wine_ins(:,ccol));
      maxV=max(wine_ins(:,ccol));
      varCol=var(wine_ins(:,ccol));
      wine_ins(:,ccol) = (wine_ins(:,ccol)-minV)/sqrt(varCol);
   end
end
fprintf(1,'Now classifying the Wine dataset with %d instances using leave-one-out.\n', size(wine_ins,1));
fprintf(1,'Probability density for each class is estimated using Parzen windows.\n');
win_h=0.01;
perf_hist=[];
while win_h < 5;
   total_ins = 0;
   total_correct = 0;
   total_wrong = 0;
   for ind=1:size(wine_ins,1),
      wine_ins_rest=wine_ins([1:(ind-1) (ind+1):size(wine_ins,1)],:);
      c1_ind=find(wine_ins_rest(:,1)==1);
      wine_ins_rest_c1=wine_ins_rest(c1_ind,2:end);
      c2_ind=find(wine_ins_rest(:,1)==2);
      wine_ins_rest_c2=wine_ins_rest(c2_ind,2:end);
      c3_ind=find(wine_ins_rest(:,1)==3);
      wine_ins_rest_c3=wine_ins_rest(c3_ind,2:end);
      x=wine_ins(ind,2:end);
      
      prob_c1_dist = wine_ins_rest_c1-repmat(x,[size(wine_ins_rest_c1,1) 1]);
      prob_c1_dist = prob_c1_dist.^2/(2*win_h^2);
      prob_c1_dist = exp(-prob_c1_dist);
      g1 = sum(log(sum(prob_c1_dist)));
      
      prob_c2_dist = wine_ins_rest_c2-repmat(x,[size(wine_ins_rest_c2,1) 1]);
      prob_c2_dist = prob_c2_dist.^2/(2*win_h^2);
      prob_c2_dist = exp(-prob_c2_dist);
      g2 = sum(log(sum(prob_c2_dist)));
      
      prob_c3_dist = wine_ins_rest_c3-repmat(x,[size(wine_ins_rest_c3,1) 1]);
      prob_c3_dist = prob_c3_dist.^2/(2*win_h^2);
      prob_c3_dist = exp(-prob_c3_dist);
      g3 = sum(log(sum(prob_c3_dist)));
      
      %fprintf(1,'For instance %d (from class %d), the discriminant functions are %8.6f %8.6f %8.6f.\n',...
      %   ind, wine_ins(ind,1), g1,g2,g3);
      disc_funcs=[g1 g2 g3];
      [dummy classified_ind]=max(disc_funcs);
      %fprintf(1,'   using minimum distance classifier, it is classified as class %d, which is ',classified_ind);
      if classified_ind==wine_ins(ind,1),
         %fprintf(1,'correct.\n');
         total_correct = total_correct + 1;
      else
         %fprintf(1,'wrong.\n');
         total_wrong = total_wrong + 1;
      end
      total_ins = total_ins + 1;
      %pause;
   end
   fprintf(1,'Using leave-one-out classification, there are %d correct out of %d, which is %8.6f %%.\n',...
      total_correct, total_ins, (100.0*total_correct/total_ins));
   perf_hist(end+1,:)=[win_h (100.0*total_correct/total_ins)];
   win_h = win_h * 1.05;
   
end
hh=semilogx(perf_hist(:,1),perf_hist(:,2));
set(hh,'LineWidth',[2]);
xlabel('Window width');
ylabel('Classification accuracy');
set(gcf,'Color',[1 1 1]);