Advertisement
st110036

StatComp_2017Q1Q2

Sep 25th, 2022 (edited)
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.12 KB | None | 0 0
  1. #2017-Q1------------------------------------------------------------------------------------
  2. # X1~exp(5); X2~trunc exp; X3~Weibull
  3. exact <- 5+5*exp(-5)*(exp(5)-1)/(1-6*exp(-5))+2*gamma(1+1/2)
  4.  
  5. f2 <- function(x){
  6.   exp(-5)*(5^x)/(factorial(x)*(1-6*exp(-5)))
  7. }
  8.  
  9. X <- vector()
  10. set.seed(1234)
  11. for(i in 1:10000){
  12.   x <- 0
  13.   while(TRUE){
  14.     u <- runif(1)
  15.     if(u<1/3){
  16.       x <- x+rexp(1,rate=1/5)
  17.       break
  18.     }
  19.     else{
  20.       if(u<2/3){
  21.         # simulation x2
  22.         u1 <- runif(1)
  23.         x2 <- 2
  24.         p <- f2(2)
  25.         while(u1>p){
  26.           x2 <- x2+1
  27.           p <- p+f2(x2)
  28.         }
  29.         x <- x+x2
  30.       }
  31.       else{
  32.         x <- x + rweibull(1,shape=2, scale = 2)
  33.       }
  34.     }
  35.   }
  36.   X[i] <- x
  37. }
  38. exact # 11.948
  39. mean(X) # 12.05248
  40.  
  41. #2017-Q2------------------------------------------------------------------------------------
  42. X <- c(9.853093,12.993558,11.294747,11.171975,8.913325,10.976033,12.353118)
  43. as.vector(X)
  44.  
  45. L_f <- function(theta){
  46.   f <- exp(-(X-theta))/(1+exp(-(X-theta)))^2
  47.   lnL <- sum(log(f))
  48.   return(lnL)
  49. }
  50. p <- optimize(f = L_f,c(-20,20), maximum = TRUE)
  51. p
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement