gpt4 book ai didi

python - 为什么 `scipy.stats.rv_continuous` 的这个子类没有在正确的范围内生成随机变量?

转载 作者:行者123 更新时间:2023-11-28 22:12:04 25 4
gpt4 key购买 nike

我有以下 scipy.stats.rv_continuous 的子类:

from scipy.stats import rv_continuous
import math

class Distribution(rv_continuous):
def _cdf(self, x, a, b, mu):
return (
math.erf(x/(math.sqrt(2)*a)) + \
math.erf((x - mu)/(math.sqrt(2)*b)) \
) / 2 + math.erf(mu/(math.sqrt(2)*b)) / 2

distribution = Distribution(a = 0, b = float('inf'))

据我所知,一切都已正确设置(我已经检查了数学,它也是正确的)。但是,出于某种原因,它只想生成 0mu 之间的值,而不是预期的 0inf 作为明确指定。例如,这是使用 distribution.rvs(3, 1.6, 10) 生成的 50 个点(连同 PDF):

enter image description here

这是一个 distribution.rvs(0.6, 0.4, 4.85) 示例:

enter image description here

为什么我的分配“上限”为 mu?我是否设置了我的 rv_continuous 子类错误?

最佳答案

您对 CDF 的实现不正确。考虑:

In [188]: distribution.cdf(25, 3, 16., 10)
Out[188]: 1.059763759070757

In [189]: distribution.cdf(40, 3, 16., 10)
Out[189]: 1.203618109186038

这些值不正确。 CDF(您在 _cdf 方法中实现的)不得超过 1.0。

关于python - 为什么 `scipy.stats.rv_continuous` 的这个子类没有在正确的范围内生成随机变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55232371/

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