def test_pdf(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_pdf, y_pdf = pdf(y_pred, quantiles) assert np.all(np.isclose(x_pdf[2:-2], arange(xp, 1.5, 8.6, 1.0))) assert np.all(np.isclose(y_pdf[0], xp.zeros_like(y_pdf[0]))) assert np.all(np.isclose(y_pdf[-1], xp.zeros_like(y_pdf[-1]))) assert np.all(np.isclose(y_pdf[2:-2], 0.1 * xp.ones_like(y_pdf[2:-2]))) # # 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=10) x_pdf, y_pdf = pdf(y_pred, quantiles) assert np.all( np.isclose(x_pdf[:, 2:-2], reshape(xp, arange(xp, 1.5, 8.6, 1.0), (1, -1)))) assert np.all(np.isclose(y_pdf[:, 0], xp.zeros_like(y_pdf[:, 0]))) assert np.all(np.isclose(y_pdf[:, -1], xp.zeros_like(y_pdf[:, -1]))) assert np.all( np.isclose(y_pdf[:, 2:-2], 0.1 * xp.ones_like(y_pdf[:, 2:-2]))) # # 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', h=10, w=10) x_pdf, y_pdf = pdf(y_pred, quantiles, quantile_axis=-1) assert np.all( np.isclose(x_pdf[:, :, 2:-2], reshape(xp, arange(xp, 1.5, 8.6, 1.0), (1, 1, -1)))) assert np.all(np.isclose(y_pdf[:, :, 0], xp.zeros_like(y_pdf[:, :, 0]))) assert np.all(np.isclose(y_pdf[:, :, -1], xp.zeros_like(y_pdf[:, :, -1]))) assert np.all( np.isclose(y_pdf[:, :, 2:-2], 0.1 * xp.ones_like(y_pdf[:, :, 2:-2]))) # # 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', h=10, w=10) x_pdf, y_pdf = pdf(y_pred, quantiles, quantile_axis=1) assert np.all( np.isclose(x_pdf[:, 2:-2, :], reshape(xp, arange(xp, 1.5, 8.6, 1.0), (1, -1, 1)))) assert np.all(np.isclose(y_pdf[:, 0, :], xp.zeros_like(y_pdf[:, 0, :]))) assert np.all(np.isclose(y_pdf[:, -1, :], xp.zeros_like(y_pdf[:, -1, :]))) assert np.all( np.isclose(y_pdf[:, 2:-2, :], 0.1 * xp.ones_like(y_pdf[:, 2:-2, :])))
def test_posterior_cdf(xp): """ Test calculation and normalization of posterior cdf. """ # # 1D predictions # bins = arange(xp, 0.0, 10.1, 0.1) y_pred = 0.1 * xp.ones(100) y_cdf = posterior_cdf(y_pred, bins) assert y_cdf[-1] == 1.0 assert y_cdf[0] == 0.0 # # 2D predictions # bins = arange(xp, 0.0, 10.1, 0.1) y_pred = 0.1 * xp.ones(100) y_pred = eo.repeat(y_pred, 'q -> w q', w=10) y_cdf = posterior_cdf(y_pred, bins) assert np.all(np.isclose(y_cdf[:, 0], 0.0)) assert np.all(np.isclose(y_cdf[:, -1], 1.0)) assert y_cdf.shape[0] == 10 assert y_cdf.shape[1] == 101 y_pred = 0.1 * xp.ones(100) y_pred = eo.repeat(y_pred, 'q -> q w', w=10) y_cdf = posterior_cdf(y_pred, bins, bin_axis=0) assert np.all(np.isclose(y_cdf[0, :], 0.0)) assert np.all(np.isclose(y_cdf[-1, :], 1.0)) assert y_cdf.shape[0] == 101 assert y_cdf.shape[1] == 10 # # 3D predictions # bins = arange(xp, 0.0, 10.1, 0.1) y_pred = 0.1 * xp.ones(100) y_pred = eo.repeat(y_pred, 'q -> v w q', v=10, w=10) y_cdf = posterior_cdf(y_pred, bins, bin_axis=-1) assert np.all(np.isclose(y_cdf[:, :, 0], 0.0)) assert np.all(np.isclose(y_cdf[:, :, -1], 1.0)) assert y_cdf.shape[0] == 10 assert y_cdf.shape[-1] == 101 y_pred = 0.1 * xp.ones(100) y_pred = eo.repeat(y_pred, 'q -> q v w', v=10, w=10) y_cdf = posterior_cdf(y_pred, bins, bin_axis=0) assert np.all(np.isclose(y_cdf[0, :, :], 0.0)) assert np.all(np.isclose(y_cdf[-1, :, :], 1.0)) assert y_cdf.shape[0] == 101 assert y_cdf.shape[-1] == 10
def test_tensordot(backend): x = arange(backend, 0, 10.1, 1) y = ones(backend, 11) z = tensordot(backend, x, y, ((0,), (0,))) assert np.isclose(z, 55)
def test_pdf_binned(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) bins = arange(xp, 1.0, 9.01, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins) assert np.all(np.isclose(y_pdf_binned, 0.1, 1e-3)) # Test extrapolation left bins = arange(xp, -2.0, -1.0, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins) assert np.all(np.isclose(y_pdf_binned, 0.0, 1e-3)) # Test extrapolation right bins = arange(xp, 11.1, 12.0, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins) assert np.all(np.isclose(y_pdf_binned, 0.0, 1e-3)) # # 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=10) bins = arange(xp, 1.0, 9.01, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins) assert np.all(np.isclose(y_pdf_binned, 0.1, 1e-3)) # Test extrapolation left bins = arange(xp, -2.0, -1.0, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins) assert np.all(np.isclose(y_pdf_binned, 0.0, 1e-3)) # Test extrapolation right bins = arange(xp, 11.1, 12.0, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins) assert np.all(np.isclose(y_pdf_binned, 0.0, 1e-3)) # # 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', h=10, w=10) bins = arange(xp, 1.0, 9.01, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins, quantile_axis=-1) assert np.all(np.isclose(y_pdf_binned, 0.1, 1e-3)) # Test extrapolation left bins = arange(xp, -2.0, -1.0, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins, quantile_axis=-1) assert np.all(np.isclose(y_pdf_binned, 0.0, 1e-3)) # Test extrapolation right bins = arange(xp, 11.1, 12.0, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins, quantile_axis=-1) assert np.all(np.isclose(y_pdf_binned, 0.0, 1e-3)) # # 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 -> q h w', h=10, w=10) bins = arange(xp, 1.0, 9.01, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins, quantile_axis=0) assert np.all(np.isclose(y_pdf_binned, 0.1, 1e-3)) # Test extrapolation left bins = arange(xp, -2.0, -1.0, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins, quantile_axis=0) assert np.all(np.isclose(y_pdf_binned, 0.0, 1e-3)) # Test extrapolation right bins = arange(xp, 11.1, 12.0, 0.1) y_pdf_binned = pdf_binned(y_pred, quantiles, bins, quantile_axis=0) assert np.all(np.isclose(y_pdf_binned, 0.0, 1e-3))
def test_softmax(backend): array = arange(backend, 0, 10.1, 1) y = softmax(backend, array)
def test_cumsum(backend): array = reshape(backend, arange(backend, 0, 10.1, 1), (11, 1)) result = cumsum(backend, array, 0) assert result[-1, 0] == 55 result = cumsum(backend, array, 1) assert result[-1, 0] == 10
def test_trapz(backend): array = arange(backend, 0, 10.1, 1) result = trapz(backend, array, array, 0) assert result == 50
def test_reshape(backend): array = arange(backend, 0, 10.1, 1) result = reshape(backend, array, (1, 11, 1)) assert result.shape[0] == 1 assert result.shape[1] == 11 assert result.shape[2] == 1
def posterior_quantiles(y_pred, quantiles, new_quantiles, quantile_axis=1): r""" Computes the median of the posterior distribution defined by an array of predicted quantiles. Args: y_pred: A rank-k 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: Rank k-1 tensor containing the posterior median for the provided inputs. """ if len(y_pred.shape) == 1: quantile_axis = 0 xp = get_array_module(y_pred) n = len(y_pred.shape) indices = arange(xp, 0, len(quantiles), 1.0) selection = [slice(0, None)] * n y_qs = [] for q in new_quantiles: mask_l = quantiles <= q mask_r = quantiles > q index_l = indices[mask_l] if len(index_l) == 0: selection[quantile_axis] = 0 selection_l = tuple(selection) y_q = expand_dims(xp, y_pred[selection_l], quantile_axis) y_qs.append(y_q) continue index_r = indices[mask_r] if len(index_r) == 0: selection[quantile_axis] = -1 selection_r = tuple(selection) y_q = expand_dims(xp, y_pred[selection_r], quantile_axis) y_qs.append(y_q) continue index = int(index_l[-1]) d = quantiles[index + 1] - quantiles[index] w_l = (quantiles[index + 1] - q) / d w_r = (q - quantiles[index]) / d selection = [slice(0, None)] * n selection[quantile_axis] = index selection_l = tuple(selection) selection[quantile_axis] = index + 1 selection_r = tuple(selection) y_q = w_l * y_pred[selection_l] + w_r * y_pred[selection_r] y_q = expand_dims(xp, y_q, quantile_axis) y_qs.append(y_q) return concatenate(xp, y_qs, quantile_axis)