def test_age_specific_rate_model_w_lower_bound_data(): # generate simulated data data_type = 'csmr' 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( data_type, n, a, pi_age_true, sigma_true) d.input_data = d.input_data.append(data_simulation.simulated_age_intervals( 'pf', n, a, pi_age_true * 2., sigma_true), ignore_index=True) d.hierarchy, d.output_template = data_simulation.small_output() # create model and priors vars = dismod_mr.model.process.age_specific_rate(d, 'pf', reference_area='all', reference_sex='total', reference_year='all', mu_age=None, mu_age_parent=None, sigma_age_parent=None, lower_bound='csmr') # fit model m = mc.MCMC(vars) m.sample(3)
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_age_standardizing_approx(): # simulate data n = 50 sigma_true = .025 * pl.ones(n) a = pl.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) ages = pl.arange(101) d = data_simulation.simulated_age_intervals('p', n, a, pi_age_true, sigma_true) # create model and priors vars = {} vars.update( age_pattern.age_pattern('test', ages, knots=pl.arange(0, 101, 5), smoothing=.01)) vars.update( age_integrating_model.age_standardize_approx( 'test', pl.ones_like(vars['mu_age'].value), vars['mu_age'], d['age_start'], d['age_end'], ages)) vars['pi'] = vars['mu_interval'] vars.update( rate_model.normal_model('test', pi=vars['pi'], sigma=0, p=d['value'], s=sigma_true)) # fit model m = mc.MCMC(vars) m.sample(3)
def test_data_model_sim(): # generate simulated data data_type = 'p' n = 50 sigma_true = .025 a = pl.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) d = data.ModelData() d.input_data = data_simulation.simulated_age_intervals(data_type, n, a, pi_age_true, sigma_true) d.hierarchy, d.output_template = data_simulation.small_output() # create model and priors vars = ism.age_specific_rate(d, data_type, reference_area='all', reference_sex='total', reference_year='all', mu_age=None, mu_age_parent=None, sigma_age_parent=None) # fit model m = mc.MCMC(vars) m.sample(3) # check estimates pi_usa = covariate_model.predict_for(d, d.parameters, 'all', 'total', 'all', 'USA', 'male', 1990, 0., vars[data_type], -pl.inf, pl.inf) # create model w/ emp prior # create model and priors vars = ism.age_specific_rate(d, data_type, reference_area='all', reference_sex='total', reference_year='all', mu_age=None, mu_age_parent=pi_usa.mean(0), sigma_age_parent=pi_usa.std(0))
def test_age_integrating_midpoint_approx(): # simulate data n = 50 sigma_true = .025 * np.ones(n) a = np.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) ages = np.arange(101) d = data_simulation.simulated_age_intervals('p', n, a, pi_age_true, sigma_true) # create model and priors vars = {} vars.update( dismod_mr.model.spline.spline('test', ages, knots=np.arange(0, 101, 5), smoothing=.01)) vars.update( dismod_mr.model.age_groups.midpoint_approx('test', vars['mu_age'], d['age_start'], d['age_end'], ages)) vars['pi'] = vars['mu_interval'] vars.update( dismod_mr.model.likelihood.normal('test', pi=vars['pi'], sigma=0, p=d['value'], s=sigma_true)) # fit model m = mc.MCMC(vars) m.sample(3)
def test_data_model_lower_bound(): # generate simulated data data_type = 'csmr' n = 50 sigma_true = .025 a = pl.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) d = data.ModelData() d.input_data = data_simulation.simulated_age_intervals(data_type, n, a, pi_age_true, sigma_true) d.input_data = d.input_data.append(data_simulation.simulated_age_intervals('pf', n, a, pi_age_true*2., sigma_true), ignore_index=True) d.hierarchy, d.output_template = data_simulation.small_output() # create model and priors vars = ism.age_specific_rate(d, 'pf', reference_area='all', reference_sex='total', reference_year='all', mu_age=None, mu_age_parent=None, sigma_age_parent=None, lower_bound='csmr') # fit model m = mc.MCMC(vars) m.sample(3)
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_age_specific_rate_model(): # generate simulated data data_type = 'p' 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( data_type, 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, data_type, reference_area='all', reference_sex='total', reference_year='all', mu_age=None, mu_age_parent=None, sigma_age_parent=None) # fit model m = mc.MCMC(vars) m.sample(3) # check estimates pi_usa = dismod_mr.model.covariates.predict_for(d, d.parameters, 'all', 'total', 'all', 'USA', 'male', 1990, 0., vars[data_type], -np.inf, np.inf) # create model w/ emp prior # create model and priors vars = dismod_mr.model.process.age_specific_rate( d, data_type, reference_area='all', reference_sex='total', reference_year='all', mu_age=None, mu_age_parent=pi_usa.mean(0), sigma_age_parent=pi_usa.std(0))
def test_age_standardizing_approx(): # simulate data n = 50 sigma_true = .025*np.ones(n) a = np.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) ages=np.arange(101) d = data_simulation.simulated_age_intervals('p', n, a, pi_age_true, sigma_true) # create model and priors vars = {} vars.update(dismod_mr.model.spline.spline('test', ages, knots=np.arange(0,101,5), smoothing=.01)) vars.update(dismod_mr.model.age_groups.age_standardize_approx('test', np.ones_like(vars['mu_age'].value), vars['mu_age'], d['age_start'], d['age_end'], ages)) vars['pi'] = vars['mu_interval'] vars.update(dismod_mr.model.likelihood.normal('test', pi=vars['pi'], sigma=0, p=d['value'], s=sigma_true)) # fit model m = mc.MCMC(vars) m.sample(3)
def test_consistent_model_sim(): m = data.ModelData() # generate simulated data n = 50 sigma_true = .025 a = pl.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) m.input_data = data_simulation.simulated_age_intervals('p', n, a, pi_age_true, sigma_true) last_index = m.input_data.index[-1] m.input_data.ix[last_index, 'data_type'] = 'r' # make sure that there are multiple data types in the data set # create model and priors vars = ism.consistent(m, 'all', 'total', 'all', {}) # fit model m = mc.MCMC(vars) m.sample(1) return vars
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 = pl.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) d = 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 = ism.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 = covariate_model.predict_for(d, d.parameters['p'], 'NAHI', 'male', 2005, 'USA', 'male', 1990, 0., vars['p'], 0., pl.inf) assert_almost_equal(pred, vars['p']['mu_age'].trace())
def test_covariate_model_shift_for_root_consistency(): # generate simulated data n = 50 sigma_true = .025 a = pl.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) d = 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 = ism.age_specific_rate(d, 'p', 'all', 'total', 'all', None, None, None) vars = ism.age_specific_rate(d, 'p', 'all', 'male', 1990, None, None, None) # fit model m = mc.MCMC(vars) m.sample(3) # check estimates pi_usa = covariate_model.predict_for(d, d.parameters['p'], 'all', 'male', 1990, 'USA', 'male', 1990, 0., vars['p'], 0., pl.inf)
import data_simulation import data_model import consistent_model import fit_model reload(consistent_model) reload(data_model) ## create model model = data.ModelData() # generate simulated data n = 50 sigma_true = .025 a = pl.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) model.input_data = data_simulation.simulated_age_intervals('p', n, a, pi_age_true, sigma_true) model.input_data['data_type'][-1] = 'r' # make sure that there are multiple data types in the data set def validate_fit_data_model(): vars = data_model.data_model('validation', model, 'p', root_area='all', root_sex='total', root_year='all', mu_age=None, mu_age_parent=None) m = fit_model.fit_data_model(vars) return m def validate_fit_consistent_model(): # create model and priors
import data_model import consistent_model import fit_model reload(consistent_model) reload(data_model) ## create model model = data.ModelData() # generate simulated data n = 50 sigma_true = .025 a = pl.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) model.input_data = data_simulation.simulated_age_intervals( 'p', n, a, pi_age_true, sigma_true) model.input_data['data_type'][ -1] = 'r' # make sure that there are multiple data types in the data set def validate_fit_data_model(): vars = data_model.data_model('validation', model, 'p', root_area='all', root_sex='total', root_year='all', mu_age=None, mu_age_parent=None) m = fit_model.fit_data_model(vars)
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
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 = pl.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) d = 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 = ism.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 = covariate_model.predict_for(d, d.parameters['p'], 'NAHI', 'male', 2005, 'USA', 'male', 1990, 0., vars['p'], 0., pl.inf) # test that the predicted value is as expected fe_usa_1990 = pl.exp(0.) re_usa_1990 = pl.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 = covariate_model.predict_for(d, d.parameters['p'], 'NAHI', 'male', 2005, 'USA', 'male', 1990, 0., vars['p'], 0., pl.inf) # test that the predicted value is as expected fe_usa_1990 = pl.exp(0.) re_usa_1990 = pl.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 pl.np.random.seed(12345) pred = covariate_model.predict_for(d, d.parameters['p'], 'NAHI', 'male', 2005, 'CAN', 'male', 1990, 0., vars['p'], 0., pl.inf) # test that the predicted value is as expected pl.np.random.seed(12345) fe = pl.exp(0.) re = pl.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 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 = pl.arange(0, 100, 1) pi_age_true = .0001 * (a * (100. - a) + 100.) d = 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 = ism.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 = covariate_model.predict_for(d, d.parameters['p'], 'all', 'total', 'all', 'USA', 'male', 1990, 0., vars['p'], 0., pl.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 = covariate_model.predict_for(d, d.parameters['p'], 'all', 'total', 'all', 'USA', 'male', 1990, 0., vars['p'], 0., pl.inf) # test that the predicted value is as expected fe_usa_1990 = 1. re_usa_1990 = pl.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 = covariate_model.predict_for(d, d.parameters['p'], 'all', 'total', 'all', 'USA', 'male', 1990, 0., vars['p'], 0., pl.inf) # test that the predicted value is as expected fe_usa_1990 = 1. re_usa_1990 = pl.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 = covariate_model.predict_for(d, d.parameters['p'], 'all', 'total', 'all', 'CAN', 'male', 1990, 0., vars['p'], 0., pl.inf) # test that the predicted value is as expected fe = 1. re = pl.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 = ism.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 = covariate_model.predict_for(d, d.parameters['p'], 'USA', 'male', 1990, 'USA', 'male', 1990, 0., vars['p'], 0., pl.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 = covariate_model.predict_for(d, d.parameters['p'], 'all', 'total', 'all', 'NAHI', 'male', 1990, 0., vars['p'], 0., pl.inf) d.vars = vars return d
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))