Top-level utilities for representing predictive distributions in a normalized
container used across test-time tooling, calibration, and conformal code.
Symbol
Description
PredictiveBatch
Frozen dataclass carrying point / mean / std / quantiles (+ quantile_levels) / bar_logits (+ bin_edges) / samples / support (+ density) / extra. .with_density(n_support=200, range_margin=0.05) auto-resolves support and density from bar_logits, quantiles, or samples.
quantiles_to_density_grid
(quantiles, quantile_levels, *, n_support=200, range_margin=0.05) — Convert quantile predictions to a regular density grid on a per-row support. Crossing quantiles are repaired by sorting each row (monotone rearrangement). The grid is the predictive law truncated to [q_0, q_K] and renormalised — see the note below.
bars_to_density_grid
(bar_logits, bin_edges, *, n_support=200, range_margin=0.05) — Convert piecewise-constant bar distributions to a regular density grid.
samples_to_density_grid
(samples, *, n_support=200, range_margin=0.05) — Convert scalar predictive samples to a regular density grid.
Truncation semantics of quantiles_to_density_grid
The tail mass outside the outermost quantiles (τ_0 below q_0 and 1 − τ_K
above q_K) is not represented: the density is zero on the range_margin
margins and the grid is renormalised to integrate to one. The grid therefore
encodes the predictive law truncated to[q_0, q_K] and renormalised, so its
CDF at knot k is (τ_k − τ_0) / (τ_K − τ_0) rather than τ_k, and densities
are inflated by 1 / (τ_K − τ_0). For levels (0.05, 0.5, 0.75) the grid median
sits at the (0.5 − 0.05) / 0.7 ≈ 64-th percentile of the grid. Use wide outer
levels (for example 0.01 and 0.99) when the tails matter. This behaviour is
unchanged pending a maintainer decision;
PredictiveBatch.with_density and the test-time transport code consume this grid
as the predictive density.
(propensity, observed=None, *, clip_min=0.01, clip_max=0.99, normalize=True) — Inverse-propensity weights 1 / e (or t / e + (1 − t) / (1 − e) when observed is given) with clipping and optional mean-one normalisation; the dtype follows float_dtype(propensity).
(*tensors) — Floating dtype that boolean / integer intermediates should adopt: the promoted dtype of the floating tensors among tensors (float64 inputs keep float64), else torch.get_default_dtype(). Never hard-codes float32.
masked_reduction
(tensor, mask, reduction="mean") — mean / sum / max / min / none ignoring mask == False; NaN at masked positions never leaks.
masked_mean
(tensor, mask, dim=None, keepdim=False) — Mean over the True entries of mask; masked NaN / Inf are ignored.
masked_sum
(tensor, mask, dim=None, keepdim=False) — Sum over the True entries of mask; masked NaN / Inf are ignored.
These helpers are internal plumbing. They are not re-exported from
torchregress.utils and are not part of the public API. Import them from their
submodule if needed, for example from torchregress.utils.tensor_ops import convert_to_tensor.
Some have a public equivalent elsewhere: parse_heteroscedastic_output in torchregress.ensemble,
low_rank_output_dim and split_low_rank_gaussian_output in torchregress.losses.
Symbol
Submodule
Description
normal_cdf
utils.distributions
(z) — Standard-normal CDF 0.5 * erfc(-z / √2); keeps full relative precision in the lower tail.
parse_heteroscedastic_output
utils.gaussian_output
(output) — Accepts tuple / dict / concatenated-tensor layouts; returns (mean, log_var). A 2-D [batch, 2 * n_outputs] tensor is split along dim 1; higher-rank tensors are split along the last dim, like split_mean_log_variance.
low_rank_output_dim
utils.gaussian_output
(n_features, rank) — Total output dim of a low-rank Gaussian head (mean, cov_factor, cov_diag).
split_low_rank_gaussian_output
utils.gaussian_output
(y_pred, n_features, rank) — Split a low-rank output into (mean, cov_factor, cov_diag).
labels_to_levels
utils.ordinal
(target, num_classes) — Convert class-index labels to cumulative binary levels 1[y > k]; the dtype follows float_dtype(target).
(x) — Enable autograd on inputs for Jacobian / Hessian computations.
compute_model_gradients
utils.tensor_ops
(y_pred, x, n_features_y, create_graph=None) — Per-sample Jacobian of predictions with respect to inputs.
calculate_gaussian_nll
utils.tensor_ops
(residuals, var, eps=1e-8) — Gaussian NLL per sample. var may match residuals (elementwise diagonal), be a per-sample variance [B] that is broadcast over the feature axis, or be a full covariance [B, D, D].
calculate_propagated_variance
utils.tensor_ops
(grad, sigma_x, sigma_y=None, sigma_xy=None) — Propagate input uncertainty through the Jacobian: J Σₓ Jᵀ (+ Σ_y + cross terms).
check_tensor
utils.validation
(tensor, name="tensor", max_elements=200_000_000) — Validate a tensor (type, size, NaN / Inf) and raise an informative error.
validate_metric_inputs
utils.validation
(y_pred, y_true) — Standard point-metric input checks.
validate_quantile
utils.validation
(q) — Assert 0 ≤ q ≤ 1 and return a tensor; NaN is rejected.
validate_range
utils.validation
(value, min_value, max_value, param_name) — Assert min_value ≤ value ≤ max_value; NaN is rejected.
validate_reduction
utils.validation
(reduction, valid_reductions=None) — Assert mean / sum / none.
validate_sample_weight
utils.validation
(sample_weight, batch_size) — Validate and flatten per-sample metric weights.
validate_weights
utils.validation
(weights, batch_size, allow_none=True, *, flatten=False) — Validate per-sample loss weights.
validate_url
utils.security
(url, allowed_schemes=("http", "https")) — URL scheme allowlist for downloads (used by example loaders).
subsample_rows
utils.numpy_stats
(X, max_rows, *, random_state) — Deterministic row subsampling to at most max_rows rows.
winsorize
utils.numpy_stats
(X, clip_quantile) — Clip values along axis 0 to the lower / upper quantiles.
Offline-friendly OpenML ARFF loading that skips the stale-MD5 check. Numeric,
boolean, and numeric-coded nominal (for example {0,1}) columns are kept as
float32; rows with any non-finite value are dropped.
Symbol
Description
fetch_openml_regression_frame_skip_checksum
(*, data_id, target_column="target", download_timeout=600.0, json_timeout=120.0) — Load (X, y, tag) float32 arrays for an OpenML dataset id without MD5 verification.
fetch_openml_regression
(*, data_id, name, version, target_column) — Resolve a dataset by id or by name / version and load (X, y, tag) with the relaxed ARFF parser.
() — Print versions and device, then run an import check, a tensor-op check, and a one-step training smoke test; exits with status 1 on failure. Import with from torchregress.health import check_health.
importtorchfromtorchregress.utilsimport(BoxCoxTransform,ipw_weights,masked_mean,validate_positive,)# Masked mean ignoring missing targets (NaN at masked positions does not leak)y=torch.tensor([1.0,float("nan"),3.0,4.0])y_pred=torch.tensor([1.5,0.0,2.0,4.5])mask=~torch.isnan(y)loss=masked_mean((y-y_pred)**2,mask)# Box-Cox transform for positive targetsbxcx=BoxCoxTransform(lam=0.5)y_pos=torch.tensor([1.0,2.0,3.0,4.0])y_t=bxcx.forward(y_pos)# transformy_back=bxcx.inverse(y_t)# invert# IPW weights for causal reweightinge=torch.tensor([0.7,0.2,0.6,0.4,0.9])w=ipw_weights(e,clip_min=0.1,clip_max=0.9,normalize=True)validate_positive(0.5,"scale")