import numpy as np
import scipy
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.randn(10000) # generate samples from normal distribution (discrete data)
norm_cdf = scipy.stats.norm.cdf(x) # calculate the cdf - also discrete
# plot the cdf
sns.lineplot(x=x, y=norm_cdf)
plt.show()
假设您知道数据是如何分布的(即您知道数据的pdf),那么scipy在计算cdf时支持离散数据
我们甚至可以打印cdf的前几个值来显示它们是离散的
同样的计算cdf的方法也适用于多维:我们使用下面的二维数据来说明
在上面的例子中,我事先知道我的数据是正态分布的,这就是我使用
scipy.stats.norm()
的原因-scipy支持多个分布。但同样,您需要事先知道数据是如何分布的才能使用这些函数。如果您不知道数据是如何分布的,而只是使用任何分布来计算cdf,那么您很可能会得到不正确的结果。(有可能我对这个问题的解释是错误的。如果问题是如何从离散PDF获取离散CDF,那么
np.cumsum
除以适当的常数就可以了,如果样本是等距的。如果数组不是等距的,那么数组的np.cumsum
乘以点之间的距离就可以了。)如果您有一个离散的样本数组,并且您想知道该样本的CDF,那么您可以对该数组进行排序。如果您查看排序结果,您会发现最小值表示0%,最大值表示100%。如果您想知道分布的50%的值,只需查看位于排序数组中间的array元素。
让我们用一个简单的例子来详细了解一下:
这给出了下面的图,其中右边的图是传统的累积分布函数。它应该反映点后面的过程的CDF,但自然不是只要点的数量是有限的。
这个函数很容易反转,这取决于你的申请表你需要。
相关问题 更多 >
编程相关推荐