class_docstr: [Independence](https://fairmlbook.org) (pg. 9) measures the statistical independence
  of the protected variable from predictions. It has been explored through many equivalent
  terms or variants, such as demographic parity, statistical parity, group fairness, and
  disparate impact.
  
  # Parameters
  
  num_classes : `int`
      Number of classes.
  num_protected_variable_labels : `int`
      Number of protected variable labels.
  dist_metric : `str`
      Distance metric (kl_divergence, wasserstein) for calculating the distance between the distribution
      over predicted labels and the distribution over predicted labels given a sensitive attribute.
  
  
  !!! Note
      Assumes integer labels, with each item to be classified having a single correct class.
class_docstr_tok: ["[","Independence","]","(","https",":","\/\/fairmlbook.org",")","(","pg",".","9",")","measures","the","statistical","independence","of","the","protected","variable","from","predictions",".","It","has","been","explored","through","many","equivalent","terms","or","variants",",","such","as","demographic","parity",",","statistical","parity",",","group","fairness",",","and","disparate","impact",".","#","Parameters","num_classes",":","`","int","`","Number","of","classes",".","num_protected_variable_labels",":","`","int","`","Number","of","protected","variable","labels",".","dist_metric",":","`","str","`","Distance","metric","(","kl_divergence",",","wasserstein",")","for","calculating","the","distance","between","the","distribution","over","predicted","labels","and","the","distribution","over","predicted","labels","given","a","sensitive","attribute",".","!","!","!","Note","Assumes","integer","labels",",","with","each","item","to","be","classified","having","a","single","correct","class","."]
docstr: # Returns
  
  distances : `Dict[int, torch.FloatTensor]`
      A dictionary mapping each protected variable label a to the KL divergence or Wasserstein distance
       of P(C | A = a) from P(C). A distance of nearly 0 implies fairness on the basis of Independence.
docstr_tok: ["#","Returns","distances",":","`","Dict","[","int",",","torch.FloatTensor","]","`","A","dictionary","mapping","each","protected","variable","label","a","to","the","KL","divergence","or","Wasserstein","distance","of","P","(","C","|","A","=","a",")","from","P","(","C",")",".","A","distance","of","nearly","0","implies","fairness","on","the","basis","of","Independence","."]
code: def get_metric(
      self, reset: bool = False
  ) -> Dict[int, torch.FloatTensor]:
      """
      # Returns
  
      distances : `Dict[int, torch.FloatTensor]`
          A dictionary mapping each protected variable label a to the KL divergence or Wasserstein distance
           of P(C | A = a) from P(C). A distance of nearly 0 implies fairness on the basis of Independence.
      """
      distances: Dict[int, torch.FloatTensor] = {}
      if self._total_predictions == 0:
          distances = {
              a: torch.tensor(float("nan"))
              for a in range(self._num_protected_variable_labels)
          }
          return distances
  
      C_dist = Categorical(
          self._predicted_label_counts / self._total_predictions
      )
      if self._dist_metric == wasserstein_distance:
          C_dist = C_dist.probs
      for a in range(self._num_protected_variable_labels):
          C_given_a_dist = Categorical(
              self._predicted_label_counts_by_protected_variable_label[
                  a
              ]
              / self._total_predictions
          )
          if self._dist_metric == kl_divergence:
              distances[a] = self._dist_metric(C_given_a_dist, C_dist)
          elif self._dist_metric == wasserstein_distance:
              C_given_a_dist = C_given_a_dist.probs
              # scipy expects empirical values as well as probabilities
              label_values = torch.tensor(range(self._num_classes))
              distances[a] = self._dist_metric(
                  label_values, label_values, C_given_a_dist, C_dist
              )
      if reset:
          self.reset()
      return distances
  
imports: from typing import Optional, Dict
  from scipy.stats import wasserstein_distance
  import torch
  import torch.distributed
  from torch.distributions.categorical import Categorical
  from torch.distributions.kl import kl_divergence
  from allennlp.common.util import is_distributed
  from allennlp.common.checks import ConfigurationError
  from allennlp.training.metrics.metric import Metric
label_desc: Use image node_id 3 for calling the Independence obj's underlying member method code with example usage: obj.get_metric(reset) and returns: distances, distances