code: def _theils_u_compute(confmat: Tensor) -> Tensor:
      """Compute Theil's U statistic based on a pre-computed confusion matrix.
  
      Args:
          confmat: Confusion matrix for observed data
  
      Returns:
          Theil's U statistic
  
      """
      confmat = _drop_empty_rows_and_cols(confmat)
  
      # compute conditional entropy
      s_xy = _conditional_entropy_compute(confmat)
  
      # compute H(x)
      total_occurrences = confmat.sum()
      p_x = confmat.sum(0) / total_occurrences
      s_x = -torch.sum(p_x * torch.log(p_x))
  
      # compute u statistic
      if s_x == 0:
          return torch.tensor(0, device=confmat.device)
  
      return (s_x - s_xy) / s_x
  
file_path: torchmetrics/src/torchmetrics/functional/nominal/theils_u.py
imports: import itertools
  from typing import Optional
  import torch
  from torch import Tensor
  from typing_extensions import Literal
  from torchmetrics.functional.classification.confusion_matrix import _multiclass_confusion_matrix_update
  from torchmetrics.functional.nominal.utils import _drop_empty_rows_and_cols, _handle_nan_in_data, _nominal_input_validation
label_desc: Use image node_id 3 for calling a global function with example usage: _theils_u_compute(confmat) and returns: unknown, torch