def test_sample_individual_estimates(testdata):
    model = Model(testdata / 'nonmem' / 'pheno_real.mod')
    np.random.seed(86)
    samples = sample_individual_estimates(model)
    assert len(samples) == 59 * 100
    assert list(samples.columns) == ['ETA(1)', 'ETA(2)']
    assert pytest.approx(samples.iloc[0]['ETA(1)'], 1e-5) == 0.0418399
    assert pytest.approx(samples.iloc[0]['ETA(2)'], 1e-5) == -0.0587623

    restricted = sample_individual_estimates(model,
                                             parameters=['ETA(2)'],
                                             samples_per_id=1)
    assert len(restricted) == 59
    assert restricted.columns == ['ETA(2)']
    assert pytest.approx(restricted.iloc[0]['ETA(2)'], 1e-5) == -0.0167246
Пример #2
0
def test_sample_individual_estimates(testdata):
    model = Model(testdata / 'nonmem' / 'pheno_real.mod')
    rng = np.random.default_rng(86)
    samples = sample_individual_estimates(model, seed=rng)
    assert len(samples) == 59 * 100
    assert list(samples.columns) == ['ETA(1)', 'ETA(2)']
    assert pytest.approx(samples.iloc[0]['ETA(1)'],
                         1e-5) == 0.21179186940672637
    assert pytest.approx(samples.iloc[0]['ETA(2)'],
                         1e-5) == -0.05771736555248238

    restricted = sample_individual_estimates(model,
                                             parameters=['ETA(2)'],
                                             samples_per_id=1,
                                             seed=rng)
    assert len(restricted) == 59
    assert restricted.columns == ['ETA(2)']
    assert pytest.approx(restricted.iloc[0]['ETA(2)'],
                         1e-5) == 0.06399039578129821
Пример #3
0
def calculate_results_using_bipp(frem_model,
                                 continuous,
                                 categorical,
                                 rescale=True,
                                 samples=2000,
                                 seed=None):
    """Estimate a covariance matrix for the frem model using the BIPP method

    Bootstrap on the individual parameter posteriors
    Only the individual estimates, individual unvertainties and the parameter estimates
    are needed.

    """
    if seed is None or isinstance(seed, int):
        seed = np.random.default_rng(seed)
    rvs, dist = frem_model.random_variables.iiv.distributions()[-1]
    etas = [rv.name for rv in rvs]
    pool = sample_individual_estimates(frem_model, parameters=etas, seed=seed)
    ninds = len(pool.index.unique())
    ishr = frem_model.modelfit_results.individual_shrinkage
    lower_indices = np.tril_indices(len(etas))
    pop_params = np.array(dist.sigma).astype(str)[lower_indices]
    parameter_samples = np.empty((samples, len(pop_params)))
    remaining_samples = samples
    k = 0
    while k < remaining_samples:
        bootstrap = pool.sample(n=ninds,
                                replace=True,
                                random_state=seed.bit_generator)
        ishk = ishr.loc[bootstrap.index]
        cf = (1 / (1 - ishk.mean()))**(1 / 2)
        corrected_bootstrap = bootstrap * cf
        bootstrap_cov = corrected_bootstrap.cov()
        if not is_posdef(bootstrap_cov.to_numpy()):
            continue
        parameter_samples[k, :] = bootstrap_cov.values[lower_indices]
        k += 1
    frame = pd.DataFrame(parameter_samples, columns=pop_params)
    res = calculate_results_from_samples(frem_model,
                                         continuous,
                                         categorical,
                                         frame,
                                         rescale=rescale)
    return res