我将此2D正态分布定义为
mu = [0 0];
Sigma = [1 0.5^0.5; 0.5^0.5 1];
当累积概率为95%时,有没有办法获得轮廓数据。我不想要绘图,而是(x,y)点的值导致95%的轮廓。
如果有人可以提供帮助,那将非常友好。提前致谢
答案 0 :(得分:1)
您可以使用数值解算器查找轮廓,如下所示:
% plot the distribution
figure;
x = (-5:0.5:5)';
y = (-5:0.5:5)';
[X1,X2] = meshgrid(x',y');
X = [X1(:) X2(:)];
p = mvncdf(X,mu,Sigma);
X3 = reshape(p,length(x),length(y));
surf(X1,X2,X3);
x = (-5:0.1:5)'; % define the x samples at which the contour should be calculated
y0 = zeros(size(x)); % initial guess
y = fsolve(@(y) mvncdf([x y], mu, Sigma) - 0.95, y0); % solve your problem
z = mvncdf([x y],mu,Sigma); % calculate the correspond cdf values
hold on
plot3(x(z>0.94), y(z>0.94), z(z>0.94), 'LineWidth', 5); % plot only the valid solutions, i.e. a solution does not exist for all sample points of x.
要获得所需轮廓的更好数值表示,可以对选定的y值重复上述方法。因此,您的线条将更好地填充整个图表。
作为替代,可以使用contour
计算轮廓上的点,如下所示:
figure
[c, h] = contour(X1, X2, X3, [0.95 0.95]);
c(3, :) = mvncdf(c',mu,Sigma);
figure(1)
plot3(c(1, :)', c(2, :)', c(3, :)', 'LineWidth', 5);
xlim([-5 5])
ylim([-5 5])
这种方法的缺点是您无法控制采样轮廓的粗糙度。其次,此方法使用(插值)3D cdf,其准确度低于使用fsolve
计算的值。
答案 1 :(得分:0)
这会有帮助吗?
clear all
close all
mu = [0 0];
Sigma = [1 0.5^0.5; 0.5^0.5 1];
x1 = -3:.2:3; x2 = -3:.2:3;
[X1,X2] = meshgrid(x1,x2);
F = mvnpdf([X1(:) X2(:)],mu,Sigma);
F = reshape(F,length(x2),length(x1));
subplot(1,3,1)
surf(x1,x2,F); axis square
X = [X1(:) X2(:)];
p = mvncdf(X,mu,Sigma);
P = reshape(p,31,31);
subplot(1,3,2)
surf(X1,X2,P); axis square
subplot(1,3,3)
P(P<0.95) = NaN;
surf(X1,X2,P); axis square