setwd("/Users/richard/Desktop/Bell/ACOnet FileSender_files/Giustina_Data+CoincCode_Eberhard_2013")

count <- function(){
  Names <- system("ls", intern = TRUE)
  NeventsA <- 0
  NeventsB <- 0
  Npairs <- 0
  for(Piece in 1:30){
    BobData <- read.table(Names[Piece])
    AliceData <- read.table(Names[Piece + 30])
    print(paste(Names[Piece], Names[Piece + 30]))
    BobData <- BobData$V1
    BobMaxRun <- BobData[1]
    BobData <- BobData[-1]
    dim(BobData) <- c(BobMaxRun, 100)
    AliceData <- AliceData$V1
    AliceMaxRun <- AliceData[1]
    AliceData <- AliceData[-1]
    dim(AliceData) <- c(AliceMaxRun, 100)
    for (Block in 1:100) {
      TimesA <- AliceData[ , Block]
      TimesB <- BobData[ , Block]
      TimesA <- TimesA[TimesA != 0] - 1
      TimesB <- TimesB[TimesB != 0]
      TimesA <- TimesA%/%49
      TimesB <- TimesB%/%49
      TimesA <- unique(TimesA)
      TimesB <- unique(TimesB)
      NeventsA <- NeventsA + length(TimesA)
      NeventsB <- NeventsB + length(TimesB)
      Diffs <- outer(TimesA, TimesB, "-")
      Npairs <- Npairs + sum(Diffs == 0)
    }
    print(paste(Piece, Npairs, NeventsA, NeventsB))
  }
  return(list(Npairs = Npairs, NeventsA = NeventsA, NeventsB = NeventsB))
}

setwd("a1b1")

results11 <- count()
## [1] "s1_a1b1_180217.txt s2_a1b1_180217.txt"
## [1] "1 35043 50869 56437"
## [1] "s1_a1b1_180236.txt s2_a1b1_180236.txt"
## [1] "2 70619 102151 113749"
## [1] "s1_a1b1_180254.txt s2_a1b1_180254.txt"
## [1] "3 106445 153763 170960"
## [1] "s1_a1b1_180313.txt s2_a1b1_180313.txt"
## [1] "4 141273 204460 226894"
## [1] "s1_a1b1_180331.txt s2_a1b1_180331.txt"
## [1] "5 176823 255923 283798"
## [1] "s1_a1b1_180350.txt s2_a1b1_180350.txt"
## [1] "6 212121 307073 340208"
## [1] "s1_a1b1_181832.txt s2_a1b1_181832.txt"
## [1] "7 247166 357465 396812"
## [1] "s1_a1b1_181850.txt s2_a1b1_181850.txt"
## [1] "8 281936 407759 452926"
## [1] "s1_a1b1_181910.txt s2_a1b1_181910.txt"
## [1] "9 316973 458384 509203"
## [1] "s1_a1b1_181928.txt s2_a1b1_181928.txt"
## [1] "10 351949 508877 565638"
## [1] "s1_a1b1_181947.txt s2_a1b1_181947.txt"
## [1] "11 386934 559319 621589"
## [1] "s1_a1b1_182005.txt s2_a1b1_182005.txt"
## [1] "12 422091 610051 677876"
## [1] "s1_a1b1_183219.txt s2_a1b1_183219.txt"
## [1] "13 457124 660563 734569"
## [1] "s1_a1b1_183238.txt s2_a1b1_183238.txt"
## [1] "14 492479 711541 791330"
## [1] "s1_a1b1_183257.txt s2_a1b1_183257.txt"
## [1] "15 527797 762274 848043"
## [1] "s1_a1b1_183315.txt s2_a1b1_183315.txt"
## [1] "16 562947 813155 904762"
## [1] "s1_a1b1_183334.txt s2_a1b1_183334.txt"
## [1] "17 598351 864069 961454"
## [1] "s1_a1b1_183353.txt s2_a1b1_183353.txt"
## [1] "18 633523 914827 1018054"
## [1] "s1_a1b1_185001.txt s2_a1b1_185001.txt"
## [1] "19 668933 966101 1074971"
## [1] "s1_a1b1_185020.txt s2_a1b1_185020.txt"
## [1] "20 704178 1017034 1131972"
## [1] "s1_a1b1_185039.txt s2_a1b1_185039.txt"
## [1] "21 739589 1068240 1188759"
## [1] "s1_a1b1_185057.txt s2_a1b1_185057.txt"
## [1] "22 774780 1119220 1245723"
## [1] "s1_a1b1_185116.txt s2_a1b1_185116.txt"
## [1] "23 810151 1170500 1302740"
## [1] "s1_a1b1_185135.txt s2_a1b1_185135.txt"
## [1] "24 845322 1221538 1359271"
## [1] "s1_a1b1_190557.txt s2_a1b1_190557.txt"
## [1] "25 880367 1271878 1415137"
## [1] "s1_a1b1_190615.txt s2_a1b1_190615.txt"
## [1] "26 914908 1321488 1470581"
## [1] "s1_a1b1_190634.txt s2_a1b1_190634.txt"
## [1] "27 949579 1371252 1526514"
## [1] "s1_a1b1_190653.txt s2_a1b1_190653.txt"
## [1] "28 984647 1421639 1582414"
## [1] "s1_a1b1_190712.txt s2_a1b1_190712.txt"
## [1] "29 1019070 1471327 1637921"
## [1] "s1_a1b1_190731.txt s2_a1b1_190731.txt"
## [1] "30 1053824 1521515 1693546"
results11
## $Npairs
## [1] 1053824
## 
## $NeventsA
## [1] 1521515
## 
## $NeventsB
## [1] 1693546
setwd("../a1b2")
results12 <- count()
## [1] "s1_a1b2_180527.txt s2_a1b2_180527.txt"
## [1] "1 37865 50501 148659"
## [1] "s1_a1b2_180546.txt s2_a1b2_180546.txt"
## [1] "2 75462 100438 296784"
## [1] "s1_a1b2_180604.txt s2_a1b2_180604.txt"
## [1] "3 112867 150490 444633"
## [1] "s1_a1b2_180622.txt s2_a1b2_180622.txt"
## [1] "4 150519 200661 592590"
## [1] "s1_a1b2_180641.txt s2_a1b2_180641.txt"
## [1] "5 188333 251135 741707"
## [1] "s1_a1b2_180659.txt s2_a1b2_180659.txt"
## [1] "6 226151 301386 890362"
## [1] "s1_a1b2_182151.txt s2_a1b2_182151.txt"
## [1] "7 264051 351947 1039030"
## [1] "s1_a1b2_182210.txt s2_a1b2_182210.txt"
## [1] "8 302085 402351 1187578"
## [1] "s1_a1b2_182229.txt s2_a1b2_182229.txt"
## [1] "9 340009 452917 1336423"
## [1] "s1_a1b2_182247.txt s2_a1b2_182247.txt"
## [1] "10 378378 503702 1485843"
## [1] "s1_a1b2_182305.txt s2_a1b2_182305.txt"
## [1] "11 416612 554394 1634564"
## [1] "s1_a1b2_182324.txt s2_a1b2_182324.txt"
## [1] "12 454923 604994 1783393"
## [1] "s1_a1b2_183829.txt s2_a1b2_183829.txt"
## [1] "13 492845 655744 1932950"
## [1] "s1_a1b2_183847.txt s2_a1b2_183847.txt"
## [1] "14 530992 706577 2082897"
## [1] "s1_a1b2_183906.txt s2_a1b2_183906.txt"
## [1] "15 569261 757651 2233081"
## [1] "s1_a1b2_183925.txt s2_a1b2_183925.txt"
## [1] "16 607312 808301 2383651"
## [1] "s1_a1b2_183944.txt s2_a1b2_183944.txt"
## [1] "17 645536 859233 2534406"
## [1] "s1_a1b2_184003.txt s2_a1b2_184003.txt"
## [1] "18 683966 910586 2684855"
## [1] "s1_a1b2_185407.txt s2_a1b2_185407.txt"
## [1] "19 722209 961679 2835475"
## [1] "s1_a1b2_185427.txt s2_a1b2_185427.txt"
## [1] "20 760526 1012720 2986388"
## [1] "s1_a1b2_185446.txt s2_a1b2_185446.txt"
## [1] "21 798588 1063707 3136120"
## [1] "s1_a1b2_185505.txt s2_a1b2_185505.txt"
## [1] "22 836996 1114885 3286655"
## [1] "s1_a1b2_185526.txt s2_a1b2_185526.txt"
## [1] "23 875267 1165936 3437223"
## [1] "s1_a1b2_185545.txt s2_a1b2_185545.txt"
## [1] "24 913420 1216925 3587635"
## [1] "s1_a1b2_190926.txt s2_a1b2_190926.txt"
## [1] "25 951362 1267052 3736065"
## [1] "s1_a1b2_190945.txt s2_a1b2_190945.txt"
## [1] "26 989068 1317028 3884499"
## [1] "s1_a1b2_191004.txt s2_a1b2_191004.txt"
## [1] "27 1027022 1367390 4032761"
## [1] "s1_a1b2_191023.txt s2_a1b2_191023.txt"
## [1] "28 1065044 1418023 4181452"
## [1] "s1_a1b2_191041.txt s2_a1b2_191041.txt"
## [1] "29 1102578 1467929 4329341"
## [1] "s1_a1b2_191100.txt s2_a1b2_191100.txt"
## [1] "30 1140429 1517938 4477700"
results12
## $Npairs
## [1] 1140429
## 
## $NeventsA
## [1] 1517938
## 
## $NeventsB
## [1] 4477700
setwd("../a2b1")

results21 <- count()
## [1] "s1_a2b1_181411.txt s2_a2b1_181411.txt"
## [1] "1 39371 156912 56287"
## [1] "s1_a2b1_181429.txt s2_a2b1_181429.txt"
## [1] "2 78852 312670 112710"
## [1] "s1_a2b1_181448.txt s2_a2b1_181448.txt"
## [1] "3 118441 468908 168948"
## [1] "s1_a2b1_181506.txt s2_a2b1_181506.txt"
## [1] "4 157364 624190 224316"
## [1] "s1_a2b1_181525.txt s2_a2b1_181525.txt"
## [1] "5 196379 779986 280022"
## [1] "s1_a2b1_181543.txt s2_a2b1_181543.txt"
## [1] "6 235658 935966 335862"
## [1] "s1_a2b1_182840.txt s2_a2b1_182840.txt"
## [1] "7 275070 1091834 392226"
## [1] "s1_a2b1_182859.txt s2_a2b1_182859.txt"
## [1] "8 314781 1248912 449102"
## [1] "s1_a2b1_182918.txt s2_a2b1_182918.txt"
## [1] "9 354045 1404831 505361"
## [1] "s1_a2b1_182937.txt s2_a2b1_182937.txt"
## [1] "10 393321 1561331 561581"
## [1] "s1_a2b1_182955.txt s2_a2b1_182955.txt"
## [1] "11 432623 1716521 617791"
## [1] "s1_a2b1_183014.txt s2_a2b1_183014.txt"
## [1] "12 472382 1872991 674617"
## [1] "s1_a2b1_184612.txt s2_a2b1_184612.txt"
## [1] "13 511906 2030163 731199"
## [1] "s1_a2b1_184632.txt s2_a2b1_184632.txt"
## [1] "14 551623 2188089 787968"
## [1] "s1_a2b1_184650.txt s2_a2b1_184650.txt"
## [1] "15 590926 2345854 844556"
## [1] "s1_a2b1_184709.txt s2_a2b1_184709.txt"
## [1] "16 630441 2503624 901248"
## [1] "s1_a2b1_184728.txt s2_a2b1_184728.txt"
## [1] "17 670018 2661501 958078"
## [1] "s1_a2b1_184747.txt s2_a2b1_184747.txt"
## [1] "18 709739 2819672 1014891"
## [1] "s1_a2b1_190132.txt s2_a2b1_190132.txt"
## [1] "19 748408 2975304 1070270"
## [1] "s1_a2b1_190151.txt s2_a2b1_190151.txt"
## [1] "20 787321 3131250 1126128"
## [1] "s1_a2b1_190210.txt s2_a2b1_190210.txt"
## [1] "21 826277 3286707 1182200"
## [1] "s1_a2b1_190229.txt s2_a2b1_190229.txt"
## [1] "22 865351 3442062 1238354"
## [1] "s1_a2b1_190248.txt s2_a2b1_190248.txt"
## [1] "23 904481 3598287 1294484"
## [1] "s1_a2b1_190306.txt s2_a2b1_190306.txt"
## [1] "24 943249 3753593 1350251"
## [1] "s1_a2b1_191606.txt s2_a2b1_191606.txt"
## [1] "25 982426 3909484 1406312"
## [1] "s1_a2b1_191625.txt s2_a2b1_191625.txt"
## [1] "26 1021512 4065225 1462282"
## [1] "s1_a2b1_191645.txt s2_a2b1_191645.txt"
## [1] "27 1060616 4220979 1518280"
## [1] "s1_a2b1_191704.txt s2_a2b1_191704.txt"
## [1] "28 1100054 4377157 1574611"
## [1] "s1_a2b1_191723.txt s2_a2b1_191723.txt"
## [1] "29 1139173 4532760 1631010"
## [1] "s1_a2b1_191743.txt s2_a2b1_191743.txt"
## [1] "30 1178250 4688462 1687327"
results21
## $Npairs
## [1] 1178250
## 
## $NeventsA
## [1] 4688462
## 
## $NeventsB
## [1] 1687327
setwd("../a2b2")

results22 <- count()
## [1] "s1_a2b2_181028.txt s2_a2b2_181028.txt"
## [1] "1 3932 155167 148063"
## [1] "s1_a2b2_181047.txt s2_a2b2_181047.txt"
## [1] "2 7948 310494 296061"
## [1] "s1_a2b2_181105.txt s2_a2b2_181105.txt"
## [1] "3 12575 465719 443972"
## [1] "s1_a2b2_181123.txt s2_a2b2_181123.txt"
## [1] "4 16759 621192 592343"
## [1] "s1_a2b2_181141.txt s2_a2b2_181141.txt"
## [1] "5 20768 776129 740079"
## [1] "s1_a2b2_181200.txt s2_a2b2_181200.txt"
## [1] "6 25009 931871 888918"
## [1] "s1_a2b2_182504.txt s2_a2b2_182504.txt"
## [1] "7 29108 1087400 1037876"
## [1] "s1_a2b2_182523.txt s2_a2b2_182523.txt"
## [1] "8 33266 1242771 1186709"
## [1] "s1_a2b2_182541.txt s2_a2b2_182541.txt"
## [1] "9 37216 1399127 1335056"
## [1] "s1_a2b2_182600.txt s2_a2b2_182600.txt"
## [1] "10 41163 1554693 1484011"
## [1] "s1_a2b2_182619.txt s2_a2b2_182619.txt"
## [1] "11 45494 1710527 1632914"
## [1] "s1_a2b2_182637.txt s2_a2b2_182637.txt"
## [1] "12 49626 1866446 1782389"
## [1] "s1_a2b2_184153.txt s2_a2b2_184153.txt"
## [1] "13 53563 2024024 1932052"
## [1] "s1_a2b2_184212.txt s2_a2b2_184212.txt"
## [1] "14 57966 2181382 2081543"
## [1] "s1_a2b2_184231.txt s2_a2b2_184231.txt"
## [1] "15 61981 2339208 2231586"
## [1] "s1_a2b2_184250.txt s2_a2b2_184250.txt"
## [1] "16 65994 2496724 2381209"
## [1] "s1_a2b2_184308.txt s2_a2b2_184308.txt"
## [1] "17 70062 2653855 2531056"
## [1] "s1_a2b2_184327.txt s2_a2b2_184327.txt"
## [1] "18 74069 2811313 2681762"
## [1] "s1_a2b2_185731.txt s2_a2b2_185731.txt"
## [1] "19 78211 2968935 2831948"
## [1] "s1_a2b2_185750.txt s2_a2b2_185750.txt"
## [1] "20 82552 3127158 2982184"
## [1] "s1_a2b2_185808.txt s2_a2b2_185808.txt"
## [1] "21 86637 3284263 3131524"
## [1] "s1_a2b2_185827.txt s2_a2b2_185827.txt"
## [1] "22 90917 3440186 3281270"
## [1] "s1_a2b2_185846.txt s2_a2b2_185846.txt"
## [1] "23 94975 3596088 3430533"
## [1] "s1_a2b2_185905.txt s2_a2b2_185905.txt"
## [1] "24 99195 3751488 3580075"
## [1] "s1_a2b2_191234.txt s2_a2b2_191234.txt"
## [1] "25 103190 3906177 3728589"
## [1] "s1_a2b2_191253.txt s2_a2b2_191253.txt"
## [1] "26 107210 4061159 3877306"
## [1] "s1_a2b2_191312.txt s2_a2b2_191312.txt"
## [1] "27 111250 4217437 4025378"
## [1] "s1_a2b2_191331.txt s2_a2b2_191331.txt"
## [1] "28 115372 4372402 4173697"
## [1] "s1_a2b2_191350.txt s2_a2b2_191350.txt"
## [1] "29 119343 4528253 4321684"
## [1] "s1_a2b2_191409.txt s2_a2b2_191409.txt"
## [1] "30 123357 4683049 4469675"
results22
## $Npairs
## [1] 123357
## 
## $NeventsA
## [1] 4683049
## 
## $NeventsB
## [1] 4469675
setwd("..")

N <- 30 * 100 * ceiling((0.1 / (20 * 10^{-9}) - 600)/49)

corr <- function(results) {
  Npairs <- results$Npairs
  NeventsA <- results$NeventsA
  NeventsB <- results$NeventsB
  Npp <- Npairs
  Npm <- NeventsA - Npairs
  Nmp <- NeventsB - Npairs
  Nmm <- N - Npp - Npm - Nmp
  (Npp + Nmm - Npm - Nmp) / N
}


corr11 <- corr(results11)
corr12 <- corr(results12)
corr21 <- corr(results21)
corr22 <- corr(results22)
corrs <- c(corr11, corr12, corr21, corr22)
corrs
## [1] 0.9927641 0.9757273 0.9737376 0.9418073
CHSH <- corr11 + corr12 + corr21 - corr22
seCHSH <- sqrt((4 - sum(corrs^2)) / N)

CHSH; seCHSH; (CHSH - 2) / seCHSH
## [1] 2.000422
## [1] 2.724527e-05
## [1] 15.47541
C11 <- results11$Npairs
S12A <- results12$NeventsA
C12 <- results12$Npairs
S21B <- results21$NeventsB
C21 <- results21$Npairs
C22 <- results22$Npairs

J <- (S12A - C12) + (S21B - C21) + C22 - C11
seJ <- sqrt((S12A - C12) + (S21B - C21) + C22 + C11)

J; seJ; (-J) / seJ
## [1] -43881
## [1] 1436.582
## [1] 30.54543
S11A <- results11$NeventsA
S11B <- results11$NeventsB

Jalt <- (S11A + S11B - C11) - C12 - C21 + C22
seJalt <- sqrt((S11A + S11B - C11) + C12 + C21 + C22)
Jalt; seJalt; (-Jalt) / seJalt
## [1] -34085
## [1] 2145.524
## [1] 15.88656