-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCMC
153 lines (130 loc) · 7.25 KB
/
MCMC
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
##############
# 1. MCMC ###
##############
MCMC_Limit <- function(i, t_mean, t_sd, initial, p_sd, lower, upper) {
current <- initial; sample <- vector()
if (i <= 0) {return(NA)}
for (i in 1:i) {
proposal <- current + rnorm(1, 0, p_sd)
p_density <- dnorm(proposal, t_mean, t_sd); c_density <- dnorm(current, t_mean, t_sd)
accept <- (p_density/c_density) > runif(1, min = 0, max = 1)
if(accept && proposal <= upper && proposal >= lower)
{current <- proposal; sample <- append(sample, current)}
}
if (length(sample) == 0) {return(NA)}
else {return(sample)}
}
MCMC_wrapper <- function(lower, upper, anchor, iteration) {
data_M <- matrix(0, 10000, upper-lower+1); colnames(data_M) <- lower:upper
for (i in lower:upper) {
estimate <- vector()
for (n in 1:10000) {
temp <- mean(MCMC_Limit(i = iteration, t_mean=i, t_sd=5, anchor, 5, lower, upper))
estimate <- append(estimate, temp)
}
data_M[,(i-lower+1)] <- estimate
}
data_M <- as.data.frame(data_M)
return(data_M)
}
###################
# 2. Simulation ###
###################
lower <- 31; upper <- 70; anchor <- 50.5
data_M40.10 <- MCMC_wrapper(lower, upper, 40.5, 10)
data_M50.10 <- MCMC_wrapper(lower, upper, 50.5, 10)
data_M60.10 <- MCMC_wrapper(lower, upper, 60.5, 10)
data_M40.20 <- MCMC_wrapper(lower, upper, 40.5, 20)
data_M50.20 <- MCMC_wrapper(lower, upper, 50.5, 20)
data_M60.20 <- MCMC_wrapper(lower, upper, 60.5, 20)
data_M40.30 <- MCMC_wrapper(lower, upper, 40.5, 30)
data_M50.30 <- MCMC_wrapper(lower, upper, 50.5, 30)
data_M60.30 <- MCMC_wrapper(lower, upper, 60.5, 30)
################
# 3. Heatmap ###
################
data_M40.10M <- heat_map(data_M40.10)
data_M50.10M <- heat_map(data_M50.10)
data_M60.10M <- heat_map(data_M60.10)
data_M40.20M <- heat_map(data_M40.20)
data_M50.20M <- heat_map(data_M50.20)
data_M60.20M <- heat_map(data_M60.20)
data_M40.30M <- heat_map(data_M40.30)
data_M50.30M <- heat_map(data_M50.30)
data_M60.30M <- heat_map(data_M60.30)
# 10 iteration
heatmap_M40.10M <- ggplot(data_M40.10M, aes(x = X2, y = X1, fill = value)) + theme_test() +
geom_tile(color="white") + ggtitle("A: anchor = 40.5") +
scale_fill_gradient(low = "white", high = "steelblue") +
xlim(lower,upper) + ylim(lower,upper) + geom_hline(yintercept = 40.5, linetype="dotted") +
xlab("True Number of Dots") + ylab("Estimated Number of Dots")
heatmap_M50.10M <- ggplot(data_M50.10M, aes(x = X2, y = X1, fill = value)) + theme_test() +
geom_tile(color="white") + ggtitle("B: anchor = 50.5") +
scale_fill_gradient(low = "white", high = "steelblue") +
xlim(lower,upper) + ylim(lower,upper) + geom_hline(yintercept = 50.5, linetype="dotted") +
xlab("True Number of Dots") + ylab("Estimated Number of Dots")
heatmap_M60.10M <- ggplot(data_M60.10M, aes(x = X2, y = X1, fill = value)) + theme_test() +
geom_tile(color="white") + ggtitle("C: anchor = 60.5") +
scale_fill_gradient(low = "white", high = "steelblue") +
xlim(lower,upper) + ylim(lower,upper) + geom_hline(yintercept = 60.5, linetype="dotted") +
xlab("True Number of Dots") + ylab("Estimated Number of Dots")
grid.arrange(heatmap_M40.10M, heatmap_M50.10M, heatmap_M60.10M, nrow=1, left="Iterations = 10")
# 20 iteration
heatmap_M40.20M <- ggplot(data_M40.20M, aes(x = X2, y = X1, fill = value)) + theme_test() +
geom_tile(color="white") + ggtitle("D: anchor = 40.5") +
scale_fill_gradient(low = "white", high = "steelblue") +
xlim(lower,upper) + ylim(lower,upper) + geom_hline(yintercept = 40.5, linetype="dotted") +
xlab("True Number of Dots") + ylab("Estimated Number of Dots")
heatmap_M50.20M <- ggplot(data_M50.20M, aes(x = X2, y = X1, fill = value)) + theme_test() +
geom_tile(color="white") + ggtitle("E: anchor = 50.5") +
scale_fill_gradient(low = "white", high = "steelblue") +
xlim(lower,upper) + ylim(lower,upper) + geom_hline(yintercept = 50.5, linetype="dotted") +
xlab("True Number of Dots") + ylab("Estimated Number of Dots")
heatmap_M60.20M <- ggplot(data_M60.20M, aes(x = X2, y = X1, fill = value)) + theme_test() +
geom_tile(color="white") + ggtitle("F: anchor = 60.5") +
scale_fill_gradient(low = "white", high = "steelblue") +
xlim(lower,upper) + ylim(lower,upper) + geom_hline(yintercept = 60.5, linetype="dotted") +
xlab("True Number of Dots") + ylab("Estimated Number of Dots")
grid.arrange(heatmap_M40.20M, heatmap_M50.20M, heatmap_M60.20M, nrow=1, left="Iterations = 20")
# 30 iteration
heatmap_M40.30M <- ggplot(data_M40.30M, aes(x = X2, y = X1, fill = value)) + theme_test() +
geom_tile(color="white") + ggtitle("I: anchor = 40.5") +
scale_fill_gradient(low = "white", high = "steelblue") +
xlim(lower,upper) + ylim(lower,upper) + geom_hline(yintercept = 40.5, linetype="dotted") +
xlab("True Number of Dots") + ylab("Estimated Number of Dots")
heatmap_M50.30M <- ggplot(data_M50.30M, aes(x = X2, y = X1, fill = value)) + theme_test() +
geom_tile(color="white") + ggtitle("J: anchor = 50.5") +
scale_fill_gradient(low = "white", high = "steelblue") +
xlim(lower,upper) + ylim(lower,upper) + geom_hline(yintercept = 50.5, linetype="dotted") +
xlab("True Number of Dots") + ylab("Estimated Number of Dots")
heatmap_M60.30M <- ggplot(data_M60.30M, aes(x = X2, y = X1, fill = value)) + theme_test() +
geom_tile(color="white") + ggtitle("K: anchor = 60.5") +
scale_fill_gradient(low = "white", high = "steelblue") +
xlim(lower,upper) + ylim(lower,upper) + geom_hline(yintercept = 60.5, linetype="dotted") +
xlab("True Number of Dots") + ylab("Estimated Number of Dots")
grid.arrange(heatmap_M40.30M, heatmap_M50.30M, heatmap_M60.30M, nrow=1, left="Iterations = 30")
###################
# 3. Additional ###
###################
ggplot(data_M50.25, aes(x = data_M$"48")) + geom_histogram(aes(y =..density..), binwidth = 0.2)+
geom_vline(xintercept=50) + geom_density() + xlab("Estimate of Dots") +
ggtitle("MCMC; 48 dots (Boundary = 50; p_sd = 2; t_sd = 3)")
Density <- dnorm(lower:upper, 40, (upper-lower+1)/5); Dots <- lower:upper
sensory <- data.frame(Density, Dots)
ggplot(sensory, aes(x = Dots, y = Density)) + xlab("Number of Dots") + xlim(lower, upper) +
ggtitle("Sensory Representation") + geom_line() + theme_classic() +
geom_vline(xintercept = anchor, linetype="dotted") +
geom_vline(xintercept = lower) + geom_vline(xintercept = upper)
ave_estimate <- colMeans(data_M)
prob_accept <- vector()
for (i in 1:(upper-lower+1)) {
prob_accept <- append(prob_accept,length(which(data_M[,i] > anchor))/length(data_M[,i]))
}
probability_B <- data.frame(Dots = lower:upper, prob_accept, ave_estimate)
ggplot(probability_B, aes(Dots, prob_accept)) + geom_point() + geom_line() +
geom_hline(yintercept = 0.5, linetype="dotted") + ylim(0, 1) + ggtitle("MCMC") +
geom_vline(xintercept = anchor, linetype="dotted") + xlim(lower, upper) +
xlab("True Number of Dots") + ylab("Proportion Judged More Than 25 Dots")
ggplot(probability_B, aes(Dots, ave_estimate)) + geom_point() + geom_line() +
xlab("True Number of Dots") + ylab("Estimated Number of Dots") + ggtitle("MCMC") +
geom_abline(intercept = 0, slope = 1) + ylim(lower, upper) + xlim(lower, upper)