feat(metrics): add spread_squared ensemble variance metric - #431
feat(metrics): add spread_squared ensemble variance metric#431Sir-Sloth-The-Lazy wants to merge 1 commit into
Conversation
Implements spread_squared following the existing metric API contract: - Signature: (pred, target, pred_std, mask, average_grid, sum_vars) - Accepts (..., S, N, d_state) ensemble predictions with S at dim=-3 - Uses Bessel's correction (S/(S-1)) for unbiased sample variance - Calls mask_and_reduce_metric internally, consistent with mse/mae/nll - Registered in DEFINED_METRICS as "spread_squared" - Asserts S > 1 (single-member variance is undefined) Adds tests/test_metrics.py with 9 tests covering mathematical correctness, Bessel's correction, S=1 guard, shape contract, mask filtering, and consistency with torch.var(unbiased=True).
|
@sadamov @joeloskarsson if this makes any good addition to #226 , I would like to keep contributing to that PR |
|
Thanks for summarizing the design choices so clearly. The One thing I am curious about: do you expect I am asking because it seems closely related to the per-metric aggregation / post-processing semantics discussed in #343 / #344, and I want to understand which level you want that distinction to live at. |
|
I intentionally kept spread_squared as the raw variance (no sqrt) to match the convention in the prob_model_lam branch and in WeatherBench 2, where spread-skill analysis compares spread_squared directly against MSE (both in squared units). The sqrt would only be needed for display purposes (e.g. logging as "spread" in physical units alongside RMSE). |
|
The spread_squared as primitive with sqrt deferred to post-processing is the right call - mirrors the MSE/RMSE pattern and keeps things composable with the class-based refactor in #343/#344. One system-level consideration worth raising: spread_squared and CRPS (being worked on in #226) will need to interoperate for spread-skill analysis - SSR requires both in consistent units and with aligned reduction paths. The ens_dim=-3 fixed convention here vs the explicit ens_dim parameter approach in #226 is a small divergence worth resolving before both land, otherwise downstream calibration metrics will need to special-case each one. I've been thinking about this coordination across the evaluation layer as part of a broader probabilistic evaluation system , happy to help align with the two PRs so the full pipeline from CRPS to spread-skill works consistently end to end. |
Describe your changes
Add
spread_squared(ensemble variance) metric to the metrics module.This metric computes the unbiased sample variance across ensemble members, following the same API pattern as all existing metrics:
(pred, target, pred_std, mask, average_grid, sum_vars). Entry-wise variance is computed first across the ensemble dimension (dim=-3), then passed throughmask_and_reduce_metricfor grid masking and optional reduction.Key design decisions:
mse,mae,nll, etc.targetandpred_stdare accepted but unused, maintaining the uniform interface expected byget_metric()callers inar_model.py.ens_dim = -3: Ensemble members are at the third-from-last dim, consistent with the (..., S, N,d_state) convention used throughout the metrics module. T from the (B, T, N, F) deterministic
shape ([RFC/Design] Standardize probabilistic vs deterministic return contract to unblock evaluation integrations #335) is a batch dim folded into (...) before calling metrics to match how
ar_model.pyalready calls all other metrics in practice.
assert S > 1: Single-member variance is undefined asdiscussed in feat/add-ensemble-metrics-crps-spread #226 by @kshirajahere and @Panchadip-128
mask_and_reduce_metriccall: Follows the same pattern as every other metric in the file.This metric is a prerequisite for spread-skill analysis of ensemble and probabilistic models (Graph-EFM, Diffusion-LAM) tracked in #62.
Dependencies: None
Issue Link
Addresses parts of #62
Related discussion: #226, #335
Type of change
Checklist before requesting a review
pullwith--rebaseoption if possible).Checklist for reviewers
Each PR comes with its own improvements and flaws. The reviewer should check the following:
Author checklist after completed review
reflecting type of change (add section where missing):
Checklist for assignee