def calculate_mae_and_mape(predictions):
    """
    Calculate the mean absolute error (MAE) and the median
    absolute percentage error (MAPE).
    """
    predicted = predictions["predicted"]
    actual = predictions["actual"]

    absolute_errors = (predicted - actual).abs()
    mean_absolute_error = absolute_errors.mean()

    # The percentage error is undefined when the actual measurement is 0.
    percentage_error = absolute_errors / actual
    percentage_error = percentage_error[actual != 0]

    # Take the absolute value after dividing to have all positive metrics
    mean_absolute_percentage_error = 100 * percentage_error.abs().median()

    return mean_absolute_error, mean_absolute_percentage_error