高斯混合模型——得到给定概率值的轮廓:Matlab

Gaussian mixture model - get the contour of given probability value : Matlab

我需要确定适合数据的 GMM 的 99% 概率等高线。按照这个例子,我希望能够指定要绘制的等高线,以及它们的 x、y。

mu1 = [1 2]; Sigma1 = [2 0; 0 0.5];
mu2 = [-3 -5]; Sigma2 = [1 0;0 1];
X = [mvnrnd(mu1,Sigma1,1000); mvnrnd(mu2,Sigma2,1000)];
GMModel = fitgmdist(X,2);
figure
y = [zeros(1000,1);ones(1000,1)];
h = gscatter(X(:,1),X(:,2),y);
hold on
gmPDF = @(x,y) arrayfun(@(x0,y0) pdf(GMModel,[x0 y0]),x,y);
g = gca;
fcontour(gmPDF,[g.XLim g.YLim])
title('{\bf Scatter Plot and Fitted Gaussian Mixture Contours}')
legend(h,'Model 0','Model1')
hold off

因此,在下图中,我希望能够用黑色虚线“k”绘制 99%。知道如何实现吗?

可以显示和获取给定轮廓线的坐标,指定fcontour的LevelList属性,然后读取轮廓句柄的ContourMatrix属性:

% Random function, insert here yours
f = @(x,y) arrayfun(@(x0,y0) x0.^2 + y0.^2 - 0.1,x,y);

% The function value you want to get the contour for
lvl = 0.99;

% Plot the contour line
cHandle = fcontour(f, '--k', 'LevelList', [lvl]);
hold on

% Get the coordinates
lvlX = cHandle.ContourMatrix(1, 2:end);
lvlY = cHandle.ContourMatrix(2, 2:end);

% For a check:
plot(lvlX, lvlY, '--r')