Advertisement
st110036

StatComp_2019Q1Q2Q3

Sep 25th, 2022
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.49 KB | None | 0 0
  1. #2019-Q1------------------------------------------------------------------------------------
  2. z <- c(2:5)
  3. exact<-10+sum(z*(((choose(5,z))*(0.5^5))/(1-6*(0.5^5))))+10
  4.  
  5. f2  <-function(x){                                 #pdf of X2
  6.   ((choose(5,x))*(0.5^5))/(1-6*(0.5^5))
  7. }
  8. replicate <- 10000
  9. X1 <- rep(0, replicate)
  10. X2 <- rep(0, replicate)
  11. for(i in 1:replicate){
  12.   x1 <- 0
  13.   x2 <- 0
  14.   while(TRUE){
  15.     u <- runif(1)
  16.     x2 <- x2+1
  17.     if(u<1/3){
  18.       x1 <- x1+rgamma(1,2,scale = 5)
  19.       break
  20.     }
  21.     else{
  22.       if(u<2/3){
  23.         u2 <- runif(1)
  24.         x <- 2
  25.         p <- f2(x)
  26.         while(u2>p){
  27.           x <- x+1
  28.           p <- p+f2(x)
  29.         }
  30.         x1 <- x1+x
  31.       }
  32.       else{
  33.         x1 <- x1 +rexp(1,rate = 1/10)
  34.       }
  35.     }
  36.   }
  37.   X1[i] <- x1
  38.   X2[i] <- x2
  39. }
  40. exact
  41. mean(X1)
  42. mean(X2)
  43.  
  44. #2019-Q2------------------------------------------------------------------------------------
  45. temp <- sum(1/c(1:10))
  46. c <- 1/temp  # c = 0.3414172
  47. p <- c/c(1:10)
  48. theta <- 1/sum(p^2) # theta = 5.535575
  49.  
  50. sim_n <- 10000; n1 <- 5; n2 <- 20
  51.  
  52. set.seed(1234)
  53. N5 <- rmultinom(n = sim_n, size = n1, prob = p)
  54. # row-i means Xi & column-j means j-th simulation with n = 5
  55.  
  56. g1 <- function(x, n) (x/n)^2
  57. g2 <- function(x, n) x*(x-1)/(n*(n-1))
  58.  
  59. theta1_n1 <- apply(N5, 2, function(x) 1/sum(g1(x,n1)))
  60. theta2_n1 <- apply(N5, 2, function(x) 1/sum(g2(x,n1)))
  61. theta1_n1 <- theta1_n1[is.finite(theta1_n1)]
  62. theta2_n1 <- theta2_n1[is.finite(theta2_n1)]
  63.  
  64. theta1_bar_n1 <- mean(theta1_n1)
  65. theta2_bar_n1 <- mean(theta2_n1)
  66. bias1_n1 <- theta1_bar_n1 - theta
  67. bias2_n1 <- theta2_bar_n1 - theta
  68. sim1_n1 <- length(theta1_n1)
  69. sim2_n1 <- length(theta2_n1)
  70. SE1_n1<-sqrt(sum((theta1_n1 - theta1_bar_n1)^2)/sim1_n1)
  71. SE2_n1<-sqrt(sum((theta2_n1 - theta2_bar_n1)^2)/sim2_n1)
  72. R_mse1_n1<-sqrt(sum((theta1_n1 - theta)^2)/sim1_n1)
  73. R_mse2_n1<-sqrt(sum((theta2_n1 - theta)^2)/sim2_n1)
  74.  
  75. N20 <- rmultinom(n = sim_n,size = n2,prob = p)
  76. # row-i means Xi & column-j means jth simulation with n = 20
  77.  
  78. theta1_n2 <- apply(N20, 2, function(x) 1/sum(g1(x,n2)))
  79. theta2_n2 <- apply(N20, 2, function(x) 1/sum(g2(x,n2)))
  80. theta1_n2 <- theta1_n2[is.finite(theta1_n2)]
  81. theta2_n2 <- theta2_n2[is.finite(theta2_n2)]
  82.  
  83. theta1_bar_n2 <- mean(theta1_n2)
  84. theta2_bar_n2 <- mean(theta2_n2)
  85. bias1_n2 <- theta1_bar_n2 - theta
  86. bias2_n2 <- theta2_bar_n2 - theta
  87. sim1_n2 <- length(theta1_n2)
  88. sim2_n2 <- length(theta2_n2)
  89. SE1_n2<-sqrt(sum((theta1_n2 - theta1_bar_n2)^2)/sim1_n2)
  90. SE2_n2<-sqrt(sum((theta2_n2 - theta2_bar_n2)^2)/sim2_n2)
  91. R_mse1_n2<-sqrt(sum((theta1_n2 - theta)^2)/sim1_n2)
  92. R_mse2_n2<-sqrt(sum((theta2_n2 - theta)^2)/sim2_n2)
  93.  
  94. result<-{data.frame("n" = c(n1," "," ",n2," "," "),
  95.                     "Mean estimate" = c(" ",theta1_bar_n1,theta2_bar_n1," ",theta1_bar_n2,theta2_bar_n2),
  96.                     "Bias" = c(" ",bias1_n1,bias2_n1," ",bias1_n2,bias2_n2),
  97.                     "Sample SE" = c(" ",SE1_n1,SE2_n1," ",SE1_n2,SE2_n2),
  98.                     "Root MSE" = c(" ",R_mse1_n1,R_mse2_n1," ",R_mse1_n2,R_mse2_n2),
  99.                     row.names = c(" ","theta1_hat","theta2_hat","  ","theta1-hat","theta2-hat"))
  100. }
  101. result
  102. # write.csv(result,"C:/Users/**Username**/Desktop/result.csv")
  103.  
  104. #2019-Q3------------------------------------------------------------------------------------
  105. X <- c(11.2,10.9,9.2, 10.5, 7.1, 6.9,11.5,5.4,7.8, 10.4)
  106. as.vector(X)
  107.  
  108. L_f <- function(theta){
  109.   f <- exp(-(X-theta))/(1+exp(-(X-theta)))^2
  110.   lnL <- sum(log(f))
  111.   return(lnL)
  112. }
  113. p <- optimize(f = L_f,c(-20,20), maximum = TRUE)
  114. p
  115.  
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement