def test_cumtrapz(backend): y = reshape(backend, arange(backend, 0, 10.1, 1), (11, 1)) x = arange(backend, 0, 10.1, 1) result = cumtrapz(backend, y, x, 0) assert result[0, 0] == 0.0 assert result[-1, 0] == 50.0 result = cumtrapz(backend, y, 2.0 * x, 0) assert result[0, 0] == 0.0 assert result[-1, 0] == 100.0
def posterior_cdf(y_pdf, bins, bin_axis=1): """ Calculate CDF from predicted probability density function. 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 the same shape as ``y_pdf`` but with the values transformed to represent the CDF corresponding to the predicted PDF in ``y_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) y_cdf = cumtrapz(xp, y_pdf, bins, bin_axis) selection = [slice(0, None)] * n selection[bin_axis] = slice(-1, None) y_cdf = y_cdf / y_cdf[tuple(selection)] return y_cdf
def posterior_cdf(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) y_cdf = cumtrapz(xp, y_pred, bins, bin_axis) selection = [slice(0, None)] * n selection[bin_axis] = slice(-1, None) y_cdf = y_cdf / y_cdf[tuple(selection)] return y_cdf
def correct_a_priori(y_pred, quantiles, r, quantile_axis=1): """ Correct predicted quantiles for a priori. Args: y_pred: Rank-k tensor containing the predicted quantiles along the axis given by 'quantile_axis'. quantiles: Rank-1 tensor containing the quantile fractions that correspond to the predicted quantiles. r: A priori density ratio to use to correct the observations. quantile_axis: The axis along which the quantile are oriented in 'y_pred'. """ if len(y_pred.shape) == 1: quantile_axis = 0 xp = get_array_module(y_pred) n_dims = len(y_pred.shape) x_pdf, y_pdf = pdf(y_pred, quantiles, quantile_axis=quantile_axis) selection = [slice(0, None)] * len(y_pred.shape) selection_c = copy(selection) selection_c[quantile_axis] = 0 selection_c = tuple(selection_c) selection_r = copy(selection) selection_r[quantile_axis] = 1 selection_r = tuple(selection_r) dy = y_pred[selection_r] - y_pred[selection_c] dy /= quantiles[1] - quantiles[0] x_cdf_l = y_pred[selection_c] - 2.0 * quantiles[0] * dy x_cdf_l = expand_dims(xp, x_cdf_l, quantile_axis) selection_l = copy(selection) selection_l[quantile_axis] = -2 selection_l = tuple(selection_l) selection_c = copy(selection) selection_c[quantile_axis] = -1 selection_c = tuple(selection_c) dy = y_pred[selection_c] - y_pred[selection_l] dy /= quantiles[-1] - quantiles[-2] x_cdf_r = y_pred[selection_c] + 2.0 * (1.0 - quantiles[-1]) * dy x_cdf_r = expand_dims(xp, x_cdf_r, quantile_axis) x_cdf = concatenate(xp, [x_cdf_l, y_pred, x_cdf_r], quantile_axis) selection_l = [slice(0, None)] * n_dims selection_l[quantile_axis] = slice(0, -1) selection_l = tuple(selection_l) selection_r = [slice(0, None)] * n_dims selection_r[quantile_axis] = slice(1, None) selection_r = tuple(selection_r) x_index = [slice(0, None)] * n_dims x_index[quantile_axis] = 0 y_pdf_new = r(x_pdf, dist_axis=quantile_axis) * y_pdf selection = [slice(0, None)] * n_dims selection[quantile_axis] = slice(1, -1) selection = tuple(selection) y_cdf_new = cumtrapz(xp, y_pdf_new[selection], x_cdf, quantile_axis) selection = [slice(0, None)] * n_dims selection[quantile_axis] = slice(-1, None) selection = tuple(selection) y_cdf_new = y_cdf_new / y_cdf_new[selection] x_cdf_l = x_cdf[selection_l] x_cdf_r = x_cdf[selection_r] y_cdf_new_l = y_cdf_new[selection_l] y_cdf_new_r = y_cdf_new[selection_r] y_pred_new = [] for i in range(0, len(quantiles)): q = quantiles[i] mask = as_type(xp, (y_cdf_new_l < q) * (y_cdf_new_r >= q), x_cdf_l) y_new = x_cdf_l * (y_cdf_new_r - q) * mask y_new += x_cdf_r * (q - y_cdf_new_l) * mask y_new /= mask * (y_cdf_new_r - y_cdf_new_l) + (1.0 - mask) y_new = expand_dims(xp, y_new.sum(quantile_axis), quantile_axis) y_pred_new.append(y_new) y_pred_new = concatenate(xp, y_pred_new, quantile_axis) return y_pred_new