def montecarlo(p, n, m): # m is 0 for simple majority
trials = int(1e6)
r = majority(n, m)
if r is None:
return None
return np.sum(np.sum((np.random.random(size=(trials, n)) < p).astype(int), axis=1) >= r) / trials
def deterministic(p, n, m): # m is 0 for simple majority
r = majority(n, m)
if r == None: return None
P = 0
while r <= n:
P += (comb(n, r) * (p**r) * ((1 - p)**(n-r)))
r += 1
return P
majority(n, m=0) from utils.py that montecarlo and deterministic use
def majority(n, m=0):
if m == 0:
if n % 2:
r = (n // 2) + 1
else:
r = n // 2
elif m > 50 and m <= 100: r = ceil((m / 100) * n)
else:
print("correctness_simulation(p, n, m) where m = 0 for simple-majority but > 50 and <= 100 for super-majority or unanimity")
r == None
return r
For some
n
values, the majority-voter curves don't pass through(0.5, 0.5)
like they do on the plot published on CJT's Wikipedia page.For example, look at
n = 12
in the three curves below:Figure 1: Monte Carlo
Figure 2: Deterministic
Figure 3: Wikipedia
For ease, I'm attaching the relevant functions.
montecarlo(p, n, m)
anddeterministic(p, n, m)
fromtheorems/condorcet.py
majority(n, m=0)
fromutils.py
thatmontecarlo
anddeterministic
useR
code published on Wikipedia:To me, the functions seem identical but I might be missing something.