Skip to content

bokbokbok.eval_metrics.regression

LogCoshMetric(XGBoost=False)

Calculates the Log Cosh Error as an alternative to Mean Absolute Error. Args: XGBoost (Bool): Set to True if using XGBoost. We assume LightGBM as default use. Note that you should also set maximize=False in the XGBoost train function

Source code in bokbokbok/eval_metrics/regression/regression_eval_metrics.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def LogCoshMetric(XGBoost: bool = False) -> Callable:
    """
    Calculates the [Log Cosh Error](https://openreview.net/pdf?id=rkglvsC9Ym) as an alternative to
    Mean Absolute Error.
    Args:
        XGBoost (Bool): Set to True if using XGBoost. We assume LightGBM as default use.
                        Note that you should also set `maximize=False` in the XGBoost train function

    """
    def log_cosh_error(
        yhat: np.ndarray, 
        dtrain: "xgb.DMatrix", 
        XGBoost: bool = XGBoost
        ) -> Union[tuple[str, float], tuple[str, float, bool]]:
        """
        Root Mean Squared Log Error.
        All input labels are required to be greater than -1.

        yhat: Predictions
        dtrain: The XGBoost / LightGBM dataset
        XGBoost (Bool): If XGBoost is to be implemented
        """

        y = dtrain.get_label()
        elements = np.log(np.cosh(yhat - y))
        if XGBoost:
            return "LogCosh", float(np.sum(elements) / len(y))
        else:
            return "LogCosh", float(np.sum(elements) / len(y)), False

    return log_cosh_error

RMSPEMetric(XGBoost=False)

Calculates the Root Mean Squared Percentage Error: https://www.kaggle.com/c/optiver-realized-volatility-prediction/overview/evaluation

The corresponding Loss function is Squared Percentage Error. Args: XGBoost (Bool): Set to True if using XGBoost. We assume LightGBM as default use. Note that you should also set maximize=False in the XGBoost train function

Source code in bokbokbok/eval_metrics/regression/regression_eval_metrics.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def RMSPEMetric(XGBoost: bool = False) -> Callable:
    """
    Calculates the Root Mean Squared Percentage Error:
    https://www.kaggle.com/c/optiver-realized-volatility-prediction/overview/evaluation

    The corresponding Loss function is Squared Percentage Error.
    Args:
        XGBoost (Bool): Set to True if using XGBoost. We assume LightGBM as default use.
                        Note that you should also set `maximize=False` in the XGBoost train function

    """
    def RMSPE(
        yhat: np.ndarray, 
        dtrain: "xgb.DMatrix", 
        XGBoost: bool = XGBoost) -> Union[tuple[str, float], tuple[str, float, bool]]:
        """
        Root Mean Squared Log Error.
        All input labels are required to be greater than -1.

        yhat: Predictions
        dtrain: The XGBoost / LightGBM dataset
        XGBoost (Bool): If XGBoost is to be implemented
        """

        y = dtrain.get_label()
        elements = ((y - yhat) / y) ** 2
        if XGBoost:
            return "RMSPE", float(np.sqrt(np.sum(elements) / len(y)))
        else:
            return "RMSPE", float(np.sqrt(np.sum(elements) / len(y))), False

    return RMSPE