Advertisement
mjaniec

Stationarity test stability

May 17th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.37 KB | None | 0 0
  1. # see more: http://reakkt.com
  2.  
  3. library(tseries)
  4.  
  5. ### PARALLEL
  6.  
  7. library(doSNOW)
  8.  
  9. registerDoSNOW(makeCluster(6,type="SOCK"))
  10.  
  11. ###
  12.  
  13. k <- 1000 # liczba prob
  14. n <- 1000 # dlugosc szeregu
  15.  
  16. removal_types <- c("back","front","random")
  17.  
  18. removal_type <- ceiling(runif(k,0,3))
  19.  
  20. removed_elements <- numeric(k)
  21.  
  22. testfun <- function(x) { adf.test(x)$p.value }
  23. testmet <- function(x) { x<=0.01 }
  24. test.initial <- 1
  25.  
  26. removed_elements <-
  27. foreach (i=1:k, .combine=rbind, .packages="tseries") %dopar% {
  28.  
  29.   # constrained data generating process
  30.  
  31.   contamination.ratio <- 0.01
  32.   x.change <- rnorm(n)
  33.  
  34.   test_stat <- test.initial
  35.  
  36.   while (!testmet(test_stat)) {
  37.    
  38.     contaminated.idx <- NULL
  39.    
  40.     while (length(contaminated.idx)!=n*contamination.ratio) {
  41.      
  42.       contaminated.idx <- unique(round(runif(n*contamination.ratio,1,n)))
  43.      
  44.     }
  45.    
  46.     x.change[contaminated.idx] <- rnorm(length(contaminated.idx),sd=6)
  47.    
  48.     x <- cumsum(x.change)
  49.    
  50.     test_stat <- testfun(x)
  51.    
  52.   }
  53.  
  54.   #
  55.  
  56.   # usuwanie elementow od konca
  57.   if (removal_type[i]==1) {
  58.    
  59.     j <- n
  60.  
  61.     while (testmet(test_stat) && j>1) {
  62.      
  63.       j <- j-1
  64.      
  65.       y <- x[1:j]
  66.      
  67.       test_stat <- testfun(y)
  68.      
  69.     }
  70.    
  71.   }  
  72.  
  73.   # usuwanie elementow od poczatku
  74.   if (removal_type[i]==2) {
  75.    
  76.     j <- 1
  77.    
  78.     while (testmet(test_stat) && j<n) {
  79.      
  80.       j <- j+1
  81.      
  82.       y <- x[j:n]
  83.      
  84.       test_stat <- testfun(y)
  85.      
  86.     }
  87.  
  88.   }
  89.  
  90.   # losowe usuwanie elementow
  91.   if (removal_type[i]==3) {
  92.    
  93.     y <- x
  94.    
  95.     while (testmet(test_stat) && length(y)>2) {
  96.      
  97.       j <- ceiling(runif(1,0,length(y)))
  98.      
  99.       y <- y[-j]
  100.      
  101.       test_stat <- testfun(y)
  102.      
  103.     }
  104.    
  105.     length(y)
  106.    
  107.   }
  108.  
  109.   n-length(y)
  110.    
  111. }
  112.  
  113. # cbind(removed_elements,removal_types[removal_type])
  114.  
  115. selected_cases <- which(removal_type==2) # front
  116. plot(density(removed_elements[selected_cases,]/n),main="front")
  117. summary(removed_elements[selected_cases,]/n)
  118.  
  119. selected_cases <- which(removal_type==1) # back
  120. plot(density(removed_elements[selected_cases,]/n),main="back")
  121. summary(removed_elements[selected_cases,]/n)
  122.  
  123. selected_cases <- which(removal_type==3) # random
  124. plot(density(removed_elements[selected_cases,]/n),main="random")
  125. summary(removed_elements[selected_cases,]/n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement