示例#1
0
def test_pyjulia_pipeline(sampler: Sampler):
    """Test that a pipeline with Julia calls runs through."""
    jl = pyabc.external.julia.Julia(
        source_file="doc/examples/model_julia/Normal.jl",
        module_name="Normal",
    )
    # just call it
    assert jl.display_source_ipython()  # noqa: S101

    model = jl.model()
    distance = jl.distance()
    obs = jl.observation()

    prior = Distribution(p=RV("uniform", -5, 10))

    if not isinstance(sampler, SingleCoreSampler):
        # call model once for Julia pre-combination
        distance(model(prior.rvs()), model(prior.rvs()))

    db_file = tempfile.mkstemp(suffix=".db")[1]
    abc = ABCSMC(model, prior, distance, population_size=100, sampler=sampler)
    abc.new("sqlite:///" + db_file, obs)
    abc.run(max_nr_populations=2)

    if os.path.exists(db_file):
        os.remove(db_file)
示例#2
0
def test_resume(db_path, gt_model):
    def model(parameter):
        return {"data": parameter["mean"] + sp.randn()}

    prior = Distribution(mean=RV("uniform", 0, 5))

    def distance(x, y):
        x_data = x["data"]
        y_data = y["data"]
        return abs(x_data - y_data)

    abc = ABCSMC(model, prior, distance, population_size=10)
    history = abc.new(db_path, {"data": 2.5}, gt_model=gt_model)
    run_id = history.id
    print("Run ID:", run_id)
    hist_new = abc.run(minimum_epsilon=0, max_nr_populations=1)
    assert hist_new.n_populations == 1

    abc_continued = ABCSMC(model, prior, distance)
    run_id_continued = abc_continued.load(db_path, run_id)
    print("Run ID continued:", run_id_continued)
    hist_contd = abc_continued.run(minimum_epsilon=0, max_nr_populations=1)

    assert hist_contd.n_populations == 2
    assert hist_new.n_populations == 2
示例#3
0
def test_pipeline(transition: Transition):
    """Test the various transitions in a full pipeline."""
    def model(p):
        return {'s0': p['a'] + p['b'] * np.random.normal()}

    prior = Distribution(a=RV('uniform', -5, 10), b=RV('uniform', 0.01, 0.09))

    abc = ABCSMC(model, prior, transitions=transition, population_size=10)
    abc.new(create_sqlite_db_id(), {'s0': 3.5})
    abc.run(max_nr_populations=3)
示例#4
0
def test_model_gets_parameter(transition: Transition):
    """Check that we use Parameter objects as model input throughout.

    This should be the case both when the parameter is created from the prior,
    and from the transition.
    """
    def model(p):
        assert isinstance(p, Parameter)
        return {'s0': p['p0'] + 0.1 * np.random.normal()}
    prior = Distribution(p0=RV('uniform', -5, 10))

    abc = ABCSMC(model, prior, transitions=transition, population_size=10)
    abc.new(create_sqlite_db_id(), {'s0': 3.5})
    abc.run(max_nr_populations=3)
示例#5
0
def test_progressbar(sampler):
    """Test whether using a progress bar gives any errors."""
    def model(p):
        return {"y": p['p0'] + 0.1 * np.random.randn(10)}

    def distance(y1, y2):
        return np.abs(y1['y'] - y2['y']).sum()

    prior = Distribution(p0=RV('uniform', -5, 10))
    obs = {'y': 1}

    abc = ABCSMC(model, prior, distance, sampler=sampler, population_size=20)
    abc.new(db=create_sqlite_db_id(), observed_sum_stat=obs)
    abc.run(max_nr_populations=3)
示例#6
0
def test_beta_binomial_two_identical_models_adaptive(db_path, sampler):
    binomial_n = 5

    def model_fun(args):
        return {"result": st.binom(binomial_n, args.theta).rvs()}

    models = [model_fun for _ in range(2)]
    models = list(map(FunctionModel, models))
    population_size = AdaptivePopulationSize(800)
    parameter_given_model_prior_distribution = [
        Distribution(theta=st.beta(1, 1)) for _ in range(2)
    ]
    abc = ABCSMC(
        models,
        parameter_given_model_prior_distribution,
        MinMaxDistance(measures_to_use=["result"]),
        population_size,
        eps=MedianEpsilon(0.1),
        sampler=sampler,
    )
    abc.new(db_path, {"result": 2})

    minimum_epsilon = 0.2
    history = abc.run(minimum_epsilon, max_nr_populations=3)
    mp = history.get_model_probabilities(history.max_t)
    assert abs(mp.p[0] - 0.5) + abs(mp.p[1] - 0.5) < 0.08
示例#7
0
def test_empty_population(db_path, sampler):
    def make_model(theta):
        def model(args):
            return {"result": 1 if random.random() > theta else 0}

        return model

    theta1 = .2
    theta2 = .6
    model1 = make_model(theta1)
    model2 = make_model(theta2)
    models = [model1, model2]
    models = list(map(SimpleModel, models))
    population_size = ConstantPopulationSize(1500)
    parameter_given_model_prior_distribution = [Distribution(), Distribution()]
    abc = ABCSMC(models, parameter_given_model_prior_distribution,
                 MinMaxDistanceFunction(measures_to_use=["result"]),
                 population_size,
                 eps=MedianEpsilon(0),
                 sampler=sampler)
    abc.new(db_path, {"result": 0})

    minimum_epsilon = -1
    history = abc.run(minimum_epsilon, max_nr_populations=3)



    mp = history.get_model_probabilities(history.max_t)
    expected_p1, expected_p2 = theta1 / (theta1 + theta2), theta2 / (theta1 +
                                                                     theta2)
    assert abs(mp.p[0] - expected_p1) + abs(mp.p[1] - expected_p2) < .05
示例#8
0
def test_two_competing_gaussians_multiple_population(db_path, sampler):
    # Define a gaussian model
    sigma = 0.5

    def model(args):
        return {"y": st.norm(args['x'], sigma).rvs()}

    # We define two models, but they are identical so far
    models = [model, model]
    models = list(map(FunctionModel, models))

    # However, our models' priors are not the same. Their mean differs.
    mu_x_1, mu_x_2 = 0, 1
    parameter_given_model_prior_distribution = [
        Distribution(x=st.norm(mu_x_1, sigma)),
        Distribution(x=st.norm(mu_x_2, sigma)),
    ]

    # We plug all the ABC setup together
    nr_populations = 1
    population_size = ConstantPopulationSize(20)
    abc = ABCSMC(
        models,
        parameter_given_model_prior_distribution,
        PercentileDistance(measures_to_use=["y"]),
        population_size,
        eps=MedianEpsilon(0.2),
        sampler=sampler,
    )

    # Finally we add meta data such as model names and
    # define where to store the results
    # y_observed is the important piece here: our actual observation.
    y_observed = 2
    abc.new(db_path, {"y": y_observed})

    # We run the ABC with 3 populations max
    minimum_epsilon = 0.05
    history = abc.run(minimum_epsilon, max_nr_populations=nr_populations)

    assert history.max_t == nr_populations - 1

    # Evaluate the model probabililties
    history.get_model_probabilities(history.max_t)

    def p_y_given_model(mu_x_model):
        res = st.norm(mu_x_model, np.sqrt(sigma ** 2 + sigma ** 2)).pdf(
            y_observed
        )
        return res

    p1_expected_unnormalized = p_y_given_model(mu_x_1)
    p2_expected_unnormalized = p_y_given_model(mu_x_2)
    _ = p1_expected_unnormalized / (
        p1_expected_unnormalized + p2_expected_unnormalized
    )
    _ = p2_expected_unnormalized / (
        p1_expected_unnormalized + p2_expected_unnormalized
    )
def test_stop_acceptance_rate_too_low(db_path):
    abc = ABCSMC(model, Distribution(par=st.uniform(0, 10)), dist, 10)
    abc.new(db_path, {"par": .5})
    history = abc.run(-1, 8, min_acceptance_rate=set_acc_rate)
    df = history.get_all_populations()
    df["acceptance_rate"] = df["particles"] / df["samples"]
    assert df["acceptance_rate"].iloc[-1] < set_acc_rate
    assert df["acceptance_rate"].iloc[-2] >= set_acc_rate
示例#10
0
def test_max_walltime(db_path):
    """Test the maximum walltime condition."""
    abc = ABCSMC(model, Distribution(par=st.uniform(0, 10)), dist, pop_size)
    abc.new(db_path, {"par": .5})
    init_walltime = datetime.now()
    max_walltime = timedelta(milliseconds=500)
    history = abc.run(-1, 100, max_walltime=max_walltime)
    assert datetime.now() - init_walltime > max_walltime
    assert history.n_populations < 100
示例#11
0
def test_stop_acceptance_rate_too_low(db_path):
    """Test the acceptance rate condition."""
    abc = ABCSMC(model, Distribution(par=st.uniform(0, 10)), dist, pop_size)
    abc.new(db_path, {"par": .5})
    history = abc.run(-1, 8, min_acceptance_rate=set_acc_rate)
    df = history.get_all_populations()
    df["acceptance_rate"] = df["particles"] / df["samples"]
    assert df["acceptance_rate"].iloc[-1] < set_acc_rate
    assert df["acceptance_rate"].iloc[-2] >= set_acc_rate \
        or df["t"].iloc[-2] == -1  # calibration iteration
示例#12
0
def test_gaussian_multiple_populations_crossval_kde(db_path, sampler):
    sigma_x = 1
    sigma_y = 0.5
    y_observed = 2

    def model(args):
        return {"y": st.norm(args['x'], sigma_y).rvs()}

    models = [model]
    models = list(map(FunctionModel, models))
    nr_populations = 4
    population_size = ConstantPopulationSize(600)
    parameter_given_model_prior_distribution = [
        Distribution(x=st.norm(0, sigma_x))
    ]
    parameter_perturbation_kernels = [
        GridSearchCV(
            MultivariateNormalTransition(),
            {"scaling": np.logspace(-1, 1.5, 5)},
        )
    ]
    abc = ABCSMC(
        models,
        parameter_given_model_prior_distribution,
        MinMaxDistance(measures_to_use=["y"]),
        population_size,
        transitions=parameter_perturbation_kernels,
        eps=MedianEpsilon(0.2),
        sampler=sampler,
    )
    abc.new(db_path, {"y": y_observed})

    minimum_epsilon = -1

    abc.do_not_stop_when_only_single_model_alive()
    history = abc.run(minimum_epsilon, max_nr_populations=nr_populations)
    posterior_x, posterior_weight = history.get_distribution(0, None)
    posterior_x = posterior_x["x"].values
    sort_indices = np.argsort(posterior_x)
    f_empirical = sp.interpolate.interp1d(
        np.hstack((-200, posterior_x[sort_indices], 200)),
        np.hstack((0, np.cumsum(posterior_weight[sort_indices]), 1)),
    )

    sigma_x_given_y = 1 / np.sqrt(1 / sigma_x**2 + 1 / sigma_y**2)
    mu_x_given_y = sigma_x_given_y**2 * y_observed / sigma_y**2
    expected_posterior_x = st.norm(mu_x_given_y, sigma_x_given_y)
    x = np.linspace(-8, 8)
    max_distribution_difference = np.absolute(
        f_empirical(x) - expected_posterior_x.cdf(x)).max()
    assert max_distribution_difference < 0.052
    assert history.max_t == nr_populations - 1
    mean_emp, std_emp = mean_and_std(posterior_x, posterior_weight)
    assert abs(mean_emp - mu_x_given_y) < 0.07
    assert abs(std_emp - sigma_x_given_y) < 0.12
示例#13
0
def test_two_competing_gaussians_multiple_population_adaptive_populatin_size(db_path, sampler):
    # Define a gaussian model
    sigma = .5

    def model(args):
        return {"y": st.norm(args['x'], sigma).rvs()}

    # We define two models, but they are identical so far
    models = [model, model]
    models = list(map(SimpleModel, models))

    # The prior over the model classes is uniform
    model_prior = RV("randint", 0, 2)

    # However, our models' priors are not the same. Their mean differs.
    mu_x_1, mu_x_2 = 0, 1
    parameter_given_model_prior_distribution = [Distribution(x=st.norm(mu_x_1, sigma)),
                                                Distribution(x=st.norm(mu_x_2, sigma))]

    # Particles are perturbed in a Gaussian fashion
    parameter_perturbation_kernels = [MultivariateNormalTransition() for _ in range(2)]

    # We plug all the ABC setup together
    nr_populations = 3
    population_size = AdaptivePopulationSize(400, mean_cv=0.05,
                                             max_population_size=1000)
    abc = ABCSMC(models, parameter_given_model_prior_distribution,
                 MinMaxDistanceFunction(measures_to_use=["y"]),
                 population_size,
                 model_prior=model_prior,
                 eps=MedianEpsilon(.2),
                 sampler=sampler)

    # Finally we add meta data such as model names and define where to store the results
    # y_observed is the important piece here: our actual observation.
    y_observed = 1
    abc.new(db_path, {"y": y_observed})

    # We run the ABC with 3 populations max
    minimum_epsilon = .05
    history = abc.run(minimum_epsilon, max_nr_populations=3)

    # Evaluate the model probabililties
    mp = history.get_model_probabilities(history.max_t)

    def p_y_given_model(mu_x_model):
        return st.norm(mu_x_model, sp.sqrt(sigma ** 2 + sigma ** 2)).pdf(y_observed)

    p1_expected_unnormalized = p_y_given_model(mu_x_1)
    p2_expected_unnormalized = p_y_given_model(mu_x_2)
    p1_expected = p1_expected_unnormalized / (p1_expected_unnormalized + p2_expected_unnormalized)
    p2_expected = p2_expected_unnormalized / (p1_expected_unnormalized + p2_expected_unnormalized)
    assert history.max_t == nr_populations-1
    assert abs(mp.p[0] - p1_expected) + abs(mp.p[1] - p2_expected) < .07
def test_stop_early(db_path):
    mc_sampler = MulticoreEvalParallelSampler(check_max_eval=True)
    sc_sampler = SingleCoreSampler(check_max_eval=True)
    for sampler in [mc_sampler, sc_sampler]:
        abc = ABCSMC(model, Distribution(par=st.uniform(0, 10)), dist, 10,
                     sampler=sampler)
        abc.new(db_path, {"par": .5})
        history = abc.run(
            max_nr_populations=8, min_acceptance_rate=set_acc_rate)
        df = history.get_all_populations()
        df["acceptance_rate"] = df["particles"] / df["samples"]
        assert df["acceptance_rate"].iloc[-1] >= set_acc_rate
示例#15
0
def test_total_nr_simulations(db_path):
    """Test the total number of samples condition."""
    abc = ABCSMC(model, Distribution(par=st.uniform(0, 10)), dist, pop_size)
    abc.new(db_path, {"par": .5})
    max_total_nr_sim = 142
    history = abc.run(-1, 100, max_total_nr_simulations=max_total_nr_sim)
    assert history.total_nr_simulations >= max_total_nr_sim
    # Directly check on the history
    df = history.get_all_populations()
    # Make sure budget is not exceeded yet in previous iteration
    assert sum(df['samples'][:-1]) < max_total_nr_sim
    # Just to make sure .total_nr_simulations does what it's supposed to
    assert sum(df['samples']) == history.total_nr_simulations
示例#16
0
def test_redis_catch_error():

    def model(pars):
        if np.random.uniform() < 0.1:
            raise ValueError("error")
        return {'s0': pars['p0'] + 0.2 * np.random.uniform()}

    def distance(s0, s1):
        return abs(s0['s0'] - s1['s0'])

    prior = Distribution(p0=RV("uniform", 0, 10))
    sampler = RedisEvalParallelSamplerServerStarter(
        batch_size=3, workers=1, processes_per_worker=1, port=8775)

    abc = ABCSMC(model, prior, distance, sampler=sampler, population_size=10)

    db_file = "sqlite:///" + os.path.join(tempfile.gettempdir(), "test.db")
    data = {'s0': 2.8}
    abc.new(db_file, data)

    abc.run(minimum_epsilon=.1, max_nr_populations=3)

    sampler.cleanup()
示例#17
0
def test_min_eps_diff(db_path):
    """Test the minimum epsilon difference condition."""
    abc = ABCSMC(model, Distribution(par=st.uniform(0, 10)), dist, pop_size)
    abc.new(db_path, {"par": 0.5})
    min_eps_diff = 1
    history = abc.run(
        minimum_epsilon=-1,
        max_nr_populations=100,
        min_eps_diff=min_eps_diff,
    )
    pops = history.get_all_populations()
    eps = pops.epsilon.to_numpy()
    assert abs(eps[-1] - eps[-2]) < min_eps_diff
    assert history.n_populations < 100
示例#18
0
def test_gaussian_single_population(db_path, sampler):
    sigma_prior = 1
    sigma_ground_truth = 1
    observed_data = 1

    def model(args):
        return {"y": st.norm(args['x'], sigma_ground_truth).rvs()}

    models = [model]
    models = list(map(FunctionModel, models))
    nr_populations = 1
    population_size = ConstantPopulationSize(600)
    parameter_given_model_prior_distribution = [
        Distribution(x=RV("norm", 0, sigma_prior))
    ]
    abc = ABCSMC(
        models,
        parameter_given_model_prior_distribution,
        MinMaxDistance(measures_to_use=["y"]),
        population_size,
        eps=MedianEpsilon(0.1),
        sampler=sampler,
    )
    abc.new(db_path, {"y": observed_data})

    minimum_epsilon = -1

    abc.do_not_stop_when_only_single_model_alive()
    history = abc.run(minimum_epsilon, max_nr_populations=nr_populations)
    posterior_x, posterior_weight = history.get_distribution(0, None)
    posterior_x = posterior_x["x"].values
    sort_indices = np.argsort(posterior_x)
    f_empirical = sp.interpolate.interp1d(
        np.hstack((-200, posterior_x[sort_indices], 200)),
        np.hstack((0, np.cumsum(posterior_weight[sort_indices]), 1)),
    )

    sigma_x_given_y = 1 / np.sqrt(1 / sigma_prior**2 +
                                  1 / sigma_ground_truth**2)
    mu_x_given_y = (sigma_x_given_y**2 * observed_data / sigma_ground_truth**2)
    expected_posterior_x = st.norm(mu_x_given_y, sigma_x_given_y)
    x = np.linspace(-8, 8)
    max_distribution_difference = np.absolute(
        f_empirical(x) - expected_posterior_x.cdf(x)).max()
    assert max_distribution_difference < 0.12
    assert history.max_t == nr_populations - 1
    mean_emp, std_emp = mean_and_std(posterior_x, posterior_weight)
    assert abs(mean_emp - mu_x_given_y) < 0.07
    assert abs(std_emp - sigma_x_given_y) < 0.1
示例#19
0
def test_stop_acceptance_rate_too_low(db_path):
    set_acc_rate = 0.2

    def model(x):
        return {"par": x["par"] + sp.randn()}

    def dist(x, y):
        return abs(x["par"] - y["par"])

    abc = ABCSMC(model, Distribution(par=st.uniform(0, 10)), dist, 10)
    abc.new(db_path, {"par": .5})
    history = abc.run(-1, 8, min_acceptance_rate=set_acc_rate)
    df = history.get_all_populations()
    df["acceptance_rate"] = df["particles"] / df["samples"]
    assert df["acceptance_rate"].iloc[-1] < set_acc_rate
    assert df["acceptance_rate"].iloc[-2] >= set_acc_rate
示例#20
0
def test_two_competing_gaussians_single_population(db_path, sampler,
                                                   transition):
    sigma_x = 0.5
    sigma_y = 0.5
    y_observed = 1

    def model(args):
        return {"y": st.norm(args['x'], sigma_y).rvs()}

    models = [model, model]
    models = list(map(FunctionModel, models))
    population_size = ConstantPopulationSize(500)
    mu_x_1, mu_x_2 = 0, 1
    parameter_given_model_prior_distribution = [
        Distribution(x=st.norm(mu_x_1, sigma_x)),
        Distribution(x=st.norm(mu_x_2, sigma_x)),
    ]
    abc = ABCSMC(
        models,
        parameter_given_model_prior_distribution,
        MinMaxDistance(measures_to_use=["y"]),
        population_size,
        transitions=[transition(), transition()],
        eps=MedianEpsilon(0.02),
        sampler=sampler,
    )
    abc.new(db_path, {"y": y_observed})

    minimum_epsilon = -1
    nr_populations = 1
    abc.do_not_stop_when_only_single_model_alive()
    history = abc.run(minimum_epsilon, max_nr_populations=1)
    mp = history.get_model_probabilities(history.max_t)

    def p_y_given_model(mu_x_model):
        return st.norm(mu_x_model,
                       np.sqrt(sigma_y**2 + sigma_x**2)).pdf(y_observed)

    p1_expected_unnormalized = p_y_given_model(mu_x_1)
    p2_expected_unnormalized = p_y_given_model(mu_x_2)
    p1_expected = p1_expected_unnormalized / (p1_expected_unnormalized +
                                              p2_expected_unnormalized)
    p2_expected = p2_expected_unnormalized / (p1_expected_unnormalized +
                                              p2_expected_unnormalized)
    assert history.max_t == nr_populations - 1
    assert abs(mp.p[0] - p1_expected) + abs(mp.p[1] - p2_expected) < 0.07
示例#21
0
def test_gaussian_multiple_populations_adpative_population_size(
        db_path, sampler):
    sigma_x = 1
    sigma_y = .5
    y_observed = 2

    def model(args):
        return {"y": st.norm(args['x'], sigma_y).rvs()}

    models = [model]
    models = list(map(SimpleModel, models))
    nr_populations = 4
    population_size = AdaptivePopulationSize(600)
    parameter_given_model_prior_distribution = [
        Distribution(x=st.norm(0, sigma_x))
    ]
    abc = ABCSMC(models,
                 parameter_given_model_prior_distribution,
                 MinMaxDistanceFunction(measures_to_use=["y"]),
                 population_size,
                 eps=MedianEpsilon(.2),
                 sampler=sampler)
    abc.new(db_path, {"y": y_observed})

    minimum_epsilon = -1

    abc.do_not_stop_when_only_single_model_alive()
    history = abc.run(minimum_epsilon, max_nr_populations=nr_populations)
    posterior_x, posterior_weight = history.get_distribution(0, None)
    posterior_x = posterior_x["x"].as_matrix()
    sort_indices = sp.argsort(posterior_x)
    f_empirical = sp.interpolate.interp1d(
        sp.hstack((-200, posterior_x[sort_indices], 200)),
        sp.hstack((0, sp.cumsum(posterior_weight[sort_indices]), 1)))

    sigma_x_given_y = 1 / sp.sqrt(1 / sigma_x**2 + 1 / sigma_y**2)
    mu_x_given_y = sigma_x_given_y**2 * y_observed / sigma_y**2
    expected_posterior_x = st.norm(mu_x_given_y, sigma_x_given_y)
    x = sp.linspace(-8, 8)
    max_distribution_difference = sp.absolute(
        f_empirical(x) - expected_posterior_x.cdf(x)).max()
    assert max_distribution_difference < 0.15
    assert history.max_t == nr_populations - 1
    mean_emp, std_emp = mean_and_std(posterior_x, posterior_weight)
    assert abs(mean_emp - mu_x_given_y) < .07
    assert abs(std_emp - sigma_x_given_y) < .12
示例#22
0
def test_all_in_one_model(db_path, sampler):
    models = [AllInOneModel() for _ in range(2)]
    population_size = ConstantPopulationSize(800)
    parameter_given_model_prior_distribution = [Distribution(theta=RV("beta",
                                                                      1, 1))
                                                for _ in range(2)]
    abc = ABCSMC(models, parameter_given_model_prior_distribution,
                 MinMaxDistanceFunction(measures_to_use=["result"]),
                 population_size,
                 eps=MedianEpsilon(.1),
                 sampler=sampler)
    abc.new(db_path, {"result": 2})

    minimum_epsilon = .2
    history = abc.run(minimum_epsilon, max_nr_populations=3)
    mp = history.get_model_probabilities(history.max_t)
    assert abs(mp.p[0] - .5) + abs(mp.p[1] - .5) < .08
示例#23
0
文件: sim.py 项目: ptti/lmic-testing
def command():
    parser = argparse.ArgumentParser()
    parser.add_argument("model",
                        nargs="+",
                        help="Kappa model files to simulate")
    parser.add_argument("-fit",
                        nargs="*",
                        default="",
                        help="Parameters to fit")
    parser.add_argument("-fix",
                        nargs="*",
                        default="",
                        help="Parameters to fix")
    parser.add_argument("-N",
                        type=int,
                        default="10000",
                        help="Population size")
    parser.add_argument("-I", type=int, default="10", help="Initial infected")
    parser.add_argument("-tmax",
                        default=365,
                        type=int,
                        help="Simulation max time")
    parser.add_argument("-db",
                        default="sqlite:///abc.db",
                        help="Database for ABC MCMC results")
    parser.add_argument("-R", default=1.0, type=float, help="Target R(t)")
    args = parser.parse_args()

    fixed = dict(
        (k, float(v)) for k, v in map(lambda s: s.split("="), args.fix))
    fixed["N"] = args.N
    fixed["INIT_I"] = args.I
    m = Model(args.model, fixed=fixed, tmax=args.tmax)

    priors = dict((n, RV("uniform", float(lb), float(ub)))
                  for (n, lb, ub) in map(lambda v: v.split(":"), args.fit))
    prior = Distribution(priors)

    abc = ABCSMC(m, prior, distance_target_R)
    abc_id = abc.new(args.db, {"R": args.R})
    history = abc.run(max_nr_populations=15)

    df, w = history.get_distribution()
    best = np.argmax(w)
    print(df.iloc[best])
示例#24
0
def test_beta_binomial_different_priors_initial_epsilon_from_sample(
        db_path, sampler):
    binomial_n = 5

    def model(args):
        return {"result": st.binom(binomial_n, args.theta).rvs()}

    models = [model for _ in range(2)]
    models = list(map(FunctionModel, models))
    population_size = ConstantPopulationSize(800)
    a1, b1 = 1, 1
    a2, b2 = 10, 1
    parameter_given_model_prior_distribution = [
        Distribution(theta=RV("beta", a1, b1)),
        Distribution(theta=RV("beta", a2, b2)),
    ]
    abc = ABCSMC(
        models,
        parameter_given_model_prior_distribution,
        MinMaxDistance(measures_to_use=["result"]),
        population_size,
        eps=MedianEpsilon(median_multiplier=0.9),
        sampler=sampler,
    )
    n1 = 2
    abc.new(db_path, {"result": n1})

    minimum_epsilon = -1
    history = abc.run(minimum_epsilon, max_nr_populations=5)
    mp = history.get_model_probabilities(history.max_t)

    def B(a, b):
        return gamma(a) * gamma(b) / gamma(a + b)

    def expected_p(a, b, n1):
        return binom(binomial_n, n1) * B(a + n1, b + binomial_n - n1) / B(a, b)

    p1_expected_unnormalized = expected_p(a1, b1, n1)
    p2_expected_unnormalized = expected_p(a2, b2, n1)
    p1_expected = p1_expected_unnormalized / (p1_expected_unnormalized +
                                              p2_expected_unnormalized)
    p2_expected = p2_expected_unnormalized / (p1_expected_unnormalized +
                                              p2_expected_unnormalized)

    assert abs(mp.p[0] - p1_expected) + abs(mp.p[1] - p2_expected) < 0.08
示例#25
0
def test_stop_early(db_path):
    """Test early stopping inside a generation."""
    mc_sampler = MulticoreEvalParallelSampler(check_max_eval=True)
    sc_sampler = SingleCoreSampler(check_max_eval=True)
    for sampler in [mc_sampler, sc_sampler]:
        abc = ABCSMC(model,
                     Distribution(par=st.uniform(0, 10)),
                     dist,
                     pop_size,
                     sampler=sampler)
        abc.new(db_path, {"par": .5})
        history = abc.run(min_acceptance_rate=set_acc_rate)
        df = history.get_all_populations()

        # offset with n_procs as more processes can have run at termination
        n_procs = sampler.n_procs if hasattr(sampler, 'n_procs') else 1
        df["corrected_acceptance_rate"] = \
            df["particles"] / (df["samples"] - (n_procs-1))

        assert df["corrected_acceptance_rate"].iloc[-1] >= set_acc_rate
示例#26
0
def test_continuous_non_gaussian(db_path, sampler):
    def model(args):
        return {"result": sp.rand() * args['u']}

    models = [model]
    models = list(map(SimpleModel, models))
    population_size = ConstantPopulationSize(250)
    parameter_given_model_prior_distribution = [Distribution(u=RV("uniform", 0,
                                                                  1))]
    abc = ABCSMC(models, parameter_given_model_prior_distribution,
                 MinMaxDistanceFunction(measures_to_use=["result"]),
                 population_size,
                 eps=MedianEpsilon(.2),
                 sampler=sampler)
    d_observed = .5
    abc.new(db_path, {"result": d_observed})
    abc.do_not_stop_when_only_single_model_alive()

    minimum_epsilon = -1
    history = abc.run(minimum_epsilon, max_nr_populations=2)
    posterior_x, posterior_weight = history.get_distribution(0, None)
    posterior_x = posterior_x["u"].values
    sort_indices = sp.argsort(posterior_x)
    f_empirical = sp.interpolate.interp1d(sp.hstack((-200,
                                                     posterior_x[sort_indices],
                                                     200)),
                                          sp.hstack((0,
                                                     sp.cumsum(
                                                         posterior_weight[
                                                             sort_indices]),
                                                     1)))

    @sp.vectorize
    def f_expected(u):
        return (sp.log(u)-sp.log(d_observed)) / (- sp.log(d_observed)) * \
               (u > d_observed)

    x = sp.linspace(0.1, 1)
    max_distribution_difference = sp.absolute(f_empirical(x) -
                                              f_expected(x)).max()
    assert max_distribution_difference < 0.12
示例#27
0
def test_stop_early(db_path, max_eval_checked_sampler):
    """Test early stopping inside a generation."""
    sampler = max_eval_checked_sampler
    abc = ABCSMC(
        model,
        Distribution(par=st.uniform(0, 10)),
        dist,
        pop_size,
        sampler=sampler,
    )
    abc.new(db_path, {"par": 0.5})
    history = abc.run(min_acceptance_rate=set_acc_rate)
    df = history.get_all_populations()

    # offset with n_procs as more processes can have run at termination
    n_procs = sampler.n_procs if hasattr(sampler, 'n_procs') else 1
    df["corrected_acceptance_rate"] = df["particles"] / (df["samples"] -
                                                         (n_procs - 1))

    # if already the first generation fails, the quotient is not meaningful
    assert (max(df.t) == -1
            or df["corrected_acceptance_rate"].iloc[-1] >= set_acc_rate)
示例#28
0
文件: main.py 项目: Ajris/miss
def main1():
    measurement_data = np.array(get_from_csv()) / 40000000
    measurement_times = np.arange(len(measurement_data))
    u = 39999999 / 40000000
    w = 0.0
    h = 0
    v = 1 / 40000000
    q = 0
    r = 0
    d = 0
    init = np.array([u, w, h, v, q, r, d])

    # beta, gamma, alpha, mi, theta, theta_0, sigma, eta, kappa_1, kappa_2
    def model(pars):
        sol = sp.integrate.odeint(
            f, init, measurement_times,
            args=(
                pars["eta"],
                # pars["gamma"],
                pars["alpha"],
                # pars["mi"],
                # pars["theta"],
                # pars["theta_0"],
                # pars["sigma"],
                # pars["eta"],
                # pars["kappa_1"],
                # pars["kappa_2"]
            ))

        new_scale = sol[:, 4]
        return {"X_2": new_scale}

    # beta, gamma, alpha, mi, theta, theta_0, sigma, eta, kappa_1, kappa_2

    parameter_prior = Distribution(
        eta=RV("uniform", 0, 1),
        # gamma=RV("uniform", 0, 1),
        alpha=RV("uniform", 0, 1),
        # mi=RV("uniform", 0, 1),
        # theta=RV("uniform", 0, 1),
        # theta_0=RV("uniform", 0, 1),
        # sigma=RV("uniform", 0, 1),
        # eta=RV("uniform", 0, 1),
        # kappa_1=RV("uniform", 0, 1),
        # kappa_2=RV("uniform", 0, 1)
    )

    abc = ABCSMC(models=model,
                 parameter_priors=parameter_prior,
                 distance_function=distance,
                 population_size=5,
                 transitions=LocalTransition(k_fraction=.3),
                 eps=MedianEpsilon(500, median_multiplier=0.7),

                 )

    db_path = ("sqlite:///" +
               os.path.join("./", "test.db"))
    abc.new(db_path, {"X_2": measurement_data})
    h = abc.run(minimum_epsilon=0.1, max_nr_populations=3)
    print(*h.get_distribution(m=0, t=h.max_t))
priors = Distribution(priors)

NUM_CORES = 3
POPULATION_SIZE = 10000
DB_PATH = "full_model_abc.db"

###### ABC ######
distance = PNormDistance(p=1)
sampler = MulticoreEvalParallelSampler(n_procs=NUM_CORES)

abc = ABCSMC(run_model, priors, distance, population_size=POPULATION_SIZE)

db_path = ("sqlite:///" + DB_PATH)

abc.new(db_path, {'distance': 0})
history = abc.run(minimum_epsilon=0.001, max_nr_populations=25)

# The database is stored and can be reloaded if needed - see pyabc documentation
# Here just print the median and 95% credible intervals.
import pyabc.visualization.credible as credible


def get_estimate_and_CI_for_param(param, df, w, confidence=0.95):
    vals = np.array(df[param])
    lb, ub = credible.compute_credible_interval(vals, w, confidence)
    median = credible.compute_quantile(vals, w, 0.5)
    return {'median': median, 'CI_lower_bound': lb, 'CI_upper_bound': ub}


df, w = history.get_distribution(
)  # Get the accepted parameters from the last generation
示例#30
0
            # 'delay': DiscreteJumpTransition(domain=delay_domain),
            # 'n01': DiscreteJumpTransition(domain=n01_domain),
            # 'n02': DiscreteJumpTransition(domain=n02_domain),
            'k': DiscreteJumpTransition(domain=k_domain, p_stay=.8),
            ('n01', 'n02', 'log_p', 'p_inf'): GridSearchCV()
        })
    id = 'n=3e5_2'
    db = "sqlite:///" + os.path.join(os.getcwd(), id + ".db")

    abc = ABCSMC(
        model,
        prior,
        distance,
        transitions=transition,
        population_size=200,
    )
    # abc.load(db, int(np.load('run_id_'+id+'.npy')))#, {'cases': data})
    abc.load(db, 1)
    # history = abc.new(db,  {'cases': data})
    np.save('run_id_' + id + '.npy', abc.history.id)

    abc.run(max_nr_populations=3)

    history = abc.history
    print(history.max_t)
    df, w = history.get_distribution()
    # kde = GridSearchCV().fit(df, w)
    # print(kde.rvs()['log_p'])
    # df['p'] = 10 ** (-df['log_p'])
    plot_kde_matrix(df, w)
    plt.show()