Given a mean and standard deviation generate random numbers based on a geometric distribution and binomial distribution

Viewed 42

I have some code to generate numbers simulating a normal distribution. Still, given a mean and standard deviation, my question is, how can I generate random numbers to resemble geometric and binomial distributions? I know numpy has these packages, but they use probability as arguments instead of mean and standard deviation.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(10, 20, 10000)

plt.hist(x, density=True, bins=30)  # density=False would make counts
plt.ylabel('Probability')
plt.xlabel('Data')
plt.show()

enter image description here

enter image description here enter image description here

1 Answers

You can generate normals with the same mean and standard deviation as a given geometric or binomial, but you generally can't go the other way. That's because all distributions are characterized by parameters, but for most distributions the mean and standard deviation are not the defining parameters, but rather they are functions of the parameters. Let's look at the two specific distributions named in your question. In both cases we'll treat variance and standard deviation as equivalent for your interests, since the latter is the square root of the former.

Given a geometric random variable with its single parameter p, the mean and variance are 1/p and (1-p)/p2, respectively. They can't be independent values chosen arbitrarily.

Similarly, a binomial random variable is fully determined by its parameters n (number of samples taken) and p (probability that a given sample is a "success"). Its mean and variance are n·p and n·p(1-p), respectively. Again, you can't just choose any old values because arbitrary choices may not yield feasible solutions of the requirements that integer n>0 and 0≤p≤1 produce the target values for mean and variance.

Going the other way is generally not a problem though. Given a binomial, geometric, exponential, Poisson,… with associated parameterization, you can calculate the corresponding mean and variance/standard-deviation and use those values to parameterize a normal. The exception would be some pathological distributions which don't have finite moments, such as the Cauchy distribution.

Related