calpit.metrics

Losses, PIT values and distances between CDFs for assessing CDEs on a grid.

Attributes

PIT_METHODS

Functions

cde_loss(→ tuple)

Calculates conditional density estimation loss on holdout data.

kolmogorov_smirnov_statistic(→ numpy.ndarray)

Calculate the Kolmogorov-Smirnov statistic between two cumulative distribution functions (CDFs).

cramer_von_mises_statistic(→ numpy.ndarray)

Calculates the Cramer-von Mises statistic between two cumulative distribution functions (CDFs).

anderson_darling_statistic(→ numpy.ndarray)

Calculates the Anderson-Darling statistic between two cumulative distribution functions (CDFs).

probability_integral_transform(→ numpy.ndarray)

Calculates the Probability Integral Transform (PIT) based on Conditional Density Estimates (CDE).

Module Contents

cde_loss(cde_estimates: numpy.ndarray, y_grid: numpy.ndarray, y_test: numpy.ndarray) → tuple[source]

Calculates conditional density estimation loss on holdout data.

Parameters:
  • cde_estimates (numpy.array) – An array where each row is a density estimate on y_grid.

  • y_grid (numpy.array) – An array of the grid points at which cde_estimates is evaluated.

  • y_test (numpy.array) – An array of the true y values corresponding to the rows of cde_estimates.

Returns:

A tuple containing the loss and the standard error of the loss.

Return type:

tuple

Raises:

ValueError – If the dimensions of the input tensors are not compatible.

kolmogorov_smirnov_statistic(cdf_test: numpy.ndarray, cdf_ref: numpy.ndarray) → numpy.ndarray[source]

Calculate the Kolmogorov-Smirnov statistic between two cumulative distribution functions (CDFs).

Parameters:
  • cdf_test (np.ndarray) – CDF of the test distribution.

  • cdf_ref (np.ndarray) – CDF of the reference distribution on the same grid.

Returns:

The Kolmogorov-Smirnov statistic.

Return type:

np.ndarray

cramer_von_mises_statistic(cdf_test: numpy.ndarray, cdf_ref: numpy.ndarray) → numpy.ndarray[source]

Calculates the Cramer-von Mises statistic between two cumulative distribution functions (CDFs).

Parameters:
  • cdf_test (np.ndarray) – CDF of the test distribution.

  • cdf_ref (np.ndarray) – CDF of the reference distribution on the same grid.

Returns:

The Cramer-von Mises statistic.

Return type:

np.ndarray

anderson_darling_statistic(cdf_test: numpy.ndarray, cdf_ref: numpy.ndarray, n_tot: int = 1) → numpy.ndarray[source]

Calculates the Anderson-Darling statistic between two cumulative distribution functions (CDFs).

Parameters:
  • cdf_test (np.ndarray) – CDF of the test distribution (1D array).

  • cdf_ref (np.ndarray) – CDF of the reference distribution on the same grid (1D array).

  • n_tot (int) – Scaling factor equal to the number of PDFs used to construct ECDF.

Returns:

The Anderson-Darling statistic.

Return type:

np.ndarray

PIT_METHODS = ('linear', 'step')[source]
probability_integral_transform(cde: numpy.ndarray, y_grid: numpy.ndarray, y_test: numpy.ndarray, method: str = 'linear') → numpy.ndarray[source]

Calculates the Probability Integral Transform (PIT) based on Conditional Density Estimates (CDE).

The CDF is the trapezoid-rule integral of the density, which treats the density as linear between grid points. With method “linear” (the default) the PIT integrates that piecewise linear density exactly up to each true value, so its error falls with the square of the grid spacing. Method “step” stops at the last grid point at or below the true value, as calpit did before version 0.2; it is biased low by up to the density times the grid spacing, and is kept to reproduce earlier results. Either way the PIT is 0 below the grid and the integral over the whole grid above it.

Parameters:
  • cde (np.ndarray) – A numpy array of conditional density estimates. Each row corresponds to an observation, each column corresponds to a grid point.

  • y_grid (np.ndarray) – A numpy array of the grid points at which cde is evaluated.

  • y_test (np.ndarray) – A numpy array of the true y values corresponding to the rows of cde.

  • method (str, optional) – “linear” or “step”, as described above. Defaults to “linear”.

Returns:

A numpy array of PIT values.

Return type:

np.ndarray

Raises:

ValueError – If the number of samples in cde is not the same as in y_test, or if the number of grid points in cde is not the same as in y_grid.