forked from ihmeuw/dismod_mr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_covariates.py
523 lines (392 loc) · 19.3 KB
/
test_covariates.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
""" Test models that use covariates
"""
import numpy as np
import pymc as mc
import pandas as pd
import dismod_mr
import data_simulation
def test_covariate_model_sim_no_hierarchy():
# simulate normal data
model = dismod_mr.data.ModelData()
model.hierarchy, model.output_template = data_simulation.small_output()
X = mc.rnormal(0., 1.**2, size=(128, 3))
beta_true = [-.1, .1, .2]
Y_true = np.dot(X, beta_true)
pi_true = np.exp(Y_true)
sigma_true = .01*np.ones_like(pi_true)
p = mc.rnormal(pi_true, 1./sigma_true**2.)
model.input_data = pd.DataFrame(dict(value=p, x_0=X[:, 0], x_1=X[:, 1], x_2=X[:, 2]))
model.input_data['area'] = 'all'
model.input_data['sex'] = 'total'
model.input_data['year_start'] = 2000
model.input_data['year_end'] = 2000
# create model and priors
vars = {}
vars.update(dismod_mr.model.covariates.mean_covariate_model(name='test', mu=1, input_data=model.input_data,
parameters={}, model=model, root_area='all', root_sex='total', root_year='all', zero_re=True))
vars.update(dismod_mr.model.likelihood.normal('test', vars['pi'], 0., p, sigma_true))
# fit model
m = mc.MCMC(vars)
m.sample(2)
def test_covariate_model_sim_w_hierarchy():
n = 50
# setup hierarchy
hierarchy, output_template = data_simulation.small_output()
# simulate normal data
area_list = np.array(['all', 'USA', 'CAN'])
area = area_list[mc.rcategorical([.3, .3, .4], n)]
sex_list = np.array(['male', 'female', 'total'])
sex = sex_list[mc.rcategorical([.3, .3, .4], n)]
year = np.array(mc.runiform(1990, 2010, n), dtype=int)
alpha_true = dict(all=0., USA=.1, CAN=-.2)
pi_true = np.exp([alpha_true[a] for a in area])
sigma_true = .05*np.ones_like(pi_true)
p = mc.rnormal(pi_true, 1./sigma_true**2.)
model = dismod_mr.data.ModelData()
model.input_data = pd.DataFrame(
dict(value=p, area=area, sex=sex, year_start=year, year_end=year))
model.hierarchy, model.output_template = hierarchy, output_template
# create model and priors
vars = {}
vars.update(dismod_mr.model.covariates.mean_covariate_model('test', 1, model.input_data, {}, model,
'all', 'total', 'all'))
vars.update(dismod_mr.model.likelihood.normal('test', vars['pi'], 0., p, sigma_true))
# fit model
m = mc.MCMC(vars)
m.sample(2)
assert 'sex' not in vars['U']
assert 'x_sex' in vars['X']
assert len(vars['beta']) == 1
def test_fixed_effect_priors():
model = dismod_mr.data.ModelData()
# set prior on sex
parameters = dict(fixed_effects={'x_sex': dict(
dist='TruncatedNormal', mu=1., sigma=.5, lower=-10, upper=10)})
# simulate normal data
n = 32
sex_list = np.array(['male', 'female', 'total'])
sex = sex_list[mc.rcategorical([.3, .3, .4], n)]
beta_true = dict(male=-1., total=0., female=1.)
pi_true = np.exp([beta_true[s] for s in sex])
sigma_true = .05
p = mc.rnormal(pi_true, 1./sigma_true**2.)
model.input_data = pd.DataFrame(dict(value=p, sex=sex))
model.input_data['area'] = 'all'
model.input_data['year_start'] = 2010
model.input_data['year_start'] = 2010
# create model and priors
vars = {}
vars.update(dismod_mr.model.covariates.mean_covariate_model('test', 1, model.input_data, parameters, model,
'all', 'total', 'all'))
print(vars['beta'])
assert vars['beta'][0].parents['mu'] == 1.
def test_random_effect_priors():
model = dismod_mr.data.ModelData()
# set prior on sex
parameters = dict(random_effects={'USA': dict(
dist='TruncatedNormal', mu=.1, sigma=.5, lower=-10, upper=10)})
# simulate normal data
n = 32
area_list = np.array(['all', 'USA', 'CAN'])
area = area_list[mc.rcategorical([.3, .3, .4], n)]
alpha_true = dict(all=0., USA=.1, CAN=-.2)
pi_true = np.exp([alpha_true[a] for a in area])
sigma_true = .05
p = mc.rnormal(pi_true, 1./sigma_true**2.)
model.input_data = pd.DataFrame(dict(value=p, area=area))
model.input_data['sex'] = 'male'
model.input_data['year_start'] = 2010
model.input_data['year_end'] = 2010
model.hierarchy.add_edge('all', 'USA')
model.hierarchy.add_edge('all', 'CAN')
# create model and priors
vars = {}
vars.update(dismod_mr.model.covariates.mean_covariate_model('test', 1, model.input_data, parameters, model,
'all', 'total', 'all'))
print(vars['alpha'])
print(vars['alpha'][1].parents['mu'])
#assert vars['alpha'][1].parents['mu'] == .1
def test_covariate_model_dispersion():
# simulate normal data
n = 100
model = dismod_mr.data.ModelData()
model.hierarchy, model.output_template = data_simulation.small_output()
Z = mc.rcategorical([.5, 5.], n)
zeta_true = -.2
pi_true = .1
ess = 10000.*np.ones(n)
eta_true = np.log(50)
delta_true = 50 + np.exp(eta_true)
p = mc.rnegative_binomial(pi_true*ess, delta_true*np.exp(Z*zeta_true)) / ess
model.input_data = pd.DataFrame(dict(value=p, z_0=Z))
model.input_data['area'] = 'all'
model.input_data['sex'] = 'total'
model.input_data['year_start'] = 2000
model.input_data['year_end'] = 2000
# create model and priors
vars = dict(mu=mc.Uninformative('mu_test', value=pi_true))
vars.update(dismod_mr.model.covariates.mean_covariate_model(
'test', vars['mu'], model.input_data, {}, model, 'all', 'total', 'all'))
vars.update(dismod_mr.model.covariates.dispersion_covariate_model(
'test', model.input_data, .1, 10.))
vars.update(dismod_mr.model.likelihood.neg_binom('test', vars['pi'], vars['delta'], p, ess))
# fit model
m = mc.MCMC(vars)
m.sample(2)
def test_covariate_model_shift_for_root_consistency():
# generate simulated data
n = 50
sigma_true = .025
a = np.arange(0, 100, 1)
pi_age_true = .0001 * (a * (100. - a) + 100.)
d = dismod_mr.data.ModelData()
d.input_data = data_simulation.simulated_age_intervals('p', n, a, pi_age_true, sigma_true)
d.hierarchy, d.output_template = data_simulation.small_output()
# create model and priors
vars = dismod_mr.model.process.age_specific_rate(
d, 'p', 'all', 'total', 'all', None, None, None)
vars = dismod_mr.model.process.age_specific_rate(d, 'p', 'all', 'male', 1990, None, None, None)
# fit model
m = mc.MCMC(vars)
m.sample(3)
# check estimates
pi_usa = dismod_mr.model.covariates.predict_for(
d, d.parameters['p'], 'all', 'male', 1990, 'USA', 'male', 1990, 0., vars['p'], 0., np.inf)
def test_predict_for():
""" Approach to testing predict_for function:
1. Create model with known mu_age, known covariate values, known effect coefficients
2. Setup MCMC with NoStepper for all stochs
3. Sample to generate trace with known values
4. Predict for results, and confirm that they match expected values
"""
# generate simulated data
n = 5
sigma_true = .025
a = np.arange(0, 100, 1)
pi_age_true = .0001 * (a * (100. - a) + 100.)
d = dismod_mr.data.ModelData()
d.input_data = data_simulation.simulated_age_intervals('p', n, a, pi_age_true, sigma_true)
d.hierarchy, d.output_template = data_simulation.small_output()
# create model and priors
vars = dismod_mr.model.process.age_specific_rate(
d, 'p', 'all', 'total', 'all', None, None, None)
# fit model
m = mc.MCMC(vars)
for n in m.stochastics:
m.use_step_method(mc.NoStepper, n)
m.sample(3)
# Prediction case 1: constant zero random effects, zero fixed effect coefficients
# check estimates with priors on random effects
d.parameters['p']['random_effects'] = {}
for node in ['USA', 'CAN', 'NAHI', 'super-region-1', 'all']:
d.parameters['p']['random_effects'][node] = dict(
dist='Constant', mu=0, sigma=1.e-9) # zero out REs to see if test passes
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'all', 'total', 'all',
'USA', 'male', 1990,
0., vars['p'], 0., np.inf)
# test that the predicted value is as expected
fe_usa_1990 = 1.
re_usa_1990 = 1.
assert_almost_equal(pred,
vars['p']['mu_age'].trace() * fe_usa_1990 * re_usa_1990)
# Prediction case 2: constant non-zero random effects, zero fixed effect coefficients
# check estimates with priors on random effects
for i, node in enumerate(['USA', 'NAHI', 'super-region-1']):
d.parameters['p']['random_effects'][node]['mu'] = (i+1.)/10.
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'all', 'total', 'all',
'USA', 'male', 1990,
0., vars['p'], 0., np.inf)
# test that the predicted value is as expected
fe_usa_1990 = 1.
re_usa_1990 = np.exp(.1+.2+.3)
assert_almost_equal(pred,
vars['p']['mu_age'].trace() * fe_usa_1990 * re_usa_1990)
# Prediction case 3: confirm that changing RE for reference area does not change results
d.parameters['p']['random_effects']['all']['mu'] = 1.
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'all', 'total', 'all',
'USA', 'male', 1990,
0., vars['p'], 0., np.inf)
# test that the predicted value is as expected
fe_usa_1990 = 1.
re_usa_1990 = np.exp(.1+.2+.3) # unchanged, since it is alpha_all that is now 1.
assert_almost_equal(pred,
vars['p']['mu_age'].trace() * fe_usa_1990 * re_usa_1990)
# Prediction case 4: see that prediction of CAN includes region and super-region effect, but not USA effect
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'all', 'total', 'all',
'CAN', 'male', 1990,
0., vars['p'], 0., np.inf)
# test that the predicted value is as expected
fe = 1.
re = np.exp(0.+.2+.3) # unchanged, since it is alpha_all that is now 1.
assert_almost_equal(pred,
vars['p']['mu_age'].trace() * fe * re)
# create model and priors
vars = dismod_mr.model.process.age_specific_rate(d, 'p', 'USA', 'male', 1990, None, None, None)
# fit model
m = mc.MCMC(vars)
for n in m.stochastics:
m.use_step_method(mc.NoStepper, n)
m.sample(3)
# check estimates
pi_usa = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'USA', 'male', 1990,
'USA', 'male', 1990,
0., vars['p'], 0., np.inf)
# test that the predicted value is as expected
assert_almost_equal(pi_usa, vars['p']['mu_age'].trace())
# Prediction case 5: confirm that const RE prior with sigma = 0 does not crash
d.parameters['p']['random_effects']['USA']['sigma'] = 0.
d.parameters['p']['random_effects']['CAN']['sigma'] = 0.
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'all', 'total', 'all',
'NAHI', 'male', 1990,
0., vars['p'], 0., np.inf)
d.vars = vars
return d
# TODO: test predict for when there is a random effect (alpha)
# TODO: test predict when zerore=True
# TODO: test predicting for various values in the output template
def test_predict_for_wo_data():
""" Approach to testing predict_for function:
1. Create model with known mu_age, known covariate values, known effect coefficients
2. Setup MCMC with NoStepper for all stochs
3. Sample to generate trace with known values
4. Predict for results, and confirm that they match expected values
"""
d = dismod_mr.data.ModelData()
d.hierarchy, d.output_template = data_simulation.small_output()
# create model and priors
vars = dismod_mr.model.process.age_specific_rate(
d, 'p', 'all', 'total', 'all', None, None, None)
# fit model
m = mc.MCMC(vars)
m.sample(1)
# Prediction case 1: constant zero random effects, zero fixed effect coefficients
# check estimates with priors on random effects
d.parameters['p']['random_effects'] = {}
for node in ['USA', 'NAHI', 'super-region-1', 'all']:
d.parameters['p']['random_effects'][node] = dict(
dist='Constant', mu=0, sigma=1.e-9) # zero out REs to see if test passes
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'all', 'total', 'all',
'USA', 'male', 1990,
0., vars['p'], 0., np.inf)
# Prediction case 2: constant non-zero random effects, zero fixed effect coefficients
# FIXME: this test was failing because PyMC is drawing from the prior of beta[0] even though I asked for NoStepper
# check estimates with priors on random effects
for i, node in enumerate(['USA', 'NAHI', 'super-region-1']):
d.parameters['p']['random_effects'][node]['mu'] = (i+1.)/10.
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'all', 'total', 'all',
'USA', 'male', 1990,
0., vars['p'], 0., np.inf)
# test that the predicted value is as expected
# beta[0] is drawn from prior, even though I set it to NoStepper, see FIXME above
fe_usa_1990 = np.exp(.5*vars['p']['beta'][0].value)
re_usa_1990 = np.exp(.1+.2+.3)
assert_almost_equal(pred,
vars['p']['mu_age'].trace() * fe_usa_1990 * re_usa_1990)
def test_predict_for_wo_effects():
""" Approach to testing predict_for function:
1. Create model with known mu_age, known covariate values, known effect coefficients
2. Setup MCMC with NoStepper for all stochs
3. Sample to generate trace with known values
4. Predict for results, and confirm that they match expected values
"""
# generate simulated data
n = 5
sigma_true = .025
a = np.arange(0, 100, 1)
pi_age_true = .0001 * (a * (100. - a) + 100.)
d = dismod_mr.data.ModelData()
d.input_data = data_simulation.simulated_age_intervals('p', n, a, pi_age_true, sigma_true)
d.hierarchy, d.output_template = data_simulation.small_output()
# create model and priors
vars = dismod_mr.model.process.age_specific_rate(
d, 'p', 'NAHI', 'male', 2005, None, None, None, include_covariates=False)
# fit model
m = mc.MCMC(vars)
for n in m.stochastics:
m.use_step_method(mc.NoStepper, n)
m.sample(10)
# Prediction case: prediction should match mu age
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'NAHI', 'male', 2005,
'USA', 'male', 1990,
0., vars['p'], 0., np.inf)
assert_almost_equal(pred,
vars['p']['mu_age'].trace())
def test_predict_for_w_region_as_reference():
""" Approach to testing predict_for function:
1. Create model with known mu_age, known covariate values, known effect coefficients
2. Setup MCMC with NoStepper for all stochs
3. Sample to generate trace with known values
4. Predict for results, and confirm that they match expected values
"""
# generate simulated data
n = 5
sigma_true = .025
a = np.arange(0, 100, 1)
pi_age_true = .0001 * (a * (100. - a) + 100.)
d = dismod_mr.data.ModelData()
d.input_data = data_simulation.simulated_age_intervals('p', n, a, pi_age_true, sigma_true)
d.hierarchy, d.output_template = data_simulation.small_output()
# create model and priors
vars = dismod_mr.model.process.age_specific_rate(d, 'p', 'NAHI', 'male', 2005, None, None, None)
# fit model
m = mc.MCMC(vars)
for n in m.stochastics:
m.use_step_method(mc.NoStepper, n)
m.sample(10)
# Prediction case 1: constant zero random effects, zero fixed effect coefficients
# check estimates with priors on random effects
d.parameters['p']['random_effects'] = {}
for node in ['USA', 'NAHI', 'super-region-1', 'all']:
d.parameters['p']['random_effects'][node] = dict(
dist='Constant', mu=0, sigma=1.e-9) # zero out REs to see if test passes
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'NAHI', 'male', 2005,
'USA', 'male', 1990,
0., vars['p'], 0., np.inf)
# test that the predicted value is as expected
fe_usa_1990 = np.exp(0.)
re_usa_1990 = np.exp(0.)
assert_almost_equal(pred,
vars['p']['mu_age'].trace() * fe_usa_1990 * re_usa_1990)
# Prediction case 2: constant non-zero random effects, zero fixed effect coefficients
# check estimates with priors on random effects
for i, node in enumerate(['USA', 'NAHI', 'super-region-1', 'all']):
d.parameters['p']['random_effects'][node]['mu'] = (i+1.)/10.
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'NAHI', 'male', 2005,
'USA', 'male', 1990,
0., vars['p'], 0., np.inf)
# test that the predicted value is as expected
fe_usa_1990 = np.exp(0.)
re_usa_1990 = np.exp(.1)
assert_almost_equal(pred,
vars['p']['mu_age'].trace() * fe_usa_1990 * re_usa_1990)
# Prediction case 3: random effect not constant, zero fixed effect coefficients
# set random seed to make randomness reproducible
np.random.seed(12345)
pred = dismod_mr.model.covariates.predict_for(d, d.parameters['p'],
'NAHI', 'male', 2005,
'CAN', 'male', 1990,
0., vars['p'], 0., np.inf)
# test that the predicted value is as expected
np.random.seed(12345)
fe = np.exp(0.)
re = np.exp(mc.rnormal(0., vars['p']['sigma_alpha'][3].trace()**-2))
assert_almost_equal(pred.mean(0),
(vars['p']['mu_age'].trace().T * fe * re).T.mean(0))
def assert_almost_equal(x, y):
log_offset_diff = np.log(x + 1.e-4) - np.log(y + 1.e-4)
assert np.all(log_offset_diff**2 <=
1.e-4), 'expected approximate equality, found means of:\n %s\n %s' % (x.mean(1), y.mean(1))
if __name__ == '__main__':
import nose
nose.runmodule()