gpt4 book ai didi

python - KS 测试的非标准分布变量?

转载 作者:太空宇宙 更新时间:2023-11-04 05:24:59 24 4
gpt4 key购买 nike

您能否将 scipy.stats 中的 kstest 用于非标准分布函数(即改变 Students t 的 DOF,或改变 Cauchy 的 gamma)?我的最终目标是为我的分布拟合找到最大 p 值和相应的参数,但这不是问题所在。

编辑:

scipy.stat 的柯西 pdf 是:

cauchy.pdf(x) = 1 / (pi * (1 + x**2))

对于位置参数和 Gamma ,它意味着 x_0 = 0Y = 1。我实际上需要它看起来像这样

cauchy.pdf(x, x_0, Y) = Y**2 / [(Y * pi) * ((x - x_0)**2 + Y**2)]

Q1) Students t, 至少, 可以像这样使用吗

stuff = []
for dof in xrange(0,100):
d, p, dof = scipy.stats.kstest(data, "t", args = (dof, ))
stuff.append(np.hstack((d, p, dof)))

因为它似乎可以选择改变参数?

问题 2)如果您需要完整的正态分布方程(需要改变 sigma)和上面写的 Cauchy(需要改变 gamma),您会怎么做? 编辑:不是在 scipy.stats 中搜索非标准分布,实际上是否可以将我编写的函数提供给 kstest 以找到 p 值?

谢谢

最佳答案

看来您真正想做的是参数估计。以这种方式使用 KT 检验并不是真正的目的。您应该为 corresponding distribution 使用 .fit 方法.

>>> import numpy as np, scipy.stats as stats
>>> arr = stats.norm.rvs(loc=10, scale=3, size=10) # generate 10 random samples from a normal distribution
>>> arr
array([ 11.54239861, 15.76348509, 12.65427353, 13.32551871,
10.5756376 , 7.98128118, 14.39058752, 15.08548683,
9.21976924, 13.1020294 ])
>>> stats.norm.fit(arr)
(12.364046769964004, 2.3998164726918607)
>>> stats.cauchy.fit(arr)
(12.921113834451496, 1.5012714431045815)

现在快速查看文档:

>>> help(cauchy.fit)

Help on method fit in module scipy.stats._distn_infrastructure:

fit(data, *args, **kwds) method of scipy.stats._continuous_distns.cauchy_gen instance
Return MLEs for shape, location, and scale parameters from data.

MLE stands for Maximum Likelihood Estimate. Starting estimates for
the fit are given by input arguments; for any arguments not provided
with starting estimates, ``self._fitstart(data)`` is called to generate
such.

One can hold some parameters fixed to specific values by passing in
keyword arguments ``f0``, ``f1``, ..., ``fn`` (for shape parameters)
and ``floc`` and ``fscale`` (for location and scale parameters,
respectively).

...

Returns
-------
shape, loc, scale : tuple of floats
MLEs for any shape statistics, followed by those for location and
scale.

Notes
-----
This fit is computed by maximizing a log-likelihood function, with
penalty applied for samples outside of range of the distribution. The
returned answer is not guaranteed to be the globally optimal MLE, it
may only be locally optimal, or the optimization may fail altogether.

所以,假设我想保持其中一个参数不变,您可以轻松做到:

>>> stats.cauchy.fit(arr, floc=10)
(10, 2.4905786982353786)
>>> stats.norm.fit(arr, floc=10)
(10, 3.3686549590571668)

关于python - KS 测试的非标准分布变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39132537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com