visualize the decision boundary and the conjunction of the two densities in matlab

To visualize the decision boundary and the conjunction of two densities in MATLAB, you can follow these steps:

  1. Generate data points from the two densities. Let's assume you have two Gaussian distributions.
main.m
% Generate data from Gaussian distributions
mu1 = [0 0];   % mean of the first Gaussian distribution
sigma1 = [1 0.5; 0.5 1];   % covariance matrix of the first Gaussian distribution
data1 = mvnrnd(mu1, sigma1, 100);   % generate 100 data points from the first Gaussian

mu2 = [2 2];   % mean of the second Gaussian distribution
sigma2 = [1 -0.5; -0.5 1];   % covariance matrix of the second Gaussian distribution
data2 = mvnrnd(mu2, sigma2, 100);   % generate 100 data points from the second Gaussian
502 chars
9 lines
  1. Plot the generated data points.
main.m
% Plot the data points
scatter(data1(:,1), data1(:,2), 'r', 'filled');
hold on;
scatter(data2(:,1), data2(:,2), 'b', 'filled');
legend('Class 1', 'Class 2');
xlabel('Feature 1');
ylabel('Feature 2');
200 chars
8 lines
  1. Compute the decision boundary. One way to do this is to fit a discriminative model, such as a Support Vector Machine (SVM), on the data using a classification algorithm.
main.m
% Train an SVM classifier
X = [data1; data2];   % concatenate the data from both classes
y = [ones(size(data1,1),1); -1*ones(size(data2,1),1)];   % create labels for the classes
svm = fitcsvm(X, y);   % train SVM classifier
224 chars
5 lines
  1. Plot the decision boundary.
main.m
% Plot the decision boundary
x1grid = linspace(min(X(:,1)), max(X(:,1)), 100);
x2grid = linspace(min(X(:,2)), max(X(:,2)), 100);
[x1, x2] = meshgrid(x1grid,x2grid);
XGrid = [x1(:),x2(:)];
[~,scores] = predict(svm,XGrid);
contour(x1, x2, reshape(scores(:,2), size(x1)), [0 0], 'k');
282 chars
8 lines
  1. Plot the conjunction of the two densities. This can be done by calculating the joint density function using the two Gaussian distributions.
main.m
% Plot the conjunction of the two densities
density = mvnpdf(XGrid, mu1, sigma1) .* mvnpdf(XGrid, mu2, sigma2);
contour(x1,x2,reshape(density,size(x1)), [0.01 0.01], 'Color', 'm');
181 chars
4 lines
  1. Adjust the axis limits if needed and add plot titles.
main.m
% Adjust axis limits and add titles
axis equal;
xlim([min(X(:,1)) max(X(:,1))]);
ylim([min(X(:,2)) max(X(:,2))]);
title('Decision Boundary and Conjunction of Densities');
171 chars
6 lines

Putting it all together:

main.m
% Generate data points from Gaussian distributions
mu1 = [0 0];
sigma1 = [1 0.5; 0.5 1];
data1 = mvnrnd(mu1, sigma1, 100);

mu2 = [2 2];
sigma2 = [1 -0.5; -0.5 1];
data2 = mvnrnd(mu2, sigma2, 100);

% Plot the data points
scatter(data1(:,1), data1(:,2), 'r', 'filled');
hold on;
scatter(data2(:,1), data2(:,2), 'b', 'filled');
legend('Class 1', 'Class 2');
xlabel('Feature 1');
ylabel('Feature 2');

% Train an SVM classifier
X = [data1; data2];
y = [ones(size(data1,1),1); -1*ones(size(data2,1),1)];
svm = fitcsvm(X, y);

% Plot the decision boundary
x1grid = linspace(min(X(:,1)), max(X(:,1)), 100);
x2grid = linspace(min(X(:,2)), max(X(:,2)), 100);
[x1, x2] = meshgrid(x1grid,x2grid);
XGrid = [x1(:),x2(:)];
[~,scores] = predict(svm,XGrid);
contour(x1, x2, reshape(scores(:,2), size(x1)), [0 0], 'k');

% Plot the conjunction of the two densities
density = mvnpdf(XGrid, mu1, sigma1) .* mvnpdf(XGrid, mu2, sigma2);
contour(x1,x2,reshape(density,size(x1)), [0.01 0.01], 'Color', 'm');

% Adjust axis limits and add titles
axis equal;
xlim([min(X(:,1)) max(X(:,1))]);
ylim([min(X(:,2)) max(X(:,2))]);
title('Decision Boundary and Conjunction of Densities');
1159 chars
40 lines

By running this code, you will obtain a plot showing the data points, the decision boundary, and the conjunction of the two densities.

Note: Make sure you have the Statistics and Machine Learning Toolbox installed in MATLAB to use functions like mvnrnd, mvnpdf, and fitcsvm.

gistlibby LogSnag