本文介绍了没有步进函数的python中的ECDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 statsmodels.distributions 中的 ECDF(经验累积分布函数)来绘制一些数据的 CDF.但是,ECDF 使用阶跃函数,因此我得到了锯齿状的图.

所以我的问题是:scipy 或 statsmodels 是否有没有阶跃函数的 ECDF 烘焙?

顺便说一下,我知道我可以做到这一点:

hist, bin_edges = histogram(b_oz, normed=True)情节(np.cumsum(历史))

但我没有得到正确的音阶.

谢谢!

解决方案

如果你只是想改变情节,那么你可以让 matplotlib 在观察值之间进行插值.

>>>xx = np.random.randn(nobs)>>>ecdf = sm.distributions.ECDF(xx)>>>plt.plot(ecdf.x, ecdf.y)[]>>>plt.show()

或对原始数据进行排序并绘图

>>>xx.sort()>>>plt.plot(xx, ecdf(xx))[]>>>plt.show()

这和直接绘制一样

>>>a=0;plt.plot(xx, np.arange(1.,nobs+1)/(nobs+a))[]>>>plt.show()

注意:根据您希望 ecdf 在边界处的行为方式以及它的居中方式,常用的绘制位置"有不同的归一化,例如参数 a我作为例子添加的 a=1 是一个常见的选择.

作为使用经验 cdf 的替代方法,您还可以使用内插或平滑的 ecdf 或直方图,或核密度估计.

I have been using ECDF (empirical cumulative distribution function) from statsmodels.distributions to plot a CDF of some data. However, ECDF uses a step function and as a consequence I get jagged-looking plots.

So my question is: Do scipy or statsmodels have a ECDF baked-in without a step function?

By the way, I know I can do this:

hist, bin_edges = histogram(b_oz, normed=True)
plot(np.cumsum(hist))

but I don't get the right scales.

Thanks!

解决方案

If you just want to change the plot, then you could let matplotlib interpolate between the observed values.

>>> xx = np.random.randn(nobs)
>>> ecdf = sm.distributions.ECDF(xx)
>>> plt.plot(ecdf.x, ecdf.y)
[<matplotlib.lines.Line2D object at 0x07A872D0>]
>>> plt.show()

or sort original data and plot

>>> xx.sort()
>>> plt.plot(xx, ecdf(xx))
[<matplotlib.lines.Line2D object at 0x07A87090>]
>>> plt.show()

which is the same as plotting it directly

>>> a=0; plt.plot(xx, np.arange(1.,nobs+1)/(nobs+a))
[<matplotlib.lines.Line2D object at 0x07A87D30>]
>>> plt.show()

Note: depending on how you want the ecdf to behave at the boundaries and how it will be centered, there are different normalizations for "plotting positions" that are in common use, like the parameter a that I added as example a=1 is a common choice.

As alternative to using the empirical cdf, you could also use an interpolated or smoothed ecdf or histogram, or a kernel density estimate.

这篇关于没有步进函数的python中的ECDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:49