Chapter 4
Week 6 lecture 2
Challenges I
- Approximate the derivative of \(f(x) = \exp(-x^2)\) by finite-differencing, and compare your approximation to the true derivative over \(x \in [-1, 1]\).
Solution
<- seq(-1, 1, by = .1)
x <- - 2 * x * exp(-x^2)
deriv0 plot(x, deriv0, type = 'l')
<- sqrt(.Machine$double.eps)
delta1 <- function(x) exp(-x^2)
f <- (f(x + delta1) - f(x)) / delta1
deriv1 lines(x, deriv1, col = 2, lwd = 3, lty = 2)
Challenges II
- Consider the exponential distribution with pdf \[ f(y \mid \lambda) = \lambda \exp(-\lambda y) \] for \(y, \lambda > 0\). Its log-likelihood, for an independent sample \(\mathbf{y} = (y_1, \ldots, y_n)\) is given by \[ \log f(\mathbf{y} \mid \lambda) = n \log \lambda -\lambda \sum_{i = 1}^n y_i. \]
Write a function, loglik(lambda, y)
, to evaluate the log-likelihood above, where \(\lambda =\) lambda
and \(\mathbf{y} =\) y
.
Solution
<- function(lambda, y) {
loglik # function to evaluate exponential log-likelihood
# lambda is a scalar
# y is a vector
# returns a scalar
<- length(y)
n * log(lambda) - lambda * sum(y)
n }
- Use
rexp(10)
to generate a sample of ten values from the Exp(1) distribution, and then useloglik(lambda, y)
to evaluate \(\log f(\mathbf{y} \mid 2)\) for the sample.
Solution
<- rexp(10, 1)
y0 loglik(2, y0)
- Then write functions to evaluate the gradient and Hessian matrix of the log-likelihood.
Solution
# gradient, i.e. first derivative
<- function(lambda, y) {
loglik_d1 # function to evaluate gradient of exponential log-likelihood w.r.t. lambda
# lambda is a scalar
# y is a vector
# returns a scalar
<- length(y)
n / lambda - sum(y)
n
}
# Hessian, i.e. second derivative
<- function(lambda, y) {
loglik_d2 # function to evaluate Hessian of exponential log-likelihood w.r.t. lambda
# lambda is a scalar
# y is a vector
# returns a 1 x 1 matrix
<- length(y)
n matrix(- n / lambda^2, 1, 1)
}
- Evaluate the gradient and Hessian for the sample generated above with \(\lambda = 2\).
Solution
loglik_d1(2, y0)
loglik_d2(2, y0)
- Check your gradient calculation against an approximation based on finite-differencing.
Solution
loglik(2 + 1e-6, y0) - loglik(2, y0)) / 1e-6
(loglik_d1(2, y0)
Week 6 lecture 3
Challenges I
- Use the midpoint rule to approximate \[ I = \int_0^2 \text{exp}\left(-\frac{x^2}{2}\right) \text{d}x \] with \(N = 10\) integration nodes.
Solution
<- 10
N <- 0
a <- 2
b <- seq(a, b, length = N + 1)
bins <- bins[2] - bins[1]
h <- bins[-1] - .5 * h
mids <- function(x) exp(-x^2/2)
f <- f(mids)
f_mids <- h * sum(f_mids) I_hat
- Find the relative absolute error of your estimate. [Hint: notice the resemblance of the integrand to the Normal(0, 1) distribution’s pdf.]
Solution
<- sqrt(2 * pi) * (pnorm(2) - .5)
I0 <- abs((I0 - I_hat) / I0)
rel_err 100 * rel_err
Week 8 lecture 1
Challenges I
- Repeat Challenge I using Simpson’s rule; i.e. approximate \[ I = \int_0^2 \text{exp}\left(-\frac{x^2}{2}\right) \text{d}x \] with \(N = 10\) and find the relative absolute error of your estimate.
Solution
<- function(x) exp(-x^2/2)
f <- 10
N <- 0
a <- 2
b <- (b - a) / N
h <- f(a) + f(b)
simpson <- a + h * (2 * (1:N) - 1) / 2
x1i <- simpson + 4 * sum(f(x1i))
simpson <- a + h * (1:(N - 1))
x2i <- simpson + 2 * sum(f(x2i))
simpson <- h * simpson / 6
simpson
<- sqrt(2 * pi) * (pnorm(b) - .5)
true <- abs((true - simpson) / true)
rel_err rel_err
Challenges II
- Use Gaussian quadrature to approximate \[ I = \int_0^2 \text{exp}(-x^2 / 2) \text{d} x \] with \(N = 7\) integration nodes.
Solution
<- function(x) exp(-x^2/2)
f <- 7
N <- pracma::gaussLegendre(N, 0, 2)
gq <- sum(gq$w * f(gq$x)) I_hat
- Calculate the relative absolute error of your approximation.
Solution
<- sqrt(2 * pi) * (pnorm(2) - .5)
true <- abs((true - I_hat) / true) rel_err
Week 8 lecture 2
Challenges I
- Consider independent Exponential random variables \(X_1\) and \(X_2\), each with pdf \[ f_{X_i}(x \mid \lambda) = \lambda \text{exp}(-\lambda x)~~~\text{for}~x > 0, \] and where \(\lambda > 0\). Estimate \(\text{Pr}(X_1 \leq 1, X_2 \leq 1)\) for \(\lambda = 2\) by numerical integration using the midpoint rule and \(N = 10\) for \(X_1\) and \(X_2\); i.e. approximate \[ \text{Pr}(X_1 \leq 1, X_2 \leq 1) = \int_0^1 \int_0^1 \lambda^2 \text{exp}\left\{-\lambda(x_1 + x_2)\right\} \text{d}x_1\text{d}x_2. \]
Solution
<- 10
N <- x2 <- (1:N - .5) / N
x1 <- 1/N
h <- h^2
w <- 2
lambda <- 0
midpoint for (i in 1:N) for (j in 1:N)
<- midpoint + w * lambda^2 * exp(-lambda*(x1[i] + x2[j]))
midpoint midpoint
- Find the relative absolute error of your approximation.
Solution
<- pexp(1, lambda)^2
true <- abs((true - midpoint) / true) rel_err
Challenges II
- Approximate \(\text{Pr}(X_1 \leq 1, X_2 \leq 2)\), where \(X_1\) and \(X_2\) are independent Exp(\(\lambda\)) random variables with \(\lambda = 2\). This time use Gaussian quadrature and \(N = 7\) for \(X_1\) and \(X_2\).
Solution
<- 7
N <- pracma::gaussLegendre(N, 0, 1)
xw1 <- pracma::gaussLegendre(N, 0, 2)
xw2 <- 0
gq <- function(x1, x2, lambda = 2)
f ^2 * exp(-lambda * (x1 + x2))
lambdafor (i in 1:N) for (j in 1:N)
<- gq + xw1$w[i] * xw2$w[j] * f(xw1$x[i], xw2$x[j])
gq gq
- Find the relative absolute error of your approximation.
Solution
<- pexp(1, 2) * pexp(2, 2)
true <- abs((true - gq) / true) rel_err
Week 8 lecture 3
Challenge I
- Using the midpoint rule with \(N = 5\) nodes per variable, approximate \[ I = \int_1^3 \ldots \int_1^3 \prod_{i = 1}^4 \left\{1 + (x_i - 2)^2\right\} \text{d}x_1 \ldots \text{d}x_4 \] and estimate its relative absolute error.
Solution
<- 5
N <- 4
d <- 3
b <- 1
a <- (b - a) / N
h <- a + h * (1:N - .5)
x <- lapply(1:d, function(i) x)
xx <- expand.grid(xx)
X <- h^d
w <- apply(1 + (X - 2)^2, 1, prod)
f <- w * sum(f)
midpoint <- (8/3)^d
true <- abs((true - midpoint) / true)
rel_err rel_err
Challenge II
- Consider \(Y \sim \text{Exp}(\lambda)\), so that its pdf is \[ f(y \mid \lambda) = \lambda \text{exp}(-\lambda y) \quad \text{for}~y > 0, \] where \(\lambda\) is a random variable with pdf \[ f(\lambda) = 4 \lambda \text{exp}(-2 \lambda) \quad \text{for}~\lambda > 0. \] Use Laplace’s method to find \(f(y)\), where \[ f(y) = \int_0^\infty f(y \mid \lambda) f(\lambda) \text{d} \lambda. \]
Solution
We first notice that, given Equation (4.4), we take \(f()\) to be \(-\log f(y, \lambda)\) and \(x\) to be \(\lambda\), as that’s what we’re integrating out.
Then we get \(f(y, \lambda) = f(y \mid \lambda) f(\lambda) = \lambda \text{e}^{-\lambda y} \times 4 \lambda \text{e}^{-2 \lambda} = 4 \lambda^2 \text{e}^{-\lambda(2 + y)}\)
So \(-\log f(y, \lambda) = -2 \log \lambda + \lambda(2 + y) - \log 4\)
Then \(-\dfrac{\partial \log f(y, \lambda)}{\partial \lambda} = -\dfrac{2}{\lambda} + (2 + y)\), which is minimised at \(\tilde \lambda = 2 / (2 + y)\)
Also \(-\dfrac{\partial^2 \log f(y, \lambda)}{\partial \lambda^2} = 2 / \lambda^2\) and \(2 / \tilde \lambda^2 = 2 / (4 / (2 + y)^2) = (2 + y)^2 / 2\)
So, if \(g''(\lambda)\) denotes \(-\partial^2 \log f(y, \lambda) / \partial \lambda^2\), then \[\begin{align*} I_n &= f(y, \tilde \lambda) \sqrt{\dfrac{2 \pi}{g''(\tilde \lambda)}}\\ &= 4 \hat \lambda^2 \text{e}^{-\hat \lambda(2 + y)} \sqrt{2\pi \times 2(2 + y)^{-2}} = \dfrac{32 \sqrt{\pi}}{\text{e}^2(2 + y)^3} \end{align*}\]
Week 9 lecture 1
Challenges I
- Use Monte Carlo integration with \(N = 10^3\) Monte Carlo samples to approximate \[ \int_{-1}^1 x^2 \text{d} x \] and then calculate the relative absolute error of your approximation.
Solution
<- 1e3
N <- 2
V <- V * mean(runif(N, -1, 1)^2)
mc <- 2/3 true
Challenges II
- Approximate \[ I = \int_1^3 \ldots \int_1^3 \prod_{i = 1}^4 \left\{1 + (x_i - 2)^2\right\} \text{d}x_1 \ldots \text{d}x_4 \] using Monte Carlo sampling and a Monte Carlo sample size of \(N = 10^4\) and estimate the relative absolute error of your approximation.
Solution
<- 10^4
N <- 4
d <- matrix(runif(N * d, 1, 3), N)
x <- 2^d
V <- V * mean(apply(1 + (x - 2)^2, 1, prod))
I_hat <- V * mean(exp(rowSums(log1p((x - 2)^2))))
I_hat2 <- (8/3)^d
true <- abs((true - I_hat) / true)
rel_err rel_err