Title: Inverse Transformation Sampling Slug: inverse-cdf-sampling Summary: Demonstrating the inverse transformation sampling method in Python Date: 2018-01-22 10:47 Category: Statistics Tags: Bayesian Authors: Thomas Pinder

Inverse CDF Transformation

If you know the CDF of a distribution and the particular CDF is invertible, then random numbers can be simulated from the distribution governed by the respective CDF through the following steps:

  1. Set the CDF $F(x)=y$
  2. Invert the CDF with respect to y, $F^{-1}(y)=x$
  3. Generate samples from $u \sim U(0,1)$
  4. Calculate $F^{-1}(u)$ to get PDF samples

For example, the exponential distribution's CDF is $$1-\exp(\lambda x)$$ setting this equal to y and inverting gives $$-\frac{1}{\lambda}\times \log(x)$$.

Simulating samples from the standard uniform distribution will now result in exponential random variables.


In [20]:
import scipy.stats
import numpy as np
import seaborn as sns
%matplotlib inline

def inverse_exponential(x, rate):
    return -np.log(x)/rate

# Simulate and sample
uniform_samples = scipy.stats.uniform.rvs(size = 10000)
exp_rvs = inverse_exponential(uniform_samples, 0.4)

# Ground truth
exp_pdf = scipy.stats.expon.rvs(size = 10000, scale = 1/0.4)

# Visualise
fig, (ax1, ax2) = plt.subplots(ncols=2);
fig.set_size_inches(14.7, 4.27);
sns.kdeplot(exp_rvs, ax =ax1).set_title('Simulations')
sns.kdeplot(exp_pdf, ax=ax2).set_title('Ground Truth')


Out[20]:
Text(0.5,1,'Ground Truth')

In [2]: