使用 For 循环重写 numpy.random.binomial

Rewriting numpy.random.binomial using a For loop

目前我有以下代码

def approx_binomial(n, p, size=None):
    gaussian = np.random.normal(n*p, n*p*(1-p), size=size)
    # Add the continuity correction to sample at the midpoint of each integral bin.
    gaussian += 0.5
    if size is not None:
        binomial = gaussian.astype(np.int64)
    else:
        # scalar
        binomial = int(gaussian)
    return binomial

但是,由于使用了随机函数,所以不是很准确。有没有其他方法可以使用 for 循环重写函数?

我的另一个问题是如何显示概率质量函数与成功次数的关系图?

def approx_binomial(n, p, size=None):
    gaussian = np.random.normal(n*p, n*p*(1-p), size=size)
    # Add the continuity correction to sample at the midpoint of each integral bin.
    gaussian += 0.5
    if size is not None:
        binomial = gaussian.astype(np.int64)
    else:
        # scalar
        binomial = int(gaussian)
    return binomial
plt.plot(n,p)
plt.show()

谢谢!

解决你的任务的方法可以在Wikipedia上找到,但它是一个非常小的部分:

One way to generate random samples from a binomial distribution is to use an inversion algorithm. To do so, one must calculate the probability that P(X=k) for all values k from 0 through n. (These probabilities should sum to a value close to one, in order to encompass the entire sample space.) Then by using a linear congruential generator to generate samples uniform between 0 and 1, one can transform the calculated samples U[0,1] into discrete numbers by using the probabilities calculated in step one.

以上有点冗长。简而言之,您必须执行以下操作才能从二项式分布中生成一个样本:使用概率质量函数 p=pmf(n, k) 计算每个可能 k 的二项式概率,并随机 select k 中的一个基于它们的概率

  1. 计算每个 k = [0..n] 的二项分布 cdf(n, k) 的累积密度函数。
  2. 生成0到1之间的均匀分布的随机数r
  3. 找到 k 的值 cdf(n, k) <= r

使用 for 循环从二项分布中获取随机样本的明显方法是:

import random

def binomial_random_sample(n, p):
    ret = 0
    for j in range(n):
        if random.random() < p:
            ret += 1
    return ret

或者,如果您更喜欢更简洁的方法:

def binomial_random_sample(n, p):
    return sum(random.random() < p for j in range(n))