code: def contingency_tables(
      ys, decision_values, positive=True, presorted=False
  ):
      """Computes contingency tables for every unique decision value.
  
      :param ys: true labels
      :type ys: iterable
      :param decision_values: decision values (higher = stronger positive)
      :type decision_values: iterable
      :param positive: the positive label
      :param presorted: whether or not ys and yhat are already sorted
      :type presorted: bool
  
      :returns: a list of contingency tables `(TP, FP, TN, FN)` and the corresponding thresholds.
      Contingency tables are built based on decision :math:`decision\_value \geq threshold`.
  
      The first contingency table corresponds with a (potentially unseen) threshold that yields all negatives.
  
      >>> y = [0, 0, 0, 0, 1, 1, 1, 1]
      >>> d = [2, 2, 1, 1, 1, 2, 3, 3]
      >>> tables, thresholds = contingency_tables(y, d, 1)
      >>> print(tables)
      [(0, 0, 4, 4), (2, 0, 4, 2), (3, 2, 2, 1), (4, 4, 0, 0)]
      >>> print(thresholds)
      [None, 3, 2, 1]
  
      """
      if presorted:
          if decision_values[0] > decision_values[-1]:
              ind = range(len(decision_values))
              srt = decision_values
          else:
              ind = reversed(range(len(decision_values)))
              srt = reversed(decision_values)
      else:
          # sort decision values
          ind, srt = zip(
              *sorted(
                  enumerate(decision_values),
                  reverse=True,
                  key=op.itemgetter(1),
              )
          )
  
      # resort y
      y = list(map(lambda x: ys[x] == positive, ind))
  
      num_instances = len(ind)
      total_num_pos = sum(y)
  
      thresholds = [None]
      tables = [(0, 0, num_instances - total_num_pos, total_num_pos)]
  
      current_idx = 0
      while current_idx < num_instances:
          # determine number of identical decision values
          num_ties = 1
          while (
              current_idx + num_ties < num_instances
              and srt[current_idx + num_ties] == srt[current_idx]
          ):
              num_ties += 1
  
          if current_idx == 0:
              previous_table = (
                  0,
                  0,
                  num_instances - total_num_pos,
                  total_num_pos,
              )
  
          # find number of new true positives at this threshold
          num_pos = 0
          for i in range(current_idx, current_idx + num_ties):
              num_pos += y[i]
  
          # difference compared to previous contingency_table
          diff = (
              num_pos,
              num_ties - num_pos,
              -num_ties + num_pos,
              -num_pos,
          )
  
          new_table = tuple(map(op.add, previous_table, diff))
          tables.append(new_table[:])
          thresholds.append(srt[current_idx])
  
          # prepare for next iteration
          previous_table = new_table
          current_idx += num_ties
  
      return tables, thresholds
  
imports: import math
  import operator
label_desc: Use image node_id 1 for calling a global function with example usage: contingency_tables(ys, decision_values, positive, presorted) and returns: tables,  thresholds