-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameterFitting.py
177 lines (143 loc) · 4.96 KB
/
parameterFitting.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
import copy
from scipy.optimize import minimize
from model import *
def llh(param, data, R=None, minimize=True, savedata=False):
df = copy.deepcopy(data)
restaurant_iter = {1: 2,
2: 3,
3: 4,
4: 1}
if R is None:
R = restaurant_val(data)
def select_utility(param, r, p):
if len(param) == 4:
u_a = u_accept(r, p, param[:-1], R)
u_r = u_reject(r, p, param[:-1], R)
elif len(param) == 3:
u_a = u_blind_accept(r, p, param[:-1], R)
u_r = 0.0
return [u_r, u_a]
# model parameters
if len(param) == 4:
alpha = param[0]
delta = param[1]
gamma = param[2]
beta = param[3]
elif len(param) == 3:
alpha = param[0]
delta = param[1]
beta = param[2]
llh = 0
# Isolate r, p, and choice. Don't care about session in this case
partial_data = data[['tone_prob', 'restaurant', 'accept']]
Data = np.ones(5)
for i in range(len(data)):
tone = float(partial_data.loc[i][0]) # data: tone probability
r = int(partial_data.loc[i][1]) # data: restaurant
a = int(partial_data.loc[i][2]) # data: choice
u = np.array(select_utility(param, r, tone))
# compute softmax probabilities
p = softmax(u, beta)
# updata log likelihood
if p[a] < 1e-5:
p[a] = 1 - 0.999
llh += np.log(p[a])
"""a=1 means accept, a=0 means reject"""
Data = np.vstack((Data, np.array([a, u, p, p[a], llh])))
if minimize == True:
return -llh
Data = Data[1:]
df['a'] = Data[:, 0]
df['u'] = Data[:, 1]
df['p'] = Data[:, 2]
df['likelihood'] = Data[:, 3]
df['llh'] = Data[:, 4]
lh = np.exp(llh / data.shape[0])
if savedata == False:
return -llh, lh, param
if minimize == True:
return -llh
return -llh, lh, param, df
def optimize(fname,
R,
bounds,
Data,
niter,
toplot=False,
):
outcomes = np.full([niter, len(bounds) + 1], np.nan)
optimcurve = np.full(niter, np.nan)
for i in range(niter):
# random starting point based on maximum bounds
params0 = np.array([bound[1] * np.random.rand() for bound in bounds])
# compute the function value at the starting point
llh0 = fname(params0, Data, R)
# run the optimizer with constraints
result = minimize(fun=fname, x0=params0, args=(Data, R), bounds=bounds)
x = result.x
bestllh = fname(x, Data, R)
outcomes[i, :] = [bestllh] + [xi for xi in x]
optimcurve[i] = min(outcomes[:(i + 1), 0])
# find the global minimum out of all outcomes
i = np.argwhere(outcomes[:, 0] == np.min(outcomes[:, 0]))
bestparameters = outcomes[i[0], 1:].flatten()
bestllh = -1 * outcomes[i[0], 0].flatten()[0]
# plot the best llh found by the optimizer as a function of iteration number.
if toplot:
plt.figure()
plt.plot(range(niter), np.round(optimcurve, 6), 'o-')
plt.xlabel('iteration')
plt.ylabel('best minimum llh')
return (bestparameters, bestllh)
def llh_reparameterized(param, data, R=None):
P = [0, 0.2, 0.8, 1.0]
df = copy.deepcopy(data)
restaurant_iter = {1: 2,
2: 3,
3: 4,
4: 1}
if R is None:
R = restaurant_val(data)
def select_utility(param, r, p):
if len(param) == 4:
u_a = u_accept(r, p, param[:-1], R)
u_r = u_reject(r, p, param[:-1], R)
elif len(param) == 3:
u_a = u_blind_accept(r, p, param[:-1], R)
u_r = 0.0
return [u_r, u_a]
# model parameters
if len(param) == 4:
alpha = param[0]
delta = param[1]
gamma = param[2]
beta = param[3]
elif len(param) == 3:
alpha = param[0]
delta = param[1]
beta = param[2]
llh = 0
# Isolate r, p, and choice. Don't care about session in this case
partial_data = data[['p', 'r', 'choice']]
total_sess = data['session'].unique()
for i in total_sess:
r = 1 # index of the first restaurant
p = np.random.choice(P)
U = np.array(select_utility(param, r, p))
trials = len(data[data['session'] == i])
data = data[data['session'] == i]
for t in range(trials):
# compute softmax probabilities
p = softmax(U, beta)
a = data['choice'][t]
# updata log likelihood
if p[a] < 1e-5:
p[a] = 1 - 0.999
llh += np.log(p[a])
"""a=1 means accept, a=0 means reject"""
# Update to the next restaurant with new probability
r = restaurant_iter[r]
p = np.random.choice(P)
U = np.array(select_utility(param, r, p))
lh = np.exp(llh / data.shape[0])
return -llh