Skip to content

Instantly share code, notes, and snippets.

@Hepic
Last active May 2, 2018 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hepic/33854e3ec75a7d7aecddbf899aaad97a to your computer and use it in GitHub Desktop.
Save Hepic/33854e3ec75a7d7aecddbf899aaad97a to your computer and use it in GitHub Desktop.
close('all');
clear;
m(:, 1) = [0 0]';
m(:, 2) = [7 7]';
S1 = 2 * eye(2);
S2 = 0.2 * eye(2);
P = [1/2 1/2];
% Generate X1 and the required class labels
N1 = 200;
randn('seed',0)
X1 = [mvnrnd(m(:,1), S1, fix(N1/2)); mvnrnd(m(:,2), S2, N1-fix(N1/2))]';
y1 = [(-1) * ones(1, fix(N1/2)) ones(1, N1-fix(N1/2))];
% Generate X2 and the required class labels
N2 = 200;
randn('seed',100)
X2=[mvnrnd(m(:,1), S1, fix(N2/2)); mvnrnd(m(:,2), S2, N2-fix(N2/2))]';
y2=[(-1) * ones(1, fix(N2/2)) ones(1, N2-fix(N2/2))];
% Compute the Bayesian classification error based on X2
S_true(:,:,1) = S1;
S_true(:,:,2) = S2;
[z]=bayes_classifier(m, S_true, P, X2);
bayY = 2 * z - 3;
err_Bayes_true = sum(bayY~=y2) / sum(N2)
% 2. Augment the data vectors of X1
X1=[X1; ones(1,sum(N1))];
% Augment the data vectors of X2
X2=[X2; ones(1,sum(N2))];
% Compute the classification error of the LS classifier based on X2
[w]=SSErr(X1, y1, 0)
SSE_out=2*(w'*X2>0)-1;
err_SSE=sum(SSE_out.*y2<0) / sum(N2)
wX = -5:0.1:12;
wY = (-w(1) * wX - w(3)) / w(2);
figure(1),
plot(X1(1, y1==-1), X1(2, y1==-1), 'bo', ...
X1(1, y1==1), X1(2, y1==1), 'r.', ...
wX, wY, 'g-');
figure(1), axis equal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment