mcy98's picture
Upload folder using huggingface_hub
2d13eb2 verified
Raw History Blame Contribute Delete
4.14 kB
from math import ceil
import numpy as np
# Define the acceptable subclaim annotation labels treated as "entailed"/"factual".
# According to the paper (Section 6.1.3), "Y" and "S" correspond to "Factual" and "Subjective",
# and are both considered to be entailed for the purpose of evaluating correctness.
CORRECT_ANNOTATIONS = ["Y", "S"]
def get_r_score(entry, confidence_method, a):
"""
Compute the r_a score for a single example (entry) using the specified confidence method
and acceptable factuality rate `a`.
Parameters:
entry (dict): A dictionary representing one LM generation with a list of subclaims.
confidence_method (str): The key prefix for confidence scores (e.g., "gpt", "freq").
a (float): The required fraction of subclaims that must be entailed (0 ≤ a ≤ 1).
Returns:
float: The minimum threshold such that the accepted subclaims (those above threshold)
have ≥ `a` fraction of entailed claims. If no such threshold is found,
return -100000 (a sentinel value indicating total failure).
Corresponds to:
- Equation (ra(x, y*) in Section 5.1)
- Implements logic similar to Proposition 5.2 with partial entailment checking.
Note:
- Adds noise for tie-breaking as described in Section 6.1.2.
- Uses linear threshold search from high to low.
"""
# Build a list of threshold values for each subclaim by adding noise to its score
threshold_set = sorted(
[
subclaim[confidence_method + "-score"] + subclaim["noise"]
for subclaim in entry["claims"]
],
reverse=True # Start from highest score to lowest
)
curr_threshold = threshold_set[0]
for threshold in threshold_set:
curr_threshold = threshold
# Subclaims accepted under current threshold
accepted_subclaims = [
subclaim
for subclaim in entry["claims"]
if subclaim[confidence_method + "-score"] + subclaim["noise"] >= threshold
]
# Compute the fraction of accepted subclaims that are annotated as factual
entailed_fraction = (
np.mean(
[
subclaim["annotation"] in CORRECT_ANNOTATIONS
for subclaim in accepted_subclaims
]
)
if accepted_subclaims else 1 # If nothing is accepted, trivially satisfied
)
# If factuality target is violated, return the current threshold (last safe point)
if entailed_fraction < a:
return curr_threshold
# All thresholds meet the constraint: use sentinel to indicate total acceptance
return -100000
def compute_threshold(alpha, calibration_data, a, confidence_method):
"""
Compute the conformal threshold from calibration data using split conformal prediction.
Parameters:
alpha (float): Target error rate (1 - desired factuality level).
calibration_data (list): List of entries (each with scored/annotated subclaims).
a (float): Required factuality fraction (same as above).
confidence_method (str): Method used to score subclaims ("gpt", "freq", etc.).
Returns:
float: The conformal threshold value for pruning new outputs.
Corresponds to:
- Algorithm 1 in the paper
- Uses Theorem 4.1 for correctness guarantee: computes the (1-alpha) quantile of r_scores.
Notes:
- Quantile index is computed as ceil((n+1)*(1-alpha)) - 1 (0-based indexing).
- Assumes r_scores are distinct due to added noise.
"""
# Step 1: Compute r_a score for each example in the calibration dataset
r_scores = [get_r_score(entry, confidence_method, a) for entry in calibration_data]
# Step 2: Determine the quantile index based on conformal prediction formula
quantile_target_index = ceil((len(r_scores) + 1) * (1 - alpha))
# Step 3: Sort r_scores and pick the (1-alpha) quantile threshold
threshold = sorted(r_scores)[quantile_target_index - 1] # -1 for 0-based indexing
return threshold