neural_lam.models.forecasters.probabilistic#

Forecasters that sample their forecast from a predictive distribution.

Classes#

BaseProbabilisticARForecaster

Probabilistic forecaster sampling each forecast auto-regressively.

BaseProbabilisticForecaster

Forecaster whose forward samples from a predictive distribution.

Module Contents#

class neural_lam.models.forecasters.probabilistic.BaseProbabilisticARForecaster(predictor: neural_lam.models.step_predictors.base.StepPredictor, datastore: neural_lam.datastore.BaseDatastore)[source]#

Bases: BaseProbabilisticForecaster

Probabilistic forecaster sampling each forecast auto-regressively.

Each call to the wrapped predictor draws a fresh sample of the next state, so one unrolling is one member.

It supplies no training objective, and so remains abstract in compute_training_loss. Concrete subclasses define an objective appropriate to how they are meant to be trained (e.g. an ensemble scoring rule such as CRPS, or a variational objective), along with whatever configuration that objective needs.

Initialize the BaseProbabilisticARForecaster.

Parameters:
  • predictor (StepPredictor) – The predictor to use for each AR step. Samples its output, so that each rollout is an independent member.

  • datastore (BaseDatastore) – The datastore providing grid metadata and boundary masks.

forward(init_states: torch.Tensor, forcing_features: torch.Tensor, boundary_states: torch.Tensor) → tuple[torch.Tensor, torch.Tensor | None][source]#

Unroll one sampled auto-regressive forecast.

Parameters:
  • init_states (torch.Tensor) – Shape (B, 2, num_grid_nodes, num_state_vars). The two initial states [X_{t-1}, X_t] used to start the rollout from.

  • forcing_features (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_forcing_vars). Forcing features for each predicted step; pred_steps defines the rollout length.

  • boundary_states (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_state_vars). True state values used ONLY to overwrite boundary nodes at each AR step.

Returns:

  • prediction (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_state_vars). One sampled trajectory. Dims: same as boundary_states.

  • pred_std (torch.Tensor or None) – Shape (B, pred_steps, num_grid_nodes, num_state_vars) when the wrapped predictor outputs an std, otherwise None. Dims: same as prediction.

predictor#
property predicts_std: bool#

Whether the forecaster predicts standard deviation.

Returns:

True if the wrapped predictor predicts standard deviation, False otherwise.

Return type:

bool

class neural_lam.models.forecasters.probabilistic.BaseProbabilisticForecaster(datastore: neural_lam.datastore.BaseDatastore)[source]#

Bases: neural_lam.models.forecasters.base.BaseForecaster

Forecaster whose forward samples from a predictive distribution.

forward keeps the signature every forecaster shares and returns one forecast, drawn afresh on each call. An ensemble is therefore not a different kind of output but repeated sampling, which is all sample_ensemble does. How a single sample is produced (auto-regressive sampling, diffusion, …) is left to subclasses.

When forward returns a pred_std, it is that one sample’s own predicted std, not a std describing the spread across members. Stacked by sample_ensemble, the predictive distribution is then a mixture of S Gaussians, one per member: p(x) = mean_s N(x; ensemble[:, s], per_member_std[:, s]**2), not a single Gaussian. In particular, the variance of that mixture is not the average of the per-member variances: it also includes the spread between the member means.

Initialize the forecaster.

Parameters:

datastore (BaseDatastore) – The datastore this forecaster is built for, providing grid metadata, boundary masks and standardization statistics.

sample_ensemble(init_states: torch.Tensor, forcing_features: torch.Tensor, boundary_states: torch.Tensor, num_members: int) → tuple[torch.Tensor, torch.Tensor | None][source]#

Sample an ensemble of forecasts.

Calls forward num_members times, each drawing fresh randomness, and stacks the results along a new ensemble dimension after the batch dimension.

Members are drawn sequentially, one full forecast at a time, so cost grows linearly with num_members. They are independent given the inputs, so this is only an implementation choice: a subclass whose sampling batches cheaply can override this to fold the member dimension into the batch dimension, at proportionally higher peak memory.

Parameters:
  • init_states (torch.Tensor) – Shape (B, 2, num_grid_nodes, num_state_vars). The two initial states [X_{t-1}, X_t] used to start each member from. Dims: B is batch size, 2 is the time index ([X_{t-1}, X_t]), num_grid_nodes is the number of spatial nodes, and num_state_vars is the state feature dimension.

  • forcing_features (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_forcing_vars). External forcings provided at each predicted step. Dims: B is batch size, pred_steps is the forecast length, num_grid_nodes is the number of spatial nodes, and num_forcing_vars is the forcing feature dimension (already concatenated past/current/future windows).

  • boundary_states (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_state_vars). True state values used only to overwrite boundary nodes at each predicted step, identically in every member. Dims: same as one member.

  • num_members (int) – Number of ensemble members S to sample.

Returns:

  • ensemble (torch.Tensor) – Shape (B, S, pred_steps, num_grid_nodes, num_state_vars). The sampled forecasts, stacked along the ensemble dimension S.

  • per_member_std (torch.Tensor or None) – Shape (B, S, pred_steps, num_grid_nodes, num_state_vars). Each member’s own predicted std (see the class docstring for why the ensemble is then a mixture, not this averaged with the others), when the forecaster predicts an std, otherwise None. Dims: same as ensemble.

Raises:

ValueError – If num_members is less than 1.