Skip to content

Commit

Permalink
Add error bars to CE and CW likelihood examples
Browse files Browse the repository at this point in the history
These are also Bernoulli yes/no trials that produce binomial
distributions.

Update results images
  • Loading branch information
endolith committed Aug 6, 2022
1 parent 1e92fbc commit a85273c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion examples/merrill_1984_table_1_fig_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import time
from collections import Counter
import numpy as np
from scipy.stats import binomtest
import matplotlib.pyplot as plt
from tabulate import tabulate
from elsim.methods import (fptp, runoff, irv, approval, borda, coombs,
Expand Down Expand Up @@ -113,7 +114,14 @@
'Black'):
x, y = zip(*sorted(count[method].items()))
CE = np.array(y)/y_cw
plt.plot(x, CE*100, '-', label=method)

# Add 95% confidence interval error bars (Clopper-Pearson exact method)
ci = np.empty((2, len(y)))
for i in range(len(y)):
ci[:, i] = binomtest(y[i], y_cw[i]).proportion_ci()
yerr = ci - CE
yerr[0] = -yerr[0]
plt.errorbar(x, CE*100, yerr*100, fmt='-', label=method)
table.append([method, *np.array(y)/y_cw*100])

# Likelihood that social utility maximizer is Condorcet Winner
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/results/Wikipedia_Condorcet_paradox_likelihood.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion examples/wikipedia_condorcet_paradox_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""
from collections import Counter
import numpy as np
from scipy.stats import binomtest
import matplotlib.pyplot as plt
from tabulate import tabulate
from joblib import Parallel, delayed
Expand Down Expand Up @@ -68,7 +69,14 @@ def func():

x, y = zip(*sorted(is_CP.items()))
CP = np.asarray(y) / iterations # Likelihood of paradox
plt.plot(x, CP*100, '-', label='Simulation')

# Add 95% confidence interval error bars (Clopper-Pearson exact method)
ci = np.empty((2, len(y)))
for i in range(len(y)):
ci[:, i] = binomtest(y[i], iterations).proportion_ci()
yerr = ci - CP
yerr[0] = -yerr[0]
plt.errorbar(x, CP*100, yerr*100, fmt='-', label='Simulation')

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

0 comments on commit a85273c

Please sign in to comment.