@HaomingJiang
2018-04-17T08:22:07.000000Z
字数 2331
阅读 1007
Haoming Jiang
We have ,
, which means
n=10
a = 10
b = 5
rec = rep(0, 11)
for(i in 0:10){
rec[i+1] = choose(n, i)*beta(a+i, b+n-i) / beta(a, b)
}
m = 100
theta = x = rep(0, m)
x[1] = 0
theta[1] = rbeta(1, a+x[1], b+n-x[1])
for(i in 2:m){
x[i] = rbinom(1, n, prob = theta[i-1])
theta[i] = rbeta(1, a+x[i-1], b+n-x[i-1])
}
plot(x, theta)
## d
q = function(x, u, v) qbinom(v, n, qbeta(u, a+x, b+n-x) )
ss = 0:10
start_times = c()
X0s = c()
for(i in 1:100)
{
a = 10
b = 5
n = 10
xx = ss
U = runif(2)
u = U[1]; v = U[2]
for(i in 1:(n+1)) xx[i] = q(ss[i], u, v)
while( length(table(xx)) != 1 ){
U = rbind(U, runif(2))
path = xx = ss
for(tau in (dim(U)[1]:1) ){
u = U[tau, 1]; v = U[tau, 2]
for(i in 1:length(ss) ) xx[i] = q(xx[i], u, v)
path = rbind(xx, path)
}
path
}
l = dim(path)[1]
plot( rep((-l:-1 +1), 11), rep(rep(0:10), each = l), cex = 0.3, xlab = "tau", ylab = "sample space" )
for(k in 1:11) points((-l:-1 +1), rev(path[, k]), "l")
X0s = c(X0s, xx[1])
start_times = c(start_times,-l+1)
}
hist(start_times)
hist(X0s)
## f
a = 10
b = 5
n = 10
k=11
set.seed(k)
xx = ss
U = runif(2)
u = U[1]; v = U[2]
for(i in 1:(n+1)) xx[i] = q(ss[i], u, v)
while( length(table(xx)) != 1){
U = rbind(U, runif(2))
path = xx = ss
for(tau in (dim(U)[1]:1) ){
u = U[tau, 1]; v = U[tau, 2]
for(i in 1:length(ss) ) xx[i] = q(xx[i], u, v)
path = rbind(xx, path)
}
#path
}
#path
theta = gibbs = perfect = rep(0, 20)
gibbs[1] = perfect[1] = 0
theta[1] = rbeta(1, a+gibbs[1], b+n-gibbs[1])
for(i in 2:20){
## perfect
u = runif(1); v = runif(1)
perfect[i] = q(perfect[i-1], u, v)
## gibbs
gibbs[i] = rbinom(1, n, prob = theta[i-1])
theta[i] = rbeta(1, a+gibbs[i-1], b+n-gibbs[i-1])
}
plot(rep((0:19), 11), rep(rep(0:10), each = 20), cex = 0.3, xlab = "tau", ylab = "sample space" )
points(0:19, perfect, "l")
points(0:19, gibbs, lty = 2, "l")
legend("topleft", legend = c("CFTP", "Gibbs"), lty=1:2)
i Yes
ii CFTP