Advertisement
st110036

StatComp_2021Q1Q2Q3

Sep 25th, 2022
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.23 KB | None | 0 0
  1. ## 2021-Q1 ##############################
  2. sim_n <- 10000; n1 <- 10; n2 <- 100
  3.  
  4. X_n1 <- matrix(rpois(sim_n*n1, lambda = 5), nrow = sim_n, ncol = n1)
  5.  
  6. theta <- exp(-5)
  7. t1_n1 <- apply(X_n1, 1, function(x){length(x[x==0])/n1})
  8. t2_n1 <- apply(X_n1, 1, function(x){sum((-1)^x)/n1})
  9. t3_n1 <- apply(X_n1, 1, function(x){exp(-sum(x)/n1)})
  10. t4_n1 <- apply(X_n1, 1, function(x){(1-1/n1)^sum(x)})
  11.  
  12. t1_bar_n1 <- mean(t1_n1)
  13. t2_bar_n1 <- mean(t2_n1)
  14. t3_bar_n1 <- mean(t3_n1)
  15. t4_bar_n1 <- mean(t4_n1)
  16.  
  17. bias1_n1 <- t1_bar_n1 - theta
  18. bias2_n1 <- t2_bar_n1 - theta
  19. bias3_n1 <- t3_bar_n1 - theta
  20. bias4_n1 <- t4_bar_n1 - theta
  21.  
  22. SE1_n1<-sqrt(sum((t1_n1 - t1_bar_n1)^2)/sim_n)
  23. SE2_n1<-sqrt(sum((t2_n1 - t2_bar_n1)^2)/sim_n)
  24. SE3_n1<-sqrt(sum((t3_n1 - t3_bar_n1)^2)/sim_n)
  25. SE4_n1<-sqrt(sum((t4_n1 - t4_bar_n1)^2)/sim_n)
  26.  
  27. R_mse1_n1<-sqrt(sum((t1_n1 - theta)^2)/sim_n)
  28. R_mse2_n1<-sqrt(sum((t2_n1 - theta)^2)/sim_n)
  29. R_mse3_n1<-sqrt(sum((t3_n1 - theta)^2)/sim_n)
  30. R_mse4_n1<-sqrt(sum((t4_n1 - theta)^2)/sim_n)
  31.  
  32.  
  33. X_n2 <- matrix(rpois(sim_n*n2, lambda = 5), nrow = sim_n, ncol = n2)
  34.  
  35. theta <- exp(-5)
  36. t1_n2 <- apply(X_n2, 1, function(x){length(x[x==0])/n2})
  37. t2_n2 <- apply(X_n2, 1, function(x){sum((-1)^x)/n2})
  38. t3_n2 <- apply(X_n2, 1, function(x){exp(-sum(x)/n2)})
  39. t4_n2 <- apply(X_n2, 1, function(x){(1-1/n2)^sum(x)})
  40.  
  41. t1_bar_n2 <- mean(t1_n2)
  42. t2_bar_n2 <- mean(t2_n2)
  43. t3_bar_n2 <- mean(t3_n2)
  44. t4_bar_n2 <- mean(t4_n2)
  45.  
  46. bias1_n2 <- t1_bar_n2 - theta
  47. bias2_n2 <- t2_bar_n2 - theta
  48. bias3_n2 <- t3_bar_n2 - theta
  49. bias4_n2 <- t4_bar_n2 - theta
  50.  
  51. SE1_n2<-sqrt(sum((t1_n2 - t1_bar_n2)^2)/sim_n)
  52. SE2_n2<-sqrt(sum((t2_n2 - t2_bar_n2)^2)/sim_n)
  53. SE3_n2<-sqrt(sum((t3_n2 - t3_bar_n2)^2)/sim_n)
  54. SE4_n2<-sqrt(sum((t4_n2 - t4_bar_n2)^2)/sim_n)
  55.  
  56. R_mse1_n2<-sqrt(sum((t1_n2 - theta)^2)/sim_n)
  57. R_mse2_n2<-sqrt(sum((t2_n2 - theta)^2)/sim_n)
  58. R_mse3_n2<-sqrt(sum((t3_n2 - theta)^2)/sim_n)
  59. R_mse4_n2<-sqrt(sum((t4_n2 - theta)^2)/sim_n)
  60.  
  61. result<-{data.frame("n" = c(n1," "," "," "," ",n2," "," "," "," "),
  62.                     "Mean estimate" = c(" ",t1_bar_n1,t2_bar_n1,t3_bar_n1,t4_bar_n1," ",t1_bar_n2,t2_bar_n2,t3_bar_n2,t4_bar_n2),
  63.                     "Bias" = c(" ",bias1_n1,bias2_n1,bias3_n1,bias4_n1," ",bias1_n2,bias2_n2,bias3_n2,bias4_n2),
  64.                     "Sample SE" = c(" ",SE1_n1,SE2_n1,SE3_n1,SE4_n1," ",SE1_n2,SE2_n2,SE3_n2,SE4_n2),
  65.                     "Root MSE" = c(" ",R_mse1_n1,R_mse2_n1,R_mse3_n1,R_mse4_n1," ",R_mse1_n2,R_mse2_n2,R_mse3_n2,R_mse4_n2),
  66.                     row.names = c(" ","theta1_hat","theta2_hat","theta3_hat","theta4_hat","  ","theta1-hat","theta2-hat","theta3-hat","theta4-hat"))
  67. }
  68. result
  69. # write.csv(result,"C:/Users/user/Desktop/result.csv")
  70.  
  71. ## 2 ########################
  72.  
  73. sim_n2 <- 5000
  74.  
  75. set.seed(123)
  76. X <- rep(0, sim_n2)
  77. for(i in 1:sim_n2)
  78. {
  79.   v <- vector()
  80.   j <- 1
  81.   k <- 0
  82.   repeat{
  83.     x <- sample(c(1:6), 1)
  84.     k <- k + 1
  85.     if(length(v[v == x]) == 0)
  86.     {
  87.       v[j] <- x
  88.       j <- j+1
  89.     }
  90.     if(length(v) == 6)
  91.       break
  92.   }
  93.   X[i] <- k
  94. }
  95. exact2 <- 6*sum(1/c(1:6))
  96. exact2 # 14.7
  97. mean(X) # 14.6988
  98.  
  99. ## 3 ###########################
  100.  
  101. sim_n3 <- 5000
  102. n <- 30
  103. set.seed(123)
  104. X <- matrix(rnorm(replicate*n), ncol=n, nrow=sim_n3)
  105. Y <- X^2
  106. ans <- matrix(0, nrow =7, ncol = 2)
  107.  
  108. # (1) #####
  109. temp <- 20*X[,19] + 21*X[,20]
  110. temp <- temp[temp<12/16]
  111. ans[1,1] <- pnorm(12/16, mean = 0, sd = sqrt(20^2+21^2))
  112. ans[1,2] <- length(temp)/sim_n3
  113.  
  114. # (2) #####
  115. temp <- apply(Y[,1:12], 1,function(x){sqrt(sum(x))})
  116. ans[2,1] <- sqrt(qchisq(.8, df = 12))
  117. ans[2,2] <- quantile(temp, prob = .8)
  118.  
  119. # (3) #####
  120. temp1 <- apply(Y[,1:12], 1, sum)
  121. temp2 <- apply(Y[,1:16], 1, sum)
  122. temp <- temp1/temp2
  123. ans[3,1] <- NA
  124. ans[3,2] <- median(temp)
  125.  
  126. # (4) #####
  127. temp <- apply(X, 1, function(x) quantile(x, prob= .5+.2021))
  128. ans[4,1] <- qnorm(.5+.2021)
  129. ans[4,2] <- mean(temp)
  130.  
  131. # (5) #####
  132. temp <- (Y[,1])/(Y[,2])
  133. ans[5,1] <- qf(.2021,1,1)
  134. ans[5,2] <- quantile(temp, prob = .2021)
  135.  
  136. temp <- (X[,1])/(X[,2])
  137. ans[6,1] <- qcauchy(.2021)
  138. ans[6,2] <- quantile(temp, prob = .2021)
  139.  
  140. # (6) #####
  141. temp1 <- X[,1]
  142. temp2 <- apply(Y[,7:15],1,sum)
  143. temp2 <- sqrt(temp2)
  144. temp <- temp1[temp1<temp2]
  145. ans[7,1] <- pt(3,df=9)
  146. ans[7,2] <- length(temp)/sim_n3
  147.  
  148. ans <- data.frame(ans)
  149. # write.csv(ans,"C:/Users/user/Desktop/ans.csv")
  150.  
  151.  
  152.  
  153.  
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement