Search code examples
forecastingloss-functionlightgbmtidymodelsr-parsnip

How to implement LINEX loss function for LightGBM time series forecast?


In demand forecasting, it is crucial, to keep in mind, that underestimating demand is hurting most businesses more, because of higher costs as in case of overestimating the demand for a certain product.

The standard loss function for Gradient Boosting machines is the RMSE, which do not have smooth derivates.

To make the objective more "business-like", we could define a custom loss function, that takes the product price into account (for example an asymmetric MSE).

Another possibility is the so called LINEX loss function. LINEX stands for linear exponential loss and accounts for asymmetric loss.

However, I struggle to implement LINEX in R. I only found a formula without in depth description. enter image description here

The illustrated example shows the implementation for an xgboost model.

Does anyone know, how to implement this in parsnip?

Here an example, how you could implement a custom loss function:

library(tidymodels)
library(lightgbm)
library(bonsai)
mod <- boost_tree("regression") %>% 
  set_engine("lightgbm",
             objective = function(preds, dtrain) {
               truth <- as.numeric(getinfo(dtrain, "label"))
               error <- truth - preds
               gradient <- -2 * error
               hess <- rep.int(2, length(preds))
               list(grad = gradient, hess = hess)
             }
             )

Solution

  • You could have to write a custom yardstick function.

    You can also add a GitHub issue to get it implemented.