fork(3) download
  1. generate.ClP <- function(C, P) { # P is x coord (col), C is y coord (row), ClP[y, x]
  2. ClP <- matrix(nrow=e_s,ncol=e_s);
  3. for(i in unique(P)) for(j in unique(C)) ClP[j,i] <- 0;
  4. for(j in seq(1, length(unique(P)))) {
  5. e_p <- sort(unique(P)[j]);
  6. C_p <- sort(C[P==e_p]);
  7. freq <- t(as.data.frame(table(C_p))[,2]);
  8. prob <- freq/sum(freq);
  9. for(i in seq(1, length(unique(C_p)))) {
  10. e_c <- unique(C_p)[i];
  11. ClP[e_c, e_p] <- prob[i];
  12. };
  13. };
  14. ClP;
  15. };
  16.  
  17. nd.ClP <- function(ClP) {
  18. l <- c();
  19. for(i in seq(1, dim(ClP)[1])) {
  20. nonna <- ClP[!is.na(ClP[,i]),i];
  21. if(!identical(nonna, numeric(0))) l <- c(l, range(nonna)[1] == range(nonna)[2]);
  22. };
  23. range(l)[1] == TRUE && range(l)[2] == TRUE;
  24. };
  25.  
  26. nd <- function(C, P) {
  27. nd.ClP(generate.ClP(C, P));
  28. };
  29.  
  30. entropy <- function (n) -n * (1/n) * log(1/n);
  31.  
  32. mi <- function(C, P) {
  33. result <- 0;
  34.  
  35. ClP <- generate.ClP(C,P);
  36.  
  37. for(i in 1:dim(ClP)[2]) {
  38. a <- ClP[,i] * log(ClP[,i]);
  39. a_ <- a[!is.nan(a) & !is.na(a)];
  40. e <- entropy(length(unique(C)));
  41. if(length(a_) > 0 && !is.nan(e))
  42. result <- result + (e + sum(a_));
  43. };
  44.  
  45. result;
  46. };
  47.  
  48. args <- commandArgs(trailingOnly = TRUE)
  49.  
  50. success_count <- c();
  51. for(choices in 1:10) {
  52. success_count[choices] <- 0;
  53. for(i in 1:1) {
  54. e_s <- choices;
  55. P1 <- sample(seq(1,e_s),e_s,replace=TRUE);
  56. C1 <- sample(seq(1,e_s),e_s,replace=TRUE);
  57. F1 <- sample(seq(1,e_s),e_s,replace=TRUE);
  58. P1lC1 <- generate.ClP(P1, C1);
  59. C1lP1 <- generate.ClP(C1, P1);
  60. F1lC1 <- generate.ClP(F1, C1);
  61. C1lF1 <- generate.ClP(C1, F1);
  62. mi(C1,P1);
  63. mi(P1,C1);
  64. mi(F1,C1);
  65. mi(C1,F1);
  66. (round(mi(C1,P1)) < round(mi(C1,F1)));
  67. (round(mi(F1,C1)) < round(mi(C1,F1)));
  68. (round(mi(C1,P1)) < round(mi(C1,F1))) && (round(mi(F1,C1)) < round(mi(C1,F1)));
  69. if ((round(mi(C1,P1)) < round(mi(C1,F1))) && (round(mi(F1,C1)) < round(mi(C1,F1)))) success_count[choices] <- success_count[choices] + 1;
  70. }
  71. }
  72. success_count;
  73.  
Success #stdin #stdout 1.39s 22840KB
stdin
Standard input is empty
stdout
 [1] 0 0 0 0 0 0 0 0 1 1