Welcome to calpit’s documentation!

Overview

calpit diagnoses and recalibrates conditional density estimates (CDEs). Given a calibration set of features x, true values y and the CDEs some model predicted for them, it learns how the probability integral transform (PIT) of those CDEs varies with x. That gives local P-P plots that show where in feature space the CDEs are miscalibrated, and a recalibration of the CDEs themselves.

  • Any representation. CDEs can come as densities on a grid, as quantiles, as samples, or as qp ensembles, and are recalibrated in the same representation.

  • Any model. Cal-PIT can be learned by any PyTorch network, trained with PyTorch Lightning, or by any scikit-learn classifier.

  • The scikit-learn API. CalPIT has fit, predict, transform, get_params and set_params, and works with sklearn.base.clone.

  • Your own training loop. Every piece of the training is importable, for models too unusual for CalPIT.fit.

Basic Usage

import calpit

# CDEs of the calibration set, here as densities on a grid.
cde_calib = calpit.GridCDE(pdf_calib, y_grid)

# Learn the conditional PIT distribution with the default monotone network.
recalibrator = calpit.CalPIT().fit(x_calib, y_calib, cde_calib)

# Local P-P curves of the test CDEs, and how far they stray from the diagonal.
local = recalibrator.diagnose(x_test)
print(local.ks)

# Recalibrated test CDEs, in the same representation.
cde_new = recalibrator.transform(x_test, calpit.GridCDE(pdf_test, y_grid))

See User guide for quantile, sample and qp CDEs, scikit-learn models and the options, and Writing your own training loop for training a network by hand.

Installation

>> pip install 'calpit[torch]'

The core of calpit needs only NumPy and SciPy. The models and file formats come with extras:

  • torch installs PyTorch, Lightning and spline-basis, for PyTorch models and the default calpit.nn.IsplineNN.

  • sklearn installs scikit-learn, for scikit-learn models.

  • qp installs qp-prob, for calpit.qp_io.

  • hdf5 installs h5py, for calpit.nn.PhotometryDataset.

  • plot installs matplotlib, for calpit.utils.plot_pit and calpit.diagnostics.plot_local_pp.

  • all installs all of the above.

To install the latest version from GitHub, or for development:

>> pip install 'calpit[torch] @ git+https://github.com/lee-group-cmu/Cal-PIT'
>> git clone https://github.com/lee-group-cmu/Cal-PIT.git
>> cd Cal-PIT
>> pip install -e '.[dev]'

Note

calpit needs Python 3.11 or later. To use a GPU, install the PyTorch build for your system first, following the PyTorch website.

References

The calpit package is based on the work described in following papers:

  • Dey et al. [2021] and Dey et al. [2022], which introduce the recalibration framework for conditional density estimates.

  • Zhao et al. [2021], which introduces diagnostics for conditional density estimation methods.

It also uses:

  • the monotone rearrangement of Chernozhukov, Fernández-Val & Galichon (2010), “Quantile and Probability Curves Without Crossing”, Econometrica 78, 1093, arXiv:0704.3649, doi:10.3982/ECTA7880;

  • the unconstrained monotonic neural network of Wehenkel and Louppe [2021], for calpit.nn.MonotonicNN.

[1]

Biprateep Dey, Jeffrey A. Newman, Brett H. Andrews, Rafael Izbicki, Ann B. Lee, David Zhao, Markus Michael Rau, and Alex I. Malz. Re-calibrating Photometric Redshift Probability Distributions Using Feature-space Regression. arXiv e-prints, pages arXiv:2110.15209, October 2021. arXiv:2110.15209, doi:10.48550/arXiv.2110.15209.

[2]

Biprateep Dey, David Zhao, Jeffrey A. Newman, Brett H. Andrews, Rafael Izbicki, and Ann B. Lee. Conditionally Calibrated Predictive Distributions by Probability-Probability Map: Application to Galaxy Redshift Estimation and Probabilistic Forecasting. arXiv e-prints, pages arXiv:2205.14568, May 2022. arXiv:2205.14568, doi:10.48550/arXiv.2205.14568.

[3]

David Zhao, Niccolò Dalmasso, Rafael Izbicki, and Ann B. Lee. Diagnostics for Conditional Density Models and Bayesian Inference Algorithms. arXiv e-prints, pages arXiv:2102.10473, February 2021. arXiv:2102.10473, doi:10.48550/arXiv.2102.10473.

[4]

Antoine Wehenkel and Gilles Louppe. Unconstrained monotonic neural networks. 2021. URL: https://arxiv.org/abs/1908.05164, arXiv:1908.05164.