Currently I have the following code
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 However, it is not very accurate as it uses the random function. Is there any other way in which I can rewrite the function using a for loop?
Another query I have is how to display a graph of the probability mass function against the number of successes?
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() Thanks!