Top-level API¶
reliably.evaluate(y_true, y_prob, *, task='auto', metrics='default', binning='adaptive', n_bins=15, ci='bca', n_bootstrap=2000, level=0.95, seed=0)
¶
Evaluate a probabilistic model and return a calibration report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
array - like
|
Integer labels, shape |
required |
y_prob
|
array - like
|
Probability matrix |
required |
task
|
str
|
|
'auto'
|
metrics
|
str | list[str]
|
|
'default'
|
binning
|
str
|
|
'adaptive'
|
n_bins
|
int
|
Number of calibration bins. |
15
|
ci
|
str | None
|
CI method: |
'bca'
|
n_bootstrap
|
int
|
Bootstrap resamples. |
2000
|
level
|
float
|
Nominal CI coverage (default 0.95). |
0.95
|
seed
|
int
|
RNG seed for reproducibility. |
0
|
Returns:
| Type | Description |
|---|---|
Report
|
Immutable report with all requested metrics and CIs. |
Examples:
>>> import numpy as np
>>> import reliably as rb
>>> rng = np.random.default_rng(0)
>>> y = rng.integers(0, 2, 300)
>>> p = rng.uniform(0, 1, 300)
>>> report = rb.evaluate(y, p, ci=None)
>>> "smECE" in report.metrics
True
Source code in src/reliably/api.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
reliably.compare(report_or_inputs_a, report_or_inputs_b, *, metric='auroc', test='auto', correction='holm', level=0.95, seed=0, y_true=None)
¶
Compare two models on a shared metric with a significance test.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report_or_inputs_a
|
Report | array - like
|
Either a :class: |
required |
report_or_inputs_b
|
Report | array - like
|
Same as above for the second model. |
required |
metric
|
str
|
Metric name to compare (default |
'auroc'
|
test
|
str
|
|
'auto'
|
correction
|
str | None
|
Multiple-comparison correction ( |
'holm'
|
level
|
float
|
Nominal CI level. |
0.95
|
seed
|
int
|
RNG seed. |
0
|
y_true
|
array - like | None
|
True labels; required if inputs are raw arrays (not Reports). |
None
|
Returns:
| Type | Description |
|---|---|
ComparisonResult
|
|
Examples:
>>> import numpy as np
>>> import reliably as rb
>>> rng = np.random.default_rng(0)
>>> y = rng.integers(0, 2, 300)
>>> p_a = rng.uniform(0, 1, 300)
>>> p_b = rng.uniform(0, 1, 300)
>>> r_a = rb.evaluate(y, p_a, ci=None)
>>> r_b = rb.evaluate(y, p_b, ci=None)
>>> cr = rb.compare(r_a, r_b, y_true=y)
>>> 0.0 <= cr.p_value <= 1.0
True
Source code in src/reliably/api.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
reliably.recalibrate
¶
Recalibration methods: temperature, platt, isotonic, beta, histogram, matrix.
Calibrator
¶
Bases: ABC
Abstract calibrator: fit on a calibration split, transform test scores.
Subclasses implement :meth:fit and :meth:transform.
Examples:
See concrete subclasses in the recalibrate sub-package.
Source code in src/reliably/recalibrate/base.py
fit(y_prob, y_true)
abstractmethod
¶
Fit the calibrator on a calibration split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Predicted probabilities. |
required |
y_true
|
array - like
|
True labels. |
required |
Returns:
| Type | Description |
|---|---|
Calibrator
|
|
Source code in src/reliably/recalibrate/base.py
transform(y_prob)
abstractmethod
¶
Apply calibration to new predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Predicted probabilities. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Calibrated probabilities. |
Source code in src/reliably/recalibrate/base.py
BetaCalibrator
¶
Bases: Calibrator
Beta calibration: logit(p_cal) = c + a·log(s) − b·log(1 − s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constrain_ab
|
bool
|
If |
True
|
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> y = rng.integers(0, 2, 300)
>>> s = rng.uniform(0.05, 0.95, 300)
>>> cal = BetaCalibrator().fit(s, y)
>>> probs = cal.transform(s)
>>> probs.shape == s.shape
True
Source code in src/reliably/recalibrate/beta.py
fit(y_prob, y_true)
¶
Fit beta calibration on calibration split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Binary scores. |
required |
y_true
|
array - like
|
Binary labels. |
required |
Returns:
| Type | Description |
|---|---|
BetaCalibrator
|
|
Source code in src/reliably/recalibrate/beta.py
transform(y_prob)
¶
Apply beta calibration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Binary scores. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Calibrated probabilities. |
Source code in src/reliably/recalibrate/beta.py
HistogramCalibrator
¶
Bases: Calibrator
Replace each bin's score with its empirical accuracy on the calibration split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_bins
|
int
|
Number of histogram bins. |
15
|
binning
|
str
|
|
'adaptive'
|
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> y = rng.integers(0, 2, 300)
>>> s = rng.uniform(0, 1, 300)
>>> cal = HistogramCalibrator().fit(s, y)
>>> probs = cal.transform(s)
>>> probs.shape == s.shape
True
Source code in src/reliably/recalibrate/histogram.py
fit(y_prob, y_true)
¶
Fit histogram binning on calibration split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Scores. |
required |
y_true
|
array - like
|
Binary labels. |
required |
Returns:
| Type | Description |
|---|---|
HistogramCalibrator
|
|
Source code in src/reliably/recalibrate/histogram.py
transform(y_prob)
¶
Apply histogram calibration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Scores. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Calibrated probabilities. |
Source code in src/reliably/recalibrate/histogram.py
IsotonicCalibrator
¶
Bases: Calibrator
Nonparametric monotone calibration via isotonic regression.
Wraps sklearn.isotonic.IsotonicRegression and requires the
scikit-learn optional dependency.
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> y = rng.integers(0, 2, 300)
>>> s = rng.uniform(0, 1, 300)
>>> cal = IsotonicCalibrator().fit(s, y)
>>> probs = cal.transform(s)
>>> probs.shape == s.shape
True
Source code in src/reliably/recalibrate/isotonic.py
fit(y_prob, y_true)
¶
Fit isotonic regression on calibration split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Scores, shape |
required |
y_true
|
array - like
|
Binary labels. |
required |
Returns:
| Type | Description |
|---|---|
IsotonicCalibrator
|
|
Source code in src/reliably/recalibrate/isotonic.py
transform(y_prob)
¶
Apply isotonic calibration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Scores. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Calibrated probabilities. |
Source code in src/reliably/recalibrate/isotonic.py
MatrixScaler
¶
Bases: Calibrator
Full K×K affine map on logits: p_cal = softmax(W·logits + b).
More expressive; gate behind method="matrix".
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> y = rng.integers(0, 3, 300)
>>> p = rng.dirichlet([1, 1, 1], 300)
>>> cal = MatrixScaler().fit(p, y)
>>> probs = cal.transform(p)
>>> np.allclose(probs.sum(axis=1), 1.0, atol=1e-6)
True
Source code in src/reliably/recalibrate/matrix.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
fit(y_prob, y_true)
¶
Fit full matrix scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Probabilities |
required |
y_true
|
array - like
|
Integer labels. |
required |
Returns:
| Type | Description |
|---|---|
MatrixScaler
|
|
Source code in src/reliably/recalibrate/matrix.py
transform(y_prob)
¶
Apply matrix scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Probabilities. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Calibrated probabilities. |
Source code in src/reliably/recalibrate/matrix.py
VectorScaler
¶
Bases: Calibrator
Per-class temperature scaling: p_cal = softmax(w ⊙ logits + b).
More expressive than scalar temperature but less prone to overfitting than full matrix scaling.
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> y = rng.integers(0, 3, 300)
>>> p = rng.dirichlet([1, 1, 1], 300)
>>> cal = VectorScaler().fit(p, y)
>>> probs = cal.transform(p)
>>> np.allclose(probs.sum(axis=1), 1.0, atol=1e-6)
True
Source code in src/reliably/recalibrate/matrix.py
fit(y_prob, y_true)
¶
Fit per-class vector scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Probabilities |
required |
y_true
|
array - like
|
Integer labels. |
required |
Returns:
| Type | Description |
|---|---|
VectorScaler
|
|
Source code in src/reliably/recalibrate/matrix.py
transform(y_prob)
¶
Apply vector scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Probabilities. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Calibrated probabilities. |
Source code in src/reliably/recalibrate/matrix.py
PlattScaler
¶
Bases: Calibrator
Binary calibration via logistic regression: p_cal = σ(A·s + B).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
None
|
|
required |
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> y = rng.integers(0, 2, 300)
>>> s = rng.uniform(0, 1, 300)
>>> cal = PlattScaler().fit(s, y)
>>> probs = cal.transform(s)
>>> probs.shape == s.shape
True
Source code in src/reliably/recalibrate/platt.py
fit(y_prob, y_true)
¶
Fit logistic regression on calibration split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Binary scores, shape |
required |
y_true
|
array - like
|
Binary labels. |
required |
Returns:
| Type | Description |
|---|---|
PlattScaler
|
|
Source code in src/reliably/recalibrate/platt.py
transform(y_prob)
¶
Apply Platt scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Binary scores. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Calibrated probabilities. |
Source code in src/reliably/recalibrate/platt.py
TemperatureScaler
¶
Bases: Calibrator
Calibrate by dividing logits by a scalar temperature > 0.
Fits temperature by minimizing NLL on the calibration split using golden-section search. Preserves the argmax (accuracy unchanged).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temp_bounds
|
tuple[float, float]
|
Search bounds for temperature. |
(0.01, 20.0)
|
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> y_true = rng.integers(0, 2, 200)
>>> y_prob = rng.dirichlet([1, 1], 200)
>>> cal = TemperatureScaler().fit(y_prob, y_true)
>>> cal.T_ > 0
True
>>> cal_probs = cal.transform(y_prob)
>>> np.allclose(cal_probs.sum(axis=1), 1.0, atol=1e-6)
True
Source code in src/reliably/recalibrate/temperature.py
fit(y_prob, y_true)
¶
Fit temperature on calibration data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Probabilities or logits, shape |
required |
y_true
|
array - like
|
Integer labels. |
required |
Returns:
| Type | Description |
|---|---|
TemperatureScaler
|
|
Source code in src/reliably/recalibrate/temperature.py
transform(y_prob)
¶
Apply temperature scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob
|
array - like
|
Probabilities to calibrate. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Calibrated probabilities. |
Source code in src/reliably/recalibrate/temperature.py
reliably.CI
dataclass
¶
Confidence interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
float
|
Lower bound. |
required |
high
|
float
|
Upper bound. |
required |
level
|
float
|
Nominal coverage, default 0.95. |
0.95
|
method
|
str
|
One of |
'bca'
|
Examples:
Source code in src/reliably/_core/results.py
reliably.MetricResult
dataclass
¶
Metric point estimate with optional confidence interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Human-readable metric name, e.g. |
required |
value
|
float
|
Point estimate. |
required |
ci
|
CI | None
|
Confidence interval; |
required |
n
|
int
|
Sample size on which the metric was computed. |
required |
extra
|
Mapping[str, float] | None
|
Optional extra scalars, e.g. Brier decomposition components. |
None
|
Examples:
Source code in src/reliably/_core/results.py
reliably.Report
dataclass
¶
Immutable result of :func:reliably.evaluate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
str
|
One of |
required |
metrics
|
Mapping[str, MetricResult]
|
All computed metrics, keyed by name. |
required |
n
|
int
|
Dataset size. |
required |
meta
|
Mapping[str, object]
|
Provenance: seed, n_bootstrap, binning, etc. |
required |
Examples:
Source code in src/reliably/_core/results.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
summary()
¶
Return a plain-text summary of all metrics.
to_html(path=None)
¶
Render the report to HTML.
Requires the report extra (pip install reliably[report]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path | None
|
If given, also write the HTML to this file. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
HTML string. |
Examples:
>>> r = Report(task="binary", metrics={}, n=100, meta={})
>>> html = r.to_html()
>>> "<html" in html
True
Source code in src/reliably/_core/results.py
to_markdown()
¶
Render the report to a Markdown table.
Returns:
| Type | Description |
|---|---|
str
|
Markdown string. |
Examples:
Source code in src/reliably/_core/results.py
reliability_diagram(y_true, y_prob, *, n_bins=15, binning='adaptive', band=True, n_bootstrap=200, seed=0, ax=None, title='Reliability Diagram')
¶
Plot a reliability diagram for this report's data.
Requires the viz extra (pip install reliably[viz]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
array - like
|
Integer labels. |
required |
y_prob
|
array - like
|
Predicted probabilities. |
required |
n_bins
|
int
|
Number of bins for the scatter overlay. |
15
|
binning
|
str
|
|
'adaptive'
|
band
|
bool
|
Whether to draw the bootstrap confidence band. |
True
|
n_bootstrap
|
int
|
Bootstrap resamples for the confidence band. |
200
|
seed
|
int
|
RNG seed. |
0
|
ax
|
Axes | None
|
Existing axes to draw on; creates a new figure if |
None
|
title
|
str
|
Plot title. |
'Reliability Diagram'
|
Returns:
| Type | Description |
|---|---|
Axes
|
|
Source code in src/reliably/_core/results.py
reliably.ComparisonResult
dataclass
¶
Result of :func:reliably.compare.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric
|
str
|
Name of the compared metric. |
required |
delta
|
float
|
Point estimate of the difference (value_a - value_b). |
required |
ci
|
CI
|
Confidence interval on the difference. |
required |
p_value
|
float
|
Two-sided p-value. |
required |
test
|
str
|
Test used: |
required |
significant
|
bool
|
|
required |
correction
|
str | None
|
Multiple-comparison correction applied (e.g. |
required |
Examples:
>>> cr = ComparisonResult(
... metric="auroc", delta=0.02, ci=CI(-0.01, 0.05),
... p_value=0.19, test="delong", significant=False, correction="holm"
... )
>>> cr.significant
False