Skip to content

Commit e71a2c2

Browse files
committed
Add error bars to CE and CW likelihood examples
These are also Bernoulli yes/no trials that produce binomial distributions. Update results images
1 parent a0d86d1 commit e71a2c2

4 files changed

Lines changed: 18 additions & 2 deletions

examples/merrill_1984_table_1_fig_1.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import time
2828
from collections import Counter
2929
import numpy as np
30+
from scipy.stats import binomtest
3031
import matplotlib.pyplot as plt
3132
from tabulate import tabulate
3233
from elsim.methods import (fptp, runoff, irv, approval, borda, coombs,
@@ -113,7 +114,14 @@
113114
'Black'):
114115
x, y = zip(*sorted(count[method].items()))
115116
CE = np.array(y)/y_cw
116-
plt.plot(x, CE*100, '-', label=method)
117+
118+
# Add 95% confidence interval error bars (Clopper-Pearson exact method)
119+
ci = np.empty((2, len(y)))
120+
for i in range(len(y)):
121+
ci[:, i] = binomtest(y[i], y_cw[i]).proportion_ci()
122+
yerr = ci - CE
123+
yerr[0] = -yerr[0]
124+
plt.errorbar(x, CE*100, yerr*100, fmt='-', label=method)
117125
table.append([method, *np.array(y)/y_cw*100])
118126

119127
# Likelihood that social utility maximizer is Condorcet Winner
4.83 KB
Loading
-2.25 KB
Loading

examples/wikipedia_condorcet_paradox_likelihood.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""
2323
from collections import Counter
2424
import numpy as np
25+
from scipy.stats import binomtest
2526
import matplotlib.pyplot as plt
2627
from tabulate import tabulate
2728
from joblib import Parallel, delayed
@@ -68,7 +69,14 @@ def func():
6869

6970
x, y = zip(*sorted(is_CP.items()))
7071
CP = np.asarray(y) / iterations # Likelihood of paradox
71-
plt.plot(x, CP*100, '-', label='Simulation')
72+
73+
# Add 95% confidence interval error bars (Clopper-Pearson exact method)
74+
ci = np.empty((2, len(y)))
75+
for i in range(len(y)):
76+
ci[:, i] = binomtest(y[i], iterations).proportion_ci()
77+
yerr = ci - CP
78+
yerr[0] = -yerr[0]
79+
plt.errorbar(x, CP*100, yerr*100, fmt='-', label='Simulation')
7280

7381
plt.legend()
7482
plt.grid(True, color='0.7', linestyle='-', which='major', axis='both')

0 commit comments

Comments
 (0)