Commit d951b038 authored by Vignesh's avatar Vignesh
Browse files

Added BEDROC metric

parent 635c9b09
Loading
Loading
Loading
Loading
+53 −0
Original line number Diff line number Diff line
@@ -161,6 +161,59 @@ def kappa_score(y_true, y_pred):
  return kappa


def bedroc_score(y_true, y_pred, alpha=20.0):
  """BEDROC metric implemented according to Truchon and Bayley that modifies
  the ROC score by allowing for a factor of early recognition

    References:
      The original paper by Truchon et al. is located at
      https://pubs.acs.org/doi/pdf/10.1021/ci600426e

    Args:
      y_true (array_like):
        Binary class labels. 1 for positive class, 0 otherwise
      y_pred (array_like):
        Predicted labels
      alpha (float), default 20.0:
        Early recognition parameter

    Returns:
      float: Value in [0, 1] that indicates the degree of early recognition

  """

  assert len(y_true) == len(y_pred), 'Number of examples do not match'

  assert np.array_equal(
      np.unique(y_true).astype(int),
      [0, 1]), ('Class labels must be binary: %s' % np.unique(y_true))

  # Calculate ratio of actives to inactives
  N = len(y_true)
  n = sum(y_true == 1)
  R_a = n / N

  # The expression for the rie_denominator
  rie_denom = R_a * (1 - np.exp(-alpha)) / (np.exp(alpha / N) - 1)

  # Rank orders and rie_numerator
  order = np.argsort(y_pred)
  r_i = (y_true[order] == 1).nonzero()[0]
  rie_numerator = np.sum(np.exp(-alpha * r_i / N))

  # Rie_score
  rie_score = rie_numerator / rie_denom

  # Factor to be multipled
  factor = R_a * np.sinh(
      alpha / 2) / (np.cosh(alpha / 2) - np.cosh(alpha / 2 - (alpha * R_a)))

  bedroc_score = rie_score * factor
  bedroc_score += 1 / (1 - np.exp(alpha * (1 - R_a)))

  return bedroc_score


class Metric(object):
  """Wrapper class for computing user-defined metrics."""