Skip to content

Commit

Permalink
Merge pull request #230 from IINemo/prr_upd
Browse files Browse the repository at this point in the history
added parameter to PRR to compute 50% of the curve
  • Loading branch information
rvashurin authored Sep 7, 2024
2 parents 3473bf9 + 5ce3175 commit c744e2e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/lm_polygraph/ue_metrics/pred_rej_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ class PredictionRejectionArea(UEMetric):
Calculates area under Prediction-Rejection curve.
"""

def __init__(self, max_rejection: float = 1.0):
"""
Parameters:
max_rejection (float): a maximum proportion of instances that will be rejected.
1.0 indicates entire set, 0.5 - half of the set
"""
super().__init__()
self.max_rejection = max_rejection

def __str__(self):
return "prr"

Expand All @@ -30,12 +39,13 @@ def __call__(self, estimator: List[float], target: List[float]) -> float:
# ue: greater is more uncertain
ue = np.array(estimator)
num_obs = len(ue)
num_rej = int(self.max_rejection * num_obs)
# Sort in ascending order: the least uncertain come first
ue_argsort = np.argsort(ue)
# want sorted_metrics to be increasing => smaller scores is better
sorted_metrics = np.array(target)[ue_argsort]
# Since we want all plots to coincide when all the data is discarded
cumsum = np.cumsum(sorted_metrics)
scores = (cumsum / np.arange(1, num_obs + 1))[::-1]
prr_score = np.sum(scores) / num_obs
cumsum = np.cumsum(sorted_metrics)[-num_rej:]
scores = (cumsum / np.arange((num_obs - num_rej) + 1, num_obs + 1))[::-1]
prr_score = np.sum(scores) / num_rej
return prr_score

0 comments on commit c744e2e

Please sign in to comment.