如何在Python中实现KSTest

2024-07-03 05:49:51 发布

您现在位置:Python中文网/ 问答频道 /正文

scipy.stats.kstest(rvs, cdf, N)可以对数据集rvs执行KS测试。它测试数据集是否遵循概率分布,其cdf在该方法的参数中指定。在

现在考虑一个N=4800样本的数据集。我已经对这些数据进行了KDE,因此,有一个估计的PDF。这个PDF看起来非常像双峰分布。当绘制估计的PDF和拟合双峰分布的曲线时,这两个图几乎完全相同。拟合双峰分布的参数为(标度1、均值1、标准差1、标度2、均值2、标准差2): [0.6 0.036 0.52, 0.23 1.25 0.4]

我如何应用scipy.stats.kstest来测试我估计的PDF是否是双峰分布的? 作为无效假设,我声明估算的PDF等于以下PDF:

hypoDist = 0.6*norm(loc=0, scale=0.2).pdf(x_grid) + 0.3*norm(loc=1, scale=0.2).pdf(x_grid)
hypoCdf = np.cumsum(hypoDist)/len(x_grid)

x_grid只是一个包含x值的向量,在这个值上我可以计算估计的PDF值。因此pdf的每个条目都有一个对应的值x_grid。可能是我对hypoCdf的计算不正确。也许我应该用len(x_grid)除以np.sum(hypoDist),而不是除以len(x_grid)?在

挑战:cdfcdf参数不能指定为双峰。我也不能将其指定为hypoDist。在

如果我想测试数据集是否是高斯分布的,我会写:

^{pr2}$

measurementError是我在其上执行KDE的数据集。这将返回: statistic=0.459, pvalue=0.0 对我来说,pvalue是0.0有点恼火


Tags: 数据参数lenpdfstatsscipygrid测试数据
1条回答
网友
1楼 · 发布于 2024-07-03 05:49:51

kstestcdf参数可以是一个可调用的,它实现了要根据其测试数据的分布的累积分布函数。要使用它,您必须实现双峰分布的CDF。你希望这个分布是两个正态分布的混合。您可以通过计算组成混合的两个正态分布的CDF的加权和来实现这个分布的CDF。在

下面是一个脚本,它展示了如何做到这一点。为了演示如何使用kstest,脚本运行kstest两次。首先,它使用一个来自分布的而不是的样本。正如预期的那样,kstest为第一个示例计算一个非常小的p值。然后从混合物中提取样本。对于这个样本,p值不小。在

import numpy as np
from scipy import stats


def bimodal_cdf(x, weight1, mean1, stdv1, mean2, stdv2):
    """
    CDF of a mixture of two normal distributions.
    """
    return (weight1*stats.norm.cdf(x, mean1, stdv1) +
            (1 - weight1)*stats.norm.cdf(x, mean2, stdv2))


# We only need weight1, since weight2 = 1 - weight1.
weight1 = 0.6
mean1 = 0.036
stdv1 = 0.52
mean2 = 1.25
stdv2 = 0.4

n = 200

# Create a sample from a regular normal distribution that has parameters
# similar to the bimodal distribution.
sample1 = stats.norm.rvs(0.5*(mean1 + mean2), 0.5, size=n)

# The result of kstest should show that sample1 is not from the bimodal
# distribution (i.e. the p-value should be very small).
stat1, pvalue1 = stats.kstest(sample1, cdf=bimodal_cdf,
                              args=(weight1, mean1, stdv2, mean2, stdv2))
print("sample1 p-value =", pvalue1)

# Create a sample from the bimodal distribution.  This sample is the
# concatenation of samples from the two normal distributions that make
# up the bimodal distribution.  The number of samples to take from the
# first distributions is determined by a binomial distribution of n
# samples with probability weight1.
n1 = np.random.binomial(n, p=weight1)
sample2 = np.concatenate((stats.norm.rvs(mean1, stdv1, size=n1),
                         (stats.norm.rvs(mean2, stdv2, size=n - n1))))

# Most of time, the p-value returned by kstest with sample2 will not
# be small.  We expect the value to be uniformly distributed in the interval
# [0, 1], so in general it will not be very small.
stat2, pvalue2 = stats.kstest(sample2, cdf=bimodal_cdf,
                              args=(weight1, mean1, stdv1, mean2, stdv2))
print("sample2 p-value =", pvalue2)

典型输出(每次运行脚本时数字都不同):

^{pr2}$

你可能会发现这个测试没用。您有4800个示例,但在代码中,您的参数的数值只有一到两个有效数字。除非你有充分的理由相信你的样本是从一个分布中提取的,而这个分布恰好带有这些参数,否则kstest很可能会返回一个非常小的p值。在

相关问题 更多 >