温馨提示:本文翻译自stackoverflow.com,查看原文请点击:numpy - Calculate the Cumulative Distribution Function (CDF) in Python
machine-learning numpy python scipy statistics

numpy - 用Python计算累积分布函数(CDF)

发布于 2020-07-19 21:01:03

如何在python中计算累积分布函数(CDF)

我想从我拥有的点数组(离散分布)中进行计算,而不是从scipy具有的连续分布中进行计算。

查看更多

提问者
wizbcn
被浏览
654
5,075 2019-06-17 23:48

(我对这个问题的解释很可能是错误的。如果问题是如何从离散的PDF转换为离散的CDF,那么np.cumsum如果样本是等距的,则将其除以合适的常数即可。如果数组不是等距的,然后np.cumsum将数组乘以点之间的距离即可。)

如果您有一个离散的样本数组,并且想知道样本的CDF,则可以对数组进行排序。如果查看排序结果,您将意识到最小值代表0%,最大值代表100%。如果您想知道分布的50%处的值,只需查看排序数组中间的array元素即可。

让我们用一个简单的例子仔细看一下:

import matplotlib.pyplot as plt
import numpy as np

# create some randomly ddistributed data:
data = np.random.randn(10000)

# sort the data:
data_sorted = np.sort(data)

# calculate the proportional values of samples
p = 1. * np.arange(len(data)) / (len(data) - 1)

# plot the sorted data:
fig = figure()
ax1 = fig.add_subplot(121)
ax1.plot(p, data_sorted)
ax1.set_xlabel('$p$')
ax1.set_ylabel('$x$')

ax2 = fig.add_subplot(122)
ax2.plot(data_sorted, p)
ax2.set_xlabel('$x$')
ax2.set_ylabel('$p$')

这给出了以下图,其中右侧图是传统的累积分布函数。它应该反映出点背后的过程的CDF,但是自然地,只要点数是有限的,它就不是。

累积分布函数

此功能易于反转,并且取决于您的应用程序所需的形式。