5
$\begingroup$

I want to use MAPE(Mean Absolute Percentage Error) as my loss function.

def mape(y, y_pred):
    grad = <<<>>>
    hess = <<<>>>
    return grad, hess

Can someone help me understand the hessian and gradient for MAPE as a loss function? We need to retuern the gradient and hessian to use it as a loss function

$\endgroup$
4
  • $\begingroup$ I ave done this same type of fitting with a Levenberg-Marquardt non-linear fitter, where the hessian and gradient are not explicitly required. You might consider a non-linear fitting algorithm as a possible option. $\endgroup$ – James Phillips Nov 28 '17 at 14:36
  • 1
    $\begingroup$ Given that the Hessian of MAPE is 0s everywhere is is defined, and not even defined in some places, OP may be interested in the log cosh loss function, which is similar to MAE but twice differentiable everywhere. $\endgroup$ – Sycorax May 4 '20 at 12:21
  • 1
    $\begingroup$ @SycoraxsaysReinstateMonica: alternatively, one could approximate the MAE (or any other quantile loss) differentiably to arbitrary precision (Zheng, 2011, Int J Mach Learning). If we can bound the actuals, we can approximate the MAPE arbitrarily well in a differentiable way. $\endgroup$ – Stephan Kolassa May 4 '20 at 12:26
  • $\begingroup$ @StephanKolassa Good find! I wasn’t familiar with that research. $\endgroup$ – Sycorax May 4 '20 at 12:28
8
$\begingroup$

The Mean Absolute Percentage Error (MAPE) is defined as

$$\text{MAPE} := \frac{1}{N}\sum_{i=1}^N\frac{|\hat{y}_i-y_i|}{y_i},$$

where the $y_i$ are actuals and the $\hat{y}_i$ are predictions. The gradient is the vector collecting the first derivatives:

$$\frac{\partial\text{MAPE}}{\partial\hat{y}_i} = \begin{cases} -\frac{1}{Ny_i}, & \text{ if } \hat{y}_i<y_i \\ \text{undefined}, & \text{ if } \hat{y}_i=y_i \\ \frac{1}{Ny_i}, & \text{ if } \hat{y}_i>y_i \\ \end{cases} $$

The interpretation is that if you are underestimating ($\hat{y}_i<y_i$), then increasing $\hat{y}_i$ by one unit will reduce your MAPE by $\frac{1}{Ny_i}$, and the converse if you reduce $ \hat{y}_i$ by one unit.

The Hessian is the matrix containing the mixed second derivatives. Since the gradient does not contain the predictions any more, taking second derivatives will result in zeros everywhere that it is defined:

$$\frac{\partial^2\text{MAPE}}{\partial\hat{y}_i\partial\hat{y}_j} = \begin{cases} 0, & \text{ if } \hat{y}_i\neq y_i \text{ and }\hat{y}_j\neq y_j \\ \text{undefined} & \text{ else} \end{cases} $$

$\endgroup$
4
  • 2
    $\begingroup$ The possible problems with MAPE, that were discussed by you many times in here as far as I remember, are also worth mentioning as a side note, e.g. stats.stackexchange.com/questions/299712/… $\endgroup$ – Tim Nov 28 '17 at 15:08
  • 2
    $\begingroup$ Where did the coef 1/yi go in your gradient? $\endgroup$ – Samos May 4 '20 at 11:28
  • 1
    $\begingroup$ @Samos: that is a very good question, which apparently hasn't occurred to six upvoters in the last three and a half years. Thank you! It's fixed now. $\endgroup$ – Stephan Kolassa May 4 '20 at 11:33
  • 1
    $\begingroup$ Glad to help :) now I can upvote too ^^. $\endgroup$ – Samos May 4 '20 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.