Ejemplo n.º 1
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])
Ejemplo n.º 2
0
def test_vanilla_bq_model():
    X_train = np.random.rand(5, 2)
    Y_train = np.random.rand(5, 1)

    mock_cparam = mock.create_autospec(ContinuousParameter)
    mock_bounds = mock.create_autospec(BoxBounds)
    mock_bounds.convert_to_list_of_continuous_parameters.return_value = 2 * [
        mock_cparam
    ]
    mock_kern = mock.create_autospec(QuadratureKernel,
                                     integral_bounds=mock_bounds)
    mock_gp = mock.create_autospec(IBaseGaussianProcess,
                                   kern=mock_kern,
                                   X=X_train,
                                   Y=Y_train)
    method = VanillaBayesianQuadrature(base_gp=mock_gp, X=X_train, Y=Y_train)

    assert_array_equal(method.X, X_train)
    assert_array_equal(method.Y, Y_train)
    assert_array_equal(method.integral_bounds.bounds, mock_bounds.bounds)
    # we assert that the integral bounds are the same in the method and the quadrature kernel, since all integration
    # happens in the kernel. The test is restrictive but easier to do for the init than behavioral test. There are some
    # behavioral tests for setting new bounds in the vanilla_bq method and the bounds itself. Setting the integral
    # bounds should be done with care.
    assert method.integral_bounds == mock_bounds
    assert method.integral_parameters == 2 * [mock_cparam]
Ejemplo n.º 3
0
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
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def create_vanilla_bq_loop_with_rbf_kernel(
    X: np.ndarray,
    Y: np.ndarray,
    integral_bounds: Optional[BoundsType] = None,
    measure: Optional[IntegrationMeasure] = None,
    rbf_lengthscale: float = 1.0,
    rbf_variance: float = 1.0,
) -> VanillaBayesianQuadratureLoop:
    """Creates a quadrature loop with a standard (vanilla) model.

    :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 d tuples, where d 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)].
                            Only used if ``measure`` is not given in which case the unnormalized Lebesgue measure is used.
    :param measure: An integration measure. Either ``measure`` or ``integral_bounds`` must be given.
                    If both ``integral_bounds`` and ``measure`` are given, ``integral_bounds`` is disregarded.
    :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 measure is not None and measure.input_dim != X.shape[1]:
        raise ValueError(
            f"Dimensionality of measure ({measure.input_dim}) does not match the dimensionality of "
            f"the data ({X.shape[1]}).")

    if (integral_bounds is not None) and (len(integral_bounds) != X.shape[1]):
        raise ValueError(
            f"Dimension of integral bounds ({len(integral_bounds)}) does not match the input dimension "
            f"of X ({X.shape[1]}).")

    if rbf_lengthscale <= 0:
        raise ValueError(
            f"rbf lengthscale must be positive. The current value is {rbf_lengthscale}."
        )
    if rbf_variance <= 0:
        raise ValueError(
            f"rbf variance must be positive. The current value is {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))

    # This function handles the omittion if the integral bounds in case measure is also given.
    emukit_model = create_emukit_model_from_gpy_model(
        gpy_model=gpy_model, integral_bounds=integral_bounds, measure=measure)
    emukit_method = VanillaBayesianQuadrature(base_gp=emukit_model, X=X, Y=Y)
    emukit_loop = VanillaBayesianQuadratureLoop(model=emukit_method)
    return emukit_loop
Ejemplo n.º 6
0
def test_vanilla_bq_transformations():
    Y = np.random.rand(5, 1)

    mock_gp = mock.create_autospec(IBaseGaussianProcess)
    method = VanillaBayesianQuadrature(base_gp=mock_gp)

    # we can use equal comparison here because vanilla bq uses the identity transform. For non-trivial transforms
    # with numerical errors use a closeness test instead.
    assert_array_equal(method.inverse_transform(Y), Y)
    assert_array_equal(method.transform(Y), Y)
    assert_array_equal(method.inverse_transform(method.transform(Y)), Y)
    assert_array_equal(method.transform(method.inverse_transform(Y)), Y)
Ejemplo n.º 7
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])
Ejemplo n.º 8
0
def test_vanilla_bq_transformations():
    Y = np.random.rand(5, 1)

    mock_gp = mock.create_autospec(IBaseGaussianProcess)
    method = VanillaBayesianQuadrature(base_gp=mock_gp)

    # we can use equal comparison here because vanilla bq uses the identity transform. For non-trivial transforms
    # with numerical errors use a closeness test instead.
    assert_array_equal(method.inverse_transform(Y), Y)
    assert_array_equal(method.transform(Y), Y)
    assert_array_equal(method.inverse_transform(method.transform(Y)), Y)
    assert_array_equal(method.transform(method.inverse_transform(Y)), Y)
Ejemplo n.º 9
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
Ejemplo n.º 10
0
def create_vanilla_bq_loop_with_rbf_kernel(
    X: np.ndarray,
    Y: np.ndarray,
    integral_bounds: Optional[List[Tuple[float, float]]],
    measure: Optional[IntegrationMeasure],
    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)]. None means infinite bounds.
    :param measure: the integration measure. None means the standard Lebesgue measure is used.
    :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 (integral_bounds is not None) and (len(integral_bounds) != X.shape[1]):
        D_bounds = len(integral_bounds)
        input_dim = X.shape[1]
        raise ValueError(
            "dimension of integral bounds provided ({}) does not match the input dimension of X ({}).".format(
                D_bounds, input_dim
            )
        )
    if rbf_lengthscale <= 0:
        raise ValueError("rbf lengthscale must be positive. The current value is {}.".format(rbf_lengthscale))
    if rbf_variance <= 0:
        raise ValueError("rbf variance must be positive. The current value is {}.".format(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_model = create_emukit_model_from_gpy_model(
        gpy_model=gpy_model, integral_bounds=integral_bounds, measure=measure
    )
    emukit_method = VanillaBayesianQuadrature(base_gp=emukit_model, X=X, Y=Y)
    emukit_loop = VanillaBayesianQuadratureLoop(model=emukit_method)
    return emukit_loop
Ejemplo n.º 11
0
def test_vanilla_bq_model():
    X_train = np.random.rand(5, 2)
    Y_train = np.random.rand(5, 1)

    mock_cparam = mock.create_autospec(ContinuousParameter)
    mock_bounds = mock.create_autospec(BoxBounds)
    mock_bounds.convert_to_list_of_continuous_parameters.return_value = 2 * [
        mock_cparam
    ]
    mock_kern = mock.create_autospec(QuadratureKernel,
                                     integral_bounds=mock_bounds)
    mock_gp = mock.create_autospec(IBaseGaussianProcess,
                                   kern=mock_kern,
                                   X=X_train,
                                   Y=Y_train)
    method = VanillaBayesianQuadrature(base_gp=mock_gp, X=X_train, Y=Y_train)

    assert_array_equal(method.X, X_train)
    assert_array_equal(method.Y, Y_train)
    assert_array_equal(method.integral_bounds.bounds, mock_bounds.bounds)
Ejemplo n.º 12
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
Ejemplo n.º 13
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
Ejemplo n.º 14
0
def test_vanilla_bq_model():
    X_train = np.random.rand(5, 2)
    Y_train = np.random.rand(5, 1)

    mock_cparam = mock.create_autospec(ContinuousParameter)
    mock_bounds = mock.create_autospec(IntegralBounds)
    mock_bounds.convert_to_list_of_continuous_parameters.return_value = 2 * [
        mock_cparam
    ]
    mock_kern = mock.create_autospec(QuadratureKernel,
                                     integral_bounds=mock_bounds)
    mock_gp = mock.create_autospec(IBaseGaussianProcess,
                                   kern=mock_kern,
                                   X=X_train,
                                   Y=Y_train)
    method = VanillaBayesianQuadrature(base_gp=mock_gp)

    assert_array_equal(method.X, X_train)
    assert_array_equal(method.Y, Y_train)
    # we assert this to make sure that integral bounds in the kernel match, since that is where the integration happens.
    # the test is restrictive but easier to do than behavioral test when integral bounds are changed.
    assert method.integral_bounds == mock_bounds
    assert method.integral_parameters == 2 * [mock_cparam]
Ejemplo n.º 15
0
def get_vanilla_bq_model():
    base_gp, dat = get_base_gp()
    return VanillaBayesianQuadrature(base_gp=base_gp, X=dat.X, Y=dat.Y)