-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudit_lab_procedure.py
212 lines (164 loc) · 6.83 KB
/
audit_lab_procedure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import numpy as np
def initialize_dirichlet(candidates, prior_parameters=None):
"""
Create the initial prior for the Dirichlet distribution
Parameters
----------
candidates : array-like
names of all candidates in the contest
prior_parameters : array-like
parameter values for the Dirichlet distribution, corresponding to each candidate
size of the values corresponds to confidence (?)
if None supplied, then use a flat prior
Returns
-------
dict of parameter values, indexed by candidate
"""
if prior_parameters == None:
return {cand : len(candidates)/2 for cand in candidates}
else:
assert len(prior_parameters)==len(candidates)
return {candidates[i] : prior_parameters[i] for i in range(len(candidates))}
def test_initialize_dirichlet():
candidates = ['A', 'B']
prior = initialize_dirichlet(candidates, [0]*2)
assert prior == {'A':0, 'B':0}
prior = initialize_dirichlet(candidates)
assert prior == {'A':1, 'B':1}
def tally_sample(candidates, sample):
"""
Tally the observed sample
Parameters
----------
candidates : array-like
names of all candidates in the contest
sample : array-like
observed sample, with labels that match values in candidate list
Returns
-------
dict of the tallied sample, indexed by candidate
"""
sample_tally = {candidates[i] : 0 for i in range(len(candidates))}
for s in sample:
sample_tally[s] += 1
return sample_tally
def test_tally_sample():
candidates = ['A', 'B']
sample = ['A']*10 + ['B']*5
assert tally_sample(candidates, sample) == {'A':10, 'B':5}
def update_dirichlet(prior_parameters, sample_tally):
"""
Update the prior for the Dirichlet distribution after observing the sample
Parameters
----------
prior_parameters : dict
prior parameter values for the Dirichlet distribution, indexed by candidate
sample_tally : dict
observed number of votes in the sample, indexed by candidate
Returns
-------
dict of parameter values, indexed by candidate
"""
updated_param = {cand: prior_parameters[cand] + sample_tally[cand] for cand in prior_parameters}
return updated_param
def update_sample_tally(sample_tally, new_sample_tally):
"""
Update the sample tally after observing a new sample
Parameters
----------
sample_tally : dict
observed number of votes in the original sample, indexed by candidate
new_sample_tally : dict
observed number of votes in the new sample, indexed by candidate
Returns
-------
dict of the tallied sample, indexed by candidate
"""
sample_tally = {cand : sample_tally[cand] + new_sample_tally[cand] for cand in sample_tally}
return sample_tally
# Step 3 (don't call this fun directly - it gets called in Step 4)
def sample_from_dirichlet(prior_parameters):
"""
Draw a sample from the prior
Parameters
----------
prior_parameters : dict
prior parameter values for the Dirichlet distribution, indexed by candidate
Returns
-------
dict of draws from the prior, indexed by candidate
These correspond to "probabilities" that add up to 1.
"""
rvs = {cand : np.random.gamma(prior_parameters[cand]) for cand in prior_parameters}
tot = sum(rvs.values())
rvs = {cand : rvs[cand]/tot for cand in rvs}
return rvs
def test_sample_from_dirichlet():
np.random.seed(2345)
prior = {'A':1, 'B':1}
np_vals = [np.random.dirichlet([1, 1])[0] for i in range(1000)]
our_vals = [sample_from_dirichlet(prior)['A'] for i in range(1000)]
np.testing.assert_almost_equal(np.mean(np_vals), np.mean(our_vals), 2)
np.testing.assert_almost_equal(np.std(np_vals), np.std(our_vals), 2)
prior = {'A':5, 'B':1}
np_vals = [np.random.dirichlet([5, 1])[0] for i in range(5000)]
our_vals = [sample_from_dirichlet(prior)['A'] for i in range(5000)]
np.testing.assert_almost_equal(np.mean(np_vals), np.mean(our_vals), 2)
np.testing.assert_almost_equal(np.std(np_vals), np.std(our_vals), 2)
# Step 3.5 (don't call this fun directly - it gets called in Step 4)
def sample_from_multinomial(probs, sample_size):
"""
Draw a sample from the Dirichlet-multinomial, where a sample from the
Dirichlet prior sets the probabilities for the multinomial distribution.
Parameters
----------
probs : dict
parameter values for the multinomial distribution, indexed by candidate
sample_size : int
number of samples to draw
Returns
-------
dict with a tally of the draws, indexed by candidate
"""
cand_sorted = sorted(probs)
ps_sorted = [probs[cand] for cand in cand_sorted]
multinomial_freqs_sorted = np.random.multinomial(sample_size, ps_sorted)
freq = {vote: vote_freq for (vote, vote_freq) in zip(cand_sorted, multinomial_freqs_sorted)}
return freq
def test_sample_from_multinomial():
np.random.seed(2345)
probs = {'A':0.5, 'B':0.5}
np_vals = [np.random.multinomial(n=5, pvals=[0.5, 0.5])[0] for i in range(5000)]
our_vals = [sample_from_multinomial(probs, sample_size=5)['A'] for i in range(5000)]
np.testing.assert_almost_equal(np.mean(np_vals), np.mean(our_vals), 1)
np.testing.assert_almost_equal(np.std(np_vals), np.std(our_vals), 1)
probs = {'A':0.9, 'B':0.1}
np_vals = [np.random.multinomial(n=5, pvals=[0.9, 0.1])[0] for i in range(5000)]
our_vals = [sample_from_multinomial(probs, sample_size=5)['A'] for i in range(5000)]
np.testing.assert_almost_equal(np.mean(np_vals), np.mean(our_vals), 2)
np.testing.assert_almost_equal(np.std(np_vals), np.std(our_vals), 1)
# Step 4
def sample_dirichlet_multinomial(prior_parameters, sample_size):
"""
Draw a sample from the Dirichlet-multinomial, where a sample from the
Dirichlet prior sets the probabilities for the multinomial distribution.
Parameters
----------
prior_parameters : dict
prior parameter values for the Dirichlet distribution, indexed by candidate
sample_size : int
number of samples to draw
Returns
-------
dict with a tally of the draws, indexed by candidate
"""
multinomial_parameters = sample_from_dirichlet(prior_parameters)
freq = sample_from_multinomial(multinomial_parameters, sample_size)
return freq
# Putting it all together: Combine observed sample with fake one
def generate_pseudo_pop(sample_tally, prior, N):
n = sum(sample_tally.values())
unsample_size = N-n
fake_data = sample_dirichlet_multinomial(prior, unsample_size)
total_votes = {cand : sample_tally[cand] + fake_data[cand] for cand in fake_data}
return total_votes