Exemplo n.º 1
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
Exemplo n.º 2
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()
Exemplo n.º 3
0
            'k':
            DiscreteJumpTransition(domain=k_domain, p_stay=0.7),
            ('n01', 'n02', 'log_p', 'p_inf'):
            MultivariateNormalTransition(scaling=0.8)
        })

    db = "sqlite:///" + os.path.join(os.getcwd(), "n=3e5_new.db")
    with Pool(processes=5) as pool:
        abc = ABCSMC(
            model,
            prior,
            distance,
            transitions=transition,
            sampler=MappingSampler(pool.map),  # SingleCoreSampler(),
            # population_size=AdaptivePopulationSize(100, max_population_size=500),
            # population_size=25
        )
        # abc.load(db, np.load('run_id.npy'))
        abc.load(db, 10)
        # history = abc.new(db, {'cases': data})
        abc.run(max_nr_populations=10)
        pool.close()
        pool.join()

    history = abc.history
    np.save('run_id_n=3e5_new.npy', history.id)
    # print(history.max_t)
    df, w = history.get_distribution()
    plot_kde_matrix(df, w)
    plt.show()
Exemplo n.º 4
0
    kmin, kmax = 1, 11
    k_domain = np.arange(kmin, kmax + 1)
    # print(len(data_ext[delaymax:tmax+delaymax]), tmax)
    prior = Distribution(k=RV('randint', kmin, kmax),
                         log_p=RV('uniform', 0, 7),
                         p_inf=RV('uniform', 0.01, 0.03))
    model = MyStochasticProcess(n, t1, data1)
    transition = AggregatedTransition(
        mapping={
            'k': DiscreteJumpTransition(domain=k_domain),
            ('log_p', 'p_inf'): GridSearchCV()
        })
    id_old = 'n=3e5_new'
    db_old = "sqlite:///" + os.path.join(os.getcwd(), id_old + ".db")
    abc_old = ABCSMC(model, prior, distance)
    abc_old.load(db_old, int(np.load('run_id_' + id_old + '.npy')))
    model = ContinuedSpread(n, t1, t2, data2, abc_old.history)
    id = 'n=3e5_late'
    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')))
    # print(int(np.load('run_id_'+id+'.npy')))
    # abc.load(db, 3)
    history = abc.new(db, {'cases': data2, 'firstcases': data1})
    # history = abc.history
    np.save('run_id_' + id + '.npy', history.id)
Exemplo n.º 5
0
    data = np.load('cases_de_feb26_mar16.npy') #* n / 1e6
    # print(data[0])
    # assert data < 3
    tmax = len(data)
    prior = Distribution(n01=RV('uniform', 0, 3 * round(data[0])),
                         n02=RV('uniform', 0, 10 * round(data[0])),
                         k=RV('randint', k_domain.min(), k_domain.max()+1),
                         log_p=RV('uniform', 0, 6),
                         p_inf=RV('uniform', 0.01, 0.07))
    model = MyStochasticProcess(n, tmax, data)
    transition = AggregatedTransition(mapping={
        # 'n01': DiscreteJumpTransition(domain=np.arange(int(data.max()))),
        # 'n02': DiscreteJumpTransition(domain=np.arange(10 * int(data.max()))),
        'k': DiscreteJumpTransition(domain=k_domain, p_stay=0.7),
        ('n01', 'n02', 'log_p', 'p_inf'): GridSearchCV()
    })

    db = "sqlite:///" + os.path.join(os.getcwd(), "early_growth.db")

    abc = ABCSMC(model, prior, distance, transitions=transition)
    abc.load(db, int(np.load('run_id_early_growth.npy')), {'cases': data})
    # history = abc.new(db, {'cases': data})
    abc.run(max_nr_populations=1)


    history = abc.history
    np.save('run_id_early_growth.npy', history.id)
    # print(history.max_t)
    df, w = history.get_distribution()
    plot_kde_matrix(df, w)
    plt.show()