module utilsforecast.losses

Loss functions for model evaluation.

Global Variables

  • pl

function mae

mae(
    df: DataFrame,
    models: List[str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Mean Absolute Error (MAE) MAE measures the relative prediction accuracy of a forecasting method by calculating the deviation of the prediction and the true value at a given time and averages these devations over the length of the series. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actual values and predictions.
  • models (list of str): Columns that identify the models predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function mse

mse(
    df: DataFrame,
    models: List[str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Mean Squared Error (MSE) MSE measures the relative prediction accuracy of a forecasting method by calculating the squared deviation of the prediction and the true value at a given time, and averages these devations over the length of the series. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actual values and predictions.
  • models (list of str): Columns that identify the models predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function rmse

rmse(
    df: DataFrame,
    models: List[str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Root Mean Squared Error (RMSE) RMSE measures the relative prediction accuracy of a forecasting method by calculating the squared deviation of the prediction and the observed value at a given time and averages these devations over the length of the series. Finally the RMSE will be in the same scale as the original time series so its comparison with other series is possible only if they share a common scale. RMSE has a direct connection to the L2 norm. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actual values and predictions.
  • models (list of str): Columns that identify the models predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function bias

bias(
    df: DataFrame,
    models: List[str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Forecast estimator bias. Defined as prediction - actual Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actual values and predictions.
  • models (list of str): Columns that identify the models predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function cfe

cfe(
    df: DataFrame,
    models: List[str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Cumulative Forecast Error (CFE) Total signed forecast error per series. Positive values mean under forecast; negative mean over forecast. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actual values and predictions.
  • models (list of str): Columns that identify the models predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function pis

pis(
    df: DataFrame,
    models: List[str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Compute the raw Absolute Periods In Stock (PIS) for one or multiple models. The PIS metric sums the absolute forecast errors per series without any scaling, yielding a scale-dependent measure of bias. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actual values and predictions.
  • models (list of str): Columns that identify the models predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function spis

spis(
    df: DataFrame,
    df_train: DataFrame,
    models: List[str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Compute the scaled Absolute Periods In Stock (sAPIS) for one or multiple models. The sPIS metric scales the sum of absolute forecast errors by the mean in-sample demand, yielding a scale-independent bias measure that can be aggregated across series. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actual values and predictions.
  • models (list of str): Columns that identify the models predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function mape

mape(
    df: DataFrame,
    models: List[str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Mean Absolute Percentage Error (MAPE) MAPE measures the relative prediction accuracy of a forecasting method by calculating the percentual deviation of the prediction and the observed value at a given time and averages these devations over the length of the series. The closer to zero an observed value is, the higher penalty MAPE loss assigns to the corresponding error. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actual values and predictions.
  • models (list of str): Columns that identify the models predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function smape

smape(
    df: DataFrame,
    models: List[str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Symmetric Mean Absolute Percentage Error (SMAPE) SMAPE measures the relative prediction accuracy of a forecasting method by calculating the relative deviation of the prediction and the observed value scaled by the sum of the absolute values for the prediction and observed value at a given time, then averages these devations over the length of the series. This allows the SMAPE to have bounds between 0% and 100% which is desirable compared to normal MAPE that may be undetermined when the target is zero. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actual values and predictions.
  • models (list of str): Columns that identify the models predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function mase

mase(
    df: DataFrame,
    models: List[str],
    seasonality: int,
    train_df: DataFrame,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Mean Absolute Scaled Error (MASE) MASE measures the relative prediction accuracy of a forecasting method by comparinng the mean absolute errors of the prediction and the observed value against the mean absolute errors of the seasonal naive model. The MASE partially composed the Overall Weighted Average (OWA), used in the M4 Competition. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actuals and predictions.
  • models (list of str): Columns that identify the models predictions.
  • seasonality (int): Main frequency of the time series; Hourly 24, Daily 7, Weekly 52, Monthly 12, Quarterly 4, Yearly 1.
  • train_df (pandas or polars DataFrame): Training dataframe with id and actual values. Must be sorted by time.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.
References:
  • [1] https: //robjhyndman.com/papers/mase.pdf

function rmae

rmae(
    df: DataFrame,
    models: List[str],
    baseline: str,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Relative Mean Absolute Error (RMAE) Calculates the RAME between two sets of forecasts (from two different forecasting methods). A number smaller than one implies that the forecast in the numerator is better than the forecast in the denominator. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, times, actuals and predictions.
  • models (list of str): Columns that identify the models predictions.
  • baseline (str): Column that identifies the baseline model predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function nd

nd(
    df: DataFrame,
    models: List[str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Normalized Deviation (ND) ND measures the relative prediction accuracy of a forecasting method by calculating the sum of the absolute deviation of the prediction and the true value at a given time and dividing it by the sum of the absolute value of the ground truth. Args:
  • df: Input dataframe with id, times, actuals and predictions.
  • models: Columns that identify the models predictions.
  • id_col: Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col: Column that contains the target. Defaults to ‘y’.
Returns: Dataframe with one row per id and one column per model.

function msse

msse(
    df: DataFrame,
    models: List[str],
    seasonality: int,
    train_df: DataFrame,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Mean Squared Scaled Error (MSSE) MSSE measures the relative prediction accuracy of a forecasting method by comparinng the mean squared errors of the prediction and the observed value against the mean squared errors of the seasonal naive model. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actuals and predictions.
  • models (list of str): Columns that identify the models predictions.
  • seasonality (int): Main frequency of the time series; Hourly 24, Daily 7, Weekly 52, Monthly 12, Quarterly 4, Yearly 1.
  • train_df (pandas or polars DataFrame): Training dataframe with id and actual values. Must be sorted by time.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.
References:
  • [1] https: //otexts.com/fpp3/accuracy.html

function rmsse

rmsse(
    df: DataFrame,
    models: List[str],
    seasonality: int,
    train_df: DataFrame,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Root Mean Squared Scaled Error (RMSSE) MSSE measures the relative prediction accuracy of a forecasting method by comparinng the mean squared errors of the prediction and the observed value against the mean squared errors of the seasonal naive model. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actuals and predictions.
  • models (list of str): Columns that identify the models predictions.
  • seasonality (int): Main frequency of the time series; Hourly 24, Daily 7, Weekly 52, Monthly 12, Quarterly 4, Yearly 1.
  • train_df (pandas or polars DataFrame): Training dataframe with id and actual values. Must be sorted by time.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.
References:
  • [1] https: //otexts.com/fpp3/accuracy.html

function quantile_loss

quantile_loss(
    df: DataFrame,
    models: Dict[str, str],
    q: float = 0.5,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Quantile Loss (QL) QL measures the deviation of a quantile forecast. By weighting the absolute deviation in a non symmetric way, the loss pays more attention to under or over estimation. A common value for q is 0.5 for the deviation from the median. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, times, actuals and predictions.
  • models (dict from str to str): Mapping from model name to the model predictions for the specified quantile.
  • q (float, optional): Quantile for the predictions’ comparison. Defaults to 0.5.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.

function scaled_quantile_loss

scaled_quantile_loss(
    df: DataFrame,
    models: Dict[str, str],
    seasonality: int,
    train_df: DataFrame,
    q: float = 0.5,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Scaled Quantile Loss (SQL) SQL measures the deviation of a quantile forecast scaled by the mean absolute errors of the seasonal naive model. By weighting the absolute deviation in a non symmetric way, the loss pays more attention to under or over estimation. A common value for q is 0.5 for the deviation from the median. This was the official measure used in the M5 Uncertainty competition with seasonality = 1. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, times, actuals and predictions.
  • models (dict from str to str): Mapping from model name to the model predictions for the specified quantile.
  • seasonality (int): Main frequency of the time series; Hourly 24, Daily 7, Weekly 52, Monthly 12, Quarterly 4, Yearly 1.
  • train_df (pandas or polars DataFrame): Training dataframe with id and actual values. Must be sorted by time.
  • q (float, optional): Quantile for the predictions’ comparison. Defaults to 0.5.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.
References:

function mqloss

mqloss(
    df: DataFrame,
    models: Dict[str, List[str]],
    quantiles: ndarray,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Multi-Quantile loss (MQL) MQL calculates the average multi-quantile Loss for a given set of quantiles, based on the absolute difference between predicted quantiles and observed values. The limit behavior of MQL allows to measure the accuracy of a full predictive distribution with the continuous ranked probability score (CRPS). This can be achieved through a numerical integration technique, that discretizes the quantiles and treats the CRPS integral with a left Riemann approximation, averaging over uniformly distanced quantiles. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, times, actuals and predictions.
  • models (dict from str to list of str): Mapping from model name to the model predictions for each quantile.
  • quantiles (numpy array): Quantiles to compare against.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.
References:

function scaled_mqloss

scaled_mqloss(
    df: DataFrame,
    models: Dict[str, List[str]],
    quantiles: ndarray,
    seasonality: int,
    train_df: DataFrame,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Scaled Multi-Quantile loss (SMQL) SMQL calculates the average multi-quantile Loss for a given set of quantiles, based on the absolute difference between predicted quantiles and observed values scaled by the mean absolute errors of the seasonal naive model. The limit behavior of MQL allows to measure the accuracy of a full predictive distribution with the continuous ranked probability score (CRPS). This can be achieved through a numerical integration technique, that discretizes the quantiles and treats the CRPS integral with a left Riemann approximation, averaging over uniformly distanced quantiles. This was the official measure used in the M5 Uncertainty competition with seasonality = 1. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, times, actuals and predictions.
  • models (dict from str to list of str): Mapping from model name to the model predictions for each quantile.
  • quantiles (numpy array): Quantiles to compare against.
  • seasonality (int): Main frequency of the time series; Hourly 24, Daily 7, Weekly 52, Monthly 12, Quarterly 4, Yearly 1.
  • train_df (pandas or polars DataFrame): Training dataframe with id and actual values. Must be sorted by time.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.
References:

function coverage

coverage(
    df: DataFrame,
    models: List[str],
    level: int,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Coverage of y with y_hat_lo and y_hat_hi. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, times, actuals and predictions.
  • models (list of str): Columns that identify the models predictions.
  • level (int): Confidence level used for intervals.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.
References:

function calibration

calibration(
    df: DataFrame,
    models: Dict[str, str],
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Fraction of y that is lower than the model’s predictions. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, times, actuals and predictions.
  • models (dict from str to str): Mapping from model name to the model predictions.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.
References:

function scaled_crps

scaled_crps(
    df: DataFrame,
    models: Dict[str, List[str]],
    quantiles: ndarray,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Scaled Continues Ranked Probability Score Calculates a scaled variation of the CRPS, as proposed by Rangapuram (2021), to measure the accuracy of predicted quantiles y_hat compared to the observation y. This metric averages percentual weighted absolute deviations as defined by the quantile losses. Args:
  • df (pandas or polars DataFrame): Input dataframe with id, times, actuals and predictions.
  • models (dict from str to list of str): Mapping from model name to the model predictions for each quantile.
  • quantiles (numpy array): Quantiles to compare against.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: dataframe with one row per id and one column per model.
References:
  • [1] https: //proceedings.mlr.press/v139/rangapuram21a.html

function tweedie_deviance

tweedie_deviance(
    df: DataFrame,
    models: List[str],
    power: float = 1.5,
    id_col: str = 'unique_id',
    target_col: str = 'y'
) → DataFrame
Compute the Tweedie deviance loss for one or multiple models, grouped by an identifier. Each group’s deviance is calculated using the mean_tweedie_deviance function, which measures the deviation between actual and predicted values under the Tweedie distribution. The power parameter defines the specific compound distribution:
  • 1: Poisson
  • (1, 2): Compound Poisson-Gamma
  • 2: Gamma
  • 2: Inverse Gaussian
Args:
  • df (pandas or polars DataFrame): Input dataframe with id, actuals and predictions.
  • models (list of str): Columns that identify the models predictions.
  • power (float, optional): Tweedie power parameter. Determines the compound distribution. Defaults to 1.5.
  • id_col (str, optional): Column that identifies each serie. Defaults to ‘unique_id’.
  • target_col (str, optional): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: DataFrame with one row per id and one column per model, containing the mean Tweedie deviance.
References:
  • [1] https: //en.wikipedia.org/wiki/Tweedie_distribution

This file was automatically generated via lazydocs.