% plot of discrete naive bayes
eps = .04; p11 = .8; p21 = .5; p10 = .1; p20 = .4; n1 = 15; n0 = 15;
x11 = [rand(n1,1) < p11];
x10 = [rand(n0,1) < p10];
x21 = [rand(n1,1) < p21];
x20 = [rand(n0,1) < p20];
plot(x11+eps*randn(n1,1),x21+eps*randn(n1,1),'+',x10+eps*randn(n0,1),x20+eps*randn(n0,1), 'o', 'LineWidth', 2, 'MarkerSize', 10); set(gca, 'FontSize', 18);
xlabel X1; ylabel X2;
w0 = log(n1)-log(n0)+log(1-p11)-log(1-p10)+log(1-p21)-log(1-p20);
w1 = log(p11)-log(1-p11)-log(p10)+log(1-p10);
w2 = log(p21)-log(1-p21)-log(p20)+log(1-p20);
drawhalfspace(-[w1,w2],-w0,10,[],1);
axis([-.1 1.1 -.1 1.1]);
axis square;
[w0, w1, w2]

% 3 normal distributions
n1 = 100; n2 = 100; n3 = 100;
mu1 = [0; 3]; mu2 = [2; 1]; mu3 = [-.5; 0];
sig1 = .2*[.7 .3; .3 .7]; sig2 = .2*[.7 -.6; -.6 .7]; sig3 = .1*eye(2);
x1 = randn(n1,2);
x2 = randn(n2,2);
x3 = randn(n3,2);
x1 = x1*chol(sig1)+repmat(mu1',n1,1);
x2 = x2*chol(sig2)+repmat(mu2', n2,1);
x3 = x3*chol(sig3)+repmat(mu3', n3,1);
plot(x1(:,1),x1(:,2), 'x', x2(:,1),x2(:,2), 'o', x3(:,1),x3(:,2),'s');
axis equal;
[ex, ey] = ellipse(mu1, sig1, 100); line(ex, ey);
[ex, ey] = ellipse(mu1, 4*sig1, 100); line(ex, ey);
[ex, ey] = ellipse(mu2, sig2, 100); line(ex, ey, 'Color', 'g');
[ex, ey] = ellipse(mu2, 4*sig2, 100); line(ex, ey, 'Color', 'g')
[ex, ey] = ellipse(mu3, sig3, 100); line(ex, ey, 'Color', 'r')
[ex, ey] = ellipse(mu3, 4*sig3, 100); line(ex, ey, 'Color', 'r')