trulens.benchmark.score_distribution¶
trulens.benchmark.score_distribution
¶
Score distribution diagnostics for feedback functions.
A feedback function can post a reasonable mean error yet still be useless if it returns nearly the same score for every input. A judge that scores everything 0.4-0.6 cannot rank examples no matter how good its average looks.
ScoreDistributionAnalyzer
runs a feedback function over a golden set and reports how well the resulting
scores discriminate: the score histogram, a calibration curve against the
expected scores, discrimination metrics (standard deviation, unique score count,
normalized entropy) and the predicted spread per expected-score bucket. It also
flags common judge pathologies such as poor discrimination, leniency bias and
binary (bimodal) scoring. It complements the scalar metrics in
GroundTruthAggregator (mae, brier, ece) by diagnosing the shape of a judge's
output rather than a single error number.
Example
from trulens.benchmark.score_distribution import ScoreDistributionAnalyzer
from trulens.providers.openai import OpenAI
analyzer = ScoreDistributionAnalyzer(
feedback_fn=OpenAI().relevance,
golden_set=my_golden_set,
)
report = analyzer.run()
report.print_summary()
report.plot() # matplotlib histogram + calibration curve
Classes¶
ScoreDistributionReport
¶
Diagnostics computed from predicted (and optional expected) scores.
| PARAMETER | DESCRIPTION |
|---|---|
predicted
|
Scores produced by the feedback function, each in
|
expected
|
Optional ground-truth scores aligned with |
num_bins
|
Number of histogram/calibration bins over
TYPE:
|
Functions¶
histogram
¶
Return the score histogram as (low, high, count) per bin.
calibration_curve
¶
Mean expected score per predicted-score bin.
| RETURNS | DESCRIPTION |
|---|---|
List[Tuple[float, float, int]]
|
|
List[Tuple[float, float, int]]
|
well-calibrated judge has |
List[Tuple[float, float, int]]
|
|
bucket_spread
¶
Predicted-score mean/std grouped by expected-score bucket.
Buckets are low [0, 1/3), medium [1/3, 2/3) and
high [2/3, 1], showing whether the judge separates examples
that humans scored low, medium and high. Empty when no expected scores.
print_summary
¶
print_summary() -> None
Print a text summary of the score distribution and any flags.
plot
¶
plot()
Plot the score histogram and calibration curve with matplotlib.
| RETURNS | DESCRIPTION |
|---|---|
|
The created |
| RAISES | DESCRIPTION |
|---|---|
ImportError
|
If |
ScoreDistributionAnalyzer
¶
Run a feedback function over a golden set and analyze its scores.
| PARAMETER | DESCRIPTION |
|---|---|
feedback_fn
|
The feedback function to evaluate. Called once per golden
example and expected to return a score in |
golden_set
|
A list of dicts, each with |
num_bins
|
Number of histogram/calibration bins over
TYPE:
|
args_fn
|
Optional function mapping a golden row to the positional
arguments passed to
TYPE:
|
Functions¶
run
¶
run() -> ScoreDistributionReport
Run the feedback function over the golden set.
| RETURNS | DESCRIPTION |
|---|---|
ScoreDistributionReport
|
A populated |
ScoreDistributionReport
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the feedback function produced no scores. |