
    i                         d dl mZmZmZmZ d dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ esdgZ G d	 d
e
      Zy)    )AnyOptionalSequenceUnionN)Tensor)_crps_update)Metric)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEz%ContinuousRankedProbabilityScore.plotc                        e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed<   eed<   eed	<   d
eddf fdZdededdfdZdefdZ	 ddeeeee   f      dee   defdZ xZS ) ContinuousRankedProbabilityScorea  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.

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``preds`` (:class:`~torch.Tensor`): Predicted float tensor with shape ``(N,d)``
    - ``target`` (:class:`~torch.Tensor`): Ground truth float tensor with shape ``(N,d)``

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``cosine_similarity`` (:class:`~torch.Tensor`): A float tensor with the cosine similarity

    Args:
        reduction: how to reduce over the batch dimension using 'sum', 'mean' or 'none' (taking the individual scores)
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

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

    Fis_differentiablehigher_is_betterfull_state_updateg        plot_lower_boundscoretotalkwargsreturnNc                     t        |   di | | j                  dt        j                  d      d       | j                  dt        j                  d      d       y )Nr      sum)defaultdist_reduce_fxr    )super__init__	add_statetorchzeros)selfr   	__class__s     q/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/regression/crps.pyr   z)ContinuousRankedProbabilityScore.__init__H   sG    "6"wAuMwAuM    predstargetc                     t        ||      \  }}}| xj                  t        j                  ||z
        z  c_        | xj                  |z  c_        y)zUpdate state with predictions and targets.

        Args:
            preds: Predictions from model
            target: Ground truth values

        N)r   r   r    r   r   )r"   r&   r'   
batch_sizediffensemble_sums         r$   updatez'ContinuousRankedProbabilityScore.updateM   sA     *6eV)D&
D,

eii| 344


j 
r%   c                 4    | j                   | j                  z  S )z;Compute the continuous ranked probability score over state.)r   r   )r"   s    r$   computez(ContinuousRankedProbabilityScore.computeY   s    zzDJJ&&r%   valaxc                 &    | j                  ||      S )aO  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> from torch import randn
            >>> # Example plotting a single value
            >>> from torchmetrics.regression import ContinuousRankedProbabilityScore
            >>> metric = ContinuousRankedProbabilityScore()
            >>> metric.update(randn(10,5), randn(10))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> from torch import randn
            >>> # Example plotting multiple values
            >>> from torchmetrics.regression import ContinuousRankedProbabilityScore
            >>> metric = ContinuousRankedProbabilityScore()
            >>> values = []
            >>> for _ in range(10):
            ...     values.append(metric(randn(10,5), randn(10)))
            >>> fig, ax = metric.plot(values)

        )_plot)r"   r/   r0   s      r$   plotz%ContinuousRankedProbabilityScore.plot]   s    P zz#r""r%   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   floatr   r   r   r,   r.   r   r   r   r   r   r3   __classcell__)r#   s   @r$   r   r      s     D $t#"d"#t#!e!MMN N N

!F 
!F 
!t 
!' '
 _c(#E&(6*:":;<(#IQRZI[(#	(#r%   r   )typingr   r   r   r   r    r   'torchmetrics.functional.regression.crpsr   torchmetrics.metricr	   torchmetrics.utilities.importsr
   torchmetrics.utilities.plotr   r   __doctest_skip__r   r   r%   r$   <module>rB      s9    2 1   @ & @ @?@h#v h#r%   