Ejemplo n.º 1
0
def make_viz():
    """Produce visualization."""

    # Set plot settings
    ylim = [-5, 5]
    gpv.update_rc_params()

    # Define model params
    gp1_hypers = {'ls': 1., 'alpha': 1.5, 'sigma': 1e-1}
    gp2_hypers = {'ls': 3., 'alpha': 1., 'sigma': 1e-1}

    # Define domain
    x_min = -10.
    x_max = 10.
    domain = RealDomain({'min_max': [(x_min, x_max)]})

    # Drawn one sample path from gp1
    gp1 = SimpleGp(gp1_hypers)
    sample_path = gput.get_sample_path(gp1, domain)

    n_obs_list = [3, 5, 10, 20, 30, 60]

    # Make full data
    data_full = gput.get_data_from_sample_path(
        sample_path, gp1.params.sigma*4.0, 100
    )

    # Define set of domain points
    dom_pt_list = list(np.linspace(x_min, x_max, 200))

    for n_obs in n_obs_list:

        data = get_data_subset(data_full, n_obs)

        # Define gp2 (posterior)
        gp2 = SimpleGp(gp2_hypers)
        gp2.set_data(data)

        # Define gp3 (modified prior)
        gp3_hypers = copy.deepcopy(gp2_hypers)
        gp3_hypers['alpha'] += 0.01
        gp3 = SimpleGp(gp3_hypers)

        lb_list, ub_list = get_lb_ub_lists(dom_pt_list, gp2, gp3, data, False)

        # Various plotting
        plt.figure()

        # Plot C_t bounds
        plt.plot(dom_pt_list, lb_list, 'g--')
        plt.plot(dom_pt_list, ub_list, 'g--')

        plt.fill_between(dom_pt_list, lb_list, ub_list, color='wheat', alpha=0.5)

        # Plot GP posterior
        save_str = 'viz_' + str(n_obs)
        gpv.visualize_gp_and_sample_path(gp2, domain, data, sample_path,
                                         show_sp=False, ylim=ylim,
                                         save_str=save_str)
        plt.close()
Ejemplo n.º 2
0
def run_gplcb():
    """Run LCB algorithm with Gaussian process (GP-LCB)."""

    # Set plot settings
    ylim = [-5, 5]
    gpv.update_rc_params()

    # Define model params
    gp1_hypers = {'ls': 1., 'alpha': 1.5, 'sigma': 1e-1}
    gp2_hypers = {'ls': 3., 'alpha': 1., 'sigma': 1e-1}

    # Define domain
    x_min = -10.
    x_max = 10.
    domain = RealDomain({'min_max': [(x_min, x_max)]})

    # Drawn one sample path from gp1
    gp1 = SimpleGp(gp1_hypers)
    sample_path_nonoise = gput.get_sample_path(gp1, domain)

    # Convert to noisy sample_path observations
    sample_path = gput.get_noisy_sample_path(sample_path_nonoise,
                                             gp1.params.sigma)

    # Setup BO: initialize data
    data = Namespace(X=[], y=np.array([]))
    init_x = np.random.choice(sample_path.x, 5)[3]
    data = query_function_update_data(init_x, data, sample_path)

    # Define set of domain points
    dom_pt_list = list(sample_path.x)  # NOTE: confirm if correct

    n_iter = 30

    print('Finished iter: ', end='')
    for i in range(n_iter):

        # Define gp2 (posterior)
        gp2 = SimpleGp(gp2_hypers)
        gp2.set_data(data)

        # Compute lb_list
        test_pts = [np.array([dom_pt]) for dom_pt in dom_pt_list]
        mean_list, std_list = gp2.get_gp_post_mu_cov(test_pts, full_cov=False)
        conf_mean = np.array(mean_list)
        std_mult = 3.
        lb_list = conf_mean - std_mult * np.array(
            std_list)  # technically is np ndarray
        ub_list = conf_mean + std_mult * np.array(
            std_list)  # technically is np ndarray

        # Compute next_query_point
        min_idx = np.nanargmin(lb_list)
        next_query_point = dom_pt_list[min_idx]

        # Various plotting
        plt.figure()

        # Plot C_t bounds
        plt.plot(dom_pt_list, mean_list, 'k--', lw=2)
        plt.plot(dom_pt_list, lb_list, 'k--')
        plt.plot(dom_pt_list, ub_list, 'k--')

        plt.fill_between(dom_pt_list,
                         lb_list,
                         ub_list,
                         color='lightsteelblue',
                         alpha=0.5)

        # Plot GP posterior
        save_str = 'viz_' + str(i)
        gpv.visualize_sample_path_and_data(sample_path_nonoise,
                                           data,
                                           ylim=ylim,
                                           save_str=save_str)
        plt.close()

        # update data
        data = query_function_update_data(next_query_point, data, sample_path)
        print('{}, '.format(i), end='')

    print('Data:')
    print(data)
def run_cslcb():
    """Run LCB algorithm with confidence sequence (CS-LCB)."""

    # Set plot settings
    ylim = [-5, 5]
    gpv.update_rc_params()

    # Define model params
    gp1_hypers = {'ls': 1., 'alpha': 1.5, 'sigma': 1e-1}
    gp2_hypers = {'ls': 1., 'alpha': 1.5, 'sigma': 1e-1}

    # Define domain
    x_min = -10.
    x_max = 10.
    domain = RealDomain({'min_max': [(x_min, x_max)]})

    # Drawn one sample path from gp1
    gp1 = SimpleGp(gp1_hypers)
    sample_path_nonoise = gput.get_sample_path(gp1, domain)

    # Convert to noisy sample_path observations
    sample_path = gput.get_noisy_sample_path(sample_path_nonoise,
                                             gp1.params.sigma)

    # Setup BO: initialize data
    data = Namespace(X=[], y=np.array([]))
    init_x = np.random.choice(sample_path.x, 5)[2]
    data = query_function_update_data(init_x, data, sample_path)

    # Define set of domain points
    dom_pt_list = list(sample_path.x)  # NOTE: confirm if correct

    n_iter = 30

    print('Finished iter: ', end='')
    for i in range(n_iter):

        # Define gp2 (posterior)
        gp2 = SimpleGp(gp2_hypers)
        gp2.set_data(data)

        # Define gp3 (modified prior)
        gp3_hypers = copy.deepcopy(gp2_hypers)
        gp3_hypers['alpha'] += 0.01
        gp3 = SimpleGp(gp3_hypers)

        lb_list, ub_list = get_lb_ub_lists(dom_pt_list, gp2, gp3, data, False)
        min_idx = np.nanargmin(lb_list)
        next_query_point = dom_pt_list[min_idx]

        # Various plotting
        plt.figure()

        # Plot C_t bounds
        plt.plot(dom_pt_list, lb_list, 'g--')
        plt.plot(dom_pt_list, ub_list, 'g--')

        plt.fill_between(dom_pt_list,
                         lb_list,
                         ub_list,
                         color='wheat',
                         alpha=0.5)

        # Plot GP posterior
        save_str = 'viz_' + str(i)
        gpv.visualize_sample_path_and_data(sample_path_nonoise,
                                           data,
                                           ylim=ylim,
                                           save_str=save_str)
        plt.close()

        # update data
        data = query_function_update_data(next_query_point, data, sample_path)
        print('{}, '.format(i), end='')

    print('Data:')
    print(data)