Guest User

Untitled

a guest
Jul 22nd, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.58 KB | None | 0 0
  1. library(dlm)
  2. library(doParallel)
  3. cl = makeCluster(detectCores()-1)
  4. registerDoParallel(cl)
  5.  
  6. t100 <- proc.time()
  7. x <- foreach(j = seq(1,1000),.combine = 'rbind',.packages = c("dlm","doParallel")) %dopar% {
  8.  
  9.   phi = runif(1,.5,.9) 
  10.   sig_epsilon_2 = runif(1,1.1,2)
  11.   print(j)
  12.  
  13. foreach(i = seq(1,100),.combine='+',.packages = c("dlm","doParallel")) %do% {
  14.         count1_3 = 0
  15.         count2_3 = 0
  16.     print("i")
  17.     print(i)
  18.     print("j")
  19.     print(j)
  20.     t<- proc.time()
  21.     nobs <- 250
  22.     #This is the AR(1) model
  23.     y_t <- arima.sim(n=nobs,list(ar=phi,ma=0),sd=sqrt(sig_epsilon_2))
  24.     #We constrain the 1st parameter to be less than one and  
  25.     #the second parameter to be positive.
  26.     parm_rest <- function(parm){
  27.         return( c(exp(parm[1])/(1+exp(parm[1])),exp(parm[2])) )
  28.     }
  29.        
  30.     original_parameters <- c(phi,sig_epsilon_2)
  31.     ssm_ar1<- function(parm) {
  32.         parm<- parm_rest(parm)
  33.         return(dlm(FF=1,V=0,GG=parm[1],W=parm[2],
  34.         m0=0,C0=solve(1-parm[1]^2)*parm[2]))
  35.     }
  36.  
  37.     result_3 <- dlmMLE(y_t,parm=c(0,0),build=ssm_ar1,hessian=T)
  38.  
  39.     coef_3 <- parm_rest(result_3$par)
  40.  
  41.     dg1_3 <- exp(result_3$par[1])/(1+exp(result_3$par[1]))^2
  42.     dg2_3<- exp(result_3$par[2])
  43.     dg_3<- diag(c(dg1_3,dg2_3))
  44.     myvar_3 <- dg_3%*%solve(result_3$hessian)%*%dg_3
  45.     myvar_3 <- sqrt(diag(myvar_3))
  46.  
  47.     lower_3 = coef_3 - 1.96 * myvar_3
  48.     upper_3 = coef_3 + 1.96 * myvar_3
  49.  
  50.  
  51. ############################################################################################################################################
  52. #       count1_3 is the count of the number of times out of 100 that phi falls OUTSIDE it's 95% CI built using dlmMLE
  53. #       count1_3 is ~Binomial(100,.05)  and should have a mean of size * p = 100 * .05 = 5 and
  54. #       a variance of size * p * q = 100 * .05* .95 = 4.75
  55. #       Similarly for count2_3.
  56. #      
  57. #       Define Y_n = X_1 + ... + X_n where X_i takes the value 1 in the event that
  58. #       the (generated) original parameter does not lie in it's 95% CI on the ith iteration and 0 otherwise.
  59. #       We compute Y_100 (since i goes from 1 to 100)        
  60. #       We can regard this as one realization of Binomial distribution with size=100 and p=.05 (probability of a parameter
  61. #       falling outside it's CI).    
  62. #       We repeat the above process j= 1000 times and accumulate the results in count1_1_vector.This will be ~Binomial(n=1000,size=100,p=.05)
  63. #          
  64. ############################################################################################################################################
  65.        
  66.         print("Time taken :")
  67.         print(proc.time()-t)
  68.  
  69.         ifelse(lower_3[1] <= original_parameters[1] && original_parameters[1] <= upper_3[1],print("phi within CI"),
  70.         {print(original_parameters[1]); print("phi outside") ; count1_3= count1_3 +1} )
  71.  
  72.     ifelse(lower_3[2] <= original_parameters[2] && original_parameters[2] <= upper_3[2],print("sigma_epsilon_2 within CI"),{ print("sigma_epsilon_2 outside") ; count2_3= count2_3 +1} )
  73.      
  74.     return(c(count1_3,count2_3))
  75.    
  76.    
  77. }
  78. }
  79.  
  80. print(proc.time()-t100)
  81.  
  82. expected_distribution<- rbinom(1000,100,.05)
  83.  
  84. count1_3_vector <- x[,1]
  85. count2_3_vector <- x[,2]
  86.  
  87. pdf("Graph-phi-dlmMLE.pdf",width = 5.6,height = 3.8)
  88. par(mai=c(.8, .8, .3, .2))
  89. qqplot(expected_distribution,count1_3_vector,main=expression(paste("Graph for ",phi)),xlab="Expected distribution",ylab="Observed values")
  90. qqline(count1_3_vector,distribution = function(probs) { qbinom(probs, size=100, prob=0.05) },col = "red",lwd = 2)
  91. dev.off()
  92.  
  93.  
  94. pdf("Graph-phi-dlmMLE-jitter.pdf",width = 5.6,height = 3.8)
  95. par(mai=c(.8, .8, .3, .2))
  96. qqplot(jitter(expected_distribution),jitter(count1_3_vector),main=expression(paste("Graph for ",phi," : jittered")),xlab="Expected distribution",ylab="Observed values")
  97. qqline(count1_3_vector,distribution = function(probs) { qbinom(probs, size=100, prob=0.05) },col = "red",lwd = 2)
  98. dev.off()
  99.  
  100. pdf("Graph-sigma-dlmMLE.pdf",width = 5.6,height = 3.8)
  101. par(mai=c(.8, .8, .3, .2))
  102. qqplot(expected_distribution,count2_3_vector,main=expression(paste("Graph for ",sigma^2)),xlab="Expected distribution",ylab = "Observed values")
  103. qqline(count2_3_vector,distribution = function(probs) { qbinom(probs, size=100, prob=0.05) },col = "red",lwd = 2)
  104. dev.off()
  105.  
  106.  
  107. pdf("Graph-sigma-dlmMLE-jitter.pdf",width = 5.6,height = 3.8)
  108. par(mai=c(.8, .8, .3, .2))
  109. qqplot(jitter(expected_distribution),jitter(count2_3_vector),main=expression(paste("Graph for ",sigma^2," : jittered")),xlab="Expected distribution",ylab = "Observed values")
  110. qqline(count2_3_vector,distribution = function(probs) { qbinom(probs, size=100, prob=0.05) },col = "red",lwd = 2)
  111. dev.off()
Add Comment
Please, Sign In to add comment