Azzera filtri
Azzera filtri

how draw mean and cov matrix?

14 visualizzazioni (ultimi 30 giorni)
alex il 21 Ott 2022
Risposto: Sai Pavan il 18 Apr 2024
I have 2 mean which is mu1=[1 1] and mu2=[1 0] and 2 2*2 cov matrix which is cov1=[1 0.2:0.2 0.5] and cov2=[0.4 0:0 0.5] ,how shoul I plot them and find best decision line in 2d?

Risposte (1)

Sai Pavan
Sai Pavan il 18 Apr 2024
Hello Alex,
I understand that you want to plot the distributions with specific means and covariance matrices and find the best decision boundary.
We get a decision line between two distributions at the midway of means when the covariance matrices are same, and when they are not same, we get a decision boundary which is not necessarily linear. To plot the distributions defined by your means (mu1, mu2) and covariance matrices (cov1, cov2) and to find the best decision line between them in 2D, please follow the below workflow:
  1. Plot the Gaussian Distributions: Use the "mvnpdf" function to evaluate the multivariate normal probability density functions for a grid of points covering the range of your data. Then, use contour plots to visualize these distributions.
  2. Calculate the Decision Boundary: The decision boundary between two Gaussian distributions can be found analytically because it's the set of points where the two distributions have equal probability density. However, for simplicity and visualization, you can numerically evaluate which of the two distributions has a higher probability for each point on a grid and use this to draw the decision boundary.
Please refer to the below code snippet that illustrates the above mentioned workflow:
% Define means and covariance matrices
mu1 = [1 1];
mu2 = [1 0];
cov1 = [1 0.2; 0.2 0.5];
cov2 = [0.4 0; 0 0.5];
% Create a grid of points covering the range of interest
[x, y] = meshgrid(linspace(-1, 3, 500), linspace(-1, 3, 500));
XGrid = [x(:) y(:)];
% Evaluate the Gaussian PDF for each point on the grid for both distributions
pdf1 = mvnpdf(XGrid, mu1, cov1);
pdf2 = mvnpdf(XGrid, mu2, cov2);
% Reshape for contour plotting
pdf1 = reshape(pdf1, size(x));
pdf2 = reshape(pdf2, size(x));
% Plot the contours of the two distributions
figure; hold on;
contour(x, y, pdf1, 'LineColor', 'b');
contour(x, y, pdf2, 'LineColor', 'r');
title('Gaussian Distributions and Decision Boundary');
% Calculate and plot the decision boundary. The decision boundary is where the PDFs are equal, so find where pdf1 = pdf2
decisionMap = pdf1 - pdf2;
contour(x, y, decisionMap, [0, 0], 'LineColor', 'k', 'LineWidth', 2);
legend('Distribution 1', 'Distribution 2', 'Decision Boundary');
% Add the means to the plot for better visualization
plot(mu1(1), mu1(2), 'bx', 'MarkerSize', 10, 'LineWidth', 2);
plot(mu2(1), mu2(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);
hold off;
Please refer to the below documentation to learn more about the "mvnpdf" function:
Hope it helps!


Scopri di più su Statistics and Machine Learning Toolbox in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by