September 10, 2018

Inverse CDF Transformation

  • \(X\) random variable, PDF of \(X\) is \(f(x)\): \(X \sim p(x)\).
  • Cumulative density function CDF: \(F(x) = \int_{-\infty}^{x} p(s) ds\)
  • IF \(F\) is invertible, \(Y = F^{-1}(U)\) will have the same distribution as \(X\).
  • This is called Inverse CDF/ Quantile/ Probability-Integral transformation.

First Example:

  • PDF of a logistic random variable. \[ f(x \mid \lambda) = \frac{\lambda e^{-\lambda x}}{\{1+e^{-\lambda x} \}^2} \]
  • Hence the CDF can be written as: \[ F(x \mid \lambda) = \frac{1}{\{1+e^{-\lambda x} \}} \]
  • How do we generate samples from logistic?
  • Generate \(U \sim U[0,1]\), and apply \(F^{-1}(U)\) as above.

First Example Continued

set.seed(123)
logistic_rvgen <- function(lamb,n=1){
    # Recall the signature for runif(n,high,low)
    u=runif(n)
    x=-log((1/u)-1)/lamb
    return(x)
}

Visualize the samples

  • Let us generate 10000 random variables from the logistic distribution
lamb=0.1
n=10000
x=logistic_rvgen(lamb, n)
hist(x)
lines(density(x))

Verify that we are correct

  • We can verify that we are correct by drawing the density along with the histogram.
y=seq(-50,50,0.5)
fy=lamb*exp(-lamb*y)/((1.0+exp(-lamb*y))^2)
hist(x, freq  = F)
lines(y,fy, col = "red")

Second Example - Exponential

  • PDF: \(f(x) = \lambda e^{-\lambda x}, \; x \ge 0, \lambda > 0\).
  • CDF: \(F(x) = 1 - e^{-\lambda x}, \; \lambda > 0\).

  • Inverse Transformation: \(F^{-1}(u) = -\frac{1}{\lambda}\log(1-u)\), \(u \sim U(0,1)\).

Second Example - Exponential

n = 1e3
lambda = 2
U = runif(n)
X = - 1/lambda*log(U)
hist(X, breaks = 30, freq=F)

Verify that we are correct

  • We can verify that we are correct by drawing the density along with the histogram.
Y=seq(0,50,0.5)
fy=lambda*exp(-lambda*Y)
hist(X, breaks = 30, freq  = F)
lines(Y,fy, col = "red")

Box-Mueller

  • Let's do the Box-Mueller transformation
  • We generate two normal random variable \(X_1\) and \(X_2\) by: \[ U_1, U_2 \sim Unif[0,1] \\ X_1 = \sqrt{-2 \log(U_1)}cos(2 \pi U_2)\\ X_2 = \sqrt{-2 \log(U_1)}sin(2 \pi U_2) \]

In R

u1 = runif(n)
u2 = runif(n)
x1 = sqrt(-2*log(u1))*cos(2*pi*u2)
x2 = sqrt(-2*log(u1))*sin(2*pi*u2)
x = c(x1,x2)

Plotting

hist(x,breaks = 30, freq = F)
curve(dnorm,add=T)

  • Do you know another visual test for normality?

QQ plots

qqnorm(x)
qqline(x)

Homework (will be added to HW 2)

  • Write a sampler to generate samples from the following density using the inverse transform.

\[ p(x) = \frac{\alpha \beta^{\alpha}}{x^{\alpha + 1}} \text{for } x \ge \beta \]

Generating from the Uniform disk

  • We want to generate samples from the Uniform disk in \(\mathbb{R}^2\). \[ p(x,y) = 1/\pi \text{ if } x^2 + y^2 \le 1 \]

Naive Idea

  • Generate \(R \sim \text{Unif}[0,1]\) and \(\theta \sim \text{Unif}[0,2\pi]\).
  • Set:

\[ X_1 = R \cos(\theta) \\ X_2 = R \sin(\theta) \] - The naive idea does not work.

Naive Idea in R

R = runif(1e4,0,1)
theta = 2*pi*runif(1e3,0,1)
X = R*cos(theta);Y = R*sin(theta)
plot(X,Y, pch = 20, col=rgb(1,0,0,0.2))

- Thickly concentrated in the center !

What works

  • Generate \(R \sim \text{Unif}[0,1]\) and \(\theta \sim \text{Unif}[0,2\pi]\).
  • Set:

\[ X_1 = \sqrt{R} \cos(\theta) \\ X_2 = \sqrt{R} \sin(\theta) \]

Extra Credit: Why does this work?

X = sqrt(R)*cos(theta);Y = sqrt(R)*sin(theta)
plot(X,Y,pch = 20, col=rgb(1,0,0,0.2))