from Gaussian and Nearest Mean Classifiers by Himanshu Ray
Estimating probability of Classification error for Gaussian and Nearest Mean Classifiers

G_N.m
%********* GAUSSIAN AND NEAREST MEAN CLASSIFICATION *********************%

% Following can be done using this M-File: 
% (a) Generate 100 random samples for each class (3-classes)with following parameters:
% 
%    Means: [5, 4] T, [15, 10] T, [10, 15] T
%    Covariance matrices:
%   (1)	Zi=Z2I, where Sigma^2 =9
%   (2)	Zi=Z2I, where Sigma1^2= 4, Sigma2^2 = 6, Sigma3^2 = 9.
%   (3)	Zi =Z and rows of Z are [9,1.5; 1.5,9].
%   (4)	The rows of Zi are [9,0;0, 9], [5,1.5; 1.5,5] and [6, -1; -1,4] for
%   I=1,2,3 respectively. 
%   Note :Only one case is demonstrated but other matrices can be developed
%   by changing appropriate parameters.
% 
%(b)Randomly partition the samples of each class into two equi-sized sets to form a training set and
%   a test set for each class. For each case, estimate the parameters of the Gaussian density function 
%   from the training set of the corresponding class.
% 
% (c)For each case, use the estimates of the parameters to determine the Gaussian discriminant function for:
% 
%   (1)	Equal prior class probabilities.
%   (2)	P(w1)= 0.3; P(w2)= 0.2; P(w3)= 0.5;
% 
% (d)Implement the Gaussian classifier for the 3-class problem. Classify the test samples of each class. 
% For each case, estimate the probability of classification error.
% 
% (e)Implement the nearest mean classifier for the 3-class problem. 
% Classify the test samples of each class. For each case, estimate the probability of classification error.

clc
close all
%~~~~~~~~~~~~ Generating random samples for 3-class pattern classification problem ~~~~~~~~~~~~~~~~~~%
x= randn(100,2);
cov_x=cov(x);
[v,d]=eig(cov(x));
Aw=v*inv(sqrt(d));
z=Aw'*x';
variance1=[9 0;0 9];        %%%%% Following parameters change according to different matrices desired
variance2=[5 1.5;1.5 5];
variance3=[6 -1;-1 4];
[v1,d1]=eig(variance1);
[v2,d2]=eig(variance2);
[v3,d3]=eig(variance3);
Aw1=v1*inv(sqrt(d1));
Aw2=v2*inv(sqrt(d2));
Aw3=v3*inv(sqrt(d3));
y1=inv(Aw1')*z;
y2=inv(Aw2')*z;
y3=inv(Aw3')*z;
cov_y1=cov(y1');
cov_y2=cov(y2');
cov_y3=cov(y3');
mn1=[5 4];                  %%%%% Mean Values 
mn2=[10 15];
mn3=[15 10];
y1=y1';
y2=y2';
y3=y3';
z=z';
x_n11=y1(:,1)+mn1(1, 1 );
x_n12=y1(:,2)+mn1(1, 2 );
x_n21=y2(:,1)+mn2(1, 1 );
x_n22=y2(:,2)+mn2(1, 2 );
x_n31=y3(:,1)+mn3(1, 1 );
x_n32=y3(:,2)+mn3(1, 2 );
x_n1=[x_n11 x_n12];
x_n2=[x_n21 x_n22];
x_n3=[x_n31 x_n32];
tr_sa1=x_n1(1:50,:);
tr_sa2=x_n2(1:50,:);
tr_sa3=x_n3(1:50,:);
m1=mean(tr_sa1);
m2=mean(tr_sa2);
m3=mean(tr_sa3);
co_tr1=cov(tr_sa1);
co_tr2=cov(tr_sa2);
co_tr3=cov(tr_sa3);

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GAUSSIAN CLASSIFIER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%

% CLASS 1
for i=1:50
   g1(i) = -0.5*(x_n1(i+50,:)-m1)*((co_tr1)^-1)*(x_n1(i+50,:)-m1)'-0.5*log(det(co_tr1))+log(1/3);
   g2(i) = -0.5*(x_n1(i+50,:)-m2)*((co_tr2)^-1)*(x_n1(i+50,:)-m2)'-0.5*log(det(co_tr2))+log(1/3);
   g3(i) = -0.5*(x_n1(i+50,:)-m3)*((co_tr3)^-1)*(x_n1(i+50,:)-m3)'-0.5*log(det(co_tr3))+log(1/3);
                           % log (1/3) is case for equal prior probabilities %
end
  correct1=0;
  wrong1=0;
  for i=1:50
      if g1(i)>g2(i) & g1(i)>g3(i)
          correct1=correct1+1;
      else
          wrong1=wrong1+1;
      end
  end
% CLASS 2 
for i=1:50
   g1(i) = -0.5*(x_n2(i+50,:)-m1)*((co_tr1)^-1)*(x_n2(i+50,:)-m1)'-0.5*log(det(co_tr1))+log(1/3);
   g2(i) = -0.5*(x_n2(i+50,:)-m2)*((co_tr2)^-1)*(x_n2(i+50,:)-m2)'-0.5*log(det(co_tr2))+log(1/3);
   g3(i) = -0.5*(x_n2(i+50,:)-m3)*((co_tr3)^-1)*(x_n2(i+50,:)-m3)'-0.5*log(det(co_tr3))+log(1/3);
end
  correct2=0;
  wrong2=0;
  for i=1:50
      if g2(i)>g1(i) & g2(i)>g3(i)
          correct2=correct2+1;
      else
          wrong2=wrong2+1;
      end
  end
% CLASS 3
  for i=1:50
   g1(i) = -0.5*(x_n3(i+50,:)-m1)*((co_tr1)^-1)*(x_n3(i+50,:)-m1)'-0.5*log(det(co_tr1))+log(1/3);
   g2(i) = -0.5*(x_n3(i+50,:)-m2)*((co_tr2)^-1)*(x_n3(i+50,:)-m2)'-0.5*log(det(co_tr2))+log(1/3);
   g3(i) = -0.5*(x_n3(i+50,:)-m3)*((co_tr3)^-1)*(x_n3(i+50,:)-m3)'-0.5*log(det(co_tr3))+log(1/3);
end
  correct3=0;
  wrong3=0;
  for i=1:50
      if g3(i)>g1(i) & g3(i)>g2(i)
          correct3=correct3+1;
      else
          wrong3=wrong3+1;
      end
  end
  disp('%********** RESULTS FOR GAUSSIAN CLASSIFIER ***********%')
  correct1
  correct2
  correct3
  wrong1
  wrong2
  wrong3
  % Calculation of probability of classification error
  pe=(wrong1+wrong2+wrong3)/150
  
  %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ NEAREST MEAN CLASSIFIER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
 % CLASS 1
  c1=0;
  w1=0;
  for i=1:50
      j=1;
      l(j)=norm((x_n1(i+50,:)-m1));
      l(j+1)=norm((x_n1(i+50,:)-m2));
      l(j+2)=norm((x_n1(i+50,:)-m3));
      d=min(l);
      if d==l(1)
          c1=c1+1;
      else
          w1=w1+1;
      end
  end
  % ClASS 2
  c2=0;
  w2=0;
  for i=1:50
      j=1;
      l(j)=norm((x_n2(i+50,:)-m1));
      l(j+1)=norm((x_n2(i+50,:)-m2));
      l(j+2)=norm((x_n2(i+50,:)-m3));
      d=min(l);
      if d==l(2)
          c2=c2+1;
      else
          w2=w2+1;
      end
  end
 % CLASS 3
  c3=0;
  w3=0;
  for i=1:50
      j=1;
      l(j)=norm((x_n3(i+50,:)-m1));
      l(j+1)=norm((x_n3(i+50,:)-m2));
      l(j+2)=norm((x_n3(i+50,:)-m3));
      d=min(l);
      if d==l(3)
          c3=c3+1;
      else
          w3=w3+1;
      end
  end
  disp('%********** RESULTS FOR NEAREST MEAN CLASSIFIER ***********%')
  c1
  c2
  c3
  w1
  w2
  w3
  % Calculation of probability of classification error
  pe=(w1+w2+w3)/150

Contact us at files@mathworks.com