-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_hddm.py
376 lines (297 loc) · 16 KB
/
my_hddm.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from collections import OrderedDict
import inspect
import kabuki
import numpy as np
import pymc as pm
import kabuki.step_methods as steps
from hddm.models import HDDMBase
from kabuki.hierarchical import Knode
from kabuki.utils import HalfCauchy
class HDDMGamma(HDDMBase):
def __init__(self, *args, **kwargs):
self.informative = kwargs.pop('informative', True)
super(HDDMGamma, self).__init__(*args, **kwargs)
def pre_sample(self, use_slice=True):
slice_widths = {'a':1, 't':0.01, 'a_var': 1, 't_var': 0.15, 'sz': 1.1, 'v': 1.5,
'st': 0.1, 'sv': 3, 'z_trans': 0.2, 'z': 0.1, 'p_outlier':1., 'v_var': 1,
'v_shift': 1, 'v(c0)': 1.5, 'v_shift_var': 0.5, 'v(c0)_var': 0.5}
for name, node_descr in self.iter_stochastics():
node = node_descr['node']
if isinstance(node, pm.Normal) and np.all([isinstance(x, pm.Normal) for x in node.extended_children]):
self.mc.use_step_method(steps.kNormalNormal, node)
else:
knode_name = node_descr['knode_name'].replace('_subj', '')
if knode_name in ['st', 'sv', 'sz']:
left = 0
else:
left = None
self.mc.use_step_method(steps.SliceStep, node, width=slice_widths.get(knode_name,1),
left=left, maxiter=5000)
def create_family_normal(self, name, value=0, g_mu=None,
g_tau=15**-2, var_S = 1,
var_value=.1):
"""Create a family of knodes. A family is a group of knodes
that belong together.
For example, a family could consist of the following distributions:
* group mean g_mean (Normal(g_mu, g_tau))
* group variability g_var (Uniform(var_lower, var_upper))
* transform node g_var_trans for g_var (x -> x**-2)
* subject (Normal(g_mean, g_var_trans))
In fact, if is_group_model is True and the name does not appear in
group_only nodes, this is the family that will be created.
Otherwise, only a Normal knode will be returned.
:Arguments:
name : str
Name of the family. Each family member will have this name prefixed.
:Optional:
value : float
Starting value.
g_mu, g_tau, var_lower, var_upper, var_value : float
The hyper parameters for the different family members (see above).
:Returns:
OrderedDict: member name -> member Knode
"""
if g_mu is None:
g_mu = value
knodes = OrderedDict()
if self.is_group_model and name not in self.group_only_nodes:
g = Knode(pm.Normal, '%s' % name, mu=g_mu, tau=g_tau,
value=value, depends=self.depends[name])
var = Knode(HalfCauchy, '%s_var' % name, S=var_S, value=var_value)
tau = Knode(pm.Deterministic, '%s_tau' % name,
doc='%s_tau' % name, eval=lambda x: x**-2, x=var,
plot=False, trace=False, hidden=True)
subj = Knode(pm.Normal, '%s_subj' % name, mu=g, tau=tau,
value=value, depends=('subj_idx',),
subj=True, plot=self.plot_subjs)
knodes['%s'%name] = g
knodes['%s_var'%name] = var
knodes['%s_tau'%name] = tau
knodes['%s_bottom'%name] = subj
else:
subj = Knode(pm.Normal, name, mu=g_mu, tau=g_tau,
value=value, depends=self.depends[name])
knodes['%s_bottom'%name] = subj
return knodes
def create_family_gamma(self, name, value=1, g_mean=1, g_std=1, std_std=2, var_value=.1):
"""Similar to create_family_normal() but adds an exponential
transform knode to the subject and group mean nodes. This is useful
when the parameter space is restricted from [0, +oo).
See create_family_normal() help for more information.
"""
knodes = OrderedDict()
g_shape = (g_mean**2) / (g_std**2)
g_rate = g_mean / (g_std**2)
if self.is_group_model and name not in self.group_only_nodes:
g = Knode(pm.Gamma, name, alpha=g_shape, beta=g_rate,
value=g_mean, depends=self.depends[name])
var = Knode(pm.HalfNormal, '%s_var' % name, tau=std_std**-2, value=var_value)
shape = Knode(pm.Deterministic, '%s_shape' % name, eval=lambda x,y: (x**2)/(y**2),
x=g, y=var, plot=False, trace=False, hidden=True)
rate = Knode(pm.Deterministic, '%s_rate' % name, eval=lambda x,y: x/(y**2),
x=g, y=var, plot=False, trace=False, hidden=True)
subj = Knode(pm.Gamma, '%s_subj'%name, alpha=shape, beta=rate,
value=value, depends=('subj_idx',),
subj=True, plot=False)
knodes['%s'%name] = g
knodes['%s_var'%name] = var
knodes['%s_rate'%name] = rate
knodes['%s_shape'%name] = shape
knodes['%s_bottom'%name] = subj
else:
g = Knode(pm.Gamma, name, alpha=g_shape, beta=g_rate, value=value,
depends=self.depends[name])
knodes['%s_bottom'%name] = g
return knodes
def create_family_invlogit(self, name, value, g_mu=None, g_tau=15**-2,
var_S=0.2, var_value=.1):
"""Similar to create_family_normal() but adds a invlogit
transform knode to the subject and group mean nodes. This is useful
when the parameter space is restricted from [0, 1].
See create_family_normal() help for more information.
"""
if g_mu is None:
g_mu = value
# logit transform values
value_trans = np.log(value) - np.log(1-value)
g_mu_trans = np.log(g_mu) - np.log(1-g_mu)
knodes = OrderedDict()
if self.is_group_model and name not in self.group_only_nodes:
g_trans = Knode(pm.Normal,
'%s_trans'%name,
mu=g_mu_trans,
tau=g_tau,
value=value_trans,
depends=self.depends[name],
plot=False,
hidden=True
)
g = Knode(pm.InvLogit, name, ltheta=g_trans, plot=True,
trace=True)
var = Knode(HalfCauchy, '%s_var'%name, S=var_S,
value=var_value)
tau = Knode(pm.Deterministic, '%s_tau'%name, doc='%s_tau'
% name, eval=lambda x: x**-2, x=var,
plot=False, trace=False, hidden=True)
subj_trans = Knode(pm.Normal, '%s_subj_trans'%name,
mu=g_trans, tau=tau, value=value_trans,
depends=('subj_idx',), subj=True,
plot=False, hidden=True)
subj = Knode(pm.InvLogit, '%s_subj'%name,
ltheta=subj_trans, depends=('subj_idx',),
plot=self.plot_subjs, trace=True, subj=True)
knodes['%s_trans'%name] = g_trans
knodes['%s'%name] = g
knodes['%s_var'%name] = var
knodes['%s_tau'%name] = tau
knodes['%s_subj_trans'%name] = subj_trans
knodes['%s_bottom'%name] = subj
else:
g_trans = Knode(pm.Normal, '%s_trans'%name, mu=g_mu_trans,
tau=g_tau, value=value_trans,
depends=self.depends[name], plot=False, hidden=True)
g = Knode(pm.InvLogit, '%s'%name, ltheta=g_trans, plot=True,
trace=True )
knodes['%s_trans'%name] = g_trans
knodes['%s_bottom'%name] = g
return knodes
def _create_stochastic_knodes(self, include):
if self.informative:
return self._create_stochastic_knodes_informative(include)
else:
return self._create_stochastic_knodes_noninformative(include)
def _create_stochastic_knodes_informative(self, include):
knodes = OrderedDict()
if 'a' in include:
knodes.update(self.create_family_gamma('a', g_mean=1.5, g_std=0.75, std_std=2, var_value=0.1, value=1))
if 'v' in include:
knodes.update(self.create_family_normal('v', value=0, g_tau=5**-2, var_S=1))
if 't' in include:
knodes.update(self.create_family_gamma('t', g_mean=.4, g_std=0.2, value=0.001, std_std=1, var_value=0.2))
if 'sv' in include:
knodes['sv_bottom'] = Knode(pm.HalfNormal, 'sv', tau=2**-2, value=1, depends=self.depends['sv'])
if 'sz' in include:
knodes['sz_bottom'] = Knode(pm.Beta, 'sz', alpha=1, beta=3, value=0.01, depends=self.depends['sz'])
if 'st' in include:
knodes['st_bottom'] = Knode(pm.HalfNormal, 'st', tau=0.3**-2, value=0.001, depends=self.depends['st'])
if 'z' in self.include:
knodes.update(self.create_family_invlogit('z', value=.5, g_tau=0.5**-2, var_S=0.05))
if 'p_outlier' in include:
knodes['p_outlier_bottom'] = Knode(pm.Beta, 'p_outlier', alpha=1, beta=15, value=0.01, depends=self.depends['p_outlier'])
return knodes
def _create_stochastic_knodes_noninformative(self, include):
knodes = OrderedDict()
if 'a' in include:
knodes.update(self.create_family_trunc_normal('a', lower=1e-3, upper=1e3, value=1))
if 'v' in include:
knodes.update(self.create_family_normal('v', value=0, g_tau=50**-2, var_S=10))
if 't' in include:
knodes.update(self.create_family_trunc_normal('t', lower=1e-3, upper=1e3, value=.01))
if 'sv' in include:
knodes['sv_bottom'] = Knode(pm.Uniform, 'sv', lower=1e-6, upper=1e3, value=1, depends=self.depends['sv'])
if 'sz' in include:
knodes['sz_bottom'] = Knode(pm.Beta, 'sz', alpha=1, beta=1, value=0.01, depends=self.depends['sz'])
if 'st' in include:
knodes['st_bottom'] = Knode(pm.Uniform, 'st', lower=1e-6, upper=1e3, value=0.01, depends=self.depends['st'])
if 'z' in self.include:
knodes.update(self.create_family_invlogit('z', value=.5, g_tau=10**-2, var_S=0.5))
if 'p_outlier' in include:
knodes['p_outlier_bottom'] = Knode(pm.Beta, 'p_outlier', alpha=1, beta=1, value=0.01, depends=self.depends['p_outlier'])
return knodes
def _create_an_average_model(self):
"""
create an average model for group model quantiles optimization.
"""
#this code only check that the arguments are as expected, i.e. the constructor was not change
#since we wrote this function
super_init_function = super(self.__class__, self).__init__
init_args = set(inspect.getargspec(super_init_function).args)
known_args = set(['wiener_params', 'include', 'self', 'bias', 'data', 'p_outlier'])
assert known_args == init_args, "Arguments of the constructor are not as expected"
#create the avg model
avg_model = self.__class__(self.data, include=self.include, is_group_model=False, **self._kwargs)
return avg_model
# class HDDM2(HDDGamma):
# def __init__(self, *args, **kwargs):
# super(self.__class__, self).__init__(*args, **kwargs)
# def pre_sample(self, use_slice=True):
# slice_widths = {'a':1, 't':0.01, 'a_var': 0.2, 't_var': 0.15, 'sz': 1.1,
# 'st': 0.1, 'sv': 3, 'v': 1.5, 'z_trans': 0.2, 'z': 0.1, 'p_outlier':1.}
# for name, node_descr in self.iter_stochastics():
# node = node_descr['node']
# knode_name = node_descr['knode_name'].replace('_subj', '')
# left = None
# if knode_name in ['v', 'z_trans'] and self.is_group_model:
# self.mc.use_step_method(steps.kNormalNormal, node)
# else:
# if knode_name in ['st', 'sv', 'sz']:
# left = 0
# else:
# left = None
# self.mc.use_step_method(steps.SliceStep, node, width=slice_widths[knode_name],
# left=left, maxiter=1000)
# def _create_stochastic_knodes(self, include):
# knodes = OrderedDict()
# if 'a' in include:
# knodes.update(self.create_family_gamma('a', g_mean=1.5, g_std=0.75, var_S=0.05, var_value=0.1, value=1))
# if 'v' in include:
# knodes.update(self.create_family_shifted_normal('v', value=0, g_tau=5**-2, var_S=1))
# if 't' in include:
# knodes.update(self.create_family_gamma('t', g_mean=.4, g_std=0.2, value=0.01, var_S=0.05, var_value=0.2))
# if 'sv' in include:
# knodes['sv_bottom'] = Knode(pm.HalfNormal, 'sv', tau=2**-2, value=1, depends=self.depends['sv'])
# if 'sz' in include:
# knodes['sz_bottom'] = Knode(pm.Beta, 'sz', alpha=1, beta=3, value=0.01, depends=self.depends['sz'])
# if 'st' in include:
# knodes['st_bottom'] = Knode(pm.HalfNormal, 'st', tau=0.3**-2, value=0.01, depends=self.depends['st'])
# if 'z' in self.include:
# knodes.update(self.create_family_invlogit('z', value=.5, g_tau=0.5**-2, var_S=0.05))
# if 'p_outlier' in include:
# knodes['p_outlier_bottom'] = Knode(pm.Beta, 'p_outlier', alpha=1, beta=15, value=0.01, depends=self.depends['p_outlier'])
# return knodes
# def create_family_shifted_normal(self, name, value=0, g_mu=None,
# g_tau=15**-2, var_S = 1,
# var_value=.1):
# """Create a family of knodes. A family is a group of knodes
# that belong together.
# For example, a family could consist of the following distributions:
# * group mean g_mean (Normal(g_mu, g_tau))
# * group variability g_var (Uniform(var_lower, var_upper))
# * transform node g_var_trans for g_var (x -> x**-2)
# * subject (Normal(g_mean, g_var_trans))
# In fact, if is_group_model is True and the name does not appear in
# group_only nodes, this is the family that will be created.
# Otherwise, only a Normal knode will be returned.
# :Arguments:
# name : str
# Name of the family. Each family member will have this name prefixed.
# :Optional:
# value : float
# Starting value.
# g_mu, g_tau, var_lower, var_upper, var_value : float
# The hyper parameters for the different family members (see above).
# :Returns:
# OrderedDict: member name -> member Knode
# """
# if g_mu is None:
# g_mu = value
# knodes = OrderedDict()
# if self.is_group_model and name not in self.group_only_nodes:
# g = Knode(pm.Normal, '%s' % name, mu=g_mu, tau=g_tau,
# value=value, depends=self.depends[name])
# var = Knode(HalfCauchy, '%s_var' % name, S=var_S, value=var_value)
# tau = Knode(pm.Deterministic, '%s_tau' % name,
# doc='%s_tau' % name, eval=lambda x: x**-2, x=var,
# plot=False, trace=False, hidden=True)
# subj = Knode(pm.Normal, '%s_subj' % name, mu=g, tau=tau,
# value=value, depends=('subj_idx',),
# subj=True, plot=self.plot_subjs)
# knodes['%s'%name] = g
# knodes['%s_var'%name] = var
# knodes['%s_tau'%name] = tau
# knodes['%s_bottom'%name] = subj
# else:
# subj = Knode(pm.Normal, name, mu=g_mu, tau=g_tau,
# value=value, depends=self.depends[name])
# knodes['%s_bottom'%name] = subj
# return knodes