def test_scatter_add(backend): x = zeros(backend, (3, 3)) y = ones(backend, (2, 3)) indices = to_array(backend, [0, 2]) z = scatter_add(backend, x, indices, y, 0) assert np.isclose(z[0, 0], 1.0) assert np.isclose(z[1, 0], 0.0) assert np.isclose(z[2, 0], 1.0) x = zeros(backend, (3, 3)) y = ones(backend, (3, 2)) z = scatter_add(backend, x, indices, y, 1) assert np.isclose(z[0, 0], 1.0) assert np.isclose(z[0, 1], 0.0) assert np.isclose(z[0, 2], 1.0)
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 crps(y_pred, y_true, quantiles, quantile_axis=1): r""" Compute the Continuous Ranked Probability Score (CRPS) for given predicted quantiles. 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 quantiles along the axis specified by ``quantile_axis``. y_true: Array containing the true point values. quantiles: 1D array containing the quantile fractions corresponding corresponding to the predicted quantiles. Returns: Tensor of rank :math:`k - 1` containing the CRPS values for each of the predictions 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_cdf, y_cdf = cdf(y_pred, quantiles, quantile_axis=quantile_axis) y_true_shape = list(x_cdf.shape) y_true_shape[quantile_axis] = 1 y_true = to_array(xp, y_true) y_true = reshape(xp, y_true, y_true_shape) mask = as_type(xp, x_cdf > y_true, y_pred) ind = ones(xp, x_cdf.shape, like=y_pred) * mask output_shape = list(x_cdf.shape) del output_shape[quantile_axis] integral = zeros(xp, output_shape, like=y_pred) x_index = [slice(0, None)] * n_dims y_l = y_cdf[0] x_index[quantile_axis] = 0 x_l = x_cdf[tuple(x_index)] ind_l = ind[tuple(x_index)] for i in range(1, len(y_cdf)): y_r = y_cdf[i] x_index[quantile_axis] = i x_r = x_cdf[tuple(x_index)] ind_r = ind[tuple(x_index)] result = (ind_l - y_l) ** 2 result += (ind_r - y_r) ** 2 dx = x_r - x_l result *= 0.5 * dx integral += result y_l = y_r x_l = x_r ind_l = ind_r return integral
def test_zeros(backend): x = ones(backend, (1, 1)) assert x[0, 0] == 1.0
def cdf(y_pred, quantiles, quantile_axis=1): """ Calculates the cumulative distribution function (CDF) from predicted quantiles. Args: y_pred: Array containing a range of predicted quantiles. The array is expected to contain the quantiles along the axis given by ``quantile_axis.`` quantiles: Array containing quantile fraction corresponding to the the predicted quantiles. quantile_axis: The index of the axis f the ``y_pred`` array, along which the quantiles are found. Returns: Tuple ``(x_cdf, y_cdf)`` of x and corresponding y-values of the CDF corresponding to quantiles given by ``y_pred``. Raises: InvalidArrayTypeException: When the data is provided neither as numpy array nor as torch tensor. InvalidDimensionException: When the provided predicted quantiles do not match the provided number of quantiles. """ if len(y_pred.shape) == 1: quantile_axis = 0 if y_pred.shape[quantile_axis] != len(quantiles): raise InvalidDimensionException( "Dimensions of the provided array 'y_pred' do not match the" "provided number of quantiles." ) output_shape = list(y_pred.shape) xp = get_array_module(y_pred) y_cdf = quantiles y_cdf = concatenate( xp, [zeros(xp, 1, like=y_cdf), y_cdf, ones(xp, 1, like=y_cdf)], 0 ) 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) dx = y_pred[selection_r] - y_pred[selection_c] dx /= quantiles[1] - quantiles[0] x_cdf_l = y_pred[selection_c] - 2.0 * quantiles[0] * dx 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) dx = y_pred[selection_c] - y_pred[selection_l] dx /= quantiles[-1] - quantiles[-2] x_cdf_r = y_pred[selection_c] + 2.0 * (1.0 - quantiles[-1]) * dx 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) return x_cdf, y_cdf