# code for running experiments is adapted from 
# Tagasovska, N., Chavez-Demoulin, V., & Vatter, T. (2020, November). 
# Distinguishing cause from effect using quantiles: Bivariate quantile causal discovery. 
# In International Conference on Machine Learning


source("bqcd.R")
source("baselines.R")
source("read_tueb.R")

cds <- integer(length(uv))

set.seed(3)


do_synthetic_benchmark <- function(method){
  # load ground truth for pairs
  method_to_run <- switch(method,
                          "bQCCD" = QCCD,
                          "CAM" = CAMWrapper,
                          "EMD" = RKHSEWrap,
                          "LINGAM" = lingamWrapper,
                          "RESIT" = ICMLWrapper,
                          "GRAN" = GaussianityWrap,
                          "Slope" = SlopeWrap,
                          "Sloppy" = SloppyWrap,
                          "IGCI" = IGCIWrap_G,
                          "bQCD_qrf" = QCD_qrf,
                          "bQCD_qnn" = QCD_qnn)
  for(i in 1:length(uv)){
    pair <- readI(uv[i])[,1:2]
    res <- method_to_run(pair)
    cds[i] <- res$cd
  }
  
  corr <- rep(0,length(uv))
  corr[gt == cds] <- 1
  weighted_acc <- sum(corr * weights) / sum(weights)
  print(weighted_acc)
  return(weighted_acc)
}

gt <- ref.uv$V6
for(i in 1:length(uv)){
  if(gt[i] == "->"){
    gt[i] <- 1
  } else{
    gt[i] <- 0
  }
}
weights = meta.uv$V6
acc_slope = c()
acc_sloppy = c()
acc_bqccd = c()
acc_cam = c()
acc_igci = c()
acc_resit = c()

methods <- list("Slope","bQCCD","CAM","Sloppy","IGCI")
resultsAll <- lapply(methods, function (m) do_synthetic_benchmark(m))
resultsAll = unlist(resultsAll)
acc_slope[1] = resultsAll[1]
acc_bqccd[1] = resultsAll[2]
acc_cam[1] = resultsAll[3]
acc_sloppy[1] = resultsAll[4]
acc_igci[1] = resultsAll[5]
datasets = c("Tuebingen")
resDF <- data.frame(Dataset =datasets , Slope = acc_slope, Sloppy = acc_sloppy,Bqccd = acc_bqccd, CAM=acc_cam, IGCI=acc_igci)
write.table(format(resDF, digits=2), file = "Results/competitors_tueb.tab",
                sep = "\t")