Example #1
0
 def calculate_crps(y_pred):
     module = get_array_module(y_pred)
     quantiles = to_array(module, self.quantiles, like=y_pred)
     return qq.crps(y_pred,
                    y_true,
                    quantiles,
                    quantile_axis=self.quantile_axis)
Example #2
0
    def crps(y_pred, y_true, quantiles):
        r"""
        Compute the Continuous Ranked Probability Score (CRPS) for given quantile
        predictions.

        This function uses a piece-wise linear fit to the approximate posterior
        CDF obtained from the predicted quantiles in :code:`y_pred` to
        approximate the continuous ranked probability score (CRPS):

        .. math::
            CRPS(\mathbf{y}, x) = \int_{-\infty}^\infty (F_{x | \mathbf{y}}(x')
            - \mathrm{1}_{x < x'})^2 \: dx'

        Arguments:

            y_pred(numpy.array): Array of shape `(n, k)` containing the `k`
                                 estimated quantiles for each of the `n`
                                 predictions.

            y_test(numpy.array): Array containing the `n` true values, i.e.
                                 samples of the true conditional distribution
                                 estimated by the QRNN.

            quantiles: 1D array containing the `k` quantile fractions :math:`\tau`
                       that correspond to the columns in `y_pred`.

        Returns:

            `n`-element array containing the CRPS values for each of the
            predictions in `y_pred`.
        """
        y_pred = self.predict(x)
        return qq.crps(y_pred, self.quantiles, y_true, quantile_axis=1)
Example #3
0
def test_crps(xp):
    """
    Tests the calculation of the CRPS for different shapes of input
    arrays.
    """

    #
    # 1D predictions
    #

    quantiles = arange(xp, 0.1, 0.91, 0.1)
    y_pred = arange(xp, 1.0, 9.1, 1.0)
    scores = crps(y_pred, 4.9, quantiles,)
    assert np.all(np.isclose(scores, 0.86 * xp.ones_like(scores)))

    #
    # 2D predictions
    #

    quantiles = arange(xp, 0.1, 0.91, 0.1)
    y_pred = eo.repeat(arange(xp, 1.0, 9.1, 1.0), 'q -> w q', w=2)
    y_true = 4.9 * xp.ones(2)
    scores = crps(y_pred, y_true, quantiles)
    assert np.all(np.isclose(scores, 0.86 * xp.ones_like(scores)))

    ##
    ## 3D predictions, quantiles along last axis
    ##

    quantiles = arange(xp, 0.1, 0.91, 0.1)
    y_pred = eo.repeat(arange(xp, 1.0, 9.1, 1.0), 'q -> h w q', w=10, h=10)
    y_true = 4.9 * xp.ones((10, 10))
    scores = crps(y_pred, y_true, quantiles, quantile_axis=2)
    assert np.all(np.isclose(scores, 0.86 * xp.ones_like(scores)))

    ##
    ## 3D predictions, quantiles along first axis
    ##

    quantiles = arange(xp, 0.1, 0.91, 0.1)
    y_pred = eo.repeat(arange(xp, 1.0, 9.1, 1.0), 'q -> h q w', w=10, h=10)
    y_true = 4.9 * xp.ones((10, 10))
    scores = crps(y_pred, y_true, quantiles, quantile_axis=1)
    assert np.all(np.isclose(scores, 0.86 * xp.ones_like(scores)))
Example #4
0
    def crps(self, y_pred, y_true):
        """
        Calculate the CRPS score from predicted quantiles.

        Args:
            y_pred: Tensor containing the quantiles predicted by the NN
                model.
            y_true: Tensor containing the true values.
        """
        module = get_array_module(y_pred)
        quantiles = to_array(module, self.quantiles, like=y_pred)
        return qq.crps(
            y_pred, y_true, quantiles, quantile_axis=self.quantile_axis
        )