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
def test_create_emukit_model_from_gpy_model_raises_warns(): input_dim = 2 gpy_kernel = GPy.kern.RBF(input_dim=input_dim) gpy_model = GPy.models.GPRegression(kernel=gpy_kernel, X=np.ones([3, input_dim]), Y=np.ones([3, 1])) bounds = input_dim * [(0, 1)] measure = LebesgueMeasure.from_bounds(bounds=bounds) # Neither measure nor bounds given with pytest.raises(ValueError): create_emukit_model_from_gpy_model(gpy_model=gpy_model) # both measure and bounds are given. Bounds will be ignored. with pytest.warns(UserWarning): create_emukit_model_from_gpy_model(gpy_model=gpy_model, integral_bounds=bounds, measure=measure)
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
def test_create_emukit_model_from_gpy_model_types(wrapper): gpy_model = GPy.models.GPRegression(kernel=wrapper["gpy_kernel"], X=wrapper["data"][0], Y=wrapper["data"][1]) emukit_gp = create_emukit_model_from_gpy_model(gpy_model=gpy_model, measure=wrapper["measure"]) assert isinstance(emukit_gp.kern, wrapper["emukit_qkernel_type"]) assert isinstance(emukit_gp.kern.kern, wrapper["gpy_kernel_wrapper_type"]) # product kernel if wrapper["is_prod"]: assert isinstance(wrapper["gpy_kernel"], GPy.kern.Prod) for k in wrapper["gpy_kernel"].parameters: assert isinstance(k, wrapper["gpy_kernel_type"]) assert k.input_dim == 1 else: assert isinstance(emukit_gp.gpy_model.kern, wrapper["gpy_kernel_type"])
def vanilla_bq_model(gpy_model, continuous_space, n_dims): integral_bounds = continuous_space.get_bounds() model = create_emukit_model_from_gpy_model(gpy_model.model, integral_bounds, None) return VanillaBayesianQuadrature(model, model.X, model.Y)