def probability_less_than(y_pdf, bins, y, bin_axis=1): """ Calculate the probability of a sample being less than a given value for a tensor of predicted PDFs. Args: y_pdf: Tensor containing the predicted PDFs. bins: The bin-boundaries corresponding to the predictions. y: The sample value. bin_axis: The index of the tensor axis which contains the predictions for each bin. Return: Tensor with rank reduced by one compared to ``y_pdf`` and with the values along ``bin_axis`` of ``y_pdf`` replaced with the probability that a sample of the distribution is smaller than the given value ``y``. """ if len(y_pdf.shape) == 1: bin_axis = 0 n_y = y_pdf.shape[bin_axis] n_b = len(bins) _check_dimensions(n_y, n_b) xp = get_array_module(y_pdf) n = len(y_pdf.shape) x = 0.5 * (bins[1:] + bins[:-1]) mask = x < y shape = [1] * n shape[bin_axis] = -1 mask = as_type(xp, reshape(xp, mask, shape), y_pdf) return trapz(xp, mask * y_pdf, bins, bin_axis)
def posterior_mean(y_pdf, bins, bin_axis=1): """ Calculate posterior mean from predicted PDFs. Args: y_pdf: Tensor containing the predicted PDFs. bins: The bin-boundaries corresponding to the predictions. bin_axis: The index of the tensor axis which contains the predictions for each bin. Return: Tensor with rank reduced by one compared to ``y_pdf`` and with the values along ``bin_axis`` of ``y_pdf`` replaced with the mean value of the PDF. """ if len(y_pdf.shape) == 1: bin_axis = 0 n_y = y_pdf.shape[bin_axis] n_b = len(bins) _check_dimensions(n_y, n_b) xp = get_array_module(y_pdf) n = len(y_pdf.shape) shape = [1] * n shape[bin_axis] = -1 bins_r = reshape(xp, 0.5 * (bins[1:] + bins[:-1]), shape) return trapz(xp, bins_r * y_pdf, bins, bin_axis)
def crps(y_pdf, y_true, bins, bin_axis=1): r""" Compute the Continuous Ranked Probability Score (CRPS) for a given discrete probability density. 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' Args: y_pred: Tensor containing the predicted discrete posterior PDF with the probabilities for different bins oriented along axis ``bin_axis`` in ``y_pred``. y_true: Array containing the true point values. bins: 1D array containing the bins corresponding to the probabilities in ``y_pred``. Returns: Tensor of rank :math:`k - 1` containing the CRPS values for each of the predictions in ``y_pred``. """ if len(y_pdf.shape) == 1: bin_axis = 0 n_y = y_pdf.shape[bin_axis] n_b = len(bins) n_dims = len(y_pdf.shape) _check_dimensions(n_y, n_b) xp = get_array_module(y_pdf) n = len(y_pdf.shape) y_cdf = posterior_cdf(y_pdf, bins, bin_axis=bin_axis) x = bins shape = [1] * n_dims shape[bin_axis] = -1 x = x.reshape(shape) if len(y_true.shape) < len(y_pdf.shape): y_true = y_true.unsqueeze(bin_axis) i = as_type(xp, x > y_true, y_cdf) crps = trapz(xp, (y_cdf - i) ** 2, x, bin_axis) return crps
def posterior_mean(y_pred, bins, bin_axis=1): if len(y_pred.shape) == 1: bin_axis = 0 n_y = y_pred.shape[bin_axis] n_b = len(bins) _check_dimensions(n_y, n_b) xp = get_array_module(y_pred) n = len(y_pred.shape) shape = [1] * n shape[bin_axis] = -1 bins_r = reshape(xp, 0.5 * (bins[1:] + bins[:-1]), shape) return trapz(xp, bins_r * y_pred, bins, bin_axis)
def probability_less_than(y_pred, bins, y, bin_axis=1): if len(y_pred.shape) == 1: bin_axis = 0 n_y = y_pred.shape[bin_axis] n_b = len(bins) _check_dimensions(n_y, n_b) xp = get_array_module(y_pred) n = len(y_pred.shape) x = 0.5 * (bins[1:] + bins[:-1]) mask = x < y shape = [1] * n shape[bin_axis] = -1 mask = as_type(xp, reshape(xp, mask, shape), y_pred) return trapz(xp, mask * y_pred, bins, bin_axis)
def posterior_mean(y_pred, quantiles, quantile_axis=1): r""" Computes the mean of the posterior distribution defined by an array of predicted quantiles. Args: y_pred: A tensor of predicted quantiles with the quantiles located along the axis given by ``quantile_axis``. quantiles: The quantile fractions corresponding to the quantiles located along the quantile axis. quantile_axis: The axis along which the quantiles are located. Returns: Array containing the posterior means for the provided inputs. """ if len(y_pred.shape) == 1: quantile_axis = 0 xp = get_array_module(y_pred) x_cdf, y_cdf = cdf(y_pred, quantiles, quantile_axis=quantile_axis) return trapz(xp, x_cdf, y_cdf, quantile_axis)
def test_trapz(backend): array = arange(backend, 0, 10.1, 1) result = trapz(backend, array, array, 0) assert result == 50