-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathrizzo_9.10.R
More file actions
79 lines (66 loc) · 1.46 KB
/
Copy pathrizzo_9.10.R
File metadata and controls
79 lines (66 loc) · 1.46 KB
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
library(coda)
l = 10
s = c(1,2,3,4)
k = length(s)
# rayleigh density.
sigma = 4
df = function (y, sigma) {
if (any(x < 0)) return(0)
stopifnot(sigma > 0)
return(y/sigma^2 * exp(-y^2/(2*sigma^2)))
}
dg = function (y, xt) {
dchisq(y, df = xt)
}
rg = function (xt) {
rchisq(1, df = xt)
}
mh = function (s, l) {
x = numeric(l)
us = runif(l)
x[1] = s
for (i in 2:l) {
xt = x[i-1]
y = rg(xt)
res = df(y, sigma)/df(xt, sigma) * dg(xt, y)/dg(y, xt)
if (us[i] <= res) {
x[i] = y
} else {
x[i] = xt
}
}
return(x)
}
xs = matrix(sapply(1:k, function(i) mh(s[i], l)), nrow = k, byrow = TRUE)
# visualize the generated samples.
plotHists = function () {
par(mfrow=c(1,k))
for (i in 1:k) {
hist(xs[i,], probability = TRUE, breaks = 100)
x.axis = seq(min(xs[i,]), max(xs[i,]), by = 0.01)
lines(x.axis, df(x.axis, sigma))
}
par(mfrow=c(1,1))
}
plotChains = function () {
burn = 2000
is = (burn+1):l
par(mfrow=c(1,k))
for (i in 1:k) {
plot(is, xs[i,is], type="l")
}
par(mfrow=c(1,1))
}
gelman.rubin = function (psis) {
psi.means = rowMeans(psis)
B = n * var(psi.means)
W = mean(apply(psis, MARGIN = 1, "var"))
var.hat = (n-1)/n*W + 1/n*B
return(var.hat/W)
}
div = matrix(sapply(1:k, function(i) 1:l), nrow = k, byrow = TRUE)
psis = t(apply(xs, MARGIN = 1, "cumsum")) / div
r.hats = sapply(2:l, function(j) gelman.rubin(psis[,1:j]))
plot(2:l, r.hats, type="l")
abline(h=1.2)
# TODO: use coda library.