Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

How do you generate a random variable that has a specified distribution? The answer is quite remarkable and is used by computational systems to generate values of random variables whose distribution you have specified.

Our goal is to generate a value of a random variable that has a particular distribution. Let FF denote the cdf of that distribution. We will construct a process to acheive our goal in the special case where the cdf FF of the specified distribution is continuous and strictly increasing on the real number line. However, if you are more careful with the proof you can show that the result is true for any cdf FF.

The statement below describes the process. Note that because we have assumed FF is continuous and strictly increasing, it has an inverse function.

  • Generate UU, a uniform (0,1)(0, 1) random variable.

  • Create a random variable XX by the formula X=F1(U)X = F^{-1}(U).

Done! The random variable XX will have your specified cdf FF.

To prove the result, remember that the cdf FUF_U of UU is given by FU(u)=uF_U(u) = u for 0<u<10 < u < 1. Let xx be any number. Our goal is to show that FX(x)=F(x)F_X(x) = F(x).

FX(x) = P(Xx)= P(F1(U)x)= P(UF(x))    because F is increasing= FU(F(x))= F(x)\begin{align*} F_X(x) ~ &= ~ P(X \le x) \\ &= ~ P(F^{-1}(U) \le x) \\ &= ~ P(U \le F(x)) ~~~~ \mbox{because } F \mbox{ is increasing} \\ &= ~ F_U(F(x)) \\ &= ~ F(x) \end{align*}

This is an extremely important result for computation as well as theory. It says that computing systems should pay great attention to the quality of their uniform (0,1)(0, 1) random number generator. After that, all they have to do is apply the deterministic (that is, non-random) function F1F^{-1}, and they will have generated a random variable that has cdf FF.

For example, here is a way to generate a normal (μ,σ2)(\mu, \sigma^2) random variable.

  • Start with UU that has the uniform (0,1)(0, 1) distribution.

  • By our result above, Z=Φ1(U)Z = \Phi^{-1}(U) has the standard normal distribution.

  • By linear transformation facts, X=σZ+μX = \sigma Z + \mu has the normal (μ,σ2)(\mu, \sigma^2) distribution.

Here is the plan in action, in the case μ=10\mu = 10 and σ2=25\sigma^2 = 25. Note that stats.uniform.rvs generates uniform (0,1)(0, 1) random numbers, known as random variates. The argument size specifies how many numbers to generate.

uniforms = stats.uniform.rvs(size=10000)
standard_normals = stats.norm.ppf(uniforms)
normal_10_25 = 5*standard_normals + 10

results = Table().with_columns(
    'Uniform (0, 1)', uniforms,
    'Standard Normal', standard_normals,
    'Normal (10, 25)', normal_10_25
)
results.hist('Uniform (0, 1)', bins=20)
<Figure size 432x288 with 1 Axes>
results.hist('Standard Normal', bins=20)
<Figure size 432x288 with 1 Axes>
results.hist('Normal (10, 25)', bins=20)
<Figure size 432x288 with 1 Axes>

You can repeat the process for any distribution you want to simulate.

For example, to generate random variables whose density is f(r)=2rf(r) = 2r for r(0,1)r \in (0, 1), first find the corresponding cdf by integrating:

  • F(x)=x2F(x) = x^2 for x(0,1)x \in (0, 1)

Then find the inverse of FF. Remember that F(x)=x2F(x) = x^2 is a continuous increasing function on (0,1)(0, 1), though it is two-to-one on the whole real line.

  • For y(0,1)y \in (0, 1), F1(y)=yF^{-1}(y) = \sqrt{y}

Then apply F1F^{-1} to a uniform (0,1)(0, 1) random variable UU:

  • X=UX = \sqrt{U} has the specified density ff.

Here is the plan in action, using the uniform random numbers generated above.

results = results.with_columns(
    'Density f(r)=2r on (0, 1)', uniforms ** 0.5

)
results.hist('Density f(r)=2r on (0, 1)', bins=20)
<Figure size 432x288 with 1 Axes>