code: def _sample_max_value_Thompson(
      model: Model,
      candidate_set: Tensor,
      num_samples: int,
      posterior_transform: Optional[PosteriorTransform] = None,
      maximize: bool = True,
  ) -> Tensor:
      """Samples the max values by discrete Thompson sampling.
  
      Should generally be called within a `with torch.no_grad()` context.
  
      Args:
          model: A fitted single-outcome model.
          candidate_set: A `n x d` Tensor including `n` candidate points to
              discretize the design space.
          num_samples: Number of max value samples.
          posterior_transform: A PosteriorTransform. If using a multi-output model,
              a PosteriorTransform that transforms the multi-output posterior into a
              single-output posterior is required.
          maximize: If True, consider the problem a maximization problem.
  
      Returns:
          A `num_samples x num_fantasies` Tensor of posterior max value samples.
      """
      posterior = model.posterior(
          candidate_set, posterior_transform=posterior_transform
      )
      weight = 1.0 if maximize else -1.0
      samples = weight * posterior.rsample(
          torch.Size([num_samples])
      ).squeeze(-1)
      # samples is num_samples x (num_fantasies) x n
      max_values, _ = samples.max(dim=-1)
      if len(samples.shape) == 2:
          max_values = max_values.unsqueeze(
              -1
          )  # num_samples x num_fantasies
  
      return max_values
  
imports: from __future__ import annotations
  from abc import ABC, abstractmethod
  from copy import deepcopy
  from math import log
  from typing import Any, Callable, Optional
  import numpy
  import torch
  from botorch.acquisition.acquisition import AcquisitionFunction, MCSamplerMixin
  from botorch.acquisition.cost_aware import CostAwareUtility, InverseCostWeightedUtility
  from botorch.acquisition.objective import PosteriorTransform
  from botorch.exceptions.errors import UnsupportedError
  from botorch.models.cost import AffineFidelityCostModel
  from botorch.models.model import Model
  from botorch.models.utils import check_no_nans
  from botorch.sampling.normal import SobolQMCNormalSampler
  from botorch.utils.transforms import match_batch_shape, t_batch_mode_transform
  from linear_operator.functions import inv_quad
  from linear_operator.utils.cholesky import psd_safe_cholesky
  from scipy.optimize import brentq
  from scipy.stats import norm
  from torch import Tensor
label_desc: Use image node_id 1 for calling a global function with example usage: _sample_max_value_Thompson(model, candidate_set, num_samples, posterior_transform, maximize) and returns: max_values