calpit.representations

Conditional density estimates (CDEs) on a grid, as quantiles or as samples.

Cal-PIT learns r(alpha; x) = P(PIT <= alpha | x), a monotone map of [0, 1] onto itself. A CDE with CDF F(y | x) is recalibrated by composing it with that map, F_new(y | x) = r(F(y | x); x), and its quantile function Q(tau | x) by composing with the inverse, Q_new(tau | x) = Q(r^-1(tau; x) | x). So every representation needs only two things, which the classes here provide:

  • pit(y_true): the PIT of the true values, to fit Cal-PIT;

  • recalibration_alpha() and recalibrate(pit_cdf): the alpha at which r must be predicted, and the recalibrated CDEs built from those predictions.

The PIT and the recalibrated CDEs are the same quantities whichever representation the CDEs come in; only the interpolation error differs.

Attributes

FloatArray

Classes

ConditionalDensities

A set of CDEs, one per object, that Cal-PIT can fit and recalibrate.

GridCDE

CDEs given as densities on a common grid of y.

QuantileCDE

CDEs given as quantiles at common levels.

SampleCDE

CDEs given as samples from each predictive distribution.

Functions

_interp_rows(→ FloatArray)

Interpolates each row linearly, like np.interp applied row by row.

Module Contents

FloatArray[source]
class ConditionalDensities[source]

Bases: Protocol

A set of CDEs, one per object, that Cal-PIT can fit and recalibrate.

__len__() → int[source]

Returns the number of objects.

pit(y_true: numpy.typing.ArrayLike) → FloatArray[source]

Returns the PIT of the true values, shape (n_objects,).

recalibration_alpha() → FloatArray[source]

Returns the alpha to predict r at, shape (n_alpha,) or (n_objects, n_alpha).

recalibrate(pit_cdf: FloatArray) → Self[source]

Returns the CDEs recalibrated with r predicted at recalibration_alpha().

_interp_rows(x_new: FloatArray, xp: FloatArray, fp: FloatArray) → FloatArray[source]

Interpolates each row linearly, like np.interp applied row by row.

Within a run of equal xp the smallest matching fp is returned, which makes this the generalized inverse when fp is a quantile function. Outside the range of xp the end values of fp are returned.

Parameters:
  • x_new – The points to interpolate at, shape (n_rows, n_new).

  • xp – The non-decreasing abscissae of each row, shape (n_rows, n_points).

  • fp – The values at xp, shape (n_rows, n_points) or (n_points,).

Returns:

The interpolated values, shape (n_rows, n_new).

class GridCDE(pdf: numpy.typing.ArrayLike, y_grid: numpy.typing.ArrayLike, pit_method: str = 'linear')[source]

CDEs given as densities on a common grid of y.

Parameters:
  • pdf – The densities, shape (n_objects, n_grid).

  • y_grid – The increasing grid, shape (n_grid,).

  • pit_method – How pit integrates up to the true value, “linear” or “step” (see calpit.metrics.probability_integral_transform). “step” reproduces calpit before version 0.2.

Variables:
  • pdf – The densities, shape (n_objects, n_grid).

  • y_grid – The grid, shape (n_grid,).

  • pit_method – How pit integrates up to the true value.

pdf[source]
y_grid[source]
pit_method = 'linear'[source]
__len__() → int[source]

Returns the number of objects.

cdf() → FloatArray[source]

Returns the CDFs on the grid by the trapezoid rule, shape (n_objects, n_grid).

pit(y_true: numpy.typing.ArrayLike) → FloatArray[source]

Returns the PIT of the true values.

The density is integrated up to each true value as calpit.metrics.probability_integral_transform does with pit_method.

Parameters:

y_true – The true values, shape (n_objects,).

Returns:

The PIT values, shape (n_objects,).

recalibration_alpha() → FloatArray[source]

Returns the CDFs on the grid, where r is needed, shape (n_objects, n_grid).

recalibrate(pit_cdf: FloatArray) → GridCDE[source]

Builds the recalibrated densities on the same grid.

The recalibrated CDF r(F(y | x); x) on the grid is interpolated with a PCHIP spline and differentiated.

Parameters:

pit_cdf – r predicted at recalibration_alpha(), shape (n_objects, n_grid).

Returns:

The recalibrated CDEs. They can be slightly negative or fail to integrate to one; calpit.utils.normalize fixes both.

class QuantileCDE(levels: numpy.typing.ArrayLike, locations: numpy.typing.ArrayLike)[source]

CDEs given as quantiles at common levels.

The CDF is linearly interpolated between the quantiles. Outside the outermost quantiles it is taken as the outermost level, so include the levels 0 and 1, at the ends of the support, to cover every true value.

Parameters:
  • levels – The increasing quantile levels in [0, 1], shape (n_levels,).

  • locations – The quantiles, non-decreasing along each row, shape (n_objects, n_levels).

Variables:
  • levels – The quantile levels, shape (n_levels,).

  • locations – The quantiles, shape (n_objects, n_levels).

levels[source]
locations[source]
__len__() → int[source]

Returns the number of objects.

pit(y_true: numpy.typing.ArrayLike) → FloatArray[source]

Returns the PIT of the true values.

Parameters:

y_true – The true values, shape (n_objects,).

Returns:

The PIT values, shape (n_objects,).

recalibration_alpha() → FloatArray[source]

Returns the quantile levels, where r is needed, shape (n_levels,).

recalibrate(pit_cdf: FloatArray) → QuantileCDE[source]

Builds the recalibrated quantiles at the same levels.

The recalibrated CDF takes the value r(tau; x) at the old quantile Q(tau | x), so the new quantile at each level is interpolated between the old quantiles.

Parameters:

pit_cdf – r predicted at the levels, non-decreasing along each row, shape (n_objects, n_levels).

Returns:

The recalibrated CDEs. A level outside the range of r(levels; x) gets the outermost old quantile.

class SampleCDE(samples: numpy.typing.ArrayLike, random_state: int | numpy.random.Generator | None = None)[source]

CDEs given as samples from each predictive distribution.

Parameters:
  • samples – The samples, shape (n_objects, n_samples).

  • random_state – The seed or generator of the randomized PIT.

Variables:
  • samples – The samples, shape (n_objects, n_samples).

  • random_state – The seed or generator of the randomized PIT.

samples[source]
random_state = None[source]
__len__() → int[source]

Returns the number of objects.

pit(y_true: numpy.typing.ArrayLike) → FloatArray[source]

Returns the randomized PIT of the true values.

With k of the m samples below y, the PIT is (k + U) / (m + 1) with U uniform on [0, 1], spread over any samples equal to y. It is exactly uniform when y is drawn from the same distribution as the samples, whereas the empirical CDF k / m only takes m + 1 values.

Parameters:

y_true – The true values, shape (n_objects,).

Returns:

The PIT values, shape (n_objects,).

to_quantiles() → QuantileCDE[source]

Returns the sorted samples as quantiles at the levels i / (m + 1), i = 1, …, m.

recalibration_alpha() → FloatArray[source]

Returns the levels of the sorted samples, where r is needed, shape (n_samples,).

recalibrate(pit_cdf: FloatArray) → SampleCDE[source]

Moves each sorted sample to the recalibrated quantile at its level.

Parameters:

pit_cdf – r predicted at recalibration_alpha(), non-decreasing along each row, shape (n_objects, n_samples).

Returns:

The recalibrated CDEs, as sorted samples.