- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试评估/测试我的数据对特定分布的拟合程度。
关于它有几个问题,有人告诉我使用 scipy.stats.kstest
或 scipy.stats.ks_2samp
。看起来很简单,给它:(A)数据; (2) 分配; (3) 拟合参数。唯一的问题是我的结果没有任何意义?我想测试我的数据的“优点”,它适合不同的分布,但是从 kstest
的输出来看,我不知道我是否可以这样做?
Goodness of fit tests in SciPy
"[SciPy] contains K-S"
Using Scipy's stats.kstest module for goodness-of-fit testing 说
"first value is the test statistics, and second value is the p-value. if the p-value is less than 95 (for a level of significance of 5%), this means that you cannot reject the Null-Hypothese that the two sample distributions are identical."
这只是展示如何适应: Fitting distributions, goodness of fit, p-value. Is it possible to do this with Scipy (Python)?
np.random.seed(2)
# Sample from a normal distribution w/ mu: -50 and sigma=1
x = np.random.normal(loc=-50, scale=1, size=100)
x
#array([-50.41675785, -50.05626683, -52.1361961 , -48.35972919,
# -51.79343559, -50.84174737, -49.49711858, -51.24528809,
# -51.05795222, -50.90900761, -49.44854596, -47.70779199,
# ...
# -50.46200535, -49.64911151, -49.61813377, -49.43372456,
# -49.79579202, -48.59330376, -51.7379595 , -48.95917605,
# -49.61952803, -50.21713527, -48.8264685 , -52.34360319])
# Try against a Gamma Distribution
distribution = "gamma"
distr = getattr(stats, distribution)
params = distr.fit(x)
stats.kstest(x,distribution,args=params)
KstestResult(statistic=0.078494356486987549, pvalue=0.55408436218441004)
pvalue=0.55408436218441004
的 p_value 是说 normal
和 gamma
采样来自相同的分布?
我认为 Gamma 分布必须包含正值? https://en.wikipedia.org/wiki/Gamma_distribution
现在针对正态分布:
# Try against a Normal Distribution
distribution = "norm"
distr = getattr(stats, distribution)
params = distr.fit(x)
stats.kstest(x,distribution,args=params)
KstestResult(statistic=0.070447707170256002, pvalue=0.70801104133244541)
据此,如果我取最低的 p_value,那么我会得出结论,我的数据来自 gamma
分布,即使它们都是负值?
np.random.seed(0)
distr = getattr(stats, "norm")
x = distr.rvs(loc=0, scale=1, size=50)
params = distr.fit(x)
stats.kstest(x,"norm",args=params, N=1000)
KstestResult(statistic=0.058435890774587329, pvalue=0.99558592119926814)
这意味着在 5% 的显着性水平下,我可以拒绝分布相同的原假设。所以我得出结论,它们是不同的,但它们显然不是?我对此的解释不正确吗?如果我使它成为单尾的,那么值越大,它们来自同一分布的可能性就越大吗?
最佳答案
因此,KT 检验的原假设是分布相同。因此,您的 p 值越低,您拒绝原假设和断定分布不同的统计证据就越多。该测试只能真正让您表达对分布不同而非相同的信心,因为该测试旨在找到 alpha,即 I 类错误的概率。
此外,我非常确定 KT 检验仅在您事先考虑了完全指定的分布时才有效。在这里,您只需拟合一些数据的 Gamma 分布,因此当然,测试产生高 p 值也就不足为奇了(即您不能拒绝分布相同的原假设)。
很快,这里是你拟合的 Gamma(蓝色)的 pdf 与你从中采样的正态分布的 pdf(绿色):In [13]: paramsd = dict(zip(('shape','loc','scale'),params))
In [14]: a = paramsd['shape']
In [15]: del paramsd['shape']
In [16]: paramsd
Out[16]: {'loc': -71.588039241913037, 'scale': 0.051114096301755507}
In [17]: X = np.linspace(-55, -45, 100)
In [18]: plt.plot(X, stats.gamma.pdf(X,a,**paramsd))
Out[18]: [<matplotlib.lines.Line2D at 0x7ff820f21d68>]
很明显,它们并没有太大区别。实际上,该测试比较了经验 CDF (ECDF) 与候选分布的 CDF(同样,您通过将数据拟合到该分布而得出),并且测试统计量是最大差异。借用 ECDF 的实现 from here ,我们可以看到任何这样的最大差异都会很小,并且检验显然不会拒绝原假设:
In [32]: def ecdf(x):
.....: xs = np.sort(x)
.....: ys = np.arange(1, len(xs)+1)/float(len(xs))
.....: return xs, ys
.....:
In [33]: plt.plot(X, stats.gamma.cdf(X,a,**paramsd))
Out[33]: [<matplotlib.lines.Line2D at 0x7ff805223a20>]
In [34]: plt.plot(*ecdf(x))
Out[34]: [<matplotlib.lines.Line2D at 0x7ff80524c208>]
关于python - 如何解释 `scipy.stats.kstest` 和 `ks_2samp` 以评估 `fit` 的数据分布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39132469/
我在一个项目中工作,该项目需要 SQL 结果的最佳性能,并且希望优化查询,但经过反复试验后,我在 IN 方面遇到了一些问题。 -- THIS RETURNS NO RESULTS AT ALL. SE
在尝试创建一个实际上非常简单的 SQL 语句时,我发现自己迷失了方向。 我有一个包含 3 个表的数据库: 食谱 - 存储一些用于 cooking 的食谱名称 配料食谱 - 将配料与食谱链接 成分 -
我正在尝试理解 PHP 中的 Hebrev 函数。 https://php.net/manual/en/function.hebrevc.php 它说:“将逻辑希伯来语文本转换为视觉文本”。但我不明白
嗨,我在 Grid view 的 android 文档中发现了一段代码对于以下代码。 gridview.setOnItemClickListener(new OnItemClickListener()
谁能解释一下 InfiniBand 是什么?与以太网相比的主要区别是什么,这些差异如何使其比以太网更快? 在官方description从 mellanox 写到 Introduce InfiniBan
这个问题已经有答案了: How are java increment statements evaluated in complex expressions (1 个回答) 已关闭 8 年前。 我知道
我正在阅读 MySQL 教程,我遇到了这个: SELECT /*! SQL_NO_CACHE */ user FROM users; 为什么优化提示 SQL_NO_CACHE 包含在: /*!
我无法理解$(this),我做了一个剪刀石头布的版本,并应用了 jQuery 让用户在计算机上选择按钮选项。我希望有人能解释一下 $(this) 指的是什么,它是 btn-primary 吗?该函数在
我不是很确定 while(choice == 1 || choice ==2);谁能解释一下。我明白这一点 if(choice ==1) displayMonthly(rainfall); e
let flyRight = CABasicAnimation(keyPath: "position.x") flyRight.toValue = view.bounds.size.width/2 f
目录 解释:int型默认值为0 但我们尝试发现并不能通过: 原因: int的默认值为0,而Integer的默认值为null
我正在处理一个查询,自从一个 SSRS 服务器传输到另一个服务器后,它似乎没有按预期执行,并且 where 语句的一部分中出现了以下行 找出不同之处,或者至少从我能找到的地方来看。 where COA
我正在制作一个退回检测程序,读取退回邮件。我们的设置是发送电子邮件,在发送的邮件中添加一个 noreply@domain.tl。一些收件人不再存在,因此我们想要读取退回邮件,并检测它发送给谁。我已经崩
我有一个关于公式通过控制点弯曲的问题。 如您所知,HTML Canvas 有 quadraticCurveTo(x1, y1, x2, y2)与 x1 and x2作为控制点。 但是,当您尝试使用它绘
我有一个 Emakefile看起来像: %% -- %% %% -- {'/Users/user/projects/custom_test/trunk/*', [debug_info, {out
我有一个非常简单的问题。这不仅适用于 spray-json,而且我已经阅读了 argonaut 和 circe 的类似声明。所以请赐教。 在 spray-json 中,我遇到了 There is no
我正在为视频添加水印。我试图让水印与视频尺寸成比例。我已经使用 scale2ref 看到了十几个不同的答案,但没有解释实际发生了什么,所以我发现很难知道如何实现/更改配置以适应我的情况。 当前覆盖命令
因为我正在学习语言,所以我在玩 Haskell,我只是发现了一些我不理解的东西,我找不到解释。如果我尝试运行此代码: map (`div` 0) [1,2,3,4] 我得到一个除以 0 的异常,这是预
我正在寻找解决错误对象引用未设置到对象实例的步骤/指南。以及问题发生原因的解释。 我正在寻找更一般的解释,所以如果我收到错误,我应该采取什么步骤来查找问题。我经常看到有人提供特定代码段的帖子,而其他人
我最近想升级我的知识React ,所以我从组件生命周期方法开始。让我好奇的第一件事是这个componentWillReceiveProps .所以,文档说当组件接收新的(不一定是更新的) Prop 时
我是一名优秀的程序员,十分优秀!