Skip to content

Use the requested backend for the tail masses of the censored CRPS - #142

Open
gopain185 wants to merge 1 commit into
frazane:mainfrom
gopain185:fix-censored-backend
Open

gopain185 wants to merge 1 commit into
frazane:mainfrom
gopain185:fix-censored-backend

Conversation

@gopain185

Copy link
Copy Markdown

Summary

Fixes #138.

crps_cnormal computed its tail masses with stats._norm_cdf(...) without passing backend, so they used the globally active backend (numpy by default) rather than the requested one. With torch tensors that require grad, this went through numpy and failed. crps_clogistic (_logis_cdf) and crps_ct (_t_cdf) had the same pattern, so this fixes all three.

Change: in each of the three wrappers, convert location, scale, lower, upper (and df for crps_ct) with the requested backend's asarray, and pass backend=backend to both CDF calls, as crps.gtcnormal already does.

crps_cnormal also sets a tail mass to exactly 0 when its bound is infinite. Once the masses really use jax, the float32 erf(+-inf) returns about +-0.9999998, so the masses are not exactly 0, and gtcnormal returns nan because it only handles an infinite bound when the mass is exactly 0. The existing test_crps_cnormal[jax] catches this. main already has the problem when jax is the active backend: sr.crps_cnormal(1.8, 0.4, 1.1) returns nan there. crps_clogistic does not need this, since its CDF gives exactly 0 and 1 at the infinite bounds.

Tests

test_crps_censored_tail_masses_use_backend (torch only) calls crps_cnormal and crps_clogistic with the torch backend requested while numpy stays active, on a location tensor that requires grad, and checks that the gradient reaches it.

Run locally on Windows, Python 3.12, from uv.lock:

  • new test with the old _crps.py: 2 failed, RuntimeError: Can't call numpy() on Tensor that requires grad
  • pytest tests/ with this change, all backends: 606 passed, 37 skipped
  • pre-commit on the changed files: passed

crps_ct is not implemented for jax/torch (its existing test skips them), so its change is only exercised on numpy and numba. The device issue in #139 is separate and not touched here.

This overlaps with the CRPS lines changed in #133. If that lands first, I am happy to rebase or close this.

Checklist

  • Tests pass across all backends: uv sync --all-extras --dev then uv run pytest tests/ (exercises every installed backend: numpy, numba, jax, torch)
  • New/changed numerics touch both the array-API and numba paths where applicable (not applicable: only the public wrappers change, not the kernels)
  • Docs / docstrings updated if public behavior changed (not applicable: no documented behavior changes)
  • A release-note label is applied (bug; I cannot set labels from a fork)

…razane#138)

crps_cnormal, crps_clogistic and crps_ct computed lmass and umass with the
active backend instead of the backend argument, so torch tensors that
require grad (or live on a GPU) went through numpy. Convert the inputs with
the requested backend and pass it to the CDFs, as crps.gtcnormal does.

In crps_cnormal, set a tail mass to exactly 0 when its bound is infinite:
jax's float32 erf stops short of +-1 at +-inf, which made gtcnormal return
nan.
@github-actions github-actions Bot added the core Core scoring API / numerics label Nov 1, 2026
@frazane

frazane commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Thanks! #133 just landed and fixes the backend issue by construction, so this now conflicts. Could you rebase and keep only the jax ±inf mass guard plus a jax test (dropping the jax skip in test_crps_cnormal)?

@frazane

frazane commented Nov 7, 2026

Copy link
Copy Markdown
Owner

@copilot resolve the merge conflicts in this pull request

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core scoring API / numerics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crps_cnormal ignores the backend argument for the tail masses

2 participants