This is an example of how a non-local hidden variables model can easily violate the CHSH inequality, even when all correlations are computed on the same set of hidden variables. Intended for pedagogical purposes.
set.seed(9875)
## For reproducibility.
M <- 10^6 ## Sample size.
## Use the same, single sample of 'M' realizations of hidden variables for
## all four correlations
## The hidden variable is just a random number t between -1 and 1. We send t
## to Alice and -t to Bob.
t <- runif(M, -1, 1)
## At both Alice's and Bob's stations the same model is used to determine
## outcomes. We put this into a function obs(hv), that takes the hidden
## variable as the argument and returns -1 or 1.
obs <- function(hv) {
s <- sign(hv)
hv <- abs(hv)
## Since the following variable depend on both settings a and b, the model is
## blatantly non-local:
L <- (1 + sum(a * b))/4
if (hv < L) {
o <- s
} else if (hv < 2 * L) {
o <- -s
} else if (hv < L + 0.5) {
o <- -1
} else {
o <- 1
}
return(o)
}
## Now we compute the four correlations in the CHSH inequality:
alpha <- 0
beta <- 45
a <- c(cos(alpha * pi/180), sin(alpha * pi/180))
b <- c(cos(beta * pi/180), sin(beta * pi/180))
## We generate the list of observations by applying the obs function to each
## element in the list of hidden variables. First for Alice:
ca <- sapply(t, obs)
## Then for Bob:
cb <- sapply(-t, obs)
E11 <- mean(ca * cb)
E11
## [1] -0.7077
alpha <- 0
beta <- 135
a <- c(cos(alpha * pi/180), sin(alpha * pi/180))
b <- c(cos(beta * pi/180), sin(beta * pi/180))
ca <- sapply(t, obs)
cb <- sapply(-t, obs)
E12 <- mean(ca * cb)
E12
## [1] 0.7085
alpha <- 90
beta <- 45
a <- c(cos(alpha * pi/180), sin(alpha * pi/180))
b <- c(cos(beta * pi/180), sin(beta * pi/180))
ca <- sapply(t, obs)
cb <- sapply(-t, obs)
E21 <- mean(ca * cb)
E21
## [1] -0.7077
alpha <- 90
beta <- 135
a <- c(cos(alpha * pi/180), sin(alpha * pi/180))
b <- c(cos(beta * pi/180), sin(beta * pi/180))
ca <- sapply(t, obs)
cb <- sapply(-t, obs)
E22 <- mean(ca * cb)
E22
## [1] -0.7077
## CHSH expression
-E11 + E12 - E21 - E22
## [1] 2.832