def test_bounded_bq_raises(gpy_model):
    gpy_model, _ = gpy_model
    measure = LebesgueMeasure.from_bounds(gpy_model.X.shape[1] * [(0, 1)],
                                          normalized=False)
    qrbf = QuadratureRBFLebesgueMeasure(RBFGPy(gpy_model.kern),
                                        measure=measure)
    base_gp_wrong_kernel = BaseGaussianProcessGPy(kern=qrbf,
                                                  gpy_model=gpy_model)

    # wrong kernel embedding
    with pytest.raises(ValueError):
        BoundedBayesianQuadrature(
            base_gp=base_gp_wrong_kernel,
            X=base_gp_wrong_kernel.X,
            Y=base_gp_wrong_kernel.Y,
            lower_bound=np.min(base_gp_wrong_kernel.Y) - 0.5,
        )

    # both upper and lower bound are given
    with pytest.raises(ValueError):
        BoundedBayesianQuadrature(
            base_gp=base_gp_wrong_kernel,
            X=base_gp_wrong_kernel.X,
            Y=base_gp_wrong_kernel.Y,
            lower_bound=np.min(base_gp_wrong_kernel.Y) - 0.5,
            upper_bound=np.min(base_gp_wrong_kernel.Y) - 0.5,
        )

    # no bound is given
    with pytest.raises(ValueError):
        BoundedBayesianQuadrature(base_gp=base_gp_wrong_kernel,
                                  X=base_gp_wrong_kernel.X,
                                  Y=base_gp_wrong_kernel.Y)
Beispiel #2
0
def test_vanilla_bq_loop():
    init_size = 5
    x_init = np.random.rand(init_size, 2)
    y_init = np.random.rand(init_size, 1)
    bounds = [(-1, 1), (0, 1)]

    gpy_model = GPy.models.GPRegression(X=x_init,
                                        Y=y_init,
                                        kernel=GPy.kern.RBF(
                                            input_dim=x_init.shape[1],
                                            lengthscale=1.,
                                            variance=1.))
    emukit_qrbf = QuadratureRBFLebesgueMeasure(RBFGPy(gpy_model.kern),
                                               integral_bounds=bounds)
    emukit_model = BaseGaussianProcessGPy(kern=emukit_qrbf,
                                          gpy_model=gpy_model)
    emukit_method = VanillaBayesianQuadrature(base_gp=emukit_model,
                                              X=x_init,
                                              Y=y_init)
    emukit_loop = VanillaBayesianQuadratureLoop(model=emukit_method)

    num_iter = 5
    emukit_loop.run_loop(user_function=UserFunctionWrapper(func),
                         stopping_condition=num_iter)

    assert emukit_loop.loop_state.X.shape[0] == num_iter + init_size
    assert emukit_loop.loop_state.Y.shape[0] == num_iter + init_size
def create_vanilla_bq_loop_with_rbf_kernel(X: np.ndarray, Y: np.ndarray, integral_bounds: List,
                                           rbf_lengthscale: float=1.0, rbf_variance: float=1.0) -> \
        VanillaBayesianQuadratureLoop:
    """

    :param X: initial training point locations, shape (n_points, input_dim)
    :param Y:  initial training point function values, shape (n_points, 1)
    :param integral_bounds: List of input_dim tuples, where input_dim is the dimensionality of the integral
    and the tuples contain the lower and upper bounds of the integral i.e.,
    [(lb_1, ub_1), (lb_2, ub_2), ..., (lb_D, ub_D)].
    :param rbf_lengthscale: the lengthscale of the rbf kernel, defaults to 1.
    :param rbf_variance: the variance of the rbf kernel, defaults to 1.
    :return: The vanilla BQ loop
    """

    if not len(integral_bounds) == X.shape[1]:
        D_bounds = len(integral_bounds)
        input_dim = X.shape[1]
        raise ValueError("number of integral bounds " + str(D_bounds) + " provided does not match the input dimension "
                                                                        + str(input_dim) + ".")
    if rbf_lengthscale <= 0:
        raise ValueError("rbf lengthscale must be positive. The current value is " + str(rbf_lengthscale) + ".")
    if rbf_variance <= 0:
        raise ValueError("rbf variance must be positive. The current value is " + str(rbf_variance) + ".")

    gpy_model = GPy.models.GPRegression(X=X, Y=Y, kernel=GPy.kern.RBF(input_dim=X.shape[1],
                                                                      lengthscale=rbf_lengthscale,
                                                                      variance=rbf_variance))
    emukit_rbf = RBFGPy(gpy_model.kern)
    emukit_qrbf = QuadratureRBF(emukit_rbf, integral_bounds=integral_bounds)
    emukit_model = BaseGaussianProcessGPy(kern=emukit_qrbf, gpy_model=gpy_model)
    emukit_method = VanillaBayesianQuadrature(base_gp=emukit_model)
    emukit_loop = VanillaBayesianQuadratureLoop(model=emukit_method)
    return emukit_loop
class EmukitRBF:
    variance = 0.5
    lengthscales = np.array([0.8, 1.3])
    kern = RBFGPy(
        GPy.kern.RBF(input_dim=2,
                     lengthscale=lengthscales,
                     variance=variance,
                     ARD=True))
def model_lebesgue_normalized(gpy_model):
    measure = LebesgueMeasure.from_bounds(bounds=gpy_model.X.shape[1] *
                                          [(-1, 2)],
                                          normalized=True)
    qrbf = QuadratureRBFLebesgueMeasure(RBFGPy(gpy_model.kern), measure)
    basegp = BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model)
    return VanillaBayesianQuadrature(base_gp=basegp,
                                     X=gpy_model.X,
                                     Y=gpy_model.Y)
Beispiel #6
0
def base_gp_data():
    X = np.array([[-1, 1], [0, 0], [-2, 0.1]])
    Y = np.array([[1], [2], [3]])
    gpy_model = GPy.models.GPRegression(
        X=X, Y=Y, kernel=GPy.kern.RBF(input_dim=X.shape[1]))
    measure = GaussianMeasure(mean=np.array([0.1, 1.8]), variance=0.8)
    qrbf = QuadratureRBFGaussianMeasure(RBFGPy(gpy_model.kern),
                                        measure=measure)
    base_gp = BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model)
    return base_gp, X, Y
def model_gaussian(gpy_model):
    X, Y = gpy_model.X, gpy_model.Y
    measure = GaussianMeasure(mean=np.arange(gpy_model.X.shape[1]),
                              variance=np.linspace(0.2, 1.5, X.shape[1]))
    qrbf = QuadratureRBFGaussianMeasure(RBFGPy(gpy_model.kern),
                                        measure=measure)
    basegp = BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model)
    return VanillaBayesianQuadrature(base_gp=basegp,
                                     X=gpy_model.X,
                                     Y=gpy_model.Y)
def model():
    rng = np.random.RandomState(42)
    x_init = rng.rand(5, 2)
    y_init = rng.rand(5, 1)

    gpy_kernel = GPy.kern.RBF(input_dim=x_init.shape[1])
    gpy_model = GPy.models.GPRegression(X=x_init, Y=y_init, kernel=gpy_kernel)
    qrbf = QuadratureRBFLebesgueMeasure(RBFGPy(gpy_kernel), integral_bounds=x_init.shape[1] * [(-3, 3)])
    basegp = BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model)
    model = VanillaBayesianQuadrature(base_gp=basegp, X=x_init, Y=y_init)
    return model
Beispiel #9
0
def test_vanilla_bq_shapes():
    X = np.array([[-1, 1], [0, 0], [-2, 0.1]])
    Y = np.array([[1], [2], [3]])
    x = np.array([[-1, 1], [0, 0], [-2, 0.1], [-3, 4]])
    D = X.shape[1]

    # integral bounds
    lb = -3
    ub = 3

    gpy_model = GPy.models.GPRegression(X=X,
                                        Y=Y,
                                        kernel=GPy.kern.RBF(input_dim=D))
    emukit_qrbf = QuadratureRBF(RBFGPy(gpy_model.kern),
                                integral_bounds=D * [(lb, ub)])
    emukit_model = BaseGaussianProcessGPy(kern=emukit_qrbf,
                                          gpy_model=gpy_model)
    vanilla_bq = VanillaBayesianQuadrature(base_gp=emukit_model)

    # integrate
    res = vanilla_bq.integrate()
    assert len(res) == 2
    assert isinstance(res[0], float)
    assert isinstance(res[1], float)

    # transformations
    assert vanilla_bq.transform(Y).shape == Y.shape
    assert vanilla_bq.inverse_transform(Y).shape == Y.shape

    # predictions base
    res = vanilla_bq.predict_base(x)
    assert len(res) == 4
    for i in range(4):
        assert res[i].shape == (x.shape[0], 1)

    # predictions base full covariance
    res = vanilla_bq.predict_base_with_full_covariance(x)
    assert len(res) == 4
    assert res[0].shape == (x.shape[0], 1)
    assert res[1].shape == (x.shape[0], x.shape[0])
    assert res[2].shape == (x.shape[0], 1)
    assert res[3].shape == (x.shape[0], x.shape[0])

    # predictions
    res = vanilla_bq.predict(x)
    assert len(res) == 2
    assert res[0].shape == (x.shape[0], 1)
    assert res[1].shape == (x.shape[0], 1)

    # predictions full covariance
    res = vanilla_bq.predict_with_full_covariance(x)
    assert len(res) == 2
    assert res[0].shape == (x.shape[0], 1)
    assert res[1].shape == (x.shape[0], x.shape[0])
 def _wrap_emukit(self, gpy_gp: GPy.core.GP):
     """
     Wrap GPy GP around Emukit interface to enable subsequent quadrature
     :param gpy_gp:
     :return:
     """
     # gpy_gp.optimize()
     rbf = RBFGPy(gpy_gp.kern)
     qrbf = QuadratureRBF(rbf, integral_bounds=[(-4, 8)] * self.dimensions)
     model = BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_gp)
     method = VanillaBayesianQuadrature(base_gp=model)
     return method
def model_with_density():
    rng = np.random.RandomState(42)
    x_init = rng.rand(5, 2)
    y_init = rng.rand(5, 1)

    gpy_kernel = GPy.kern.RBF(input_dim=x_init.shape[1])
    gpy_model = GPy.models.GPRegression(X=x_init, Y=y_init, kernel=gpy_kernel)
    measure = IsotropicGaussianMeasure(mean=np.arange(x_init.shape[1]), variance=2.)
    qrbf = QuadratureRBFIsoGaussMeasure(RBFGPy(gpy_kernel), measure=measure)
    basegp = BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model)
    model = VanillaBayesianQuadrature(base_gp=basegp, X=x_init, Y=y_init)
    return model
def qrbf_iso_gauss_infinite():
    x1 = np.array([[-1, 1], [0, 0], [-2, 0.1]])
    x2 = np.array([[-1, 1], [0, 0], [-2, 0.1], [-3, 3]])
    M1 = x1.shape[0]
    M2 = x2.shape[0]
    D = x1.shape[1]

    gpy_kernel = GPy.kern.RBF(input_dim=D)
    emukit_rbf = RBFGPy(gpy_kernel)
    measure = IsotropicGaussianMeasure(mean=np.arange(D), variance=2.)
    emukit_qrbf = QuadratureRBFIsoGaussMeasure(rbf_kernel=emukit_rbf,
                                               measure=measure)
    return emukit_qrbf, x1, x2, M1, M2, D
Beispiel #13
0
def vanilla_bq():
    X = np.array([[-1, 1], [0, 0], [-2, 0.1]])
    Y = np.array([[1], [2], [3]])
    D = X.shape[1]
    integral_bounds = [(-1, 2), (-3, 3)]

    gpy_model = GPy.models.GPRegression(X=X,
                                        Y=Y,
                                        kernel=GPy.kern.RBF(input_dim=D))
    qrbf = QuadratureRBFLebesgueMeasure(RBFGPy(gpy_model.kern),
                                        integral_bounds=integral_bounds)
    model = BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model)
    vanilla_bq = VanillaBayesianQuadrature(base_gp=model, X=X, Y=Y)
    return vanilla_bq
def qrbf_lebesgue_finite():
    x1 = np.array([[-1, 1], [0, 0], [-2, 0.1]])
    x2 = np.array([[-1, 1], [0, 0], [-2, 0.1], [-3, 3]])
    M1 = x1.shape[0]
    M2 = x2.shape[0]
    D = x1.shape[1]

    # integral bounds
    bounds = [(-1, 2), (-3, 3)]

    gpy_kernel = GPy.kern.RBF(input_dim=D)
    emukit_rbf = RBFGPy(gpy_kernel)
    emukit_qrbf = QuadratureRBFLebesgueMeasure(emukit_rbf,
                                               integral_bounds=bounds)
    return emukit_qrbf, x1, x2, M1, M2, D
def qrbf_uniform_infinite():
    x1 = np.array([[-1, 1], [0, 0], [-2, 0.1]])
    x2 = np.array([[-1, 1], [0, 0], [-2, 0.1], [-3, 3]])
    M1 = x1.shape[0]
    M2 = x2.shape[0]
    D = x1.shape[1]

    # measure
    measure_bounds = [(0, 2), (-4, 3)]
    measure = UniformMeasure(bounds=measure_bounds)

    gpy_kernel = GPy.kern.RBF(input_dim=D)
    emukit_rbf = RBFGPy(gpy_kernel)
    emukit_qrbf = QuadratureRBFUniformMeasure(rbf_kernel=emukit_rbf,
                                              integral_bounds=None,
                                              measure=measure)
    return emukit_qrbf, x1, x2, M1, M2, D
Beispiel #16
0
def test_vanilla_bq_loop_initial_state():
    x_init = np.random.rand(5, 2)
    y_init = np.random.rand(5, 1)

    bounds = [(-1, 1), (0, 1)]

    gpy_model = GPy.models.GPRegression(X=x_init,
                                        Y=y_init,
                                        kernel=GPy.kern.RBF(
                                            input_dim=x_init.shape[1],
                                            lengthscale=1.,
                                            variance=1.))
    emukit_qrbf = QuadratureRBF(RBFGPy(gpy_model.kern), integral_bounds=bounds)
    emukit_model = BaseGaussianProcessGPy(kern=emukit_qrbf,
                                          gpy_model=gpy_model)
    emukit_method = VanillaBayesianQuadrature(base_gp=emukit_model)
    emukit_loop = VanillaBayesianQuadratureLoop(model=emukit_method)

    assert_array_equal(emukit_loop.loop_state.X, x_init)
    assert_array_equal(emukit_loop.loop_state.Y, y_init)
    assert emukit_loop.loop_state.iteration == 0
Beispiel #17
0
def loop():
    init_size = 5
    x_init = np.random.rand(init_size, 2)
    y_init = np.random.rand(init_size, 1)
    bounds = [(-1, 1), (0, 1)]

    gpy_model = GPy.models.GPRegression(X=x_init,
                                        Y=y_init,
                                        kernel=GPy.kern.RBF(
                                            input_dim=x_init.shape[1],
                                            lengthscale=1.,
                                            variance=1.))
    emukit_qrbf = QuadratureRBFLebesgueMeasure(RBFGPy(gpy_model.kern),
                                               integral_bounds=bounds)
    emukit_model = BaseGaussianProcessGPy(kern=emukit_qrbf,
                                          gpy_model=gpy_model)
    emukit_method = VanillaBayesianQuadrature(base_gp=emukit_model,
                                              X=x_init,
                                              Y=y_init)
    emukit_loop = VanillaBayesianQuadratureLoop(model=emukit_method)
    return emukit_loop, init_size, x_init, y_init
def test_rbf_qkernel_shapes():
    x1 = np.array([[-1, 1], [0, 0], [-2, 0.1]])
    x2 = np.array([[-1, 1], [0, 0], [-2, 0.1], [-3, 3]])
    M1 = x1.shape[0]
    M2 = x2.shape[0]
    D = x1.shape[1]

    # integral bounds
    lb = -3
    ub = 3

    gpy_kernel = GPy.kern.RBF(input_dim=D)
    emukit_rbf = RBFGPy(gpy_kernel)
    emukit_qrbf = QuadratureRBF(emukit_rbf, integral_bounds=D * [(lb, ub)])

    # kernel shapes
    assert emukit_qrbf.K(x1, x2).shape == (M1, M2)
    assert emukit_qrbf.qK(x2).shape == (1, M2)
    assert emukit_qrbf.Kq(x1).shape == (M1, 1)
    assert isinstance(emukit_qrbf.qKq(), float)

    # gradient shapes
    assert emukit_qrbf.dKq_dx(x1).shape == (M1, D)
    assert emukit_qrbf.dqK_dx(x2).shape == (D, M2)

if __name__ == "__main__":
    np.random.seed(0)

    METHOD = "Vanilla BQ"

    X = np.array([[-1, 1], [0, 0], [-2, 0.1]])
    Y = np.array([[1], [2], [3]])
    D = X.shape[1]
    integral_bounds = [(-1, 2), (-3, 3)]

    gpy_model = GPy.models.GPRegression(X=X,
                                        Y=Y,
                                        kernel=GPy.kern.RBF(input_dim=D))
    qrbf = QuadratureRBFLebesgueMeasure(RBFGPy(gpy_model.kern),
                                        integral_bounds=integral_bounds)
    model = BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model)

    vanilla_bq = VanillaBayesianQuadrature(base_gp=model, X=X, Y=Y)

    print()
    print("method: {}".format(METHOD))
    print("no dimensions: {}".format(D))
    print()

    # === mean =============================================================
    num_runs = 100
    num_samples = 1e6
    num_std = 3
Beispiel #20
0
def get_base_gp():
    gpy_model, dat = get_gpy_model()
    measure = GaussianMeasure(mean=dat.measure_mean, variance=dat.measure_var)
    qrbf = QuadratureRBFGaussianMeasure(RBFGPy(gpy_model.kern),
                                        measure=measure)
    return BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model), dat
Beispiel #21
0
def base_gp_wrong_kernel(gpy_model):
    integral_bounds = [(-1, 2), (-3, 3)]
    qrbf = QuadratureRBFLebesgueMeasure(RBFGPy(gpy_model.kern),
                                        integral_bounds=integral_bounds)
    return BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model)
Beispiel #22
0
def base_gp(gpy_model):
    measure = IsotropicGaussianMeasure(mean=np.array([0.1, 1.8]), variance=0.8)
    qrbf = QuadratureRBFIsoGaussMeasure(RBFGPy(gpy_model.kern),
                                        measure=measure)
    return BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model)
Beispiel #23
0
    # METHOD = 'WSABI-l adapt'
    METHOD = "WSABI-l fixed"

    # the GPy model
    X = np.array([[-1, 1], [0, 0], [-2, 0.1]])
    Y = np.array([[1], [2], [3]])
    D = X.shape[1]
    gpy_model = GPy.models.GPRegression(X=X,
                                        Y=Y,
                                        kernel=GPy.kern.RBF(input_dim=D))

    # the measure
    measure = IsotropicGaussianMeasure(mean=np.array([0.1, 1.8]), variance=0.8)

    # the emukit base GP
    qrbf = QuadratureRBFIsoGaussMeasure(RBFGPy(gpy_model.kern),
                                        measure=measure)
    base_gp = BaseGaussianProcessGPy(kern=qrbf, gpy_model=gpy_model)

    # the emukit bounded BQ model
    if METHOD == "Bounded BQ lower":
        bound = np.min(base_gp.Y) - 0.5
        model = BoundedBayesianQuadrature(base_gp=base_gp,
                                          X=X,
                                          Y=Y,
                                          bound=bound,
                                          is_lower_bounded=True)
    elif METHOD == "Bounded BQ upper":
        bound = np.max(base_gp.Y) + 0.5
        model = BoundedBayesianQuadrature(base_gp=base_gp,
                                          X=X,
if __name__ == "__main__":
    np.random.seed(0)

    # === Choose MEASURE BELOW ======
    # MEASURE_INTBOUNDS = 'Lebesgue-finite'
    # MEASURE_INTBOUNDS = 'GaussIso-infinite'
    # MEASURE_INTBOUNDS = 'Uniform-infinite'
    MEASURE_INTBOUNDS = "Uniform-finite"
    # === CHOOSE MEASURE ABOVE ======

    x1 = np.array([[-1, 1], [0, 0], [-2, 0.1]])
    x2 = np.array([[-1, 1], [0, 0], [-2, 0.1], [-3, 3]])
    D = x1.shape[1]

    gpy_kernel = GPy.kern.RBF(input_dim=D)
    emukit_rbf = RBFGPy(gpy_kernel)

    if MEASURE_INTBOUNDS == "Lebesgue-finite":
        emukit_qrbf = QuadratureRBFLebesgueMeasure(emukit_rbf,
                                                   integral_bounds=[(-1, 2),
                                                                    (-3, 3)])
    elif MEASURE_INTBOUNDS == "GaussIso-infinite":
        measure = IsotropicGaussianMeasure(mean=np.arange(D), variance=2.0)
        emukit_qrbf = QuadratureRBFIsoGaussMeasure(rbf_kernel=emukit_rbf,
                                                   measure=measure)
    elif MEASURE_INTBOUNDS == "Uniform-infinite":
        measure = UniformMeasure(bounds=[(0, 2), (-4, 3)])
        emukit_qrbf = QuadratureRBFUniformMeasure(emukit_rbf,
                                                  integral_bounds=None,
                                                  measure=measure)
    elif MEASURE_INTBOUNDS == "Uniform-finite":