gpt4 book ai didi

python - 在 Python 中计算累积分布函数 (CDF)

转载 作者:太空狗 更新时间:2023-10-29 17:55:27 30 4
gpt4 key购买 nike

我如何在 python 中计算 Cumulative Distribution Function (CDF)

我想根据我拥有的点数组(离散分布)计算它,而不是使用连续分布,例如 scipy。

最佳答案

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

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

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

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 = plt.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$')

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

cumulative distribution function

这个函数很容易反转,具体要看你的应用需要哪种形式。

关于python - 在 Python 中计算累积分布函数 (CDF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24788200/

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