API reference¶
Audit¶
veracle.audit.JudgeAudit(true_positive, false_negative, true_negative, false_positive, human=None, judge=None, clusters=None)
dataclass
¶
Agreement between judge verdicts and human labels.
Create it with :meth:from_labels or :meth:from_counts.
Attributes:
| Name | Type | Description |
|---|---|---|
true_positive |
int
|
Items that humans and the judge both marked as passing. |
false_negative |
int
|
Items that humans marked as passing and the judge as failing. |
true_negative |
int
|
Items that both marked as failing. |
false_positive |
int
|
Items that humans marked as failing and the judge as passing. |
human |
NDArray[int8] | None
|
Human labels in input order, if the audit was built from labels. |
judge |
NDArray[int8] | None
|
Judge verdicts in input order, if the audit was built from labels. |
clusters |
NDArray[intp] | None
|
Integer cluster codes of the audited items, if given. |
n
property
¶
Number of audited items.
n_positive
property
¶
Number of truly passing items in the audit.
n_negative
property
¶
Number of truly failing items in the audit.
sensitivity
property
¶
Share of truly passing items that the judge passes.
specificity
property
¶
Share of truly failing items that the judge fails.
youden
property
¶
Youden's index sensitivity + specificity - 1.
Zero means a judge that is no better than a coin flip; one means a perfect judge. The correction divides by this number, so small values inflate the uncertainty.
agreement
property
¶
Raw share of items on which judge and humans agree.
has_labels
property
¶
Whether item level labels are available (needed for the bootstrap).
from_labels(human, judge, clusters=None)
classmethod
¶
Build an audit from paired human labels and judge verdicts.
Examples:
>>> audit = JudgeAudit.from_labels([1, 1, 1, 0, 0], [1, 1, 0, 0, 1])
>>> audit.sensitivity, audit.specificity
(0.6666666666666666, 0.5)
from_counts(true_positive, false_negative, true_negative, false_positive)
classmethod
¶
Build an audit from the four cells of the confusion matrix.
Examples:
>>> round(JudgeAudit.from_counts(90, 10, 40, 10).youden, 3)
0.7
sensitivity_variance()
¶
Variance of the estimated sensitivity.
Uses the Laplace adjusted proportion (x + 1) / (n + 2) so that
the variance stays positive when the judge is perfect on the audit.
With clusters, the cluster-robust ratio variance is used when it is
larger.
specificity_variance()
¶
Variance of the estimated specificity. See :meth:sensitivity_variance.
intervals(alpha=0.05)
¶
Exact (Clopper-Pearson) intervals for sensitivity and specificity.
The intervals ignore clustering.
to_dict()
¶
Return counts and derived quantities as a dictionary.
summary(alpha=0.05)
¶
Plain text summary with exact confidence intervals.
veracle.audit.audit_by_group(human, judge, groups)
¶
Build one audit per group, for example per evaluated model.
Examples:
>>> out = audit_by_group([1, 0, 1, 0], [1, 0, 0, 0], ["a", "a", "b", "b"])
>>> out["a"].sensitivity, out["b"].sensitivity
(1.0, 0.0)
veracle.audit.judge_consistency(audits, alpha=0.05)
¶
Test whether sensitivity and specificity are the same in every group.
The misclassification correction assumes that the judge's error rates do not depend on which model produced the answer (non-differential error). A significant result means that each group needs its own audit. Absence of significance is not proof of equality, especially with small audits.
Examples:
>>> a = JudgeAudit.from_counts(90, 10, 45, 5)
>>> b = JudgeAudit.from_counts(60, 40, 45, 5)
>>> judge_consistency({"model_a": a, "model_b": b}).consistent
False
veracle.audit.ConsistencyResult(sensitivity_pvalue, specificity_pvalue, sensitivities, specificities, consistent, alpha)
dataclass
¶
Test of whether the judge errs equally often in every group.
Attributes:
| Name | Type | Description |
|---|---|---|
sensitivity_pvalue, specificity_pvalue |
Chi-square test p-values for equal sensitivity and equal specificity across groups. |
|
sensitivities, specificities |
Estimates per group. |
|
consistent |
bool
|
Whether neither test rejects at level |
alpha |
float
|
Level of the tests. |
to_dict()
¶
Return the result as a dictionary.
veracle.audit.audit_size(target_half_width, true_rate, sensitivity, specificity, n_items=math.inf, alpha=0.05, design='random')
¶
Smallest audit that reaches a target margin of error for the corrected rate.
Examples:
>>> audit_size(0.03, 0.7, 0.9, 0.8, n_items=5000)
1542
>>> audit_size(0.05, 0.7, 0.9, 0.8, n_items=5000)
403
veracle.audit.corrected_std_error(true_rate, sensitivity, specificity, n_items, n_audit, design='random')
¶
Return the expected standard error of the Rogan-Gladen corrected rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
true_rate
|
float
|
Planning values. |
required |
sensitivity
|
float
|
Planning values. |
required |
specificity
|
float
|
Planning values. |
required |
n_items
|
float
|
Number of judged items; |
required |
n_audit
|
float
|
Total number of human-labelled audit items. |
required |
design
|
Design
|
|
'random'
|
Examples:
>>> round(corrected_std_error(0.7, 0.9, 0.8, 2000, 200), 4)
0.0368
veracle.audit.naive_bias(true_rate, sensitivity, specificity)
¶
Bias of the uncorrected judge rate: q - theta.
Examples:
>>> round(naive_bias(0.7, 0.95, 0.80), 3)
0.025
Correction¶
veracle.correction.correct_rate(audit, judge=None, *, n_items=None, n_pass=None, alpha=0.05, method='delta', clusters=None, n_boot=2000, seed=None)
¶
Correct a judge-based pass rate for the judge's errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audit
|
JudgeAudit
|
Human audit of the judge, see :class: |
required |
judge
|
ArrayLike | None
|
Judge verdicts (0 or 1) on the evaluated items. Alternatively give
|
None
|
n_items
|
int | None
|
Number of evaluated items and number judged as passing. |
None
|
n_pass
|
int | None
|
Number of evaluated items and number judged as passing. |
None
|
alpha
|
float
|
Error level of the two-sided confidence interval. |
0.05
|
method
|
Method
|
.. math::
|
'delta'
|
clusters
|
ArrayLike | None
|
Cluster labels of the evaluated items, for example the prompt that several answers share. Cluster labels of the audit are given when building the audit. |
None
|
n_boot
|
int
|
Number of bootstrap draws. |
2000
|
seed
|
int | Generator | None
|
Seed for the bootstrap. |
None
|
Returns:
| Type | Description |
|---|---|
CorrectedRate
|
|
Raises:
| Type | Description |
|---|---|
NonInformativeJudgeError
|
If the judge is no better than chance on the audit. |
Examples:
>>> audit = JudgeAudit.from_counts(true_positive=95, false_negative=5,
... true_negative=80, false_positive=20)
>>> result = correct_rate(audit, n_items=2000, n_pass=1400)
>>> round(result.naive, 3), round(result.estimate, 3)
(0.7, 0.667)
veracle.correction.ppi_rate(judge, human, *, alpha=0.05, method='analytic', clusters=None, n_boot=2000, seed=None)
¶
Prediction-powered estimate of the true pass rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
judge
|
ArrayLike
|
Judge verdicts for all |
required |
human
|
ArrayLike
|
Human labels for the same items, with |
required |
alpha
|
float
|
Error level of the two-sided interval. |
0.05
|
method
|
Literal['analytic', 'bootstrap']
|
|
'analytic'
|
clusters
|
ArrayLike | None
|
Cluster labels of the |
None
|
n_boot
|
int
|
Bootstrap settings. |
2000
|
seed
|
int
|
Bootstrap settings. |
2000
|
Returns:
| Type | Description |
|---|---|
CorrectedRate
|
|
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> y = rng.binomial(1, 0.6, 5000)
>>> j = np.where(rng.uniform(size=5000) < 0.85, y, 1 - y)
>>> audited = rng.uniform(size=5000) < 0.05
>>> result = ppi_rate(j, np.where(audited, y, np.nan))
>>> bool(result.ci_lower < 0.6 < result.ci_upper)
True
veracle.correction.compare_rates(audit, judge_a, judge_b, *, paired=False, alpha=0.05, method='delta', clusters=None, n_boot=2000, seed=None)
¶
Corrected difference of true pass rates between two models.
Both models are scored by the same judge, audited once. Under non-differential error (the judge errs equally often for both models) the corrected difference is
.. math::
\hat\theta_A - \hat\theta_B = \frac{\hat q_A - \hat q_B}{\hat s_e + \hat s_p - 1},
so judge errors shrink observed differences towards zero, and only
Youden's index matters. Use :func:~veracle.audit.judge_consistency to
check the non-differential assumption.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audit
|
JudgeAudit
|
Audit of the judge. |
required |
judge_a
|
ArrayLike
|
Judge verdicts for the two models. |
required |
judge_b
|
ArrayLike
|
Judge verdicts for the two models. |
required |
paired
|
bool
|
Whether both models answered the same items in the same order. Pairing removes the item difficulty from the variance. |
False
|
alpha
|
float
|
As in :func: |
0.05
|
method
|
float
|
As in :func: |
0.05
|
n_boot
|
float
|
As in :func: |
0.05
|
seed
|
float
|
As in :func: |
0.05
|
clusters
|
ArrayLike | None
|
Cluster labels shared by both models when |
None
|
Returns:
| Type | Description |
|---|---|
CorrectedRate
|
The estimate is |
Examples:
>>> import numpy as np
>>> audit = JudgeAudit.from_counts(90, 10, 85, 15)
>>> a = np.r_[np.ones(780), np.zeros(220)]
>>> b = np.r_[np.ones(700), np.zeros(300)]
>>> diff = compare_rates(audit, a, b)
>>> round(diff.naive, 3), round(diff.estimate, 3)
(0.08, 0.107)
veracle.correction.rogan_gladen(observed, sensitivity, specificity)
¶
Rogan-Gladen corrected rate, truncated to [0, 1].
Examples:
>>> round(rogan_gladen(0.70, 0.95, 0.80), 4)
0.6667
veracle.correction.CorrectedRate(estimate, naive, std_error, ci_lower, ci_upper, alpha, method, n_items, n_audit, lower_limit=0.0, upper_limit=1.0, draws=None)
dataclass
¶
A judge-based rate corrected for the judge's measurement error.
Attributes:
| Name | Type | Description |
|---|---|---|
estimate |
float
|
Corrected estimate of the true rate (or of a difference of rates). |
naive |
float
|
The uncorrected judge-based rate, for comparison. |
std_error |
float
|
Standard error of the corrected estimate. |
ci_lower, ci_upper |
Two-sided confidence interval at level |
|
alpha |
float
|
Error level of the interval. |
method |
str
|
|
n_items |
int
|
Number of judged items (per model for a difference). |
n_audit |
int
|
Number of human-labelled audit items. |
lower_limit, upper_limit |
Range of the estimand, used to clip intervals: |
|
draws |
NDArray[float64] | None
|
Bootstrap draws of the estimate, if the bootstrap was used. |
ci
property
¶
The confidence interval as a tuple.
bias
property
¶
Naive minus corrected estimate: how much the judge distorts the rate.
uses_bootstrap
property
¶
Whether bounds and p-values come from bootstrap draws.
lower_bound(alpha=0.05)
¶
One-sided lower confidence bound at level 1 - alpha.
upper_bound(alpha=0.05)
¶
One-sided upper confidence bound at level 1 - alpha.
pvalue_above(threshold)
¶
P-value for the claim that the true value exceeds threshold.
The null hypothesis is value <= threshold. With bootstrap draws the
p-value is (1 + #{draws <= threshold}) / (B + 1).
pvalue_below(threshold)
¶
P-value for the claim that the true value is below threshold.
to_dict()
¶
Return the result as a dictionary (without bootstrap draws).
summary()
¶
One line summary.
Decisions¶
veracle.decisions.threshold_decision(result, threshold, alpha=0.05, name='claim')
¶
Decide whether a corrected rate is above or below a threshold.
Examples:
>>> from veracle.audit import JudgeAudit
>>> from veracle.correction import correct_rate
>>> audit = JudgeAudit.from_counts(190, 10, 160, 40)
>>> result = correct_rate(audit, n_items=3000, n_pass=2460)
>>> d = threshold_decision(result, threshold=0.8, name="model")
>>> d.naive_verdict, d.verdict
('pass', 'inconclusive')
veracle.decisions.decide_many(results, threshold, alpha=0.05, method='holm')
¶
Make several threshold decisions with multiplicity control.
Typical uses are several models against one release bar, or one model
against several benchmarks. The pass and fail p-values are each
adjusted across all claims.
Examples:
>>> from veracle.audit import JudgeAudit
>>> from veracle.correction import correct_rate
>>> audit = JudgeAudit.from_counts(380, 20, 360, 40)
>>> results = {
... "model_a": correct_rate(audit, n_items=5000, n_pass=4600),
... "model_b": correct_rate(audit, n_items=5000, n_pass=3500),
... }
>>> {k: d.verdict for k, d in decide_many(results, 0.8).items()}
{'model_a': 'pass', 'model_b': 'fail'}
veracle.decisions.adjust_pvalues(pvalues, method='holm')
¶
Adjust p-values for multiple claims.
"holm" and "bonferroni" control the family-wise error rate,
"bh" (Benjamini-Hochberg) the false discovery rate under
independence or positive dependence.
Examples:
>>> [round(p, 3) for p in adjust_pvalues([0.01, 0.04, 0.03], "holm")]
[0.03, 0.06, 0.06]
veracle.decisions.Decision(name, verdict, threshold, estimate, lower_bound, upper_bound, pvalue_pass, pvalue_fail, alpha, naive_verdict)
dataclass
¶
A decision about whether a rate is above a threshold.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Label of the claim, for example the model name. |
verdict |
Verdict
|
|
threshold |
float
|
The threshold :math: |
estimate |
float
|
Corrected estimate. |
lower_bound, upper_bound |
One-sided confidence bounds at level |
|
pvalue_pass, pvalue_fail |
P-values (after any multiplicity adjustment) for the claims
|
|
alpha |
float
|
Error level of each claim. |
naive_verdict |
Literal['pass', 'fail']
|
The verdict one would reach by comparing the uncorrected judge rate with the threshold, ignoring all uncertainty. |
Datasets¶
veracle.datasets.simulate_evaluation(n_items, true_rate, sensitivity, specificity, n_audit=200, audit_design='random', cluster_size=1, icc=0.0, seed=None)
¶
Simulate items, true labels and judge verdicts.
With cluster_size > 1 items come in groups (for example several
answers to the same prompt) whose pass probabilities share a beta
distributed prompt effect with intra-cluster correlation icc.
Examples:
>>> data = simulate_evaluation(1000, 0.7, 0.9, 0.8, n_audit=100, seed=0)
>>> data.judge.shape, data.audit.n
((1000,), 100)
veracle.datasets.JudgedEvaluation(judge, human, clusters, audit, audit_mask, true_rate)
dataclass
¶
A simulated evaluation scored by an imperfect judge.
Attributes:
| Name | Type | Description |
|---|---|---|
judge |
NDArray[int8]
|
Judge verdicts for all evaluated items. |
human |
NDArray[int8]
|
True labels of all items (hidden in practice). |
clusters |
NDArray[int64] | None
|
Prompt identifier of every item, if items are clustered. |
audit |
JudgeAudit
|
A separate human audit of the judge. |
audit_mask |
NDArray[bool_]
|
Items of the evaluation set chosen for a within-set audit (for
:func: |
true_rate |
float
|
Population pass rate used to generate the data. |
human_for_audited
property
¶
Human labels with NaN for items outside audit_mask.