Simulation Based on Michel Fodje's epr-simple simulation translated from 
Python to Mathematica by John Reed 13 Nov 2013 
Modified by Fred Diether for Completely Local-Realistic Aug 2021
Some matching parts by Bill Nelson

Set Run Time Parameters, Initialize Arrays and Tables

m=400000;
trialDeg=721;
s=ConstantArray[0,m];
λ=ConstantArray[0,m];
outA=Table[{0,0,0,0,0},m];
outB=Table[{0,0,0,0,0},m];
outA4=Table[{0,0,0,0,0},m];
outB4=Table[{0,0,0,0,0},m];
a1=ConstantArray[0,m];
b1=ConstantArray[0,m];
A=ConstantArray[0, m];
B=ConstantArray[0, m];
nPP=ConstantArray[0,trialDeg];
nNN=ConstantArray[0,trialDeg];
nPN=ConstantArray[0,trialDeg];
nNP=ConstantArray[0,trialDeg];
nAP=ConstantArray[0,trialDeg];
nBP=ConstantArray[0,trialDeg];
nAN=ConstantArray[0,trialDeg];
nBN=ConstantArray[0,trialDeg];
η=3;β=0.3;ξ=-15;

Generate Particle Data with 3 Do Loops

Do[e=RandomReal[{0,360}];  (*Singlet vector angle*)
s[[i]]=e;
λ[[i]]=β(Cos[(e/η) ]^2), (*Hidden Variables*)
{i,m}]

Do[a=RandomInteger[{0,360}];  (*Detector vector angle 1 degree increments*)
If[Abs[Cos[((a-s[[i]])Degree)]]<λ[[i]],C1=f1,C1=g1];
If[Abs[Cos[((a-s[[i]])Degree)]]>λ[[i]],Aa=-Sign[Cos[((a-s[[i]])Degree)]],Aa=-Sign[Sin[((a-s[[i]]+ξ)Degree)]]];
A5=-Sign[Sin[((a-s[[i]]+ξ)Degree)]];
outA4[[i]]={a,Aa,i,C1,A5},{i,m}]
outA1=Select[outA4,MemberQ[#,g1]&];
outA2=Select[outA4,MemberQ[#,f1]&];

Do[b=RandomInteger[{0,360}];  (*Detector vector angle 1 degree increments*)
If[Abs[Cos[((b-s[[i]])Degree)]]<λ[[i]],C2=f2,C2=g2];
If[Abs[Cos[((b-s[[i]])Degree)]]>λ[[i]],Bb=Sign[Cos[((b-s[[i]])Degree)]],Bb=Sign[Sin[((b-s[[i]]+ξ)Degree)]]];
B5=Sign[Sin[((b-s[[i]]+ξ)Degree)]];
outB4[[i]]={b,Bb,i,C2,B5},{i,m}]
outB1=Select[outB4,MemberQ[#,g2]&];
outB2=Select[outB4,MemberQ[#,f2]&];

Match Events Using Trial Numbers

listad=outA1[[All,3]]; 
listbd=outB1[[All,3]];

Local Detection Analysis of Events Observed by Alice

listA4=Select[outA1,Intersection[{#[[3]]},listbd]=={#[[3]]}&];
listad2=outA1[[All,3]]; 
listad3=listA4[[All,3]];
listA3=Select[outA1,Intersection[{#[[3]]},listad3]!={#[[3]]}&];
listAa4=Select[listA4,Intersection[{#[[3]]},listad2]!={#[[3]]}&];
M=Length[listA3];
listA6=Table[{0,0,0,0,0},M];
a2=ConstantArray[0,M];
A2=ConstantArray[0,M];
ind2=ConstantArray[0,M];
A3=ConstantArray[0,M];
A7=ConstantArray[0,M];
A4=ConstantArray[0,M];
A6=ConstantArray[0,M];
a2=listA3[[All,1]];
A2=listA3[[All,2]];
ind2=listA3[[All,3]];
A7=listA3[[All,5]];
Do[A4=A2[[i]];A6=A7[[i]];
If[A4==A6,A2=A2,A2=A7];
listA6[[i]]={a2[[i]],A2[[i]],ind2[[i]],f1,A7[[i]]},{i,M}]
outA=Sort[Catenate[{listA4,outA2,listA6}],#1[[3]]<#2[[3]]&];
a1=outA[[All,1]];
A=outA[[All,2]];

Local Detection Analysis of Events Observed by Bob

listB4=Select[outB1,Intersection[{#[[3]]},listad]=={#[[3]]}&];
listbd2=outB1[[All,3]];  
listbd3=listB4[[All,3]];
listB3=Select[outB1,Intersection[{#[[3]]},listbd3]!={#[[3]]}&];
listBb4=Select[listB4,Intersection[{#[[3]]},listbd2]!={#[[3]]}&];
M2=Length[listB3];
listB6=Table[{0,0,0,0,0},M2];
b2=ConstantArray[0,M2];
B2=ConstantArray[0,M2];
ind3=ConstantArray[0,M2];
B3=ConstantArray[0,M2];
B7=ConstantArray[0,M2];
B4=ConstantArray[0,M2];
B6=ConstantArray[0,M2];
b2=listB3[[All,1]];
B2=listB3[[All,2]];
ind3=listB3[[All,3]];
B7=listB3[[All,5]];
Do[B4=B2[[i]];B6=B7[[i]];
If[B4==B6,B2=B2,B2=B7];
listB6[[i]]={b2[[i]],B2[[i]],ind3[[i]],f1,B7[[i]]},{i,M2}]
outB=Sort[Catenate[{listB4,outB2,listB6}],#1[[3]]<#2[[3]]&];
b1=outB[[All,1]];
B=outB[[All,2]];

Statistical Analysis of Particle Data

Do[θ=a1[[j]]-b1[[j]]+361;  (*All angles are shifted by 2π since θ is an index*)
aliceD=A[[j]];bobD=B[[j]];
If[aliceD==1,nAP[[θ]]++];
If[bobD==1,nBP[[θ]]++];
If[aliceD==-1,nAN[[θ]]++];
If[bobD==-1,nBN[[θ]]++];
If[aliceD==1&&bobD==1,nPP[[θ]]++];
If[aliceD==1&&bobD==-1,nPN[[θ]]++];
If[aliceD==-1&&bobD==1,nNP[[θ]]++];
If[aliceD==-1&&bobD==-1,nNN[[θ]]++],{j,m}]

Calculate Mean Values and Plot

pPP=0; pPN=0; pNP=0;pNN=0; 
mean=ConstantArray[0,trialDeg];
Do[sum=nPP[[i]]+nPN[[i]]+nNP[[i]]+nNN[[i]];
If[sum==0,Goto[jump],
{pPP=nPP[[i]]/sum;
       pNP=nNP[[i]]/sum;
       pPN=nPN[[i]]/sum;
       pNN=nNN[[i]]/sum;
mean[[i]]=pPP+pNN-pPN-pNP}];
Label[jump],{i,trialDeg}]

simulation=ListPlot[mean,PlotMarkers->{Automatic,Tiny}];
negcos=Plot[-Cos[x Degree],{x,0,720},PlotStyle->{Magenta}, Ticks->{{{0,-360°},{90,-270°},{180,-180°},{270,-90°},{360,0°},{450,90°},{540,180°},{630,270°},{720,360°}},Automatic},GridLines->Automatic];
p1=Plot[-1+(2x Degree)/π,{x,0,180},PlotStyle->{Gray,Dashed}];
p2=Plot[3-2x Degree/π,{x,180,360},PlotStyle->{Gray,Dashed}];
p3=Plot[-5+2x Degree/π,{x,360,540},PlotStyle->{Gray,Dashed}];
p4=Plot[7-2x Degree/π,{x,540,720},PlotStyle->{Gray,Dashed}];

Compare mean values with -Cosine Curve and compute averages

Show[negcos,p1,p2,p3,p4,simulation]

AveA=N[Sum[A[[i]], {i, m}]/m];
AveB=N[Sum[B[[i]], {i, m}]/m];
Print["AveA = ",AveA]
Print["AveB = ",AveB]
PAP=N[Sum[nAP[[i]], {i, trialDeg}]];
PBP=N[Sum[nBP[[i]], {i, trialDeg}]];
PAN=N[Sum[nAN[[i]], {i, trialDeg}]];
PBN=N[Sum[nBN[[i]], {i, trialDeg}]];
PA1=PAP/(PAP+PAN);
PB1=PBP/(PBP+PBN);
Print["P(A+)= ", PA1]
Print["P(B+)= ",PB1]
totAB=Sum[nPP[[i]]+nNN[[i]]+nPN[[i]]+nNP[[i]], {i, trialDeg}]
PP=N[Sum[nPP[[i]], {i, trialDeg}]/totAB]
NN=N[Sum[nNN[[i]], {i, trialDeg}]/totAB]
PN=N[Sum[nPN[[i]], {i, trialDeg}]/totAB]
NP=N[Sum[nNP[[i]], {i, trialDeg}]/totAB]
CHSH=Abs[N[mean[[315]]]-N[mean[[225]]]+N[mean[[405]]]+N[mean[[45]]]]