code: def fit_max_value_distribution(
      predictive_distribution,
      observations,
      num_grid_points=100,
      seed=None,
  ):
      """Computes a Gumbel approximation to the max-value distribution p(y* | D)."""
      # This is based on the Gumbel approximation in [1].
  
      # First approximate the CDF of the maximum F(y* < z | D) as prod_k F_k(z),
      # where F_k is the marginal (Normal) CDF at various points.
  
      # Adjoin a grid of points so the approximation is more accurate.
      grid_points = sample_halton_sequence_lib.sample_halton_sequence(
          dim=predictive_distribution.index_points.shape[-1],
          num_results=num_grid_points,
          dtype=predictive_distribution.index_points.dtype,
          seed=seed,
      )
  
      dtype = dtype_util.as_numpy_dtype(
          predictive_distribution.index_points.dtype
      )
      norm = normal_lib.Normal(dtype(0.0), 1.0)
  
      broadcast_grid_shape = tf.concat(
          [
              ps.shape(predictive_distribution.index_points)[:-2],
              ps.shape(grid_points),
          ],
          axis=0,
      )
      grid_points = tf.broadcast_to(grid_points, broadcast_grid_shape)
      full_points = tf.concat(
          [grid_points, predictive_distribution.index_points], axis=-2
      )
  
      # Compute the stddev and mean at each point.
      stddev = predictive_distribution.stddev(index_points=full_points)
      mean = predictive_distribution.mean(index_points=full_points)
  
      # The values below will be used to initialize the root search in somewhere
      # in the center of the region.
  
      # We'll make the very greedy approximation that the max of the function is
      # more than the observations we have seen so far.
      min_value = tf.math.reduce_max(observations, axis=-1)
  
      # We'll assume that the maximum is no more than 5 standard deviations away
      # from any point.
      max_value = tf.math.reduce_max(mean + 5 * stddev, axis=-1)
  
      initial_position = tf.stack([0.5 * (min_value + max_value)] * 3)
  
      percentiles = np.array([0.25, 0.5, 0.75]).astype(dtype)
      percentiles_expanded = tf.reshape(
          percentiles,
          tf.concat(
              [[3], tf.ones_like(tf.shape(initial_position)[1:])],
              axis=0,
          ),
      )
  
      # Finally compute the approximation to the log CDF. We want to find the
      # quantile values, so we will use a root search algorithm to do this.
      def log_cdf(x):
          return tf.math.log(percentiles_expanded) - tf.reduce_sum(
              norm.log_cdf((x[..., tf.newaxis] - mean) / stddev),
              axis=-1,
          )
  
      quantiles = root_search.find_root_secant(
          log_cdf, initial_position=initial_position
      ).estimated_root
  
      y1, y2, y3 = tf.unstack(quantiles, num=3)
      p1, p2, p3 = percentiles
  
      # Estimate the univariate Gumbel CDF through quantile matching.
      scale = (y3 - y1) / (np.log(-np.log(p1)) - np.log(-np.log(p3)))
      loc = y2 + scale * np.log(-np.log(p2))
      return gumbel.Gumbel(loc=loc, scale=scale)
  
file_path: probability/tensorflow_probability/python/experimental/bayesopt/acquisition/max_value_entropy_search.py
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import gumbel
  from tensorflow_probability.python.distributions import normal
  from tensorflow_probability.python.experimental.bayesopt.acquisition import acquisition_function
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.math import root_search
  from tensorflow_probability.python.math import special
  from tensorflow_probability.python.mcmc import sample_halton_sequence_lib
label_desc: Use image node_id 1 for calling a global function with example usage: fit_max_value_distribution(predictive_distribution, observations, num_grid_points, seed) and returns: gumbel, unknown