[1] How would you write \(\text{var}(X)\) as an expectation?
[2] Can you think of a quantity that cannot be written as an expectation but that nevertheless can be estimated by using random draws from a distribution?
- by the law of large numbers, \(\hat{J} \rightarrow J\) as \(M \rightarrow \infty\)
[1] Estimate the mean of a standard normal distribution using a Monte
Carlo sample (use rnorm()) with \(M = 10\) simulations, but also try larger
values of \(M\) (e.g., 1000 or
10000).
[2] Assume \(X \sim N(0, 1)\). Estimate (using simulations, \(M = 1000\)) the probability that \(X\) is between \(-1.96\) than \(1.96\): \(\Pr(-1.96 < X < 1.96)\). (Note that you can also look at this probability as \(E\left( I_{|X| < 1.96}(X) \right)\) or \(\int_{-1.96}^{1.96} p(x) dx\).)
[3] Simulate \(M = 10000\) values
from the standard normal. Compute the cumulative moving average/running
mean: For each \(m\), \(\hat{J}_m = \frac{x_1 + \dots + x_m}{m}\).
(Hint: use the cumsum() function and divide by
1:M.) Then plot the cumulative moving average/running mean
\(\hat{J}_m\) against \(m\). You get something like the figure
below. Discuss this result.
[4] A Monte Carlo classic: Estimate the value of \(\pi\) by simulating values in a square centered at the origin and with side lengths 2. Count the proportion of points falling in the inscribed circle (see plot) and multiply by 4 (why?) to get your estimate.
[5] Here’s an example of using simulations to estimate a quantity
that cannot be written as an expectation. Estimate the \(.95\) (or \(95\%\)) quantile of a standard normal
distribution. Check against the true value using the
qnorm() function.
set.seed())set.seed(324)
runif(n = 4)
## [1] 0.72902036 0.42364850 0.57661580 0.09934177
set.seed(324)
runif(n = 4)
## [1] 0.72902036 0.42364850 0.57661580 0.09934177
runif(n = 4)
## [1] 0.70636940 0.60503700 0.71185461 0.06374409
[1] Assume a positive real valued \(X\) follows an exponential distribution
with density function \(p(x) = \lambda
e^{-\lambda x}\) and distribution function \(P(x) = 1 - e^{-\lambda x}\) (with \(\lambda > 0\)). Simulate \(M = 1000\) random draws from an exponential
distribution with \(\lambda = 2\) using
the PIT method. Make a histogram and compare that to the density (using
dexp()).
[2] Assume a real valued \(X\)
follows a Laplace (or double exponential) distribution with density
function \(g(x) = \frac{1}{2}e^{-|x|}\)
(for \(x \in \cal{R}\)). Adapt your
code from the first exercise to simulate from the Laplace distribution
(\(M = 1000\)). Make a histogram and
compare that to the density. (Hint: The Laplace density can be
considered as an exponential density with \(\lambda = 1\) for the positive \(x\)-axis and the same density mirrored for
the negative axis (divided by two in order to get a normalized pdf).
With this knowledge, you can easily sample from the Laplace
distribution, by first sampling from the exponential and then tossing a
fair coin to add a minus sign.)
[3] Implement the rejection algorithm to sample from the beta
distribution (with \(a, b \geq 1\)).
What is the proportion of accepted proposals? Compare it to \(\frac{1}{C}\). Can you explain this? What
goes wrong if \(a < 1\) or \(b < 1\)?
[4] Implement a rejection algorithm to sample from the standard
normal distribution \(p(x)\). Use as
\(g(x)\) the Laplace density from
Exercise [2] and \(C =
\frac{2e^{\frac{1}{2}}}{\sqrt{2\pi}}\). (You may also show that
\(C g(x) \geq p(x)\) by using the
following identity for this relation: \(\frac{x^2}{2} + \frac{1}{2} - |x| =
\frac{1}{2}(|x| - 1)^2 \geq 0\).)
[5] A bivariate rejection algorithm: How to simulate points uniformly distributed on a disc with radius 1? (Hint: see the Monte Carlo classic of exercises ii.)
sample(x, replace = TRUE, prob = w))[1] Estimate the variance of a logistic distribution (i.e., \(p(x) = \frac{e^x}{(1 + e^{x})^2}\) ) using
importance sampling (i.e., \(f(x) =
x^2\), the mean is zero). Use the Laplace distribution as the
importance distribution \(g(x)\) and
\(M = 10000\). (The true value is \(\frac{\pi^2}{3}\).) Plot also the running
mean \(\hat{J}_m\) against \(m\). Repeat the exercise, but now use the
normal as importance distribution.
[2] Go back to [1] and use the weights to resample \(x\)-values. Compare to the density of a
logistic distribution. What happens if you use the normal as the
importance distribution?
[1] Assume \(N = 10\) coin tosses and \(y = 2\) heads. \(\theta\) is the probability of heads coming up. The binomial likelihood is: \(p(y|\theta) = {N \choose y} \theta^y (1 - \theta)^{N - y}\). Now consider two models: \(p(\theta|M_1) = \text{beta}(\theta|a_1 = 1, b_1 = 3)\) and \(p(\theta|M_2) = \text{beta}(\theta|a_2 = 3, b_2 = 1)\).
In this case, \(BF_{12} = \frac{B(a_1 + y, b_1 + N - y) B(a_2, b_2)}{B(a_2 + y, b_2 + N - y)B(a_1, b_1)} = \frac{p(y|M_1)}{p(y|M_2)} = 7.5\).
Estimate this Bayes factor by using the importance sampling method
applied twice (once for \(p(y|M_1)\)
and once for \(p(y|M_2)\)). (Use
uniform importance distributions.)
Some background on the Bayes factor: We expand the Bayesian inference by
considering a set of models \(M_i\)
that include their (possibly model-specific set of) parameters \(\theta_i\), \[
p(\theta_i | y, M_i) = \frac{p(\theta_i | M_i) p(y | \theta_i, M_i)}
{p(y | M_i)},
\] where the marginal likelihood of the data given the model is
\[
p(y | M_i) = \int p(\theta_i | M_i) p(y | \theta_i, M_i) d\theta_i.
\] The Bayes factor comparing two models is the ratio of marginal
likelihoods \[
BF_{12} = \frac{p(y | M_1)}{p(y | M_2)},
\] and it is used to update the prior model odds to the posterior
model odds, \[
\frac{p(M_1 | y)}{p(M_2 | y)} =
\frac{p(M_1) p(y | M_1)}{p(M_2) p(y | M_2)} =
\frac{p(M_1)}{p(M_2)} \times BF_{12}.
\] In the above example, the marginal likelihood for \(M_1\) is \[
\begin{aligned}
p(y | M_1) &= \int p(\theta | M_1) p(y | \theta, M_1) d\theta\\
&= \int \frac{\theta^{a_1 - 1} (1 - \theta)^{b_1 - 1}}
{B(a_1, b_1)}
\binom{N}{y} \theta^{y} (1 - \theta)^{N - y}
d\theta\\
&= \binom{N}{y}/B(a_1, b_1)
\int \theta^{y + a_1 - 1} (1 - \theta)^{N - y + b_1 - 1}
d\theta\\
&= \binom{N}{y}/B(a_1, b_1) B(y + a_1, N - y + b_1).
\end{aligned}
\] For two binomial models \(M_1\), \(M_2\) with different beta priors the Bayes
factor is \[
\begin{aligned}
BF_{12} &= \frac{\binom{N}{y}/B(a_1, b_1) B(y + a_1, N - y + b_1)}
{\binom{N}{y}/B(a_2, b_2) B(y + a_2, N - y + b_2)}\\
&= \frac{B(y + a_1, N - y + b_1) B(a_2, b_2)}
{B(y + a_2, N - y + b_2) B(a_1, b_1)}.
\end{aligned}
\]
[1] Multinomial processing tree (see picture). Suppose I answer 10
two-alternative questions on Bayesian stats. I know the answer to a
question with probability \(\theta\).
If I know the answer, I always answer correctly; if I don’t know it, I
guess. Suppose I have 9 correct responses. What does this tell me about
my underlying Bayesian stats ability \(\theta\)? (Continued on next slide.)
We have 9 successes out of 10 trials. The binomial likelihood is \(p(y|\theta) = {10 \choose 9} \left(\frac{1 + \theta}{2}\right)^{9} \left(\frac{1 - \theta}{2} \right)^{1}\). Assuming a \(\text{beta}(2, 2)\) prior for \(\theta\), we find for the posterior: \[ \begin{aligned} p(\theta|y) &\propto p(y|\theta)p(\theta) = q(\theta) \\ &= \left(\frac{1 + \theta}{2}\right)^{9} \left(\frac{1 - \theta}{2} \right)^{1} \theta(1 - \theta) \end{aligned} \] Implement a Metropolis algorithm to sample from the posterior using as a proposal density \(g(\theta) = 1\) (i.e., a uniform). You may use the function for the log-posterior below.
logpost <- function(theta) {
9*log((1 + theta)/2) + 1*log((1 - theta)/2) + log(theta) + log(1 - theta)
}
[1] Program the Gibbs sampler to sample from a bivariate normal
distribution. Use different starting points. To refresh your memory:
\(p(x_1|x_2) = N(x_1|\rho x_2, 1 -
\rho^2)\) and \(p(x_2|x_1) = N(x_2|\rho
x_1, 1 - \rho^2)\).
[2] (advanced) The MPT example revisited. You may consider the
correct category as the collapse of “knowing” and “guessing correct”.
The number of times you guessed may be considered as a parameter \(\gamma\) as well (assume a uniform prior on
\(\gamma\)).
The bivariate posterior then becomes: \[
\begin{aligned}
p(\theta, \gamma|y) &\propto p(y|\theta, \gamma) p(\theta, \gamma)
= q(\theta, \gamma) \\
&= \theta^{9 - \gamma} \left( \frac{1-\theta}{2} \right)^{\gamma}
\left( \frac{1 - \theta}{2} \right)^{1} \theta(1 - \theta) \\
&\propto \theta^{9 - \gamma + 1}
\left( \frac{1 - \theta}{2} \right)^{\gamma+2}
\end{aligned}
\] The two full conditionals are (derive these): \(p(\theta|y, \gamma) = \text{beta}(9 - \gamma + 2,
\gamma + 3)\) and \(p(\gamma|y, \theta)
= \text{binomial}\left( \frac{1 - \theta}{1 + \theta}, 9
\right)\). Compare your results to the Metropolis exercise.