【问题标题】:How to implement kolmogorov-smirnov test to extract the best distributions suitable to my data?如何实施 kolmogorov-smirnov 测试以提取适合我的数据的最佳分布?
【发布时间】:2019-06-21 11:27:49
【问题描述】:

我找到了关于如何enter link description here 的说明 我需要根据 Kolmogorov-Smirnov 检验在我的样本和每个分布之间比较我的分布以使其适合。但我不知道如何解释结果并根据此测试选择最佳分布? 此代码未实现 Kolmogorov-Smirnov 测试。所以 1 - 如何实现 kolmogorov-smirnov 测试? 2 - 如何选择最佳分布?

def best_fit_distribution(data, bins=200, ax=None):
    """Model data by finding best fit distribution to data"""
    # Get histogram of original data
    y, x = np.histogram(data, bins=bins, density=True)
    x = (x + np.roll(x, -1))[:-1] / 2.0

    # Distributions to check
    DISTRIBUTIONS = [st.alpha, st.anglit]

    # Best holders
    best_distribution = st.norm
    best_params = (0.0, 1.0)
    best_sse = np.inf

    runs = []
    # Estimate distribution parameters from data
    for distribution in DISTRIBUTIONS:

        # Try to fit the distribution
        try:
            # Ignore warnings from data that can't be fit
            with warnings.catch_warnings():
                warnings.filterwarnings('ignore')

                # fit dist to data
                params = distribution.fit(data)
                print(params)
                # Separate parts of parameters
                arg = params[:-2]
                print(arg)
                loc = params[-2]
                print(loc)
                scale = params[-1]
                print(scale)

                # Calculate fitted PDF and error with fit in distribution
                pdf = distribution.pdf(x, loc=loc, scale=scale, *arg)
                sse = np.sum(np.power(y - pdf, 2.0))

                # if axis pass in add to plot
                try:
                    if ax:
                        pd.Series(pdf, x).plot(ax=ax)
                    end
                except Exception:
                    pass

                runs.append([distribution.name, sse])
                # identify if this distribution is better
                if best_sse > sse > 0:
                    best_distribution = distribution
                    best_params = params
                    best_sse = sse

        except Exception:
            pass
    print(runs)
    return (best_distribution.name, best_params)

【问题讨论】:

  • 首先,非常感谢您的帮助。其次,我在这里找到了代码stackoverflow.com/questions/6620471/…,因为我关注的是同一个论坛。另外,我需要用 python 计算这个测试
  • @pjs 我编辑我的问题。我很抱歉。我不小心。

标签: python machine-learning scipy statistics distribution


【解决方案1】:

首先,让我注意到您提供的源代码的 sn-p 不包括 Kolmogorov-Smirnov 测试,而是进行参数 MLE 估计,然后计算平方误差总和以选择最佳拟合.

为了回答您的第一个问题,让我举一个 scipy.stats 中正态分布的 Kolmogorov-Smirnov 拟合优度检验示例:

stats.kstest(samples, 'norm', args=(0, 1))

在哪里

  • 样本 - 收集/观察到的实验数据
  • 'norm' - 理论连续分布的预定义名称
  • args - 理论分布的参数,在示例中 mean=0 和 std=1

因此,要对其他分布进行测试,只需按照与上例中的正态分布相同的方式迭代所需的理论分布名称及其参数。

stats.kstest 函数返回两个值:

  • D - 一个 K-S 统计数据
  • p 值 - 原假设的 p 值,即 样本来自提供的理论分布

因此,要回答您的第二个问题,如果 p 值小于您的显着性值,您应该拒绝测试。如果不能拒绝原假设,那么您可以比较 D 值并选择 D 值最小的分布,因为它表示拟合优度:D 值越小,它与数据的拟合越好。

【讨论】:

  • 非常感谢,此代码不包括 Kolmogorov-Smirnov 测试。我使用此代码作为参考,因为它包含数据集并且很容易更改。
猜你喜欢
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
  • 1970-01-01
  • 2019-03-26
  • 2018-07-11
  • 2018-03-10
相关资源
最近更新 更多