欢迎回到我的离群值博客系列。 在第一集,我们着眼于定义和直观地识别异常值。 在第二集,我们使用分位数(通过箱线图)来帮助识别一维中的异常值。
在本集中,我们将研究使用马氏距离识别多个维度的异常值。 我们也快速浏览一下 T 2 ,这是马哈拉诺比斯距离的简单扩展。
多维异常值
图 1 是其中一张图的复制品第1集并显示 1,000 个数据点的二维图,其中有明显的异常值(用红色方块标记):
Figure 1: 1,000 data points in two dimensions, outlier marked in redFigure 1: 1,000 data points in two dimensions, outlier marked in redFigure 1: 1,000 data points in two dimensions, outlier marked in redFigure 1: 1,000 data points in two dimensions, outlier marked in red图 1:二维 1,000 个数据点,异常值标记为红色
图 1 中的散点图使我们能够轻松地在二维中直观地确定离群值,即使离群值从单变量直方图分布来看并不明显。 因此,盒须(分位数)技术描述于第2集分析多维数据时并不总是可靠。 并直观地识别异常值(如第1集)随着维数的增加变得困难。 那么我们如何才能更可靠地检测这些多维异常值呢?
马哈拉诺比斯距离
马氏距离 (D中号)为我们提供了一种识别多维异常值的数值方法。 它以其创建者印度统计学家 Prasanta Chandra Mahalanobis (1893-1972) 的名字命名。 有关马哈拉诺比斯博士的更多信息,我引导您访问这篇维基百科文章。
广告中号图 1 所示数据集的绘图如下所示:
Figure 2: Mahalanobis Distance plot for 1,000 data points from Figure 1Figure 2: Mahalanobis Distance plot for 1,000 data points from Figure 1Figure 2: Mahalanobis Distance plot for 1,000 data points from Figure 1Figure 2: Mahalanobis Distance plot for 1,000 data points from Figure 1图 2:图 1 中 1,000 个数据点的马氏距离图
图 2 按行顺序显示所有数据点与 D中号在 Y 轴上。 D 的值中号每个点表示每个点距所有数据点质心的标准化“距离”。 红色数据点明显从其余点中脱颖而出,因此需要将其作为异常值进行进一步调查。
那么这个计算是如何进行的呢?
一般方法
假设我们有 1,000 个点的二维分布。 在每个维度中,999 个点是从均值为零、标准差为 1.0 的正态分布总体中抽取的。 然后我添加了一个离群点作为样本中的最后一个点。 如果我们制作 XY 散点图,我们可能会得到这样的结果:
Figure 3: Scatterplot of 1,000 uncorrelated data points, with one marked outlier (red)Figure 3: Scatterplot of 1,000 uncorrelated data points, with one marked outlier (red)Figure 3: Scatterplot of 1,000 uncorrelated data points, with one marked outlier (red)Figure 3: Scatterplot of 1,000 uncorrelated data points, with one marked outlier (red)图 3:1,000 个不相关数据点的散点图,其中一个标记为异常值(红色)
请注意图 3 中的红色兴趣点。 我们可以很容易地计算出1000个点样本在每个维度上的均值和标准差,并且我们可以利用毕达哥拉斯定理计算出红点到点质心的欧氏距离。 对于多维:

事实上,我们可以画以质心为中心的圆,表示与质心的距离相等,如图 4 所示:
Figure 4: Scatterplot of 1,000 uncorrelated data points, with equidistant circles from centroid overlaidFigure 4: Scatterplot of 1,000 uncorrelated data points, with equidistant circles from centroid overlaidFigure 4: Scatterplot of 1,000 uncorrelated data points, with equidistant circles from centroid overlaidFigure 4: Scatterplot of 1,000 uncorrelated data points, with equidistant circles from centroid overlaid图 4:1,000 个不相关数据点的散点图,其中与质心等距的圆重叠
在图 4 中,圆圈表示到点质心 1、2、3 和 4 个单位(从最小到最大)的距离。 这里可以清楚地看到,红色的点距离点的质心最远。 其距离用“d”表示。
如果我们有类似的尺度分布(即标准差相同),那么这种方法就很有效。但是,如果我们有不同的比例,如图 5 所示,该怎么办?
Figure 5: Scatterplot of 1,000 uncorrelated data points, having different means and standard deviations in each dimension.Figure 5: Scatterplot of 1,000 uncorrelated data points, having different means and standard deviations in each dimension.Figure 5: Scatterplot of 1,000 uncorrelated data points, having different means and standard deviations in each dimension.Figure 5: Scatterplot of 1,000 uncorrelated data points, having different means and standard deviations in each dimension.图 5:1,000 个不相关数据点的散点图,每个维度具有不同的平均值和标准差。
这里我们有两个问题。 首先,X3 维度中的点的平均值不为零。 这很简单,只需从所有数据点中减去平均值即可解决。
第二个问题是X3维度的标准差远大于X1维度的标准差。 这使得距离计算毫无意义,除非我们能够补偿差异。 这也很容易完成。 校正从零开始的均值偏移后,我们只需将每个维度除以其标准差即可。 这可以对标准差中的任何不匹配进行标准化。 评估该距离的多维方程变为:

其中 m 和 s 代表每个维度的平均值和标准差。 如果我们进行这些计算并重新绘制标准化数据,我们会得到下图:
Figure 6: Data from Figure 5 after transforming to correct for mean and standard deviation of each dimensionFigure 6: Data from Figure 5 after transforming to correct for mean and standard deviation of each dimensionFigure 6: Data from Figure 5 after transforming to correct for mean and standard deviation of each dimensionFigure 6: Data from Figure 5 after transforming to correct for mean and standard deviation of each dimension图 6:经过转换以校正每个维度的平均值和标准差后的图 5 中的数据
所以现在我们有一种方法可以调整数据的非零均值和不等的标准差。 只要每个维度的数据相互独立(即不相关),这种方法就很有效。但是,如果存在如图 1 所示的相关性,那么我们该怎么办?
实现此目的的一种方法是使用主组件来建立与数据趋势一致的新轴,然后执行上述操作。 但这需要大量的点击。 (如果对其如何工作感兴趣,请参阅附录 A。)
幸运的是,Mahalanobis 博士为我们提供了一种简单而优雅的方法来计算这个距离!
用于计算距离的马哈拉诺比斯方程
Mahalanobis 博士计算 D 的优雅方程中号是:
在哪里:

是 i 的 N 维值的向量th单独观察到的数据点。
N = 感兴趣的维度(或列)数。
T表示转置操作。

是感兴趣的 N 个维度(或列)中每个维度的均值向量。
S -1是所有数据点的 NxN 协方差矩阵的逆矩阵(可从 JMP 中的多元平台获取)。
将适当的值代入上述方程可得出 D中号从我th指向数据集中所有点的质心。 如果您对这种计算的原理感兴趣,请参阅附录 B 中的一个小样本问题。
JMP 中的 Mahalanobis 图
D中号其本身对我们来说没有什么用处。 它仅在与其他数据点相比时才有意义。 JMP 中的 Mahalanobis 图显示了这些结果。
我们可以通过转到分析/多元方法/多元,选择列,然后从红色下拉列表中选择离群值分析/马氏距离来生成马氏图。 对于图 1 的示例数据集,给出:
Figure 7: Mahalanobis distances for 1,000 data point sample (repeat of Figure 2)Figure 7: Mahalanobis distances for 1,000 data point sample (repeat of Figure 2)Figure 7: Mahalanobis distances for 1,000 data point sample (repeat of Figure 2)Figure 7: Mahalanobis distances for 1,000 data point sample (repeat of Figure 2)图 7:1,000 个数据点样本的马氏距离(重复图 2)
这里我们看到数据表第1行的数据点距离最大(D中号),表明它距离所有 10 个数据点的质心最远。 蓝线代表我们是否相信 D 的 95% 置信上限 (UCL)中号与零显着不同。 红点明显高于 UCL,因此需要将其作为异常值进行进一步调查。
但异常值并不总是那么容易检测到,尤其是对于小数据集。 在附录 B 的示例中,我展示了小数据集(三个维度上的 10 个数据点)的示例计算。 该数据集生成一个D中号情节如下图所示:
Figure 8: Mahalanobis distances for 10 data point sample in Appendix BFigure 8: Mahalanobis distances for 10 data point sample in Appendix BFigure 8: Mahalanobis distances for 10 data point sample in Appendix BFigure 8: Mahalanobis distances for 10 data point sample in Appendix B图 8:附录 B 中 10 个数据点样本的马氏距离
D.中号绘图已将点 1 识别为异常值。 但我们也看到D中号第 1 行与 D 没有什么不同中号对于第 7 行! 为什么是这样? 这个问题的答案还得等下一集才能揭晓!
关于 T 的快速说明2
联合管理计划还提供 T 选项2在分析/多变量方法/多变量下进行计算,然后进行离群值分析。 时间2就是 D 的平方中号。 有些人喜欢用它来强制异常值和大量数据之间有更多的视觉分离,但是 T 2确实包含与 D 相同的信息中号。
下一集
在下一集中,我们将介绍 Jackknife 距离,这是查看异常值的另一种方式,这将使上述示例数据集中的异常值脱颖而出。
查看本系列的所有帖子了解异常值。
附录 A:使用主成分来识别异常值
如果我们有办法旋转图 1 中的轴,使它们与数据趋势对齐,那就太好了,如下所示:
Figure 9: Conceptual rotation of axes for two-dimensional data setFigure 9: Conceptual rotation of axes for two-dimensional data setFigure 9: Conceptual rotation of axes for two-dimensional data setFigure 9: Conceptual rotation of axes for two-dimensional data set图 9:二维数据集轴的概念旋转
然后我们可以使用前面描述的技术来标准化数据以检测异常值。
实现此目的的一种方法是主成分分析 (PCA)。 PCA 找到最适合数据的正交(相互垂直)轴集。 您可以在数据集中拥有与原始维度(列)一样多的主成分。 在 JMP 中,PCA 可以在两个位置找到:在分析/多元方法/主成分下,或在分析/多元方法/多元下,然后从多元旁边的红色下拉列表中选择主成分/相关性。 红色下拉菜单还提供了将主成分保存回数据表的选项。 完成此操作后,您可以使用上述方法来校正上述平均值和标准差,以确定距质心的距离。
附录 B:D中号示例数据集的计算
假设我们有一个包含 3 列 10 行的示例数据集:
Figure 10: Small sample data set used for DM example calculationsFigure 10: Small sample data set used for DM example calculationsFigure 10: Small sample data set used for DM example calculationsFigure 10: Small sample data set used for DM example calculations图 10:用于 DM 示例计算的小样本数据集
在 JMP 中,我们可以对这三列运行分析/多元方法/多元来生成以下散点图矩阵:
Figure 11: Scatterplot matrix for example dataFigure 11: Scatterplot matrix for example dataFigure 11: Scatterplot matrix for example dataFigure 11: Scatterplot matrix for example data图 11:示例数据的散点图矩阵
在图 11 中,我们可以看到,在 X1-X2 维度中进行比较时,第 1 行数据(以红色突出显示)似乎是异常值,但在 X1-X3 或 X2-X3 图中并不那么清晰。
我们想要计算第一个点的马氏距离。 所以,填写上面D中的向量和矩阵中号等式中,我们从兴趣点开始:

每列的平均值可以通过多种不同的方式找到。 让我们使用 Cols/Column Viewer,然后显示摘要:
Figure 12: Summary statistics for example three-dimensional 10-point data setFigure 12: Summary statistics for example three-dimensional 10-point data setFigure 12: Summary statistics for example three-dimensional 10-point data setFigure 12: Summary statistics for example three-dimensional 10-point data set图 12:三维 10 点数据集示例的汇总统计
因此均值向量变为:

接下来,我们需要协方差矩阵,可以在“分析/多元方法”下找到该矩阵。 从红色下拉三角形中选择协方差矩阵:
Figure 13: Covariance matrix for example three-dimensional 10-point data setFigure 13: Covariance matrix for example three-dimensional 10-point data setFigure 13: Covariance matrix for example three-dimensional 10-point data setFigure 13: Covariance matrix for example three-dimensional 10-point data set图 13:三维 10 点数据集的协方差矩阵示例
我们还需要协方差矩阵的逆矩阵 S -1 。 JMP 不会从菜单系统向我们提供该信息,但我们可以从自定义脚本轻松获取该信息,如下所示:
Figure 14: JMP JSL script to find inverse of a matrixFigure 14: JMP JSL script to find inverse of a matrixFigure 14: JMP JSL script to find inverse of a matrixFigure 14: JMP JSL script to find inverse of a matrix图 14:用于查找矩阵逆的 JMP JSL 脚本
这导致:
S -1 =

然后我们只需插入并求解矩阵方程:





马氏距离 (D中号对于示例数据集第 1 行中显示的数据, ) 为 2.814。
Figure 15: Mahalanobis plot for example data set (repeat of Figure 8)Figure 15: Mahalanobis plot for example data set (repeat of Figure 8)Figure 15: Mahalanobis plot for example data set (repeat of Figure 8)Figure 15: Mahalanobis plot for example data set (repeat of Figure 8)图 15:示例数据集的 Mahalanobis 图(重复图 8)