-
Notifications
You must be signed in to change notification settings - Fork 2
/
crowd_bt.py
131 lines (106 loc) · 5.21 KB
/
crowd_bt.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
from numpy import exp, log
from scipy.special import beta, psi
# See this paper for more information:
# http://people.stern.nyu.edu/xchen3/images/crowd_pairwise.pdf
# parameters chosen according to experiments in paper
GAMMA = float(0.1) # tradeoff parameter
LAMBDA = float(1) # regularization parameter
KAPPA = float(0.0001) # to ensure positivity of variance
MU_PRIOR = float(0)
SIGMA_SQ_PRIOR = float(1)
ALPHA_PRIOR = float(10)
BETA_PRIOR = float(1)
EPSILON = 0.25 # epsilon-greedy
def argmax(f, xs):
return max(xs, key=f)
def divergence_gaussian(mu_1, sigma_sq_1, mu_2, sigma_sq_2):
# https://en.wikipedia.org/wiki/Normal_distribution#cite_ref-40:~:text=The%20Kullback%E2%80%93Leibler%20divergence%20of%20one%20normal,_%7B1%7D%5E%7B2%7D%7D%7B%5Csigma%20_%7B2%7D%5E%7B2%7D%7D%7D%2D1%2D%5Cln%20%7B%5Cfrac%20%7B%5Csigma%20_%7B1%7D%5E%7B2%7D%7D%7B%5Csigma%20_%7B2%7D%5E%7B2%7D%7D%7D%5Cright)%7D
ratio = sigma_sq_1 / sigma_sq_2
return (mu_1 - mu_2) ** 2 / (2.0 * sigma_sq_2) + (ratio - 1.0 - log(ratio)) / 2.0
def divergence_beta(alpha_1, beta_1, alpha_2, beta_2):
# https://en.wikipedia.org/wiki/Beta_distribution#cite_ref-Cover_and_Thomas_30-1:~:text=The%20relative%20entropy%2C%20or%20Kullback%E2%80%93Leibler%20divergence,or%20Kullback%E2%80%93Leibler%20divergence%2C%20is%20always%20non%2Dnegative.
return (
log(beta(alpha_2, beta_2) / beta(alpha_1, beta_1))
+ (alpha_1 - alpha_2) * psi(alpha_1)
+ (beta_1 - beta_2) * psi(beta_1)
+ (alpha_2 - alpha_1 + beta_2 - beta_1) * psi(alpha_1 + beta_1)
)
def update(alpha, beta, mu_winner, sigma_sq_winner, mu_loser, sigma_sq_loser):
(updated_alpha, updated_beta, _) = _updated_annotator(
alpha, beta, mu_winner, sigma_sq_winner, mu_loser, sigma_sq_loser
)
(updated_mu_winner, updated_mu_loser) = _updated_mus(
alpha, beta, mu_winner, sigma_sq_winner, mu_loser, sigma_sq_loser
)
(updated_sigma_sq_winner, updated_sigma_sq_loser) = _updated_sigma_sqs(
alpha, beta, mu_winner, sigma_sq_winner, mu_loser, sigma_sq_loser
)
return (
updated_alpha,
updated_beta,
updated_mu_winner,
updated_sigma_sq_winner,
updated_mu_loser,
updated_sigma_sq_loser,
)
def expected_information_gain(alpha, beta, mu_a, sigma_sq_a, mu_b, sigma_sq_b):
(alpha_1, beta_1, c) = _updated_annotator(
alpha, beta, mu_a, sigma_sq_a, mu_b, sigma_sq_b
)
(mu_a_1, mu_b_1) = _updated_mus(alpha, beta, mu_a, sigma_sq_a, mu_b, sigma_sq_b)
(sigma_sq_a_1, sigma_sq_b_1) = _updated_sigma_sqs(
alpha, beta, mu_a, sigma_sq_a, mu_b, sigma_sq_b
)
prob_a_ranked_above = c
(alpha_2, beta_2, _) = _updated_annotator(
alpha, beta, mu_b, sigma_sq_b, mu_a, sigma_sq_a
)
(mu_b_2, mu_a_2) = _updated_mus(alpha, beta, mu_b, sigma_sq_b, mu_a, sigma_sq_a)
(sigma_sq_b_2, sigma_sq_a_2) = _updated_sigma_sqs(
alpha, beta, mu_b, sigma_sq_b, mu_a, sigma_sq_a
)
return prob_a_ranked_above * (
divergence_gaussian(mu_a_1, sigma_sq_a_1, mu_a, sigma_sq_a)
+ divergence_gaussian(mu_b_1, sigma_sq_b_1, mu_b, sigma_sq_b)
+ GAMMA * divergence_beta(alpha_1, beta_1, alpha, beta)
) + (1 - prob_a_ranked_above) * (
divergence_gaussian(mu_a_2, sigma_sq_a_2, mu_a, sigma_sq_a)
+ divergence_gaussian(mu_b_2, sigma_sq_b_2, mu_b, sigma_sq_b)
+ GAMMA * divergence_beta(alpha_2, beta_2, alpha, beta)
)
def _updated_mus(alpha, beta, mu_winner, sigma_sq_winner, mu_loser, sigma_sq_loser):
mult = (alpha * exp(mu_winner)) / (
alpha * exp(mu_winner) + beta * exp(mu_loser)
) - (exp(mu_winner)) / (exp(mu_winner) + exp(mu_loser))
updated_mu_winner = mu_winner + sigma_sq_winner * mult
updated_mu_loser = mu_loser - sigma_sq_loser * mult
return (updated_mu_winner, updated_mu_loser)
def _updated_sigma_sqs(
alpha, beta, mu_winner, sigma_sq_winner, mu_loser, sigma_sq_loser
):
mult = (alpha * exp(mu_winner) * beta * exp(mu_loser)) / (
(alpha * exp(mu_winner) + beta * exp(mu_loser)) ** 2
) - (exp(mu_winner) * exp(mu_loser)) / ((exp(mu_winner) + exp(mu_loser)) ** 2)
updated_sigma_sq_winner = sigma_sq_winner * max(1 + sigma_sq_winner * mult, KAPPA)
updated_sigma_sq_loser = sigma_sq_loser * max(1 + sigma_sq_loser * mult, KAPPA)
return (updated_sigma_sq_winner, updated_sigma_sq_loser)
def _updated_annotator(
alpha, beta, mu_winner, sigma_sq_winner, mu_loser, sigma_sq_loser
):
c_1 = exp(mu_winner) / (exp(mu_winner) + exp(mu_loser)) + 0.5 * (
sigma_sq_winner + sigma_sq_loser
) * (exp(mu_winner) * exp(mu_loser) * (exp(mu_loser) - exp(mu_winner))) / (
(exp(mu_winner) + exp(mu_loser)) ** 3
)
c_2 = 1 - c_1
c = (c_1 * alpha + c_2 * beta) / (alpha + beta) # pr i >k j
expt = (c_1 * (alpha + 1) * alpha + c_2 * alpha * beta) / (
c * (alpha + beta + 1) * (alpha + beta)
)
expt_sq = (
c_1 * (alpha + 2) * (alpha + 1) * alpha + c_2 * (alpha + 1) * alpha * beta
) / (c * (alpha + beta + 2) * (alpha + beta + 1) * (alpha + beta))
variance = expt_sq - expt**2
updated_alpha = ((expt - expt_sq) * expt) / variance
updated_beta = (expt - expt_sq) * (1 - expt) / variance
return (updated_alpha, updated_beta, c)