o
    ¶ôjè  ã                   @   s~   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s5dgZG d	d
„ d
e
ƒZdS )é    )ÚAnyÚOptionalÚSequenceÚUnionN)ÚTensor)Ú_crps_update)ÚMetric)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEú%ContinuousRankedProbabilityScore.plotc                       s¾   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d„Zdededdfdd„Zdefdd„Z	ddeeeee f  dee defdd„Z‡  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                    sB   t ƒ jdi |¤Ž | jdt d¡dd� | jdt d¡dd� d S )Nr   é   Úsum)ÚdefaultÚdist_reduce_fxr   © )ÚsuperÚ__init__Ú	add_stateÚtorchÚzeros)Úselfr   ©Ú	__class__r   úo/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torchmetrics/regression/crps.pyr   H   s   z)ContinuousRankedProbabilityScore.__init__ÚpredsÚtargetc                 C   s:   t ||ƒ\}}}|  jt || ¡7  _|  j|7  _dS )z•Update 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_sumr   r   r#   ÚupdateM   s   z'ContinuousRankedProbabilityScore.updatec                 C   s   | j | j S )z;Compute the continuous ranked probability score over state.)r   r   )r    r   r   r#   ÚcomputeY   s   z(ContinuousRankedProbabilityScore.computeÚvalÚaxc                 C   s   |   ||¡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+   r,   r   r   r#   Úplot]   s   (r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   Úfloatr   r   r   r)   r*   r   r   r   r
   r   r.   Ú__classcell__r   r   r!   r#   r      s&   
 "ÿÿÿþ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   r#   Ú<module>   s   