Gaussian mixture distribution plot
9 views (last 30 days)
Show older comments
I'm trying to bild a gaussian mixture distribution. I'm quite new on Matlab, but I think the script is ok. I'm not able to plot my distribution. Here you can find what I have. Many thanks for your answers, cheers
%quadmodal mixture
mu=[30 0 0 0; 22 0 0 0;12 0 0 0;4 0 0 0];
sigma=cat(3,[9^2 0 0 0;0 6.6^2 0 0;0 0 3.6^2 0;0 0 0 1.2^2],[1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1],[1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1],[1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1]);
p=ones(1,4)/4;
obj = gmdistribution(mu,sigma,p);
ezsurf(@(x,y)pdf(obj,[x y]),[-25 25],[-25 25])
1 Comment
zepp
on 30 Apr 2014
your mixture distribution has four dimensions, so you cannot plot that using ezsurf. Are you sure you have initialised the mu and sigma values correctly? Or is it just a 1 dimensional 4 component Gaussian distribution?
Answers (1)
Prateekshya
on 16 Oct 2024
Hello Marco,
It looks like you're attempting to create and plot a Gaussian Mixture Model (GMM) in MATLAB using the gmdistribution function. Your script is mostly correct, but there are a few adjustments needed to properly plot the distribution.
Here is an updated version of your script with explanations:
% Define the means of the Gaussian components
mu = [30 0; 22 0; 12 0; 4 0];
% Define the covariance matrices for each component
sigma = cat(3, ...
[9^2 0; 0 6.6^2], ...
[1 0; 0 1], ...
[1 0; 0 1], ...
[1 0; 0 1]);
% Define the mixing proportions for each component
p = ones(1, 4) / 4;
% Create a Gaussian Mixture Model object
obj = gmdistribution(mu, sigma, p);
% Plot the Gaussian Mixture Model
% Define a grid of points over which to evaluate the GMM
x = linspace(-25, 35, 100);
y = linspace(-25, 35, 100);
[X, Y] = meshgrid(x, y);
% Evaluate the GMM PDF over the grid
Z = pdf(obj, [X(:) Y(:)]);
Z = reshape(Z, size(X));
% Plot the surface
figure;
surf(X, Y, Z);
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Probability Density');
title('Gaussian Mixture Model');
Ensure that the covariance matrices are the correct size for the means you have specified. Each covariance matrix should be 2x2 since your means are 2-dimensional. Use meshgrid to create a grid of points over which to evaluate the GMM. This allows you to compute the probability density function (PDF) values over a range of x and y values. After evaluating the PDF, reshape the resulting vector Z back into a matrix that matches the grid dimensions for plotting. Use surf instead of ezsurf for more control over the plot. This also allows you to label the axes and add a title. The above code produces the below result:
I hope this helps!
0 Comments
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!