neural_lam.models.forecasters.probabilistic
===========================================

.. py:module:: neural_lam.models.forecasters.probabilistic

.. autoapi-nested-parse::

   Forecasters that sample their forecast from a predictive distribution.



Classes
-------

.. autoapisummary::

   neural_lam.models.forecasters.probabilistic.BaseProbabilisticARForecaster
   neural_lam.models.forecasters.probabilistic.BaseProbabilisticForecaster


Module Contents
---------------

.. py:class:: BaseProbabilisticARForecaster(predictor: neural_lam.models.step_predictors.base.StepPredictor, datastore: neural_lam.datastore.BaseDatastore)

   Bases: :py:obj:`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.

   :param predictor: The predictor to use for each AR step. Samples its output, so
                     that each rollout is an independent member.
   :type predictor: StepPredictor
   :param datastore: The datastore providing grid metadata and boundary masks.
   :type datastore: BaseDatastore


   .. py:method:: forward(init_states: torch.Tensor, forcing_features: torch.Tensor, boundary_states: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor | None]

      Unroll one sampled auto-regressive forecast.

      :param init_states: Shape ``(B, 2, num_grid_nodes, num_state_vars)``. The two initial
                          states ``[X_{t-1}, X_t]`` used to start the rollout from.
      :type init_states: torch.Tensor
      :param forcing_features: Shape ``(B, pred_steps, num_grid_nodes, num_forcing_vars)``.
                               Forcing features for each predicted step; ``pred_steps`` defines
                               the rollout length.
      :type forcing_features: torch.Tensor
      :param boundary_states: Shape ``(B, pred_steps, num_grid_nodes, num_state_vars)``. True
                              state values used ONLY to overwrite boundary nodes at each AR
                              step.
      :type boundary_states: torch.Tensor

      :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``.



   .. py:attribute:: predictor


   .. py:property:: predicts_std
      :type: bool


      Whether the forecaster predicts standard deviation.

      :returns: ``True`` if the wrapped predictor predicts standard deviation,
                ``False`` otherwise.
      :rtype: bool


.. py:class:: BaseProbabilisticForecaster(datastore: neural_lam.datastore.BaseDatastore)

   Bases: :py:obj:`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.

   :param datastore: The datastore this forecaster is built for, providing grid
                     metadata, boundary masks and standardization statistics.
   :type datastore: BaseDatastore


   .. py:method:: sample_ensemble(init_states: torch.Tensor, forcing_features: torch.Tensor, boundary_states: torch.Tensor, num_members: int) -> tuple[torch.Tensor, torch.Tensor | None]

      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.

      :param init_states: 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.
      :type init_states: torch.Tensor
      :param forcing_features: 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).
      :type forcing_features: torch.Tensor
      :param boundary_states: 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.
      :type boundary_states: torch.Tensor
      :param num_members: Number of ensemble members ``S`` to sample.
      :type num_members: int

      :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.



