[关闭]
@HaomingJiang 2018-04-17T08:22:07.000000Z 字数 2331 阅读 995

Homework 7

Haoming Jiang


8.1


We have ,

8.3

a

, which means

b

c

  1. n=10
  2. a = 10
  3. b = 5
  4. rec = rep(0, 11)
  5. for(i in 0:10){
  6. rec[i+1] = choose(n, i)*beta(a+i, b+n-i) / beta(a, b)
  7. }
  8. m = 100
  9. theta = x = rep(0, m)
  10. x[1] = 0
  11. theta[1] = rbeta(1, a+x[1], b+n-x[1])
  12. for(i in 2:m){
  13. x[i] = rbinom(1, n, prob = theta[i-1])
  14. theta[i] = rbeta(1, a+x[i-1], b+n-x[i-1])
  15. }
  16. plot(x, theta)

1.png-6.3kB

d

  1. ## d
  2. q = function(x, u, v) qbinom(v, n, qbeta(u, a+x, b+n-x) )
  3. ss = 0:10
  4. start_times = c()
  5. X0s = c()
  6. for(i in 1:100)
  7. {
  8. a = 10
  9. b = 5
  10. n = 10
  11. xx = ss
  12. U = runif(2)
  13. u = U[1]; v = U[2]
  14. for(i in 1:(n+1)) xx[i] = q(ss[i], u, v)
  15. while( length(table(xx)) != 1 ){
  16. U = rbind(U, runif(2))
  17. path = xx = ss
  18. for(tau in (dim(U)[1]:1) ){
  19. u = U[tau, 1]; v = U[tau, 2]
  20. for(i in 1:length(ss) ) xx[i] = q(xx[i], u, v)
  21. path = rbind(xx, path)
  22. }
  23. path
  24. }
  25. l = dim(path)[1]
  26. plot( rep((-l:-1 +1), 11), rep(rep(0:10), each = l), cex = 0.3, xlab = "tau", ylab = "sample space" )
  27. for(k in 1:11) points((-l:-1 +1), rev(path[, k]), "l")
  28. X0s = c(X0s, xx[1])
  29. start_times = c(start_times,-l+1)
  30. }
  31. hist(start_times)
  32. hist(X0s)

3.png-8.9kB
31.png-9.4kB
32.png-8.2kB

e

e.png-13.2kB

f

  1. ## f
  2. a = 10
  3. b = 5
  4. n = 10
  5. k=11
  6. set.seed(k)
  7. xx = ss
  8. U = runif(2)
  9. u = U[1]; v = U[2]
  10. for(i in 1:(n+1)) xx[i] = q(ss[i], u, v)
  11. while( length(table(xx)) != 1){
  12. U = rbind(U, runif(2))
  13. path = xx = ss
  14. for(tau in (dim(U)[1]:1) ){
  15. u = U[tau, 1]; v = U[tau, 2]
  16. for(i in 1:length(ss) ) xx[i] = q(xx[i], u, v)
  17. path = rbind(xx, path)
  18. }
  19. #path
  20. }
  21. #path
  22. theta = gibbs = perfect = rep(0, 20)
  23. gibbs[1] = perfect[1] = 0
  24. theta[1] = rbeta(1, a+gibbs[1], b+n-gibbs[1])
  25. for(i in 2:20){
  26. ## perfect
  27. u = runif(1); v = runif(1)
  28. perfect[i] = q(perfect[i-1], u, v)
  29. ## gibbs
  30. gibbs[i] = rbinom(1, n, prob = theta[i-1])
  31. theta[i] = rbeta(1, a+gibbs[i-1], b+n-gibbs[i-1])
  32. }
  33. plot(rep((0:19), 11), rep(rep(0:10), each = 20), cex = 0.3, xlab = "tau", ylab = "sample space" )
  34. points(0:19, perfect, "l")
  35. points(0:19, gibbs, lty = 2, "l")
  36. legend("topleft", legend = c("CFTP", "Gibbs"), lty=1:2)

4.png-10kB

i Yes
ii CFTP

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注