code: def calculate_n_louder(bstat, fstat, dec, skip_background=False):
      
      sort = bstat.argsort()
      bstat = bstat[sort]
      dec = dec[sort]
  
      # calculate cumulative number of triggers louder than the trigger in
      # a given index. We need to subtract the decimation factor, as the cumsum
      # includes itself in the first sum (it is inclusive of the first value)
      n_louder = dec[::-1].cumsum()[::-1] - dec
  
      # Determine how many values are louder than the foreground ones
      # We need to subtract one from the index, to be consistent with the definition
      # of n_louder, as here we do want to include the background value at the
      # found index
      idx = numpy.searchsorted(bstat, fstat, side='left') - 1
  
      # If the foreground are *quieter* than the background or at the same value
      # then the search sorted alorithm will choose position -1, which does not exist
      # We force it back to zero.
      if isinstance(idx, numpy.ndarray): # Handle the case where our input is an array
          idx[idx < 0] = 0
      else: # Handle the case where we are simply given a scalar value
          if idx < 0:
              idx = 0
  
      fore_n_louder = n_louder[idx]
  
      if not skip_background:
          unsort = sort.argsort()
          back_cum_num = n_louder[unsort]
          return back_cum_num, fore_n_louder
      else:
          return fore_n_louder
docs: Calculate for each foreground event the number of background events
      that are louder than it.
  
      Parameters
      ----------
      bstat: numpy.ndarray
          Array of the background statistic values
      fstat: numpy.ndarray
          Array of the foreground statitsic values
      dec: numpy.ndarray
          Array of the decimation factors for the background statistics
      skip_background: optional, {boolean, False}
          Skip calculating cumulative numbers for background triggers
  
      Returns
      -------
      cum_back_num: numpy.ndarray
          The cumulative array of background triggers. Does not return this
          argument if skip_background == True
      fore_n_louder: numpy.ndarray
          The number of background triggers above each foreground trigger