def circular_gaussian( mean: np.float = 0., variance: np.float = 1.) -> Tuple[UserFunctionWrapper, Callable]: """ 2D toy integrand that is a Gaussian on a circle """ return UserFunctionWrapper( _circular_gaussian), lambda x: _circular_gaussian(x, mean, variance)
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 test_vanilla_bq_loop(loop): emukit_loop, init_size, _, _ = loop 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 hennig1D() -> Tuple[UserFunctionWrapper, Callable]: """ 1D toy integrand coined by Philipp Hennig Evaluates the function at x. .. math:: e^{-x^2 -\sin^2(3x)} """ return UserFunctionWrapper(_hennig1D), _hennig1D
def hennig2D() -> Tuple[UserFunctionWrapper, List[Tuple[float, float]]]: r"""2D toy integrand coined by Philipp Hennig. .. math:: f(x) = e^{-x'Sx -\sin(3\|x\|^2)} :return: The wrapped test function, and the integrals bounds (the latter default to [-3, 3]^2). """ integral_bounds = 2 * [(-3.0, 3.0)] return UserFunctionWrapper(_hennig2D), integral_bounds
def hennig1D() -> Tuple[UserFunctionWrapper, List[Tuple[float, float]]]: """ 1D toy integrand coined by Philipp Hennig .. math:: e^{-x^2 -\sin^2(3x)} :return: the wrapped test function, and the integrals bounds (defaults to interval [-3, 3]). """ integral_bounds = [(-3., 3.)] return UserFunctionWrapper(_hennig1D), integral_bounds
def sombrero2D(freq: float = 1.0) -> Tuple[UserFunctionWrapper, List[Tuple[float, float]]]: """ 2D Sombrero function .. math:: \frac{\sin(\pi r freq)}{\pi r freq} :param freq: frequency of the sombrero (must be > 0, defaults to 1) :return: the wrapped test function, and the integrals bounds (defaults to area [-3, 3]^2). """ func = lambda x: _sombrero2D(x, freq) integral_bounds = 2 * [(-3.0, 3.0)] return UserFunctionWrapper(func), integral_bounds
def sombrero2D( freq: float = 1.0 ) -> Tuple[UserFunctionWrapper, List[Tuple[float, float]]]: r"""2D Sombrero function. .. math:: f(x) = \frac{\operatorname{sin}(\pi r \omega)}{\pi r \omega} where :math:`\omega` is the :attr:`freq` parameter and :math:`r=\|x\|` is the length of the input vector :math:`x`. :param freq: The frequency of the sombrero (must be > 0, defaults to 1). :return: The wrapped test function, and the integrals bounds (the latter defaults to [-3, 3]^2). """ func = lambda x: _sombrero2D(x, freq) integral_bounds = 2 * [(-3.0, 3.0)] return UserFunctionWrapper(func), integral_bounds
def circular_gaussian( mean: np.float = 0.0, variance: np.float = 1.0 ) -> Tuple[UserFunctionWrapper, List[Tuple[float, float]]]: """ 2D toy integrand that is a Gaussian on a circle .. math:: (2\pi \sigma^2)^{-0.5} r^2 e^{-\frac{(r - \mu)^2}{2 \sigma^2}} :param mean: the mean of the circular Gaussian in units of radius (must be >= 0, defaults to 0) :param variance: variance of the Gaussian (must be > 0, defaults to 1.) :return: the wrapped test function, and the integrals bounds (defaults to area [-3, 3]^2). """ func = lambda x: _circular_gaussian(x, mean, variance) integral_bounds = 2 * [(-3.0, 3.0)] return UserFunctionWrapper(func), integral_bounds
def circular_gaussian( mean: float = 0.0, variance: float = 1.0 ) -> Tuple[UserFunctionWrapper, List[Tuple[float, float]]]: r"""2D toy integrand that is a Gaussian on a circle. .. math:: f(x) = (2\pi \sigma^2)^{-\frac{1}{2}} r^2 e^{-\frac{(r - \mu)^2}{2 \sigma^2}} where :math:`\sigma^2` is the :attr:`variance` attribute, :math:`\mu` is the :attr:`mean` attribute and :math:`r = \|x\|` is the length of the input :math:`x`. :param mean: The mean of the circular Gaussian in units of radius (must be >= 0, defaults to 0). :param variance: The variance of the Gaussian (must be > 0, defaults to 1.). :return: The wrapped test function, and the integrals bounds (the latter defaults to [-3, 3]^2). """ func = lambda x: _circular_gaussian(x, mean, variance) integral_bounds = 2 * [(-3.0, 3.0)] return UserFunctionWrapper(func), integral_bounds
def test_cost_sensitive_bayesian_optimization_loop(): space = ParameterSpace([ContinuousParameter('x', 0, 1)]) x_init = np.random.rand(10, 1) def function_with_cost(x): return np.sin(x), x user_fcn = UserFunctionWrapper(function_with_cost) y_init, cost_init = function_with_cost(x_init) gpy_model_objective = GPy.models.GPRegression(x_init, y_init) gpy_model_cost = GPy.models.GPRegression(x_init, cost_init) model_objective = GPyModelWrapper(gpy_model_objective) model_cost = GPyModelWrapper(gpy_model_cost) loop = CostSensitiveBayesianOptimizationLoop(space, model_objective, model_cost) loop.run_loop(user_fcn, 10) assert loop.loop_state.X.shape[0] == 20 assert loop.loop_state.cost.shape[0] == 20
def rp_emukit(): # Wrap around Emukit interface from emukit.core.loop.user_function import UserFunctionWrapper return UserFunctionWrapper(_rp_emukit), _rp_emukit
def press_fem( self) -> Tuple[UserFunctionWrapper, List[Tuple[float, float]]]: integral_bound = [(1, 15), (0.2, 0.4)] return UserFunctionWrapper(self._press_fem), integral_bound
def circular_gaussian() -> Tuple[UserFunctionWrapper, Callable]: """ 2D toy integrand that is a Gaussian on a circle """ return UserFunctionWrapper(_circular_gaussian), _circular_gaussian