credit.losses.kcrps
===================

.. py:module:: credit.losses.kcrps


Classes
-------

.. autoapisummary::

   credit.losses.kcrps.KCRPSLoss


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

.. py:class:: KCRPSLoss(reduction, biased: bool = False)

   Bases: :py:obj:`torch.nn.Module`


   Adapted from Nvidia Modulus.

   Estimate the CRPS from a finite ensemble

   Computes the local Continuous Ranked Probability Score (CRPS) by using
   the kernel version of CRPS. The cost is O(m log m).

   Creates a map of CRPS and does not accumulate over lat/lon regions.
   Approximates:

   .. math::
       CRPS(X, y) = E[X - y] - 0.5 E[X-X']

   with

   .. math::
       sum_i=1^m |X_i - y| / m - 1/(2m^2) sum_i,j=1^m |x_i - x_j|

   :param pred: Tensor containing the ensemble predictions. The ensemble dimension
                is assumed to be the leading dimension
   :type pred: Tensor
   :param obs: Tensor or array containing an observation over which the CRPS is computed
               with respect to.
   :type obs: Union[Tensor, np.ndarray]
   :param biased:
                  When False, uses the unbiased estimators described in (Zamo and Naveau, 2018)::
                      
                      E|X-y|/m - 1/(2m(m-1)) sum_(i,j=1)|x_i - x_j|
                      
                      Unlike ``crps`` this is fair for finite ensembles. Non-fair ``crps`` favors less
                      dispersive ensembles since it is biased high by E|X- X'|/ m where m is the
                      ensemble size.
   :type biased: bool


   .. py:attribute:: biased
      :value: False



   .. py:attribute:: batched_forward


   .. py:method:: forward(target, pred)


   .. py:method:: single_sample_forward(target, pred)

      Forward pass for KCRPS loss for a single sample.

      :param target: Target tensor.
      :type target: torch.Tensor
      :param pred: Predicted tensor.
      :type pred: torch.Tensor

      :returns: CRPS loss values at each lat/lon
      :rtype: torch.Tensor



   .. py:method:: _kernel_crps_implementation(pred: torch.Tensor, obs: torch.Tensor, biased: bool) -> torch.Tensor

      An O(m log m) implementation of the kernel CRPS formulas



