## The correct version of Richard Gill's incorrect calculations of the four
## 'Bell test' correlations predicted by my 3-sphere model for the EPR-Bohm
## correlations. All lines except those generating the set of directions 'u'
## and/or 'v' have been ignored from the code in http://rpubs.com/jjc/16415.

## For the exact terms of the challenge put forward by Richard Gill please
## see http://www.sciphysicsforums.com/spfbb1/viewtopic.php?f=6&t=52#p1898.

set.seed(9875)

N <- 10^5

r <- runif(N, 0, 2 * pi)
s <- runif(N, 0, pi)
t <- runif(N, -1, +1)

x <- cos(r)
y <- 1.199 * (-1 + (2/(sqrt(1 + (3 * s/pi))))) * sign(t)

u <- rbind(x, x)
v <- rbind(x, y)

## 'u' and 'v' are 2xN matrices. The N columns of 'u' represent the x and y
## coordinates of points on a circle of radius sqrt(x^2+y^2) in the xy plane.

alpha <- 0 * pi/180
beta <- 45 * pi/180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
(E_0_45 <- mean(sign(colSums(a * u)) * -sign(colSums(b * v))))
## [1] -0.7072

alpha <- 0 * pi/180
beta <- 135 * pi/180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
(E_0_135 <- mean(sign(colSums(a * u)) * -sign(colSums(b * v))))
## [1] 0.707

## Next, we calculate two 'mirror images' of the above correlations. One can
## think of these 'mirror images' as correlations observed (and calculated)
## from a clockwise perspective rather than a counter-clockwise perspective.
## Mathematically this means calculating the correlations using left-handed
## basis (e_y, e_x) for the spins instead of right-handed basis (e_x, e_y).

u <- rbind(x, x)
v <- rbind(y, x)

alpha <- 90 * pi/180
beta <- 45 * pi/180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
(E_90_45 <- mean(sign(colSums(a * u)) * -sign(colSums(b * v))))
## [1] -0.7072

alpha <- 90 * pi/180
beta <- 135 * pi/180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
(E_90_135 <- mean(sign(colSums(a * u)) * -sign(colSums(b * v))))
## [1] -0.707

## The Bell-CHSH inequality is violated:

-E_0_45 + E_0_135 - E_90_45 - E_90_135
## [1] 2.828

## The data frame of Alice's N spin directions:

write.table(t(u), "AliceDu.txt", sep = "   ")

## The data frame of Bob's N spin directions:

write.table(t(v), "BobDu.txt", sep = "   ")