1 (1)

1.1 (a) Prove dist of param given data

1.2 (b) Express data given param in terms of data avr.

1.3 (c)

x <- seq(0, 8, length = 200)

prior <- dgamma(x, shape = 6, rate = 2)
post1 <- dgamma(x, shape = 26, rate = 7)

prior_mean <- 6 / 2
sample_mean <- 20 / 5
posterior_mean <- 26 / 7

ymax <- max(prior, post1)

plot(x, prior, type = "l", lwd = 2, col = "blue",
     ylim = c(0, ymax * 1.1),
     ylab = "Density", xlab = expression(lambda),
     main = "Prior and Posterior: Case 1")

lines(x, post1, col = "red", lwd = 2)

# Vertical lines for the means
abline(v = prior_mean, col = "blue", lwd = 2, lty = 2)
abline(v = sample_mean, col = "darkgreen", lwd = 2, lty = 2)
abline(v = posterior_mean, col = "purple", lwd = 2, lty = 3)

legend("topright",
       legend = c("Prior density",
                  "Posterior density",
                  "Prior mean = 3",
                  "Sample mean = 4",
                  "Posterior mean = 3.71"),
       col = c("blue", "red", "blue", "darkgreen", "purple"),
       lwd = 2,
       lty = c(1, 1, 2, 2, 3),
       cex = 0.5)

x <- seq(0, 8, length = 200)

prior <- dgamma(x, shape = 6, rate = 2)
post2 <- dgamma(x, shape = 86, rate = 22)

prior_mean <- 6 / 2
sample_mean <- 80 / 20
posterior_mean <- 86 / 22

ymax <- max(prior, post2)

plot(x, prior, type = "l", lwd = 2, col = "blue",
     ylim = c(0, ymax * 1.1),
     ylab = "Density", xlab = expression(lambda),
     main = "Prior and Posterior: Case 2")

lines(x, post2, col = "red", lwd = 2)

# Vertical lines for the means
abline(v = prior_mean, col = "blue", lwd = 2, lty = 2)
abline(v = sample_mean, col = "darkgreen", lwd = 2, lty = 2)
abline(v = posterior_mean, col = "purple", lwd = 2, lty = 3)

legend("topright",
       legend = c("Prior density",
                  "Posterior density",
                  "Prior mean = 3",
                  "Sample mean = 4",
                  "Posterior mean = 3.91"),
       col = c("blue", "red", "blue", "darkgreen", "purple"),
       lwd = 2,
       lty = c(1, 1, 2, 2, 3),
       cex = .5)

notice with more samples we become more certain on out belief of the parameter

2 Curiosity :

2.1 Antithetic Variables

U1 <- runif(1000)
U2 <- 1-U1

plot(U1, U2)

cor(U1, U2) # notice cor()
## [1] -1

requirements :

  • X, Y are must be from the same underlying dist.

  • X,Y should be negatively correlated to reduce variance

resource : Antithetic Variates + R Demo