evalsuite-python 0.6.0__cp39-abi3-win_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- evalsuite/__init__.py +268 -0
- evalsuite/__main__.py +5 -0
- evalsuite/_bench_metrics.py +2080 -0
- evalsuite/_core.pyd +0 -0
- evalsuite/_lazy.py +32 -0
- evalsuite/_native.py +60 -0
- evalsuite/api.py +275 -0
- evalsuite/benchmarks.py +946 -0
- evalsuite/calibration.py +392 -0
- evalsuite/classification/__init__.py +51 -0
- evalsuite/classification/_common.py +111 -0
- evalsuite/classification/metrics.py +945 -0
- evalsuite/cli/__init__.py +5 -0
- evalsuite/cli/main.py +606 -0
- evalsuite/clinical/__init__.py +18 -0
- evalsuite/clinical/metrics.py +342 -0
- evalsuite/clinical/report.py +366 -0
- evalsuite/core/__init__.py +1 -0
- evalsuite/core/context.py +199 -0
- evalsuite/core/exceptions.py +52 -0
- evalsuite/core/export.py +100 -0
- evalsuite/core/registry.py +90 -0
- evalsuite/core/result.py +379 -0
- evalsuite/core/types.py +23 -0
- evalsuite/core/validation.py +256 -0
- evalsuite/llmsys/__init__.py +189 -0
- evalsuite/llmsys/_common.py +215 -0
- evalsuite/llmsys/agents.py +384 -0
- evalsuite/llmsys/code.py +923 -0
- evalsuite/llmsys/efficiency.py +346 -0
- evalsuite/llmsys/longcontext.py +361 -0
- evalsuite/llmsys/multilingual.py +339 -0
- evalsuite/llmsys/robustness.py +504 -0
- evalsuite/llmsys/safety.py +509 -0
- evalsuite/llmsys/uncertainty.py +229 -0
- evalsuite/plot.py +651 -0
- evalsuite/py.typed +0 -0
- evalsuite/regression/__init__.py +41 -0
- evalsuite/regression/metrics.py +629 -0
- evalsuite/reporting.py +241 -0
- evalsuite/stats/__init__.py +46 -0
- evalsuite/stats/_resolve.py +229 -0
- evalsuite/stats/compare.py +420 -0
- evalsuite/stats/effect.py +159 -0
- evalsuite/stats/hypothesis.py +593 -0
- evalsuite/stats/intervals.py +338 -0
- evalsuite/stats/paired.py +214 -0
- evalsuite/stats/results.py +129 -0
- evalsuite/text/__init__.py +154 -0
- evalsuite/text/_common.py +97 -0
- evalsuite/text/factuality.py +469 -0
- evalsuite/text/generation.py +1346 -0
- evalsuite/text/judge.py +556 -0
- evalsuite/text/qa.py +108 -0
- evalsuite/text/rag.py +235 -0
- evalsuite/text/reasoning.py +215 -0
- evalsuite/text/report.py +158 -0
- evalsuite/text/retrieval.py +238 -0
- evalsuite/text/semantic.py +499 -0
- evalsuite/text/spice.py +162 -0
- evalsuite/text/structured.py +793 -0
- evalsuite/version.py +3 -0
- evalsuite/vision/__init__.py +47 -0
- evalsuite/vision/detection.py +696 -0
- evalsuite/vision/segmentation.py +1028 -0
- evalsuite_python-0.6.0.dist-info/METADATA +509 -0
- evalsuite_python-0.6.0.dist-info/RECORD +72 -0
- evalsuite_python-0.6.0.dist-info/WHEEL +4 -0
- evalsuite_python-0.6.0.dist-info/entry_points.txt +2 -0
- evalsuite_python-0.6.0.dist-info/licenses/LICENSE +21 -0
- evalsuite_python-0.6.0.dist-info/licenses/LICENSE-APACHE +191 -0
- evalsuite_python-0.6.0.dist-info/sboms/evalsuite-py.cyclonedx.json +576 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"""Calibration and uncertainty for LLM answers (v0.5.0).
|
|
2
|
+
|
|
3
|
+
Inputs are, per question, whether the answer was correct and the model's confidence in it (a verbalised
|
|
4
|
+
probability, a token probability or a self-consistency share). ECE, MCE, the Brier score and log loss are the
|
|
5
|
+
existing ``es.expected_calibration_error``, ``es.maximum_calibration_error``, ``es.brier_score`` and
|
|
6
|
+
``es.log_loss`` applied to (correct, confidence); abstention quality is ``es.abstention_accuracy``. This module
|
|
7
|
+
adds adaptive calibration error and selective prediction: risk-coverage curves, AURC, risk at a coverage and
|
|
8
|
+
coverage at a risk, and confidence-accuracy correlation.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
|
|
17
|
+
from ..core.exceptions import InputValidationError
|
|
18
|
+
from ..core.registry import register
|
|
19
|
+
from ..core.result import MetricResult
|
|
20
|
+
from ._common import bools, floats, same_length
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"adaptive_calibration_error",
|
|
24
|
+
"aurc",
|
|
25
|
+
"confidence_accuracy_correlation",
|
|
26
|
+
"coverage_at_risk",
|
|
27
|
+
"risk_at_coverage",
|
|
28
|
+
"risk_coverage_curve",
|
|
29
|
+
"selective_risk",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
_C = "uncertainty"
|
|
33
|
+
_REF_NIXON = (
|
|
34
|
+
"Nixon J, Dusenberry M, Jerfel G, Nguyen T, Liu J, Zhang L, Tran D. Measuring calibration in deep learning. "
|
|
35
|
+
"CVPR Workshops. 2019."
|
|
36
|
+
)
|
|
37
|
+
_REF_GEIFMAN = (
|
|
38
|
+
"Geifman Y, Uziel G, El-Yaniv R. Bias-reduced uncertainty estimation for deep neural classifiers. ICLR. 2019."
|
|
39
|
+
)
|
|
40
|
+
_REF_ELYANIV = (
|
|
41
|
+
"El-Yaniv R, Wiener Y. On the foundations of noise-free selective classification. JMLR. 2010;11:1605-1641."
|
|
42
|
+
)
|
|
43
|
+
_REF_KADAVATH = (
|
|
44
|
+
"Kadavath S, Conerly T, Askell A, et al. Language models (mostly) know what they know. arXiv:2207.05221. 2022."
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _inputs(correct: Any, confidence: Any) -> tuple[np.ndarray, np.ndarray]:
|
|
49
|
+
c = bools(correct, "correct")
|
|
50
|
+
p = floats(confidence, "confidence", lo=0, hi=1)
|
|
51
|
+
same_length(("correct", c), ("confidence", p))
|
|
52
|
+
return c, p
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def risk_coverage_curve(correct: Any, confidence: Any) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
56
|
+
"""(coverage, selective risk, confidence threshold) when answering the k most confident questions, for
|
|
57
|
+
k = 1..n. Tied confidences are kept together (the curve only has points between distinct values)."""
|
|
58
|
+
c, p = _inputs(correct, confidence)
|
|
59
|
+
order = np.argsort(-p, kind="mergesort")
|
|
60
|
+
err = (~c[order]).astype(float)
|
|
61
|
+
ps = p[order]
|
|
62
|
+
k = np.arange(1, c.size + 1)
|
|
63
|
+
risk = np.cumsum(err) / k
|
|
64
|
+
last = np.r_[ps[1:] != ps[:-1], True] # end of each block of tied confidences
|
|
65
|
+
return k[last] / c.size, risk[last], ps[last]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@register(
|
|
69
|
+
category=_C,
|
|
70
|
+
task="calibration",
|
|
71
|
+
name="Adaptive calibration error (ACE)",
|
|
72
|
+
definition="Calibration error with equal-mass bins: the unweighted mean |accuracy − confidence| over R bins "
|
|
73
|
+
"that each hold the same number of predictions, so sparse high-confidence regions do not hide errors.",
|
|
74
|
+
formula="ACE = (1/R) Σ_r |acc(r) − conf(r)|, bins of equal count",
|
|
75
|
+
range="[0, 1]",
|
|
76
|
+
input_requirements=("correct", "confidence"),
|
|
77
|
+
references=(_REF_NIXON,),
|
|
78
|
+
higher_is_better=False,
|
|
79
|
+
)
|
|
80
|
+
def adaptive_calibration_error(correct: Any, confidence: Any, *, n_bins: int = 15) -> MetricResult:
|
|
81
|
+
c, p = _inputs(correct, confidence)
|
|
82
|
+
if isinstance(n_bins, bool) or not isinstance(n_bins, int) or n_bins < 1:
|
|
83
|
+
raise InputValidationError("n_bins must be a positive integer.")
|
|
84
|
+
r = min(n_bins, c.size)
|
|
85
|
+
order = np.argsort(p, kind="mergesort")
|
|
86
|
+
gaps = [abs(c[idx].mean() - p[idx].mean()) for idx in np.array_split(order, r)]
|
|
87
|
+
return MetricResult(
|
|
88
|
+
"adaptive_calibration_error", "Adaptive calibration error", float(np.mean(gaps)), {"n_bins": r}
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@register(
|
|
93
|
+
category=_C,
|
|
94
|
+
task="selective-prediction",
|
|
95
|
+
name="Selective risk",
|
|
96
|
+
definition="Error rate on the questions the model answers when it abstains on the least confident ones so "
|
|
97
|
+
"that a given share (coverage) is answered.",
|
|
98
|
+
formula="risk(c) = errors among the ⌈c·n⌉ most confident / ⌈c·n⌉",
|
|
99
|
+
range="[0, 1]",
|
|
100
|
+
input_requirements=("correct", "confidence"),
|
|
101
|
+
references=(_REF_ELYANIV, _REF_GEIFMAN),
|
|
102
|
+
higher_is_better=False,
|
|
103
|
+
)
|
|
104
|
+
def selective_risk(correct: Any, confidence: Any, *, coverage: float = 0.8) -> MetricResult:
|
|
105
|
+
if not 0 < coverage <= 1:
|
|
106
|
+
raise InputValidationError("coverage must be in (0, 1].")
|
|
107
|
+
c, p = _inputs(correct, confidence)
|
|
108
|
+
k = int(np.ceil(coverage * c.size))
|
|
109
|
+
order = np.argsort(-p, kind="mergesort")
|
|
110
|
+
risk = float((~c[order[:k]]).mean())
|
|
111
|
+
return MetricResult(
|
|
112
|
+
"selective_risk",
|
|
113
|
+
"Selective risk",
|
|
114
|
+
risk,
|
|
115
|
+
{"coverage": k / c.size, "threshold": float(p[order[k - 1]]), "full_coverage_risk": float((~c).mean())},
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@register(
|
|
120
|
+
category=_C,
|
|
121
|
+
task="selective-prediction",
|
|
122
|
+
name="Area under the risk-coverage curve (AURC)",
|
|
123
|
+
definition="Mean selective risk over all coverages when questions are answered in order of decreasing "
|
|
124
|
+
"confidence; lower means confidence ranks errors last. E-AURC subtracts the AURC of a perfect ranking.",
|
|
125
|
+
formula="AURC = (1/n) Σ_k risk(k/n); E-AURC = AURC − AURC*",
|
|
126
|
+
range="[0, 1]",
|
|
127
|
+
input_requirements=("correct", "confidence"),
|
|
128
|
+
references=(_REF_GEIFMAN,),
|
|
129
|
+
higher_is_better=False,
|
|
130
|
+
)
|
|
131
|
+
def aurc(correct: Any, confidence: Any) -> MetricResult:
|
|
132
|
+
c, p = _inputs(correct, confidence)
|
|
133
|
+
order = np.argsort(-p, kind="mergesort")
|
|
134
|
+
n = c.size
|
|
135
|
+
k = np.arange(1, n + 1)
|
|
136
|
+
value = float(np.mean(np.cumsum(~c[order]) / k))
|
|
137
|
+
errors = int((~c).sum())
|
|
138
|
+
optimal_curve = np.maximum(0, k - (n - errors)) / k
|
|
139
|
+
opt = float(np.mean(optimal_curve))
|
|
140
|
+
return MetricResult("aurc", "AURC", value, {"e_aurc": value - opt, "optimal_aurc": opt, "n": n})
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
@register(
|
|
144
|
+
category=_C,
|
|
145
|
+
task="selective-prediction",
|
|
146
|
+
name="Risk at fixed coverage",
|
|
147
|
+
definition="Selective risk at the target coverage, read from the risk-coverage curve at the smallest "
|
|
148
|
+
"confidence threshold whose coverage reaches the target (ties are answered together).",
|
|
149
|
+
formula="risk at the first curve point with coverage ≥ target",
|
|
150
|
+
range="[0, 1]",
|
|
151
|
+
input_requirements=("correct", "confidence"),
|
|
152
|
+
references=(_REF_ELYANIV, _REF_GEIFMAN),
|
|
153
|
+
higher_is_better=False,
|
|
154
|
+
)
|
|
155
|
+
def risk_at_coverage(correct: Any, confidence: Any, *, coverage: float = 0.8) -> MetricResult:
|
|
156
|
+
if not 0 < coverage <= 1:
|
|
157
|
+
raise InputValidationError("coverage must be in (0, 1].")
|
|
158
|
+
cov, risk, thr = risk_coverage_curve(correct, confidence)
|
|
159
|
+
i = int(np.searchsorted(cov, coverage - 1e-12))
|
|
160
|
+
return MetricResult(
|
|
161
|
+
"risk_at_coverage",
|
|
162
|
+
"Risk at coverage",
|
|
163
|
+
float(risk[i]),
|
|
164
|
+
{"target_coverage": coverage, "achieved_coverage": float(cov[i]), "threshold": float(thr[i])},
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
@register(
|
|
169
|
+
category=_C,
|
|
170
|
+
task="selective-prediction",
|
|
171
|
+
name="Coverage at fixed risk",
|
|
172
|
+
definition="The largest share of questions the model can answer (abstaining on the rest by confidence) while "
|
|
173
|
+
"keeping the selective error rate at or below the target risk.",
|
|
174
|
+
formula="max coverage over curve points with risk ≤ target",
|
|
175
|
+
range="[0, 1]",
|
|
176
|
+
input_requirements=("correct", "confidence"),
|
|
177
|
+
references=(_REF_ELYANIV, _REF_GEIFMAN),
|
|
178
|
+
)
|
|
179
|
+
def coverage_at_risk(correct: Any, confidence: Any, *, risk: float = 0.05) -> MetricResult:
|
|
180
|
+
if not 0 <= risk < 1:
|
|
181
|
+
raise InputValidationError("risk must be in [0, 1).")
|
|
182
|
+
cov, r, thr = risk_coverage_curve(correct, confidence)
|
|
183
|
+
ok = np.flatnonzero(r <= risk + 1e-12)
|
|
184
|
+
if ok.size == 0:
|
|
185
|
+
return MetricResult("coverage_at_risk", "Coverage at risk", 0.0, {"target_risk": risk, "threshold": None})
|
|
186
|
+
i = int(ok[-1])
|
|
187
|
+
return MetricResult(
|
|
188
|
+
"coverage_at_risk",
|
|
189
|
+
"Coverage at risk",
|
|
190
|
+
float(cov[i]),
|
|
191
|
+
{"target_risk": risk, "achieved_risk": float(r[i]), "threshold": float(thr[i])},
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
@register(
|
|
196
|
+
category=_C,
|
|
197
|
+
task="calibration",
|
|
198
|
+
name="Confidence–accuracy correlation",
|
|
199
|
+
definition="How well confidence discriminates correct from incorrect answers: the AUROC of confidence for "
|
|
200
|
+
"correctness (P(true) discrimination, Kadavath et al.), with the point-biserial and Spearman correlations.",
|
|
201
|
+
formula="AUROC = P(conf_correct > conf_wrong) + ½ P(tie)",
|
|
202
|
+
range="[0, 1] (0.5 = no signal)",
|
|
203
|
+
input_requirements=("correct", "confidence"),
|
|
204
|
+
references=(_REF_KADAVATH,),
|
|
205
|
+
)
|
|
206
|
+
def confidence_accuracy_correlation(correct: Any, confidence: Any) -> MetricResult:
|
|
207
|
+
from scipy import stats
|
|
208
|
+
|
|
209
|
+
c, p = _inputs(correct, confidence)
|
|
210
|
+
if c.all() or not c.any():
|
|
211
|
+
raise InputValidationError("Need both correct and incorrect answers.")
|
|
212
|
+
ranks = stats.rankdata(p)
|
|
213
|
+
n1 = int(c.sum())
|
|
214
|
+
auroc = float((ranks[c].sum() - n1 * (n1 + 1) / 2) / (n1 * (c.size - n1)))
|
|
215
|
+
if np.ptp(p) == 0:
|
|
216
|
+
pb = sp = float("nan")
|
|
217
|
+
else:
|
|
218
|
+
cf = c.astype(float)
|
|
219
|
+
pb = float(np.corrcoef(cf, p)[0, 1])
|
|
220
|
+
# Spearman = Pearson correlation of average ranks; the ranks of p are already computed
|
|
221
|
+
n0 = c.size - n1
|
|
222
|
+
rc = np.where(c, n0 + (n1 + 1) / 2, (n0 + 1) / 2)
|
|
223
|
+
sp = float(np.corrcoef(rc, ranks)[0, 1])
|
|
224
|
+
return MetricResult(
|
|
225
|
+
"confidence_accuracy_correlation",
|
|
226
|
+
"Confidence–accuracy AUROC",
|
|
227
|
+
auroc,
|
|
228
|
+
{"point_biserial": pb, "spearman": sp, "accuracy": float(c.mean()), "mean_confidence": float(p.mean())},
|
|
229
|
+
)
|