【问题标题】:Why is this subclass of `scipy.stats.rv_continuous` not generating random variates in the correct range?为什么 scipy.stats.rv_continuous 的这个子类没有在正确的范围内生成随机变量?
【发布时间】:2019-03-19 01:23:35
【问题描述】:

我有以下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)(连同 PDF)生成的 50 个点:

下面是distribution.rvs(0.6, 0.4, 4.85) 的示例:

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

【问题讨论】:

    标签: python random scipy


    【解决方案1】:

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

    【讨论】:

    • 这很奇怪,我复制公式时一定搞砸了。我从论文到 wolfram alpha 验证了 CDF 渐近接近 1。我再看看我是怎么写的。感谢您的帮助!
    • 我建议的更正不正确!我删除了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 2021-12-29
    • 2018-04-08
    • 1970-01-01
    相关资源
    最近更新 更多