Example #1
0
def test_acquisition():
    n_params = 2
    n = 10
    n2 = 5
    parameter_names = ['a', 'b']
    bounds = {'a': [-2, 3], 'b': [5, 6]}
    target_model = GPyRegression(parameter_names, bounds=bounds)
    x1 = np.random.uniform(*bounds['a'], n)
    x2 = np.random.uniform(*bounds['b'], n)
    x = np.column_stack((x1, x2))
    y = np.random.rand(n)
    target_model.update(x, y)

    # check acquisition without noise
    acq_noise_var = 0
    t = 1
    acquisition_method = acquisition.LCBSC(target_model, noise_var=acq_noise_var)
    new = acquisition_method.acquire(n2, t=t)
    assert np.allclose(new[1:, 0], new[0, 0])
    assert np.allclose(new[1:, 1], new[0, 1])

    # check acquisition with scalar noise
    acq_noise_var = 2
    t = 1
    acquisition_method = acquisition.LCBSC(target_model, noise_var=acq_noise_var)
    new = acquisition_method.acquire(n2, t=t)
    assert new.shape == (n2, n_params)
    assert np.all((new[:, 0] >= bounds['a'][0]) & (new[:, 0] <= bounds['a'][1]))
    assert np.all((new[:, 1] >= bounds['b'][0]) & (new[:, 1] <= bounds['b'][1]))

    # check acquisition with separate variance for dimensions
    acq_noise_var = np.random.uniform(0, 5, size=2)
    t = 1
    acquisition_method = acquisition.LCBSC(target_model, noise_var=acq_noise_var)
    new = acquisition_method.acquire(n2, t=t)
    assert new.shape == (n2, n_params)
    assert np.all((new[:, 0] >= bounds['a'][0]) & (new[:, 0] <= bounds['a'][1]))
    assert np.all((new[:, 1] >= bounds['b'][0]) & (new[:, 1] <= bounds['b'][1]))

    # check acquisition with arbitrary covariance matrix
    acq_noise_cov = np.random.rand(n_params, n_params) * 0.5
    acq_noise_cov += acq_noise_cov.T
    acq_noise_cov += n_params * np.eye(n_params)
    t = 1
    with pytest.raises(ValueError):
        acquisition.LCBSC(target_model, noise_var=acq_noise_cov)

    # test Uniform Acquisition
    t = 1
    acquisition_method = acquisition.UniformAcquisition(target_model, noise_var=acq_noise_var)
    new = acquisition_method.acquire(n2, t=t)
    assert new.shape == (n2, n_params)
    assert np.all((new[:, 0] >= bounds['a'][0]) & (new[:, 0] <= bounds['a'][1]))
    assert np.all((new[:, 1] >= bounds['b'][0]) & (new[:, 1] <= bounds['b'][1]))
Example #2
0
def _get_dependencies_acq_fn():
    """Provide the requirements for the MaxVar-based acquisition function initialisation.

    Returns
    -------
    (GPy.model.GPRegression, elfi.methods.utils.ModelPrior)
        Tuple containing a fit gp and a prior.

    """
    mean = [4, 4]
    cov_matrix = [[1, .5], [.5, 1]]
    names_param = ['mu_0', 'mu_1']
    eps_prior = 5  # The prior's range indicator used in the Gaussian noise model.
    bounds_param = {
        'mu_0': (mean[0] - eps_prior, mean[0] + eps_prior),
        'mu_1': (mean[1] - eps_prior, mean[1] + eps_prior)
    }

    # Initialising the prior.
    gm_2d = elfi.examples.gauss.get_model(true_params=mean,
                                          nd_mean=True,
                                          cov_matrix=cov_matrix)
    prior = ModelPrior(gm_2d)

    # Generating the coordinates and the values of the fitting data.
    n_pts_fit = 10
    x1 = np.random.uniform(*bounds_param['mu_0'], n_pts_fit)
    x2 = np.random.uniform(*bounds_param['mu_1'], n_pts_fit)
    x = np.column_stack((x1, x2))
    y = np.random.rand(n_pts_fit)

    # Fitting the gp with the generated points.
    gp = GPyRegression(names_param, bounds=bounds_param)
    gp.update(x, y)

    return gp, prior