Example #1
0
    def cdf(self, x):
        r"""
        Approximate the posterior CDF for given inputs `x`.

        Propagates the inputs in `x` forward through the network and
        approximates the posterior CDF by a piecewise linear function.

        The piecewise linear function is given by its values at approximate
        quantiles :math:`x_\tau`` for :math:`\tau = \{0.0, \tau_1, \ldots,
        \tau_k, 1.0\}` where :math:`\tau_k` are the quantiles to be estimated
        by the network. The values for :math:`x_{0.0}` and :math:`x_{1.0}` are
        computed using

        .. math::

            x_{0.0} = 2.0 x_{\tau_1} - x_{\tau_2}

            x_{1.0} = 2.0 x_{\tau_k} - x_{\tau_{k-1}}

        Arguments:

            x(np.array): Array of shape `(n, m)` containing `n` inputs for which
                         to predict the conditional quantiles.

        Returns:

            Tuple (xs, fs) containing the :math:`x`-values in `xs` and corresponding
            values of the posterior CDF :math:`F(x)` in `fs`.

        """
        y_pred = self.predict(x)
        return qq.cdf(y_pred, self.quantiles, quantile_axis=1)
Example #2
0
def test_cdf(xp):
    """
    Tests the calculation of the pdf 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)
    x_cdf, y_cdf = cdf(y_pred, quantiles)
    assert np.all(np.isclose(x_cdf[0], -xp.ones_like(x_cdf[0])))
    assert np.all(np.isclose(x_cdf[-1], 11.0 * xp.ones_like(x_cdf[-1])))

    #
    # 2D predictions
    #

    y_pred = eo.repeat(arange(xp, 1.0, 9.1, 1.0), 'q -> w q', w=10)
    x_cdf, y_cdf = cdf(y_pred, quantiles)
    assert np.all(np.isclose(x_cdf[:, 0], -xp.ones_like(x_cdf[:, 0])))
    assert np.all(np.isclose(x_cdf[:, -1], 11.0 * xp.ones_like(x_cdf[:, -1])))

    #
    # 3D predictions, quantiles along last axis
    #

    y_pred = eo.repeat(arange(xp, 1.0, 9.1, 1.0), 'q -> h w q', h=10, w=10)
    x_cdf, y_cdf = cdf(y_pred, quantiles, quantile_axis=-1)
    assert np.all(np.isclose(x_cdf[:, :, 0], -xp.ones_like(x_cdf[:, :, 0])))
    assert np.all(
        np.isclose(x_cdf[:, :, -1], 11.0 * xp.ones_like(x_cdf[:, :, -1])))

    #
    # 3D predictions, quantiles along first axis
    #

    y_pred = eo.repeat(arange(xp, 1.0, 9.1, 1.0), 'q -> h q w', h=10, w=10)
    x_cdf, y_cdf = cdf(y_pred, quantiles, quantile_axis=1)
    assert np.all(np.isclose(x_cdf[:, 0, :], -xp.ones_like(x_cdf[:, 0, :])))
    assert np.all(
        np.isclose(x_cdf[:, -1, :], 11.0 * xp.ones_like(x_cdf[:, -1, :])))
Example #3
0
    def cdf(self, y_pred):
        """
        Calculate CDF from predicted quantiles.

        Args:
            y_pred: Tensor containing the quantiles predicted by the NN
                model.
        """
        module = get_array_module(y_pred)
        quantiles = to_array(module, self.quantiles, like=y_pred)
        return qq.cdf(y_pred,
                      quantiles,
                      quantile_axis=self.quantile_axis)
Example #4
0
 def calculate_cdf(y_pred):
     module = get_array_module(y_pred)
     quantiles = to_array(module, self.quantiles, like=y_pred)
     return qq.cdf(y_pred, quantiles, quantile_axis=self.quantile_axis)