【问题标题】:Python equivalent of R's rnbinom parametrized with mu用 mu 参数化 R 的 rnbinom 的 Python 等价物
【发布时间】:2016-08-16 04:23:42
【问题描述】:

R 有一个negative binomial function,可以用mu 参数化而不是概率(例如,浮点数 >= 0 和

rnbinom(1000,size=0.3,mu=15)

在 Python 中。据我所知,Numpy's negative binomial function 只允许概率。另外,我不清楚 Numpy 中的 size 参数是什么。

【问题讨论】:

    标签: python r numpy


    【解决方案1】:

    这是您传递给rnbinom的参数:

    In [131]: num_samples = 10000
    
    In [132]: size = 0.3
    
    In [133]: mu = 15
    

    如您链接到的 R 文档中所述,您可以按如下方式计算概率:

    In [134]: prob = size/(size + mu)
    

    numpy.random.negative_binomial 的前两个参数对应于 R 函数的 sizeprob 参数。 negative_binomial 的第三个参数是样本数。 (注意——numpy 调用这个参数size;它指的是要生成的样本的大小。所有的numpy 随机函数都采用size 参数。)

    In [135]: sample = np.random.negative_binomial(size, prob, num_samples)
    

    样本的平均值应该接近15。

    In [136]: sample.mean()
    Out[136]: 14.9032
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-20
      • 2020-09-14
      • 2018-07-18
      • 2016-06-14
      • 2016-05-23
      • 1970-01-01
      • 2022-10-13
      • 2014-04-21
      相关资源
      最近更新 更多