Recalibration¶
reliably.recalibrate.temperature.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.recalibrate.platt.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
reliably.recalibrate.isotonic.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
reliably.recalibrate.beta.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
reliably.recalibrate.histogram.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
reliably.recalibrate.matrix.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
reliably.recalibrate.matrix.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. |