calpit.coverage
Building blocks of Cal-PIT that do not depend on a machine-learning library.
Cal-PIT learns the conditional CDF of the probability integral transform (PIT), r(alpha; x) = P(PIT <= alpha | x), by regressing the indicator PIT <= alpha on the coverage level alpha and the features x. The functions here split the calibration data, build the (alpha, x) training rows for estimators that are fitted once on a fixed data set, and make predictions of r non-decreasing in alpha by monotone rearrangement.
Attributes
Functions
|
Splits calibration objects at random into a training and a validation set. |
|
Draws coverage levels that cover [0, 1] evenly for every object. |
|
Builds the rows that a classifier of PIT <= alpha is trained on. |
|
Makes predictions of the PIT CDF non-decreasing in alpha by sorting them. |
Module Contents
- train_val_split(x: numpy.typing.ArrayLike, pit: numpy.typing.ArrayLike, val_fraction: float, random_state: SeedLike = None) tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray][source]
Splits calibration objects at random into a training and a validation set.
The split is by object, so the many (alpha, x) rows that one object yields all fall on the same side of it.
- Parameters:
x – The features, shape (n_objects, n_features).
pit – The PIT values, shape (n_objects,).
val_fraction – The fraction of objects in the validation set, in [0, 1).
random_state – The seed or generator of the permutation.
- Returns:
The training features, training PIT values, validation features and validation PIT values, as a tuple (x_train, pit_train, x_val, pit_val).
- stratified_alpha(n_objects: int, n_alpha: int, random_state: SeedLike = None) FloatArray[source]
Draws coverage levels that cover [0, 1] evenly for every object.
[0, 1] is cut into n_alpha equal strata and each object gets one uniform draw in each stratum. This covers [0, 1] as evenly as a fixed grid without every object sharing the same alpha values.
- Parameters:
n_objects – The number of objects.
n_alpha – The number of coverage levels per object.
random_state – The seed or generator of the draws.
- Returns:
The coverage levels, shape (n_objects, n_alpha), increasing along each row.
- expand_coverage(x: numpy.typing.ArrayLike, pit: numpy.typing.ArrayLike, alpha: numpy.typing.ArrayLike) tuple[FloatArray, numpy.ndarray][source]
Builds the rows that a classifier of PIT <= alpha is trained on.
Each object yields one row per coverage level: the features are alpha followed by x, and the target is whether the object’s PIT is at most alpha. The rows of one object are consecutive.
- Parameters:
x – The features, shape (n_objects, n_features).
pit – The PIT values, shape (n_objects,).
alpha – The coverage levels, shape (n_alpha,) to use the same levels for every object, or (n_objects, n_alpha).
- Returns:
The features [alpha, x], shape (n_objects * n_alpha, n_features + 1), and the boolean targets, shape (n_objects * n_alpha,), as a tuple (features, targets).
- rearrange(pit_cdf: numpy.typing.ArrayLike, alpha: numpy.typing.ArrayLike) FloatArray[source]
Makes predictions of the PIT CDF non-decreasing in alpha by sorting them.
This is the monotone rearrangement of Chernozhukov, Fernandez-Val and Galichon (2010), https://arxiv.org/abs/0704.3649: the values predicted at a set of alpha are sorted and handed out in increasing order of alpha, which makes any estimate of r non-decreasing in alpha. If the true r is non-decreasing, the rearranged estimate is never further from it in Lp distance (here over the points evaluated, with equal weight) than the original estimate was (their Proposition 4). Predictions that are already non-decreasing come back unchanged.
- Parameters:
pit_cdf – The predicted PIT CDF, shape (n_objects, n_alpha).
alpha – The coverage levels it was predicted at, shape (n_alpha,) or (n_objects, n_alpha).
- Returns:
The rearranged PIT CDF, shape (n_objects, n_alpha).