Coincidence.R

richard — May 7, 2014, 6:51 AM

set.seed(2345678)

ell <- 3 * (3 - 2 * sqrt(2))
es <- 1 - ell
ll <- ell * pi / 2
ss <- es * pi / 2


N <- 10^4

r <- runif(N, 0, 1)
theta <- runif(N, 0, 2 * pi)

alphas <- c(0, pi / 2)
betas <- c(pi / 4, 2 * pi - pi / 4)

settingAlice <- sample(c(1, 2), N, replace = TRUE)
settingBob <- sample(c(1, 2), N, replace = TRUE)
Alpha <- alphas[settingAlice]
Beta <- betas[settingBob]

timeAlice <- rep(0, N)
timeBob <- rep(0, N)

thetaAlice <- theta + Alpha
thetaBob <- theta + Beta

thetaAlice <- ifelse(thetaAlice > 2 * pi, thetaAlice - 2 * pi, thetaAlice)
thetaBob <- ifelse(thetaBob > 2 * pi, thetaBob - 2 * pi, thetaBob)


spinAlice <- ifelse(thetaAlice < pi, +1, -1)
spinBob <- ifelse(thetaBob < pi, +1, -1)

timeAlice <- ifelse((r > ell) & (thetaAlice > 0) & (thetaAlice < ss), timeAlice - 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > pi - ss) & (thetaAlice < pi), timeAlice + 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > pi) & (thetaAlice < pi + ss), timeAlice - 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > 2 * pi - ss) & (thetaAlice < 2 * pi), timeAlice + 1, timeAlice)

timeBob <- ifelse((r > ell) & (thetaBob > 0) & (thetaBob < ss), timeBob - 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > pi - ss) & (thetaBob < pi), timeBob + 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > pi) & (thetaBob < pi + ss), timeBob - 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > 2 * pi - ss) & (thetaBob < 2 * pi), timeBob + 1, timeBob)

dataSet <- data.frame(settingAlice = settingAlice, spinAlice = spinAlice, timeAlice = timeAlice, 
    settingBob = settingBob, spinBob = spinBob, timeBob = timeBob)

head(dataSet, n = 20)
   settingAlice spinAlice timeAlice settingBob spinBob timeBob
1             1         1         0          1       1       0
2             1         1         0          1       1       0
3             1         1        -1          1       1       0
4             1         1         1          2       1       0
5             1        -1         1          2      -1       0
6             1         1         0          2       1       0
7             1         1         0          2      -1       0
8             1        -1         0          1       1       0
9             2         1         0          1      -1       0
10            2         1        -1          1      -1       1
11            2        -1         0          1      -1       0
12            1        -1        -1          2       1       1
13            2         1         0          1       1       0
14            1         1         0          2       1       0
15            1         1         0          1       1       1
16            1         1         1          1      -1      -1
17            2        -1        -1          2       1       0
18            2         1         0          2      -1       0
19            2        -1         0          1      -1       0
20            1        -1         0          2      -1       0

delta <- abs(timeAlice - timeBob)

sample11 <- (delta < 1.5) & (settingAlice == 1) & (settingBob == 1)
sample12 <- (delta < 1.5) & (settingAlice == 1) & (settingBob == 2)
sample21 <- (delta < 1.5) & (settingAlice == 2) & (settingBob == 1)
sample22 <- (delta < 1.5) & (settingAlice == 2) & (settingBob == 2)

N11 <- sum((delta < 1.5) & (settingAlice == 1) & (settingBob == 1))
N12 <- sum((delta < 1.5) & (settingAlice == 1) & (settingBob == 2))
N21 <- sum((delta < 1.5) & (settingAlice == 2) & (settingBob == 1))
N22 <- sum((delta < 1.5) & (settingAlice == 2) & (settingBob == 2))

Nrun11 <- sum((settingAlice == 1) & (settingBob == 1))
Nrun12 <- sum((settingAlice == 1) & (settingBob == 2))
Nrun21 <- sum((settingAlice == 2) & (settingBob == 1))
Nrun22 <- sum((settingAlice == 2) & (settingBob == 2))

rho11 <- mean((spinAlice*spinBob)[sample11])
rho12 <- mean((spinAlice*spinBob)[sample12])
rho21 <- mean((spinAlice*spinBob)[sample21])
rho22 <- mean((spinAlice*spinBob)[sample22])


gamma11 <- N11 / Nrun11
gamma12 <- N12 / Nrun12
gamma21 <- N21 / Nrun21
gamma22 <- N22 / Nrun22

(S <- rho11 + rho12 + rho21 - rho22)
[1] 2.826
(B <- 6 /min(c(gamma11, gamma12, gamma21, gamma22)) - 4)
[1] 2.799

S - B
[1] 0.02725


## Now consider the counterfactual situation in which, always, 
## Alice had chosen setting 1 and Bob had chosen setting 1

settingAlice <- rep(1, N)
settingBob <- rep(1, N)

Alpha <- alphas[settingAlice]
Beta <- betas[settingBob]

timeAlice <- rep(0, N)
timeBob <- rep(0, N)

thetaAlice <- theta + Alpha
thetaBob <- theta + Beta
thetaAlice <- theta + Alpha
thetaBob <- theta + Beta

thetaAlice <- ifelse(thetaAlice > 2 * pi, thetaAlice - 2 * pi, thetaAlice)
thetaBob <- ifelse(thetaBob > 2 * pi, thetaBob - 2 * pi, thetaBob)

spinAlice <- ifelse(thetaAlice < pi, +1, -1)
spinBob <- ifelse(thetaBob < pi, +1, -1)

timeAlice <- ifelse((r > ell) & (thetaAlice > 0) & (thetaAlice < ss), timeAlice - 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > pi - ss) & (thetaAlice < pi), timeAlice + 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > pi) & (thetaAlice < pi + ss), timeAlice - 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > 2 * pi - ss) & (thetaAlice < 2 * pi), timeAlice + 1, timeAlice)

timeBob <- ifelse((r > ell) & (thetaBob > 0) & (thetaBob < ss), timeBob - 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > pi - ss) & (thetaBob < pi), timeBob + 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > pi) & (thetaBob < pi + ss), timeBob - 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > 2 * pi - ss) & (thetaBob < 2 * pi), timeBob + 1, timeBob)

dataSet <- data.frame(settingAlice = settingAlice, spinAlice = spinAlice, timeAlice = timeAlice, 
                      settingBob = settingBob, spinBob = spinBob, timeBob = timeBob)

head(dataSet, n = 20)
   settingAlice spinAlice timeAlice settingBob spinBob timeBob
1             1         1         0          1       1       0
2             1         1         0          1       1       0
3             1         1        -1          1       1       0
4             1         1         1          1      -1      -1
5             1        -1         1          1       1      -1
6             1         1         0          1      -1       0
7             1         1         0          1       1       0
8             1        -1         0          1       1       0
9             1        -1         0          1      -1       0
10            1        -1         0          1      -1       1
11            1        -1         0          1      -1       0
12            1        -1        -1          1      -1       0
13            1        -1         0          1       1       0
14            1         1         0          1      -1       0
15            1         1         0          1       1       1
16            1         1         1          1      -1      -1
17            1         1         0          1       1       1
18            1        -1         0          1      -1       0
19            1         1         0          1      -1       0
20            1        -1         0          1      -1       0

delta <- abs(timeAlice - timeBob)

sample11 <- (delta < 1.5) & (settingAlice == 1) & (settingBob == 1)

sum(sample11)
[1] 8841


sample11 <- (delta < 1.5)

sum(sample11)
[1] 8841



## Now consider the counterfactual situation in which, always, 
## Alice had chosen setting 1 and Bob had chosen setting 2

settingAlice <- rep(1, N)
settingBob <- rep(2, N)

Alpha <- alphas[settingAlice]
Beta <- betas[settingBob]

timeAlice <- rep(0, N)
timeBob <- rep(0, N)

thetaAlice <- theta + Alpha
thetaBob <- theta + Beta
thetaAlice <- theta + Alpha
thetaBob <- theta + Beta

thetaAlice <- ifelse(thetaAlice > 2 * pi, thetaAlice - 2 * pi, thetaAlice)
thetaBob <- ifelse(thetaBob > 2 * pi, thetaBob - 2 * pi, thetaBob)

spinAlice <- ifelse(thetaAlice < pi, +1, -1)
spinBob <- ifelse(thetaBob < pi, +1, -1)

timeAlice <- ifelse((r > ell) & (thetaAlice > 0) & (thetaAlice < ss), timeAlice - 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > pi - ss) & (thetaAlice < pi), timeAlice + 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > pi) & (thetaAlice < pi + ss), timeAlice - 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > 2 * pi - ss) & (thetaAlice < 2 * pi), timeAlice + 1, timeAlice)

timeBob <- ifelse((r > ell) & (thetaBob > 0) & (thetaBob < ss), timeBob - 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > pi - ss) & (thetaBob < pi), timeBob + 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > pi) & (thetaBob < pi + ss), timeBob - 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > 2 * pi - ss) & (thetaBob < 2 * pi), timeBob + 1, timeBob)

dataSet <- data.frame(settingAlice = settingAlice, spinAlice = spinAlice, timeAlice = timeAlice, 
                      settingBob = settingBob, spinBob = spinBob, timeBob = timeBob)

head(dataSet, n = 20)
   settingAlice spinAlice timeAlice settingBob spinBob timeBob
1             1         1         0          2      -1       0
2             1         1         0          2       1      -1
3             1         1        -1          2      -1       1
4             1         1         1          2       1       0
5             1        -1         1          2      -1       0
6             1         1         0          2       1       0
7             1         1         0          2      -1       0
8             1        -1         0          2      -1       0
9             1        -1         0          2      -1       0
10            1        -1         0          2      -1       0
11            1        -1         0          2      -1       0
12            1        -1        -1          2       1       1
13            1        -1         0          2      -1       0
14            1         1         0          2       1       0
15            1         1         0          2       1       0
16            1         1         1          2       1       0
17            1         1         0          2       1       0
18            1        -1         0          2      -1       0
19            1         1         0          2       1       0
20            1        -1         0          2      -1       0

delta <- abs(timeAlice - timeBob)

sample12 <- (delta < 1.5) & (settingAlice == 1) & (settingBob == 2)

sum(sample12)
[1] 8882


sample12 <- (delta < 1.5)

sum(sample12)
[1] 8882


## Now consider the counterfactual situation in which, always, 
## Alice had chosen setting 2 and Bob had chosen setting 1

settingAlice <- rep(2, N)
settingBob <- rep(1, N)

Alpha <- alphas[settingAlice]
Beta <- betas[settingBob]

timeAlice <- rep(0, N)
timeBob <- rep(0, N)

thetaAlice <- theta + Alpha
thetaBob <- theta + Beta
thetaAlice <- theta + Alpha
thetaBob <- theta + Beta

thetaAlice <- ifelse(thetaAlice > 2 * pi, thetaAlice - 2 * pi, thetaAlice)
thetaBob <- ifelse(thetaBob > 2 * pi, thetaBob - 2 * pi, thetaBob)

spinAlice <- ifelse(thetaAlice < pi, +1, -1)
spinBob <- ifelse(thetaBob < pi, +1, -1)

timeAlice <- ifelse((r > ell) & (thetaAlice > 0) & (thetaAlice < ss), timeAlice - 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > pi - ss) & (thetaAlice < pi), timeAlice + 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > pi) & (thetaAlice < pi + ss), timeAlice - 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > 2 * pi - ss) & (thetaAlice < 2 * pi), timeAlice + 1, timeAlice)

timeBob <- ifelse((r > ell) & (thetaBob > 0) & (thetaBob < ss), timeBob - 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > pi - ss) & (thetaBob < pi), timeBob + 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > pi) & (thetaBob < pi + ss), timeBob - 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > 2 * pi - ss) & (thetaBob < 2 * pi), timeBob + 1, timeBob)

dataSet <- data.frame(settingAlice = settingAlice, spinAlice = spinAlice, timeAlice = timeAlice, 
                      settingBob = settingBob, spinBob = spinBob, timeBob = timeBob)

head(dataSet, n = 20)
   settingAlice spinAlice timeAlice settingBob spinBob timeBob
1             2         1         0          1       1       0
2             2         1         1          1       1       0
3             2         1         0          1       1       0
4             2        -1         0          1      -1      -1
5             2         1         0          1       1      -1
6             2        -1         0          1      -1       0
7             2         1         0          1       1       0
8             2         1         0          1       1       0
9             2         1         0          1      -1       0
10            2         1        -1          1      -1       1
11            2        -1         0          1      -1       0
12            2        -1         0          1      -1       0
13            2         1         0          1       1       0
14            2        -1         0          1      -1       0
15            2        -1        -1          1       1       1
16            2        -1         0          1      -1      -1
17            2        -1        -1          1       1       1
18            2         1         0          1      -1       0
19            2        -1         0          1      -1       0
20            2         1         0          1      -1       0

delta <- abs(timeAlice - timeBob)

sample21 <- (delta < 1.5) & (settingAlice == 2) & (settingBob == 1)

sum(sample21)
[1] 8858


sample21 <- (delta < 1.5)

sum(sample21)
[1] 8858



## Now consider the counterfactual situation in which, always, 
## Alice had chosen setting 2 and Bob had chosen setting 2

settingAlice <- rep(2, N)
settingBob <- rep(2, N)

Alpha <- alphas[settingAlice]
Beta <- betas[settingBob]

timeAlice <- rep(0, N)
timeBob <- rep(0, N)

thetaAlice <- theta + Alpha
thetaBob <- theta + Beta
thetaAlice <- theta + Alpha
thetaBob <- theta + Beta

thetaAlice <- ifelse(thetaAlice > 2 * pi, thetaAlice - 2 * pi, thetaAlice)
thetaBob <- ifelse(thetaBob > 2 * pi, thetaBob - 2 * pi, thetaBob)

spinAlice <- ifelse(thetaAlice < pi, +1, -1)
spinBob <- ifelse(thetaBob < pi, +1, -1)

timeAlice <- ifelse((r > ell) & (thetaAlice > 0) & (thetaAlice < ss), timeAlice - 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > pi - ss) & (thetaAlice < pi), timeAlice + 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > pi) & (thetaAlice < pi + ss), timeAlice - 1, timeAlice)
timeAlice <- ifelse((r > ell) & (thetaAlice > 2 * pi - ss) & (thetaAlice < 2 * pi), timeAlice + 1, timeAlice)

timeBob <- ifelse((r > ell) & (thetaBob > 0) & (thetaBob < ss), timeBob - 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > pi - ss) & (thetaBob < pi), timeBob + 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > pi) & (thetaBob < pi + ss), timeBob - 1, timeBob)
timeBob <- ifelse((r > ell) & (thetaBob > 2 * pi - ss) & (thetaBob < 2 * pi), timeBob + 1, timeBob)

dataSet <- data.frame(settingAlice = settingAlice, spinAlice = spinAlice, timeAlice = timeAlice, 
                      settingBob = settingBob, spinBob = spinBob, timeBob = timeBob)

head(dataSet, n = 20)
   settingAlice spinAlice timeAlice settingBob spinBob timeBob
1             2         1         0          2      -1       0
2             2         1         1          2       1      -1
3             2         1         0          2      -1       1
4             2        -1         0          2       1       0
5             2         1         0          2      -1       0
6             2        -1         0          2       1       0
7             2         1         0          2      -1       0
8             2         1         0          2      -1       0
9             2         1         0          2      -1       0
10            2         1        -1          2      -1       0
11            2        -1         0          2      -1       0
12            2        -1         0          2       1       1
13            2         1         0          2      -1       0
14            2        -1         0          2       1       0
15            2        -1        -1          2       1       0
16            2        -1         0          2       1       0
17            2        -1        -1          2       1       0
18            2         1         0          2      -1       0
19            2        -1         0          2       1       0
20            2         1         0          2      -1       0

delta <- abs(timeAlice - timeBob)

sample22 <- (delta < 1.5) & (settingAlice == 2) & (settingBob == 2)

sum(sample22)
[1] 8822


sample22 <- (delta < 1.5)

sum(sample22)
[1] 8822

###########

all <- sum(sample11 & sample12 & sample21 & sample22)

delta11 <- all / sum(sample11)

delta12 <- all / sum(sample12)

delta21 <- all / sum(sample21)

delta22 <- all / sum(sample22)

delta <- min(c(delta11, delta12, delta21, delta22))

delta
[1] 0.6083

(bound <- 4 - 2 * delta)
[1] 2.783

S
[1] 2.826

S - bound
[1] 0.04297