-
Notifications
You must be signed in to change notification settings - Fork 0
/
mardia_dm1633_assignment5.Rmd
213 lines (163 loc) · 6 KB
/
mardia_dm1633_assignment5.Rmd
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
---
title: "HW05"
output: html_document
date: "2023-03-03"
---
---
title: "R Notebook"
output:
pdf_document: default
html_notebook: default
---
```{r}
#HW05.R
#In this example, the Metropolis-Hastings algorithm does not converge because the
#proposal standard deviation is too small. The algorithm proposes very small changes
#to theta, which results in a lack of mixing and slow convergence. To improve the
#performance of the Metropolis-Hastings algorithm, you can increase the proposal
#standard deviation to encourage more exploration of the posterior distribution.
# Define the likelihood function
likelihood <- function(theta) {
return(dnorm(theta[1], mean = 0, sd = 1) *
dnorm(theta[2], mean = theta[1], sd = 1) *
dnorm(theta[3], mean = theta[2], sd = 1))
}
# Define the prior distribution
prior <- function(theta) {
return(dnorm(theta[1], mean = 0, sd = 1) *
dnorm(theta[2], mean = 0, sd = 1) *
dnorm(theta[3], mean = 0, sd = 1))
}
# Define the posterior distribution
posterior <- function(theta) {
return(likelihood(theta) * prior(theta))
}
```
```{r}
# Define the Metropolis-Hastings algorithm
metropolis_hastings <- function(theta_0, iter, proposal_sd) {
theta <- theta_0
samples <- matrix(numeric(iter * 3), ncol = 3)
for (i in 1:iter) {
theta_proposed <- theta + rnorm(3, mean = 0, sd = proposal_sd)
log_alpha <- log(posterior(theta_proposed)) - log(posterior(theta))
if (log(runif(1)) < log_alpha) {
theta <- theta_proposed
}
samples[i, ] <- theta
}
return(samples)
}
```
```{r}
library(coda)
# Run the Metropolis-Hastings algorithm and check for convergence
set.seed(123)
samples_1 <- metropolis_hastings(theta_0 = c(0, 0, 0), iter = 5000, proposal_sd = 2.6)
set.seed(123)
samples_2 <- metropolis_hastings(theta_0 = c(0, 0, 0), iter = 5000, proposal_sd = 2.65)
# Combine the results and check for convergence
samples_list <- lapply(1:3, function(x) {
as.mcmc(cbind(samples_1[, x], samples_2[, x]))
})
plot(samples_1[,1])
plot(samples_1[,2])
plot(samples_1[,3])
plot(samples_2[,1])
plot(samples_2[,2])
plot(samples_2[,3])
```
```{r}
#Z-score for a test of equality that compares the means of the first and last parts of each chain
geweke.diag(samples_1[,])
geweke.diag(samples_2[,])
#Gelman-Rubin Diagnostic
(diagnostics <- gelman.diag(samples_list)) #need at least two chains to check for convergence
#This will produce the potential scale reduction factor (PSRF) for each parameter,
#allowing you to check for convergence of the Metropolis-Hastings algorithm.
#If the PSRF is substantially greater than 1.1 for any parameter, it suggests
#that the chain has not converged.
```
```{r}
#Effective Sample Size
effectiveSize(samples_list)
# Part B
## On changing the std of sample 1 closer to std of sample2, we see decent but not perfect convergence for both chains as gelman rubin has a value of 1.01 and effective sample size is large enough to show the decent convergence for both chains but its not a perfect convergence. Perhaps if we increase the number of iterations it would converge perfectly.
##Part A is given below
```
```{r}
#HW05.R
#In this example, the Metropolis-Hastings algorithm does not converge because the
#proposal standard deviation is too small. The algorithm proposes very small changes
#to theta, which results in a lack of mixing and slow convergence. To improve the
#performance of the Metropolis-Hastings algorithm, you can increase the proposal
#standard deviation to encourage more exploration of the posterior distribution.
# Define the likelihood function
likelihood <- function(theta) {
return(dnorm(theta[1], mean = 0, sd = 1) *
dnorm(theta[2], mean = theta[1], sd = 1) *
dnorm(theta[3], mean = theta[2], sd = 1))
}
# Define the prior distribution
prior <- function(theta) {
return(dnorm(theta[1], mean = 0, sd = 1) *
dnorm(theta[2], mean = 0, sd = 1) *
dnorm(theta[3], mean = 0, sd = 1))
}
# Define the posterior distribution
posterior <- function(theta) {
return(likelihood(theta) * prior(theta))
}
```
```{r}
# Define the Metropolis-Hastings algorithm
metropolis_hastings <- function(theta_0, iter, proposal_sd) {
theta <- theta_0
samples <- matrix(numeric(iter * 3), ncol = 3)
for (i in 1:iter) {
theta_proposed <- theta + rnorm(3, mean = 0, sd = proposal_sd)
log_alpha <- log(posterior(theta_proposed)) - log(posterior(theta))
if (log(runif(1)) < log_alpha) {
theta <- theta_proposed
}
samples[i, ] <- theta
}
return(samples)
}
```
```{r}
library(coda)
# Run the Metropolis-Hastings algorithm and check for convergence
set.seed(123)
samples_1 <- metropolis_hastings(theta_0 = c(0, 0, 0), iter = 5000, proposal_sd = 0.001)
set.seed(123)
samples_2 <- metropolis_hastings(theta_0 = c(0, 0, 0), iter = 5000, proposal_sd = 3.000)
# Combine the results and check for convergence
samples_list <- lapply(1:3, function(x) {
as.mcmc(cbind(samples_1[, x], samples_2[, x]))
})
plot(samples_1[,1])
plot(samples_1[,2])
plot(samples_1[,3])
plot(samples_2[,1])
plot(samples_2[,2])
plot(samples_2[,3])
```
```{r}
#Z-score for a test of equality that compares the means of the first and last parts of each chain
geweke.diag(samples_1[,])
geweke.diag(samples_2[,])
#Gelman-Rubin Diagnostic
(diagnostics <- gelman.diag(samples_list)) #need at least two chains to check for convergence
#This will produce the potential scale reduction factor (PSRF) for each parameter,
#allowing you to check for convergence of the Metropolis-Hastings algorithm.
#If the PSRF is substantially greater than 1.1 for any parameter, it suggests
#that the chain has not converged.
```
```{r}
#Effective Sample Size
effectiveSize(samples_list)
# Part A
## Using the Geweke, Maybe reasonably converged for chain 2 as z score is less than 2, however one of the variable of chain 1 has a z score greater than 5 meaning that chain 1 has not converged.
## Using the gelman-rubin we see from the values chain 2 has converged decently but chain 1 has not converged as the value is greater than 1.
```