
    i              	       |    d dl mZ d dlZd dlmZ d dlmZ dededeeeef   fdZd	ed
ededefdZdededefdZ	y)    )TupleN)Tensor)_check_same_shapepredstargetreturnc                    t        | dddf   |       | j                  \  }}|dk  rt        d| j                   d      t        j                  | d      d   } |j                  d      j                  |       }t        j                  t        j                  | |z
        d      |z  }t        j                  | j                  d      | j                  d      z
        }t        j                  |d      d|z  |z  z  }|||fS )	aC  Compute intermediate CRPS values before aggregation.

    Args:
        preds: Tensor of shape (batch_size, ensemble_members)
        target: Tensor of shape (batch_size,)

    Returns:
        batch_size: int
        diff: Tensor (batch-wise absolute error term)
        ensemble_sum: Tensor (pairwise ensemble term)

    Nr      z<CRPS requires at least 2 ensemble members, but you provided .   )dim)r   r
   )	r   shape
ValueErrortorchsort	unsqueeze	expand_assumabs)r   r   
batch_sizen_ensemble_membersobservation_inflateddiffensemble_diffsensemble_sums           |/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/regression/crps.py_crps_updater      s     eAqDk6*%*[["J"AWX]XcXcWddefgg JJu!$Q'E "++A.88? 99UYYu';;<!DGYYD YYuq1EOOA4FFGN99^8A@R<RUg<ghLt\))    r   r   r   c                 2    t        j                  ||z
        S )zFinal CRPS computation.)r   mean)r   r   r   s      r   _crps_computer!   ;   s    ::d\)**r   c                 <    t        | |      \  }}}t        |||      S )a2  Computes continuous ranked probability score.

    .. math::
        CRPS(F, y) = \int_{-\infty}^{\infty} (F(x) - 1_{x \geq y})^2 dx

    where :math:`F` is the predicted cumulative distribution function and :math:`y` is the true target. The metric is
    usually used to evaluate probabilistic regression models, such as forecasting models. A lower CRPS indicates a
    better forecast, meaning that forecasted probabilities are closer to the true observed values. CRPS can also be
    seen as a generalization of the brier score for non binary classification problems.

    Args:
        preds: a 2d tensor of shape (batch_size, ensemble_members) with predictions. The second dimension represents
            the ensemble members.
        target: a 1d tensor of shape (batch_size) with the target values.

    Return:
        Tensor with CRPS

    Raises:
        ValueError:
            If the number of ensemble members is less than 2.
        ValueError:
            If the first dimension of preds and target do not match.

    Example::
        >>> from torchmetrics.functional.regression import continuous_ranked_probability_score
        >>> from torch import randn
        >>> preds = randn(10, 5)
        >>> target = randn(10)
        >>> continuous_ranked_probability_score(preds, target)
        tensor(0.7731)

    )r   r!   )r   r   r   r   r   s        r   #continuous_ranked_probability_scorer#   @   s'    D &2%%@"JlT<88r   )
typingr   r   r   torchmetrics.utilities.checksr   intr   r!   r#    r   r   <module>r(      su       ;!* !* !*5ff9L3M !*H+c + +v +& +
#9v #9v #9& #9r   