【问题标题】:Mahalanobis distance in matlab: pdist2() vs. mahal() functionmatlab 中的马氏距离:pdist2() 与 mahal() 函数
【发布时间】:2013-11-24 21:06:51
【问题描述】:

我有两个矩阵 X 和 Y。它们都代表 3D 空间中的多个位置。 X 是一个 50*3 的矩阵,Y 是一个 60*3 的矩阵。

我的问题:为什么在 pdist2() 的输出上应用均值函数并结合 'Mahalanobis' 不会给出使用 mahal() 获得的结果?

更多关于我在下面尝试做的事情的详细信息,以及我用来测试它的代码。

假设矩阵 Y 中的 60 个观测值是在某种实验操作之后获得的。我正在尝试评估这种操作是否对 Y 中观察到的位置产生显着影响。因此,我使用pdist2(X,X,'Mahalanobis') 将 X 与 X 进行比较以获得基线,然后将 X 与 Y 进行比较(以 X 为参考矩阵: pdist2(X,Y,'Mahalanobis')),我绘制了两个分布以查看重叠情况。

随后,我计算了两个分布的平均马氏距离和 95% 置信区间,并进行了 t 检验和 Kolmogorov-Smirnoff 检验,以评估分布之间的差异是否显着。这对我来说似乎很直观,但是,当使用 mahal() 进行测试时,我得到不同的值,尽管参考矩阵是相同的。我不明白这两种计算马氏距离的方法到底有什么区别。

评论太长@3lectrologos: 你的意思是:d(I) = (Y(I,:)-mu)inv(SIGMA)(Y(I,:)-mu)'?这只是计算 mahalanobis 的公式,因此对于 pdist2() 和 mahal() 函数应该是相同的。我认为 mu 是一个标量,而 SIGMA 是一个基于 pdist2() 和 mahal() 中的整体参考分布的矩阵。仅在 mahal 中,您将样本集的每个点与参考分布的点进行比较,而在 pdist2 中,您将基于参考分布进行成对比较。实际上,考虑到我的目的,我认为我应该选择 mahal() 而不是 pdist2()。我可以根据参考分布解释成对距离,但我认为这不是我需要的。

% test pdist2 vs. mahal in matlab

% the purpose of this script is to see whether the average over the rows of E equals the values in d...

% data
X = []; % 50*3 matrix, data omitted
Y = []; % 60*3 matrix, data omitted


% calculations
S = nancov(X);

% mahal()
d = mahal(Y,X); % gives an 60*1 matrix with a value for each Cartesian element in Y (second matrix is always the reference matrix)

% pairwise mahalanobis distance with pdist2()
E = pdist2(X,Y,'mahalanobis',S); % outputs an 50*60 matrix with each ij-th element the pairwise distance between element X(i,:) and Y(j,:) based on the covariance matrix of X: nancov(X)
%{
 so this is harder to interpret than mahal(), as elements of Y are not just compared to the "mahalanobis-centroid" based on X,
% but to each individual element of X
% so the purpose of this script is to see whether the average over the rows of E equals the values in d...
%}

F = mean(E); % now I averaged over the rows, which means, over all values of X, the reference matrix

mean(d)
mean(E(:)) % not equal to mean(d)
d-F' % not zero

% plot output
figure(1)
plot(d,'bo'), hold on
plot(mean(E),'ro')
legend('mahal()','avaraged over all x values pdist2()')
ylabel('Mahalanobis distance')

figure(2)
plot(d,'bo'), hold on
plot(E','ro')
plot(d,'bo','MarkerFaceColor','b')
xlabel('values in matrix Y (Yi) ... or ... pairwise comparison Yi. (Yi vs. all Xi values)')
ylabel('Mahalanobis distance')
legend('mahal()','pdist2()')

【问题讨论】:

  • 添加了代码格式并为您删除了最后的咆哮。

标签: matlab


【解决方案1】:

注意

mahal(X,Y)

等价于

pdist2(X,mean(Y),'mahalanobis',cov(Y)).^2

【讨论】:

    【解决方案2】:

    嗯,我想有两种不同的方法来计算两个数据集群之间的马氏距离,就像你在上面解释的那样: 1)您将样本集中的每个数据点与从您的参考分布计算的 mu 和 sigma 矩阵进行比较(尽管标记一个集群样本集和另一个参考分布可能是任意的),从而计算每个点到这个所谓的 mahalanobis -参考分布的质心。 2)您将矩阵 Y 的每个数据点与矩阵 X 的每个数据点进行比较,其中 X 是参考分布(mu 和 sigma 仅从 X 计算)

    距离的值会有所不同,但我猜在使用方法 1 或方法 2 时会保留集群之间不同的顺序?我真的想知道当将 10 个不同的集群与参考矩阵 X 或彼此进行比较时,如果使用方法 1 或方法 2 不同的顺序会有所不同?另外,我无法想象一种方法错误而另一种方法错误的情况。虽然方法 1 在某些情况下看起来更直观,比如我的。

    【讨论】:

    • 我测试了许多集群,并且在这两种方法中似乎都保留了序数属性,以及集群之间差异幅度的一部分。
    【解决方案3】:

    两者之间的一个直接区别是mahal 在计算距离之前从Y 中的每个点减去X 的样本均值。

    尝试使用E = pdist2(X,Y-mean(X),'mahalanobis',S); 之类的方法,看看它是否提供与mahal 相同的结果。

    【讨论】:

    • 你在哪里读到的?我试过 E = pdist2(X,Y-repmat(mean(X),size(Y,1),1),'mahalanobis',S);但这只会增加成对距离,减去平均值的大小......因此,我现在得到了巨大的距离值,这是不正确的...... .
    • 我在各个函数的 Matlab 文档中读到了。
    • 我在问题中添加了对您评论的回复。在这里发帖太长了。
    猜你喜欢
    • 2016-04-21
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    相关资源
    最近更新 更多