"""Calibrate a judge threshold against human labels.

这个示例用极小的数据集演示“Judge 分数阈值校准”：
模型 Judge 通常会输出 0~1 的置信分数，但线上系统需要一个明确阈值
来决定“通过/不通过”。这里用人工标签作为参照，遍历候选阈值并选择准确率最高的值。
"""

# 每条样本包含两类信息：
# - judge：自动评审器给出的分数；
# - human：人工标注的真实通过结果。
# 在真实项目中，这个列表通常来自标注平台或历史人工复核数据。
SAMPLES = [
    {"judge": 0.95, "human": True},
    {"judge": 0.78, "human": True},
    {"judge": 0.62, "human": False},
    {"judge": 0.40, "human": False},
]


def accuracy(threshold: float) -> float:
    """计算某个阈值下，Judge 判定与人工标签的一致率。"""

    # 当 judge 分数大于等于阈值时视为通过；
    # 这个布尔结果要和 human 标签一致才算预测正确。
    correct = sum((sample["judge"] >= threshold) == sample["human"] for sample in SAMPLES)

    # 准确率 = 正确样本数 / 总样本数。
    return correct / len(SAMPLES)


if __name__ == "__main__":
    # 候选阈值可以按业务风险调整：阈值越高越保守，越低越容易放行。
    candidates = [0.5, 0.6, 0.7, 0.8]

    # max 会依次调用 accuracy(threshold)，选出准确率最高的阈值。
    best = max(candidates, key=accuracy)

    # 输出最佳阈值及其准确率，便于复制到配置文件或实验报告。
    print({"threshold": best, "accuracy": accuracy(best)})
