Please explain your code so I can learn. Thank you!
Problem 4: Consider simulation of the bivariate normal distribution with mean / = (1, 2) and covariance matrix = = 10.9 1). by the Metropolis-Hastings algorithm, using a random walk chain. Specifically, let each new value x* in the chain be generates by x* = x() + e where E ~ N2(0, D) with D being a diagonal matrix with diagonal elements o, and 62. Note that o, and 62 control the spread of values generated along the first and second coordinate axes, respectively. a) [5 points] Code the Metropolis-Hastings algorithm in R, with o, and oz being part of the input parameters to your function. Let o, = 0.001 and 62 = 0.001, and generate a chain, starting at x(0) = (0,0), of size 10,000. Give a summary of your chain using the coda package. b) [2 points] Compute the acceptance rate, and comment on the acceptance rate. c) [2 points] Burn 1000 points, and draw the trace plot and the density plot for the chain for each of the variables. You may use the coda package. Comment on mixing and the density plots. d) [3 points] Obtain a sample of 9000 from the multivariate normal with # = (1, 2) and covariance matrix E = 10.9 1 0.9) 1 )using the function that I provided In class or a package In R. Draw the points that you obtain on a scatterplot, and overlay this plot by the last 9000 pairs that you generated using the M-H algorithm in part {b}. Use different colors and set the alpha value for your colors to say 0.5 to see both sets of points on the plot. You will see that the M-H generated values do not match the target? Explain why this is the case. [3 points] Choose appropriate 6, and 63 so that the M-H algotlthm would generate values that meet the target requirement. Draw similar plots as in part [d] for this case. [2 points] How do you expect the acceptance rate would change if you choose 61 and 63 to be very large? Briey explain why