根据分布方差绘制等高线

plotting contours according to distribution variance

我是 运行 一个实验,我得到一组二维点形式的数据作为输出

# read csv file
samples = csvread('results.csv');

假设它们服从正态分布,我可以计算它们的均值和协方差并评估 pdf

mu = mean(samples);
sigma = cov(samples);
y = mvnpdf(X,mu,sigma);

其中 X 是使用 meshgrid 计算的二维网格。

我现在想做的是用 iso-contours 将此分布绘制为 here

而且,我想选择与协方差相关的特定值的轮廓,如图所示:

举个例子,我应该知道点分布的值 = (X: mu(1)+sigma(1,1), Y: mu(1)+sigma(1,1)) .我的猜测是我可以用这种方式使用 normpdf 函数

value = normpdf(point,mu,sigma)

但我得到这个作为输出:

value =

   116.39   297.63
   297.63   409.88

而且我真的不知道如何解释它。

有人可以告诉我怎么做吗?

谢谢。

在你开始谈论 normpdf 的那一刻我就迷失了你。您有一个多元正态分布,这就是您需要处理的问题。不确定您期望从此处的单变量正态得到什么。

如果您想获得马哈拉诺比斯距离为 1 的多元正态分布的概率值(即与分布均值相差一个标准差),您可以通过在值 [m+s,0] 其中,m = mu(1)s = sqrt( sigma(1,1) ).

然后您可以将其与适当的轮廓函数一起使用以获得所需的轮廓。

编辑:这是一个例子。

  pkg load statistics

% create 1000 samples from a known multivariate normal
  Observations     = mvnrnd( [0,0], [4, 1; 1 ,2], 1000 );

% Get mean and covariance estimates from sample
  SampleMean       = mean( Observations, 1 ) % average along rows
  SampleCovariance = cov( Observations )

% Get mean and standard deviation in first dimension (i.e. "x-axis")
  Mean_firstDimension   = SampleMean(1)
  StdDev_firstDimension = sqrt( SampleCovariance(1,1) )

% Get gaussian values at malanobis distance of 1, 2, and 3
  MVN_value_at_mahalanobis_distance_of_one   = mvnpdf( [ Mean_firstDimension +     StdDev_firstDimension, 0], SampleMean, SampleCovariance )
  MVN_value_at_mahalanobis_distance_of_two   = mvnpdf( [ Mean_firstDimension + 2 * StdDev_firstDimension, 0], SampleMean, SampleCovariance )    
  MVN_value_at_mahalanobis_distance_of_three = mvnpdf( [ Mean_firstDimension + 3 * StdDev_firstDimension, 0], SampleMean, SampleCovariance )    

% Define grid:
  [GridX, GridY] = ndgrid( -8:0.1:8, -8:0.1:8 );
  GridValues = mvnpdf( [GridX(:), GridY(:)], SampleMean, SampleCovariance );
  GridValues = reshape( GridValues, size( GridX ) );

% Plot Observations
  plot( Observations(:,1), Observations(:,2), 'o', 'markerfacecolor', 'g', 'markeredgecolor', [0,0.5,0], 'linewidth', 1.5 );
  hold on;

% Plot contours over grid
  contour( GridX, GridY, GridValues,   ...
           [ MVN_value_at_mahalanobis_distance_of_three, ...
             MVN_value_at_mahalanobis_distance_of_two, ...
             MVN_value_at_mahalanobis_distance_of_one ...
           ],
           'linewidth', 2
  )
  hold off;

% Set nice limits and colours for background
  axis([-8, +8, -8, +8]); axis equal square;
  set(gca, 'color', 'k');
  set(gcf, 'color', [0.75, 0.75, 0.75]);