gpt4 book ai didi

Python scipy - 指定自定义离散分布

转载 作者:太空狗 更新时间:2023-10-30 01:12:37 24 4
gpt4 key购买 nike

我使用 scipy.stats 中的各种连续分布(例如 norm)。所以如果我想找到 P(Z < 0.5) 我会这样做:

from scipy.stats import norm
norm(0, 1).cdf(0.5) # Z~N(0,1)

是否有一种工具(scipy.stats 或 statsmodels 或其他)可用于描述离散分布,然后计算 CDF/CMF 等?我可以自己编写代码,但我想知道是否存在某些东西,例如:

pdf(x) = 1/3 for x = 1,2,3; else 0

然后我可以构建 2 个向量 x=[1,2,3], p = [1/3, 1/3, 1/3] 并将它们输入到一个库类中,该库类将提供 .cdf() 等?

最佳答案

我猜你正在寻找 scipy.stats.rv_discrete 在这里。来自docs :

rv_discrete is a base class to construct specific distribution classes and instances for discrete random variables. It can also be used to construct an arbitrary distribution defined by a list of support points and corresponding probabilities.

来自文档的示例:

from scipy import stats
xk = np.arange(7)
pk = (0.1, 0.2, 0.3, 0.1, 0.1, 0.0, 0.2)
custm = stats.rv_discrete(name='custm', values=(xk, pk))

你的例子:

In [1]: import numpy as np

In [2]: from scipy import stats

In [3]: custm = stats.rv_discrete(name='custm', values=((1, 2, 3), (1./3, 1./3, 1./3)))

In [4]: custm.cdf(2.5)
Out[4]: 0.66666666666666663

关于Python scipy - 指定自定义离散分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41664533/

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