生成零附近的二项式分布 [英] Generating a binomial distribution around zero

查看:102
本文介绍了生成零附近的二项式分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找生成二项式分布.我想要一个二项式分布,但我希望它以零为中心(我知道这对二项式分布的定义没有多大意义,但这仍然是我的目标.)

I'm looking to generate a binomial-esque distribution. I want a binomial distribution but I want it centred around zero (I know this doesn't make much sense with respect to the definition of binomial distributions but still, this is my goal.)

我发现在python中执行此操作的唯一方法是:

The only way I have found of doing this in python is:

def zeroed_binomial(n,p,size=None):
    return numpy.random.binomial(n,p,size) - n*p

此发行版是否有真实名称?这段代码是否真的给了我我想要的(以及如何告诉我)?有没有更清洁/更好/规范/已经实施的方法?

Is there a real name for this distribution? Does this code actually give me what I want (and how can I tell)? Is there a cleaner / nicer / canonical / already implemented way of doing this?

推荐答案

scipy.stats模块中实现的概率分布允许您通过在构造函数中指定loc关键字来任意移动分布.要获得均值接近于0的二项式分布,可以调用

The probability distributions implemented in the scipy.stats module allow you to shift distributions arbitrarily by specifying the loc keyword in the constructor. To get a binomial distribution with mean shifted close to 0, you can call

p = stats.binom(N, p, loc=-round(N*p))

(请确保对loc使用具有离散分布的整数值.)

(Be sure to use an integer value for loc with a discrete distribution.)

这是一个例子:

p = stats.binom(20, 0.1, loc=-2)
x = numpy.arange(-3,5)
bar(x, p.pmf(x))

要生成实际的随机数,请使用scipy.stats模块中每个随机分布随附的rvs()方法.例如:

To generate the actual random numbers, use the rvs() method which comes with every random distribution in the scipy.stats module. For example:

>>> stats.binom(20,0.1,loc=-2).rvs(10)
array([-2,  0,  0,  1,  1,  1, -1,  1,  2,  0]) 

这篇关于生成零附近的二项式分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆