If we have familiar distributions, we can simply simulate samples in R. What if there isn’t a built in function for doing this?
Grid approximation.
If there are more parameters, we consider deterministic tools.
Monte Carlo methods are more general.
Suppose that we are given U∼U(0,1) and want to simulate a continuous r.v. X with cdf FX. Put Y=F−1X(U), we have FY(y)=P(Y≤y)=P(F−1X(U)≤y)=P(U≤FX(y))=FX(y).
For any continuous r.v. X, FX(X)∼U(0,1). Why?
Define Z=FX(x), then FZ(x)=P(FX(X)≤x)=P(X≤F−1X(x))=FX(F−1X(x))=x.
If X∼exp(λ), then FX(x)={0 for x<01−e−λx for x≥0
How to work with inverse CDF transformation?
lambda <- 1u <- runif(20)x <- -1/lambda * log(1 - u)curve(1 - exp(-x), 0, 5, ylab = "u", main = "Inverse CDF transformation for exp(1)")for (i in 1:length(x)) { lines(c(x[i], x[i]), c(u[i], 0), lty = "dashed") lines(c(x[i], 0), c(u[i], u[i]), lty = "dashed")}
# Direct samplinglambda <- 1u <- runif(1000)x <- -1/lambda * log(1 - u)hist(x)
Although we cannot easily sample from f(θ|y), there exists another density g(θ) (called proposal density), like a Normal distribution or perhaps a t-distribution, from which it is easy for us to sample.
Then we can sample from g(θ) directly and then "reject" the samples in a strategic way to make the resulting "non-rejected" samples look like they came from f(θ|y).
Sample U∼Unif(0,1).
Sample θ at random from the probability density proportional to g(θ).
If U≤f(θ|y)Mg(θ), accept θ as a draw from f. If the drawn θ is rejected, return to step 1. (With probability f(θ|y)Mg(θ)), accept θ).
The algorithm can be repeated until the desired number of samples from the target density f has been accepted.
Xf⊂Xg, where Xf is the support of f(θ|y).
For any θ∈Xf, f(θ|y)g(θ)≤M, that is M=supθ∈Xff(θ|y)g(θ)<∞.
A simple choice is choose g(θ) that heavier tails.
Suppose we want to get samples from N(0,1), We could use the t2 distribution as our proposal density as it has heavier tails than the Normal.
curve(dnorm(x), -6, 6, xlab = expression(theta), ylab = "Density", n = 200)curve(dt(x, 2), -6, 6, add = TRUE, col = 4, n = 200)legend("topright", c("Normal density", "t density"), col = c(1, 4), bty = "n", lty = 1)x <- rt(200, 2)rug(x, col = 4)y <- rnorm(200)rug(y, col = 1)
P(θ accepted )=P(U≤f(θ|y)Mg(θ))=∫P(U≤f(θ|y)Mg(θ)|θ)g(θ)dθ=∫f(θ|y)Mg(θ)g(θ)dθ=1M
P(θ accepted )=P(U≤f(θ|y)Mg(θ))=∫P(U≤f(θ|y)Mg(θ)|θ)g(θ)dθ=∫f(θ|y)Mg(θ)g(θ)dθ=1M
This has implications for how we choose the proposal density g.
We want to choose g so that it matches f as closely as possible.
f/g is maximized at x=1, so M=1.257. Acceptance probability is about 0.8.
It can be shown that the distribution function of the accepted values is equal to the distribution function corresponding to the target density: P(Θ≤θ|Θ accepted)=F(θ).
Suppose we are interested in Ef[h(θ|y)], what do we do?
Note that in most cases, the candidates generated from g fall within the domain of f, so that they are in fact values that could plausibly come from f.
They are simply over- or under-represented in the frequency with which they appear.
For example, if g has heavier tails than f, then there will be too many extreme values generated from g. Rejection sampling simply thins out those extreme values to obtain the right proportion.
But what if we could take those rejected values and, instead of discarding them, simply downweight or upweight them in a specific way?
Use the rejection sampling algorithm to simulate from N(0,1). Try the following proposal densities.
For each proposal record the percentage of iterates that are accepted. Which is the best proposal?
If we have familiar distributions, we can simply simulate samples in R. What if there isn’t a built in function for doing this?
Grid approximation.
If there are more parameters, we consider deterministic tools.
Monte Carlo methods are more general.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
o | Tile View: Overview of Slides |
s | Toggle scribble toolbox |
Esc | Back to slideshow |
If we have familiar distributions, we can simply simulate samples in R. What if there isn’t a built in function for doing this?
Grid approximation.
If there are more parameters, we consider deterministic tools.
Monte Carlo methods are more general.
Suppose that we are given U∼U(0,1) and want to simulate a continuous r.v. X with cdf FX. Put Y=F−1X(U), we have FY(y)=P(Y≤y)=P(F−1X(U)≤y)=P(U≤FX(y))=FX(y).
For any continuous r.v. X, FX(X)∼U(0,1). Why?
Define Z=FX(x), then FZ(x)=P(FX(X)≤x)=P(X≤F−1X(x))=FX(F−1X(x))=x.
If X∼exp(λ), then FX(x)={0 for x<01−e−λx for x≥0
How to work with inverse CDF transformation?
lambda <- 1u <- runif(20)x <- -1/lambda * log(1 - u)curve(1 - exp(-x), 0, 5, ylab = "u", main = "Inverse CDF transformation for exp(1)")for (i in 1:length(x)) { lines(c(x[i], x[i]), c(u[i], 0), lty = "dashed") lines(c(x[i], 0), c(u[i], u[i]), lty = "dashed")}
# Direct samplinglambda <- 1u <- runif(1000)x <- -1/lambda * log(1 - u)hist(x)
Although we cannot easily sample from f(θ|y), there exists another density g(θ) (called proposal density), like a Normal distribution or perhaps a t-distribution, from which it is easy for us to sample.
Then we can sample from g(θ) directly and then "reject" the samples in a strategic way to make the resulting "non-rejected" samples look like they came from f(θ|y).
Sample U∼Unif(0,1).
Sample θ at random from the probability density proportional to g(θ).
If U≤f(θ|y)Mg(θ), accept θ as a draw from f. If the drawn θ is rejected, return to step 1. (With probability f(θ|y)Mg(θ)), accept θ).
The algorithm can be repeated until the desired number of samples from the target density f has been accepted.
Xf⊂Xg, where Xf is the support of f(θ|y).
For any θ∈Xf, f(θ|y)g(θ)≤M, that is M=supθ∈Xff(θ|y)g(θ)<∞.
A simple choice is choose g(θ) that heavier tails.
Suppose we want to get samples from N(0,1), We could use the t2 distribution as our proposal density as it has heavier tails than the Normal.
curve(dnorm(x), -6, 6, xlab = expression(theta), ylab = "Density", n = 200)curve(dt(x, 2), -6, 6, add = TRUE, col = 4, n = 200)legend("topright", c("Normal density", "t density"), col = c(1, 4), bty = "n", lty = 1)x <- rt(200, 2)rug(x, col = 4)y <- rnorm(200)rug(y, col = 1)
P(θ accepted )=P(U≤f(θ|y)Mg(θ))=∫P(U≤f(θ|y)Mg(θ)|θ)g(θ)dθ=∫f(θ|y)Mg(θ)g(θ)dθ=1M
P(θ accepted )=P(U≤f(θ|y)Mg(θ))=∫P(U≤f(θ|y)Mg(θ)|θ)g(θ)dθ=∫f(θ|y)Mg(θ)g(θ)dθ=1M
This has implications for how we choose the proposal density g.
We want to choose g so that it matches f as closely as possible.
f/g is maximized at x=1, so M=1.257. Acceptance probability is about 0.8.
It can be shown that the distribution function of the accepted values is equal to the distribution function corresponding to the target density: P(Θ≤θ|Θ accepted)=F(θ).
Suppose we are interested in Ef[h(θ|y)], what do we do?
Note that in most cases, the candidates generated from g fall within the domain of f, so that they are in fact values that could plausibly come from f.
They are simply over- or under-represented in the frequency with which they appear.
For example, if g has heavier tails than f, then there will be too many extreme values generated from g. Rejection sampling simply thins out those extreme values to obtain the right proportion.
But what if we could take those rejected values and, instead of discarding them, simply downweight or upweight them in a specific way?
Use the rejection sampling algorithm to simulate from N(0,1). Try the following proposal densities.
For each proposal record the percentage of iterates that are accepted. Which is the best proposal?