def max_abs_normalization(x: np.ndarray) -> np.ndarray: """ max abs normalization formula: y = x / max(|x|) """ factor = x.abs().max() if factor == 0: canceled_to_avoid_div0("max_abs_normalization") return x else: return x / factor
def numpy_normalised_quantile_loss(y: ndarray, y_pred: ndarray, quantile: float) -> float: """Computes normalised quantile loss for numpy arrays. Uses the q-Risk metric as defined in the "Training Procedure" section of the main TFT paper. Args: y: Targets y_pred: Predictions quantile: Quantile to use for loss calculations (between 0 & 1) Returns: Float for normalised quantile loss. """ prediction_underflow: ndarray = y - y_pred weighted_errors: ndarray = quantile * np.maximum(prediction_underflow, 0.) \ + (1. - quantile) * np.maximum(-prediction_underflow, 0.) quantile_loss: float = weighted_errors.mean() normaliser: float = y.abs().mean() return 2 * quantile_loss / normaliser