Adventures with R and Python

First some R code – Fred Diether’s latest simulation of an EPR-B experiment converted from Mathematica to R. Obviously I should combine these two loops into just one since almost all computations are being duplicated to get the two pictures.

set.seed(1234)
M <- 10000
theta <- runif(M, 0, 360)
beta1 <- 0.32
beta2 <- 0.938
phi <- 3
xi <- 0
s1 <- theta
s2 <- theta + 180
cosD <- function(x) cos(pi * x / 180)
sinD <- function(x) sin(pi * x / 180)
lambda1 <- beta1 * cos(theta/phi)^2
lambda2 <- beta2 * cos(theta/phi)^2



rate <- function(a, b){
  Aa1 <- ifelse(abs(cosD(a - s1)) > lambda1, sign(cosD(a - s1)), 0)
  Aa2 <- ifelse(abs(cosD(a - s1)) < lambda2, sign(sinD(a - s1 + xi)), 0)
  Bb1 <- ifelse(abs(cosD(b - s2)) > lambda1, sign(cosD(b - s2)), 0)
  Bb2 <- ifelse(abs(cosD(b - s2)) < lambda2, sign(sinD(b - s2 + xi)), 0)
  (sum(Aa1*Bb1 != 0) + sum(Aa2*Bb2 != 0))/(2 * M)
}

a <- 0

rates <- rep(0, 37)
i <- 1
for (b in seq(0, 180, 5)) {rates[i] <- rate(a, b); i <- i + 1}
plot(seq(0, 180, 5), rates, type = "l", ylim = c(0, 1))



corr <- function(a, b){
  Aa1 <- ifelse(abs(cosD(a - s1)) > lambda1, sign(cosD(a - s1)), 0)
  Aa2 <- ifelse(abs(cosD(a - s1)) < lambda2, sign(sinD(a - s1 + xi)), 0)
  Bb1 <- ifelse(abs(cosD(b - s2)) > lambda1, sign(cosD(b - s2)), 0)
  Bb2 <- ifelse(abs(cosD(b - s2)) < lambda2, sign(sinD(b - s2 + xi)), 0)
  sum(Aa1*Bb1 + Aa2*Bb2)/(sum(Aa1*Bb1 != 0) + sum(Aa2*Bb2 != 0))
}
a <- 0

corrs <- rep(0, 37)
i <- 1
for (b in seq(0, 180, 5)) {corrs[i] <- corr(a, b); corrs[i]; i <- i + 1}
plot(seq(0, 180, 5), corrs, type = "l")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s