celestialgod

iamsyu

Mar 27th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.37 KB | None | 0 0
  1. ## 1.
  2. #    x 2 3 4 ... 7  8 ... 12
  3. # p(x) 1 2 3 ... 6  5 ...  1 / 36
  4. generating_f1 = function(N){
  5.   X = rep(0, N)
  6.   p = c(1:6, 5:1) / sum(c(1:6, 5:1))
  7.   p_cum = cumsum(p)
  8.   values = 2:12
  9.   u = runif(N)
  10.   X[u <= p_cum[1]] = values[1]
  11.   for (i in 2:length(p_cum))
  12.     X[u <= p_cum[i] & u >= p_cum[i-1]] = values[i]
  13.   X
  14. }
  15. (XX = generating_f1(1000))
  16. (freq_table = table(XX))
  17. (output = data.frame(points = names(freq_table),
  18.     simu_prob = as.vector(freq_table)/1000,
  19.     real_prob = c(1:6, 5:1) / sum(c(1:6, 5:1))))
  20.  
  21.  
  22. ## 2.
  23. # a.
  24. # f(x) / g(x) = 20*x*(1-x)^3
  25. # d/dx (f/g) = 20*[(1-x)^3 - 3*x*(1-x)^2] = 0 => x = 1/4
  26. # c = f(1/4)/g(1/4) = 20*(1/4)*(1-1/4)^3 = 135/64
  27. # accept if U < f(x) /(g(x)*c) = 256/27*x*(1-x)^3
  28. generating_f2 = function(N){
  29.   X = rep(NA, N)
  30.   f = function(x) 256/27*x*(1-x)^3
  31.   while(any(is.na(X)))
  32.   {
  33.     locToGen = which(is.na(X))
  34.     u1 = runif(length(locToGen))
  35.     u2 = runif(length(locToGen))
  36.     X[locToGen[u2 < f(u1)]] = u1[u2 < f(u1)]
  37.   }
  38.   X
  39. }
  40. qqplot(generating_f2(1000), rbeta(1000, 2, 4))
  41.  
  42.  
  43. ## 3.
  44. #    x    0    1    2    3    4
  45. # p(x)  0.1  0.2  0.2  0.2  0.3
  46. generating_f3 = function(N){
  47.   X = rep(0, N)
  48.   p = c(1,2,2,2,3) / sum(c(1,2,2,2,3))
  49.   p_cum = cumsum(p)
  50.   values = 0:4
  51.   u = runif(N)
  52.   X[u <= p_cum[1]] = values[1]
  53.   for (i in 2:length(p_cum))
  54.     X[u <= p_cum[i] & u >= p_cum[i-1]] = values[i]
  55.   X
  56. }
  57. (XX = generating_f3(1000))
  58. (freq_table = table(XX))
  59. (output = data.frame(points = names(freq_table),
  60.     simu_prob = as.vector(freq_table)/1000,
  61.     real_prob = c(1,2,2,2,3) / sum(c(1,2,2,2,3))))
  62.  
  63. generating_f4 = function(N) sample(0:4, N, TRUE,
  64.   c(1,2,2,2,3) / sum(c(1,2,2,2,3)))
  65. (XX = generating_f4(1000))
  66. (freq_table = table(XX))
  67. (output = data.frame(points = names(freq_table),
  68.     simu_prob = as.vector(freq_table)/1000,
  69.     real_prob = c(1,2,2,2,3) / sum(c(1,2,2,2,3))))
  70.  
  71.  
  72. ## 4.
  73. # a.
  74. # f(x) / g(x) = 3*x^2
  75. # d/dx (f/g) = 6*x = 0 => x = 0
  76. # d^2/dx^2 (f/g) = 6 > 0 => f(0) / g(0) is minimum
  77. # d/dx (f/g) > 0 for 0 <= x <= 1 => increasing
  78. # c = f(1)/g(1) = 3
  79. # accept if U < f(x) /(g(x)*c) = x^2
  80. generating_f5 = function(N){
  81.   X = rep(NA, N)
  82.   f = function(x) x^2
  83.   while(any(is.na(X)))
  84.   {
  85.     locToGen = which(is.na(X))
  86.     u1 = runif(length(locToGen))
  87.     u2 = runif(length(locToGen))
  88.     X[locToGen[u2 < f(u1)]] = u1[u2 < f(u1)]
  89.   }
  90.   X
  91. }
  92. qqplot(generating_f5(1000), rbeta(1000, 3, 1))
Advertisement
Add Comment
Please, Sign In to add comment