Python scipy指定自定义离散分布

2024-05-23 22:39:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用各种连续分布scipy.stats公司(例如标准)。因此,如果我想找到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()等?在


Tags: 工具代码fromimportltnorm标准stats
1条回答
网友
1楼 · 发布于 2024-05-23 22:39:30

我想你是在找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))

你的例子是:

^{pr2}$

相关问题 更多 >