celestialgod

dist_test (R part)

Oct 27th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.10 KB | None | 0 0
  1. library(Rcpp)
  2. sourceCpp("dist_test.cpp")
  3.  
  4. sigLvl <- 0.03
  5. # case 1
  6. X <- rnorm(40, 4, 10)
  7. Y <- rnorm(20, 4, 10)
  8.  
  9. st <- proc.time()
  10. testStat <- dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000)
  11. proc.time() - st
  12.  
  13. if (any(abs(testStat) > qnorm(1-sigLvl))) {
  14.   message("Two datasets are different.")
  15. } else {
  16.   message("Two datasets are the same.")
  17. }
  18.  
  19. # case 2
  20. X <- rnorm(40, 4, 7)
  21. Y <- rnorm(20, 4, 7)
  22.  
  23. st <- proc.time()
  24. testStat <- dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000)
  25. proc.time() - st
  26.  
  27. if (any(abs(testStat) > qnorm(1-sigLvl))) {
  28.   message("Two datasets are different.")
  29. } else {
  30.   message("Two datasets are the same.")
  31. }
  32.  
  33. ## general test
  34.  
  35. mean(sapply(1:1e4, function(i){
  36.   X <- rnorm(40, 4, 7)
  37.   Y <- rnorm(20, 4, 10)
  38.   any(abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000)) > qnorm(1-0.01))
  39. }))
  40. # 0.1706
  41.  
  42. mean(sapply(1:1e4, function(i){
  43.   X <- rnorm(40, 4, 7)
  44.   Y <- rnorm(20, 4, 14)
  45.   any(abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000)) > qnorm(1-0.01))
  46. }))
  47. # 0.4274
  48.  
  49. mean(sapply(1:1e4, function(i){
  50.   X <- rnorm(40, 4, 7)
  51.   Y <- rnorm(20, 8, 7)
  52.   any(abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000)) > qnorm(1-0.01))
  53. }))
  54. # 0.5243
  55.  
  56. mean(sapply(1:1e4, function(i){
  57.   X <- rnorm(40, 4, 10)
  58.   Y <- rnorm(20, 4, 10)
  59.   any(abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000)) > qnorm(1-0.01))
  60. }))
  61. # 0.1027
  62.  
  63. ## general test 2
  64.  
  65. mean(sapply(1:1e4, function(i){
  66.   X <- rnorm(40, 4, 7)
  67.   Y <- rnorm(20, 4, 10)
  68.   sum(abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000)) > qnorm(1-0.01)) >= 2
  69. }))
  70. # 0.041
  71.  
  72. mean(sapply(1:1e4, function(i){
  73.   X <- rnorm(40, 4, 7)
  74.   Y <- rnorm(20, 4, 14)
  75.   sum(abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000)) > qnorm(1-0.01)) >= 2
  76. }))
  77. # 0.139
  78.  
  79. mean(sapply(1:1e4, function(i){
  80.   X <- rnorm(40, 4, 7)
  81.   Y <- rnorm(20, 8, 7)
  82.   sum(abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000)) > qnorm(1-0.01)) >= 2
  83. }))
  84. # 0.291
  85.  
  86. mean(sapply(1:1e4, function(i){
  87.   X <- rnorm(40, 4, 10)
  88.   Y <- rnorm(20, 4, 10)
  89.   sum(abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000)) > qnorm(1-0.01)) >= 2
  90. }))
  91. # 0.0193
  92.  
  93. ## general test 3
  94.  
  95. mean(sapply(1:1e4, function(i){
  96.   X <- rnorm(40, 4, 7)
  97.   Y <- rnorm(20, 4, 10)
  98.   absStat <- abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000))
  99.   sum(absStat > qnorm(1-0.01)) >= 1 && sum(absStat > qnorm(1-0.03)) >= 2
  100. }))
  101. # 0.092
  102.  
  103. mean(sapply(1:1e4, function(i){
  104.   X <- rnorm(40, 4, 7)
  105.   Y <- rnorm(20, 4, 14)
  106.   absStat <- abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000))
  107.   sum(absStat > qnorm(1-0.01)) >= 1 && sum(absStat > qnorm(1-0.03)) >= 2
  108. }))
  109. # 0.2643
  110.  
  111. mean(sapply(1:1e4, function(i){
  112.   X <- rnorm(40, 4, 7)
  113.   Y <- rnorm(20, 8, 7)
  114.   absStat <- abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000))
  115.   sum(absStat > qnorm(1-0.01)) >= 1 && sum(absStat > qnorm(1-0.03)) >= 2
  116. }))
  117. # 0.432
  118.  
  119. mean(sapply(1:1e4, function(i){
  120.   X <- rnorm(40, 4, 10)
  121.   Y <- rnorm(20, 4, 10)
  122.   absStat <- abs(dist_test(X, Y, c(0.1, 0.25, 0.5, 0.75, 0.9), 10000))
  123.   sum(absStat > qnorm(1-0.01)) >= 1 && sum(absStat > qnorm(1-0.03)) >= 2
  124. }))
  125. # 0.046
Add Comment
Please, Sign In to add comment