Example #1
0
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)
Example #2
0
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)
Example #3
0
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))
Example #4
0
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(
            '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)
Example #5
0
def test_covariate_model_sim_no_hierarchy():
    # simulate normal data
    model = 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 = pl.dot(X, beta_true)

    pi_true = pl.exp(Y_true)
    sigma_true = .01*pl.ones_like(pi_true)

    p = mc.rnormal(pi_true, 1./sigma_true**2.)

    model.input_data = pandas.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(covariate_model.mean_covariate_model('test', 1, model.input_data, {}, model, 'all', 'total', 'all'))
    vars.update(rate_model.normal_model('test', vars['pi'], 0., p, sigma_true))

    # fit model
    m = mc.MCMC(vars)
    m.sample(2)
Example #6
0
def test_covariate_model_dispersion():
    # simulate normal data
    n = 100

    model = 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.*pl.ones(n)
    eta_true = pl.log(50)
    delta_true = 50 + pl.exp(eta_true)

    p = mc.rnegative_binomial(pi_true*ess, delta_true*pl.exp(Z*zeta_true)) / ess

    
    model.input_data = pandas.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(covariate_model.mean_covariate_model('test', vars['mu'], model.input_data, {}, model, 'all', 'total', 'all'))
    vars.update(covariate_model.dispersion_covariate_model('test', model.input_data, .1, 10.))
    vars.update(rate_model.neg_binom_model('test', vars['pi'], vars['delta'], p, ess))

    # fit model
    m = mc.MCMC(vars)
    m.sample(2)
Example #7
0
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)
Example #8
0
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
    fe_usa_1990 = np.exp(
        .5 * vars['p']['beta'][0].value
    )  # beta[0] is drawn from prior, even though I set it to NoStepper, see FIXME above
    re_usa_1990 = np.exp(.1 + .2 + .3)
    assert_almost_equal(
        pred, vars['p']['mu_age'].trace() * fe_usa_1990 * re_usa_1990)
Example #9
0
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 = data.ModelData()
    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)
    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 = covariate_model.predict_for(d, d.parameters['p'],
                                         'all', 'total', 'all',
                                         'USA', 'male', 1990,
                                         0., vars['p'], 0., pl.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 = 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 = pl.exp(.5*vars['p']['beta'][0].value) # beta[0] is drawn from prior, even though I set it to NoStepper, see FIXME above
    re_usa_1990 = pl.exp(.1+.2+.3)
    assert_almost_equal(pred,
                        vars['p']['mu_age'].trace() * fe_usa_1990 * re_usa_1990)
Example #10
0
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())
Example #11
0
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))
Example #12
0
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
Example #13
0
def test_consistent():
    dm = dismod_mr.data.ModelData()
    dm.hierarchy, dm.output_template = data_simulation.small_output()

    # create model and priors
    dm.vars = dismod_mr.model.process.consistent(dm)

    mc.MCMC(dm.vars).sample(3)

    # try it again with expert priors on prevalence

    dm.parameters['p']['level_value'] = dict(value=.1, age_before=15, age_after=95)
    dm.parameters['p']['level_bounds'] = dict(upper=.01, lower=.001)

    # create model and priors
    dm.vars = dismod_mr.model.process.consistent(dm)


    mc.MCMC(dm.vars).sample(3)
Example #14
0
def test_consistent():
    dm = dismod_mr.data.ModelData()
    dm.hierarchy, dm.output_template = data_simulation.small_output()

    # create model and priors
    dm.vars = dismod_mr.model.process.consistent(dm)

    mc.MCMC(dm.vars).sample(3)

    # try it again with expert priors on prevalence

    dm.parameters['p']['level_value'] = dict(value=.1,
                                             age_before=15,
                                             age_after=95)
    dm.parameters['p']['level_bounds'] = dict(upper=.01, lower=.001)

    # create model and priors
    dm.vars = dismod_mr.model.process.consistent(dm)

    mc.MCMC(dm.vars).sample(3)
Example #15
0
def test_consistent_w_non_integral_ages():
    dm = dismod_mr.data.ModelData()
    dm.hierarchy, dm.output_template = data_simulation.small_output()

    # change to non-integral ages
    dm.parameters['ages'] = np.arange(0., 5.1, .1)
    for k in dm.parameters:
        if type(dm.parameters[k]) == dict:
            dm.parameters[k]['parameter_age_mesh'] = [0,5]

    # create model and priors
    dm.vars = dismod_mr.model.process.consistent(dm)

    mc.MCMC(dm.vars).sample(3)

    # try again, this time with m_all data
    dm.input_data = pd.DataFrame([['m_all', 0, 100, .1]], columns=['data_type', 'age_start', 'age_end', 'value'])

    dm.vars = dismod_mr.model.process.consistent(dm)

    mc.MCMC(dm.vars).sample(3)
Example #16
0
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())
Example #17
0
def test_covariate_model_sim_w_hierarchy():
    n = 50

    # setup hierarchy
    hierarchy, output_template = data_simulation.small_output()

    # simulate normal data
    area_list = pl.array(['all', 'USA', 'CAN'])
    area = area_list[mc.rcategorical([.3, .3, .4], n)]

    sex_list = pl.array(['male', 'female', 'total'])
    sex = sex_list[mc.rcategorical([.3, .3, .4], n)]

    year = pl.array(mc.runiform(1990, 2010, n), dtype=int)
        
    alpha_true = dict(all=0., USA=.1, CAN=-.2)

    pi_true = pl.exp([alpha_true[a] for a in area])
    sigma_true = .05*pl.ones_like(pi_true)

    p = mc.rnormal(pi_true, 1./sigma_true**2.)

    model = data.ModelData()
    model.input_data = pandas.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(covariate_model.mean_covariate_model('test', 1, model.input_data, {}, model,
                                                     'all', 'total', 'all'))
    vars.update(rate_model.normal_model('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
Example #18
0
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)
Example #19
0
def test_consistent_w_non_integral_ages():
    dm = dismod_mr.data.ModelData()
    dm.hierarchy, dm.output_template = data_simulation.small_output()

    # change to non-integral ages
    dm.parameters['ages'] = np.arange(0., 5.1, .1)
    for k in dm.parameters:
        if type(dm.parameters[k]) == dict:
            dm.parameters[k]['parameter_age_mesh'] = [0, 5]

    # create model and priors
    dm.vars = dismod_mr.model.process.consistent(dm)

    mc.MCMC(dm.vars).sample(3)

    # try again, this time with m_all data
    dm.input_data = pd.DataFrame(
        [['m_all', 0, 100, .1]],
        columns=['data_type', 'age_start', 'age_end', 'value'])

    dm.vars = dismod_mr.model.process.consistent(dm)

    mc.MCMC(dm.vars).sample(3)
Example #20
0
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)
Example #21
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
Example #22
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 = 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
Example #23
0
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))
Example #24
0
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))