-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMCMC_Normal (2).R
59 lines (49 loc) · 1.54 KB
/
MCMC_Normal (2).R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#####################################
## Aim: MCMC samples from a N(0,1)
## Varying the spread of the proposal
#####################################
set.seed(10)
# Normal target distribution
Ftarget <- function(X) {
return(dnorm(X))
}
# Proposal is Normal(x,h)
proposal <- function(x,h) {
return(rnorm(1, mean = x, sd = sqrt(h)))
}
init <- 0 # Good starting value
n <- 1e3 # number of samples
output <- matrix(nrow = n, ncol = 3) # Matrix to store MCMC output
hval <- c(1,5,10) # Varying the spread of proposal
acceptance_prob <- numeric(length = 3)
output[1,] = init
alpha <- 0
proposed <- 0
# Running the MH algorithm for proposals N(x,1), N(x,5), N(x,10)
for(i in 1:3) {
h <- hval[i]
for(j in 2:n) {
proposed = proposal(output[j-1, i], h)
alpha = Ftarget(proposed)/Ftarget(output[j-1, i])
if(runif(1) < alpha) { # Acceptance step
output[j,i] = proposed
acceptance_prob[i] = acceptance_prob[i]+1
} else { # Rejection step
output[j,i] = output[j-1, i]
}
}
}
# Plotting MCMC output
print(acceptance_prob/n)
par(mfrow = c(3,1))
plot.ts(output[,1])
plot.ts(output[,2])
plot.ts(output[,3])
par(mfrow = c(3,1))
acf(output[,1])
acf(output[,2])
acf(output[,3])
par(mfrow = c(3,1))
plot(density(output[,1]))
plot(density(output[,2]))
plot(density(output[,3]))