为什么`scipy.stats.rv_continuous`的子类不能在正确的范围内生成随机变量?

问题描述 投票:2回答:1

我有以下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子类错了吗?

python random scipy
1个回答
4
投票

您对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。

© www.soinside.com 2019 - 2024. All rights reserved.