---
title: "Solutions for module 7"
author: ""
date: ""
output: html_document
---

## Markov chain Monte Carlo methods

### Part 1

$$a(x,y) = \min\{1, \exp(-\frac 12(y^2-x^2)) \}.$$ 

### Part 2

Since $u$ is uniformly distributed on $[0,1]$, the probability that $u < H(x,y)$ is exactly $\min\{1,H(x,y)\}$.

### Part 3

Example of **R** implementation:
```{r}
sigma <- 2
n <- 10000 # Chain length
x <- rep(0, n) # Vector to store chain
x[1] <- 0 ## initial value
for(i in 2:n){
  y <- rnorm(1, x[i-1], sigma)
  u <- runif(1)
  H <- dnorm(y)/dnorm(x[i-1])
  if(u<H) x[i]=y else x[i]=x[i-1]
}
plot(x, type="l")
hist(x, prob=TRUE, breaks=50)
curve(dnorm(x), -4, 4, add=TRUE) ## add normal pdf to histogram
```

Plots using `coda` package instead of manual graphics:
```{r}
library(coda)
x <- mcmc(x) # Convert to coda format
plot(x)
```

### Part 4

The estimate is obtained by
```{r}
mean(x<=-1)
```
and the result is close to
```{r}
pnorm(-1)
```

### Part 5

The proposal is independent of the current state $x$. Furthermore, the proposal
follows the same distribution as the target density $\pi(x)$. The acceptance
probability is in this case $a(x,y)=1$.
