calpit.utils

Helpers for CDEs on a grid: normalization, integration and the PIT histogram plot.

Functions

normalize(→ numpy.ndarray)

Normalizes conditional density estimates to be non-negative and integrate to one.

_normalize(density, y_grid[, tol, max_iter])

trapz_grid(→ numpy.ndarray)

Does trapezoid integration between the same limits as the grid.

plot_pit(pit_values, ci_level[, n_bins, y_true, ax])

Plots the PIT histogram and the P-P plot of PIT values against a uniform distribution.

Module Contents

normalize(cde_estimates: numpy.ndarray, y_grid: numpy.ndarray, tol: float = 1e-06, max_iter: int = 200) → numpy.ndarray[source]

Normalizes conditional density estimates to be non-negative and integrate to one.

Parameters:
  • cde_estimates (numpy.ndarray) – A numpy array or matrix of conditional density estimates.

  • y_grid (numpy.ndarray) – The array of grid points.

  • tol (float) – The tolerance to accept for abs(area - 1).

  • max_iter (int) – The maximal number of search iterations.

Returns:

The normalized conditional density estimates.

Return type:

numpy.ndarray

_normalize(density, y_grid, tol=1e-06, max_iter=500)[source]
trapz_grid(y: numpy.ndarray, x: numpy.ndarray) → numpy.ndarray[source]

Does trapezoid integration between the same limits as the grid.

Parameters:
  • y (np.ndarray) – The array of values to integrate.

  • x (np.ndarray) – The array of grid points.

Returns:

The integrated values.

Return type:

np.ndarray

plot_pit(pit_values, ci_level, n_bins=30, y_true=None, ax=None, **fig_kw)[source]

Plots the PIT histogram and the P-P plot of PIT values against a uniform distribution.

The histogram is drawn with the band that a uniform distribution would fall in with probability ci_level. When y_true is given, the P-P plot is annotated with the Kolmogorov-Smirnov, Cramer-von Mises and Anderson-Darling statistics.

Parameters:
  • pit_values (np.ndarray) – The PIT values, shape (n_samples,).

  • ci_level (float) – The confidence level of the uniform band, between 0 and 1.

  • n_bins (int, optional) – The number of histogram bins. Defaults to 30.

  • y_true (np.ndarray, optional) – The true values; only its length is used, to scale the Anderson-Darling statistic. Defaults to None, which omits the statistics.

  • ax (sequence of matplotlib.axes.Axes, optional) – Two axes to draw the histogram and the P-P plot on. Defaults to None, which creates a new figure.

  • **fig_kw – Keyword arguments passed to matplotlib.pyplot.subplots when ax is None.

Returns:

The matplotlib figure and the array of two axes, as a tuple (fig, ax).

Return type:

tuple

Raises:

ImportError – If matplotlib is not installed.