code: def bayesian_optimize(
      func, domain_min, domain_max, num_iters=20, callback=None
  ):
      D = len(domain_min)
  
      num_params, predict, log_marginal_likelihood = make_gp_funs(
          rbf_covariance, num_cov_params=D + 1
      )
  
      model_params = init_covariance_params(num_params)
  
      def optimize_gp_params(init_params, X, y):
          log_hyperprior = lambda params: np.sum(
              norm.logpdf(params, 0.0, 100.0)
          )
          objective = lambda params: -log_marginal_likelihood(
              params, X, y
          ) - log_hyperprior(params)
          return minimize(
              value_and_grad(objective),
              init_params,
              jac=True,
              method="CG",
          ).x
  
      def choose_next_point(
          domain_min,
          domain_max,
          acquisition_function,
          num_tries=15,
          rs=npr.RandomState(0),
      ):
          """Uses gradient-based optimization to find next query point."""
          init_points = (
              rs.rand(num_tries, D) * (domain_max - domain_min)
              + domain_min
          )
  
          grad_obj = value_and_grad(lambda x: -acquisition_function(x))
  
          def optimize_point(init_point):
              print(".", end="")
              result = minimize(
                  grad_obj,
                  x0=init_point,
                  jac=True,
                  method="L-BFGS-B",
                  options={"maxiter": 10},
                  bounds=list(zip(domain_min, domain_max)),
              )
              return result.x, acquisition_function(result.x)
  
          optimzed_points, optimized_values = list(
              zip(*list(map(optimize_point, init_points)))
          )
          print()
          best_ix = np.argmax(optimized_values)
          return np.atleast_2d(optimzed_points[best_ix])
  
      # Start by evaluating once in the middle of the domain.
      X = np.zeros((0, D))
      y = np.zeros((0))
      X = np.concatenate(
          (X, np.reshape((domain_max - domain_min) / 2.0, (D, 1)))
      )
      y = np.concatenate((y, np.reshape(np.array(func(X)), (1,))))
  
      for i in range(num_iters):
          if i > 1:
              print("Optimizing model parameters...")
              model_params = optimize_gp_params(model_params, X, y)
  
          print("Choosing where to look next", end="")
  
          def predict_func(xstar):
              mean, cov = predict(model_params, X, y, xstar)
              return mean, np.sqrt(np.diag(cov))
  
          def acquisition_function(xstar):
              xstar = np.atleast_2d(
                  xstar
              )  # To work around a bug in scipy.minimize
              mean, std = predict_func(xstar)
              return expected_new_max(mean, std, defaultmax(y))
  
          next_point = choose_next_point(
              domain_min, domain_max, acquisition_function
          )
  
          print("Evaluating expensive function...")
          new_value = func(next_point)
  
          X = np.concatenate((X, next_point))
          y = np.concatenate((y, np.reshape(np.array(new_value), (1,))))
  
          if callback:
              callback(
                  X,
                  y,
                  predict_func,
                  acquisition_function,
                  next_point,
                  new_value,
              )
  
      best_ix = np.argmax(y)
      return X[best_ix, :], y[best_ix]
  
file_path: autograd/examples/bayesian_optimization.py
imports: from __future__ import absolute_import
  from __future__ import print_function
  import matplotlib.pyplot
  import autograd.numpy
  import autograd.numpy.random
  from autograd import value_and_grad
  from scipy.optimize import minimize
  from gaussian_process import make_gp_funs, rbf_covariance
  from autograd.scipy.stats import norm
label_desc: Use image node_id 5 for calling a global function with example usage: bayesian_optimize(func, domain_min, domain_max, num_iters, callback) and returns: X,  y, minimize, np, result,  acquisition_function, mean,  np, expected_new_max