docstr: Compute the p-value resulting from a permutation test using the maximum mean discrepancy
  as a distance measure between the reference data and the data to be tested.
  
  Parameters
  ----------
  x
      Batch of instances.
  
  Returns
  -------
  p-value obtained from the permutation test, the MMD^2 between the reference and test set,         and the MMD^2 threshold above which drift is flagged.
docstr_tok: ["Compute","the","p-value","resulting","from","a","permutation","test","using","the","maximum","mean","discrepancy","as","a","distance","measure","between","the","reference","data","and","the","data","to","be","tested",".","Parameters","--","--","--","--","--","x","Batch","of","instances",".","Returns","--","--","--","-","p-value","obtained","from","the","permutation","test",",","the","MMD^2","between","the","reference","and","test","set",",","and","the","MMD^2","threshold","above","which","drift","is","flagged","."]
code: def score(
      self, x: Union[np.ndarray, list]
  ) -> Tuple[float, float, float]:
      """
          Compute the p-value resulting from a permutation test using the maximum mean discrepancy
          as a distance measure between the reference data and the data to be tested.
  
          Parameters
          ----------
          x
              Batch of instances.
  
          Returns
          -------
          p-value obtained from the permutation test, the MMD^2 between the reference and test set, \
          and the MMD^2 threshold above which drift is flagged.
          """
      x_ref, x = self.preprocess(x)
      x_ref = torch.from_numpy(x_ref).to(self.device)  # type: ignore[assignment]
      x = torch.from_numpy(x).to(self.device)  # type: ignore[assignment]
      # compute kernel matrix, MMD^2 and apply permutation test using the kernel matrix
      # TODO: (See https://github.com/SeldonIO/alibi-detect/issues/540)
      n = x.shape[0]
      kernel_mat = self.kernel_matrix(x_ref, x)  # type: ignore[arg-type]
      kernel_mat = kernel_mat - torch.diag(
          kernel_mat.diag()
      )  # zero diagonal
      mmd2 = mmd2_from_kernel_matrix(
          kernel_mat, n, permute=False, zero_diag=False
      )
      mmd2_permuted = torch.Tensor(
          [
              mmd2_from_kernel_matrix(
                  kernel_mat, n, permute=True, zero_diag=False
              )
              for _ in range(self.n_permutations)
          ]
      )
      if self.device.type == "cuda":
          mmd2, mmd2_permuted = mmd2.cpu(), mmd2_permuted.cpu()
      p_val = (mmd2 <= mmd2_permuted).float().mean()
      # compute distance threshold
      idx_threshold = int(self.p_val * len(mmd2_permuted))
      distance_threshold = torch.sort(
          mmd2_permuted, descending=True
      ).values[idx_threshold]
      return (
          p_val.numpy().item(),
          mmd2.numpy().item(),
          distance_threshold.numpy(),
      )
  
file_path: alibi-detect/alibi_detect/cd/pytorch/mmd.py
imports: import logging
  import numpy
  import torch
  from typing import Callable, Dict, Optional, Tuple, Union
  from alibi_detect.cd.base import BaseMMDDrift
  from alibi_detect.utils.pytorch import get_device
  from alibi_detect.utils.pytorch.distance import mmd2_from_kernel_matrix
  from alibi_detect.utils.pytorch.kernels import GaussianRBF
  from alibi_detect.utils.warnings import deprecated_alias
  from alibi_detect.utils.frameworks import Framework
  from alibi_detect.utils._types import TorchDeviceType
label_desc: Use image node_id 3 for calling the MMDDriftTorch obj's underlying member method code with example usage: obj.score(x) and returns: p_val,  mmd2,  distance_threshold