Ways to generate normal distribution random variable

Wencao Yang
Nov 7, 2021

--

  1. directly use NumPy method
import numpy as nprv = np.random.normal(0,1)

2. use the central limit theorem

rv = (np.mean(np.random.uniform(0,1,n))-0.5)*np.sqrt(12*n)

3. use inverse-CDF

from scipy.stats import normiid = np.random.uniform(0,1,size)rv = norm.ppf(iid)

quick proof of inverse-CDF correctness [1]:

Let F be a continuous cumulative distribution function, and let F^-1 be its inverse function

Claim: If U is a uniform random variable on (0, 1) then F^-1(U) has F as its CDF.

Proof:

Pr(F^-1(U) < x)

= Pr(F(x) > U) (apply F both side)

= F(x) (because of uniform distribution)

4. Box-Muller method [2]

x = np.random.uniform(0,1)y = np.random.uniform(0,1)rv1 = np.cos(2*pi*x)*np.sqrt(-2*np.log(1-y))rv2 = np.sin(2*pi*x)*np.sqrt(-2*np.log(1-y))

The key of Box-Muller is still inverse-CDF, but we consider a joint distribution of x, y

References:

  1. https://en.wikipedia.org/wiki/Inverse_transform_sampling
  2. https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform

--

--

Wencao Yang
Wencao Yang

Written by Wencao Yang

Data Scientist & Physics PhD

No responses yet