# Gradient and hessian of the MAPE

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

def mape(y, y_pred):
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

• 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. – James Phillips Nov 28 '17 at 14:36
• 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. – Sycorax May 4 '20 at 12:21
• @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. – Stephan Kolassa May 4 '20 at 12:26
• @StephanKolassa Good find! I wasn’t familiar with that research. – Sycorax May 4 '20 at 12:28

$$\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}_iy_i \\ \end{cases}$$

The interpretation is that if you are underestimating ($$\hat{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}$$

• 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/… – Tim Nov 28 '17 at 15:08
• Where did the coef 1/yi go in your gradient? – Samos May 4 '20 at 11:28
• @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. – Stephan Kolassa May 4 '20 at 11:33
• Glad to help :) now I can upvote too ^^. – Samos May 4 '20 at 12:19