The standard sklearn linear regression class finds an approximated linear relationship between variate and covariates that minimises the mean squared error (MSE). Specifically, let N be the number of observations and let us ignore the intercept for simplicity. Let yj be the variate value of the j-th observation and x1,j,…,xn,j be the values of the n covariates of the j-th observation. The linear relationship is of the form

y=β1x1+…βnxn;

where the coefficients β1,…,βn are given by

β1,…,βn=argmin˜β1,…,˜βn(N∑j=1(yj−˜β1x1,j−⋯−˜βnxn,j)2).I now wish to find the coefficients that minimise the mean absolute deviation (MAD) instead of the mean squared error. Namely, I want the coefficients given by

β1,…,βn=argmin˜β1,…,˜βn(N∑j=1|yj−˜β1x1,j−⋯−˜βnxn,j|).I understand that, in sharp contrast to the MSE case, the lack of differentiability of the absolute value function at 0 implies there is no analytic solution for the MAD case. But the latter is still a convex optimisation problem, and, according to this answer, it can be easily solved by means of linear programming.

Is it possible to implement this linear regression in sklearn? What about using other statistics toolkits?

**Answer**

The expected MAD is minimized by the median of the distribution (Hanley, 2001, *The American Statistician*; see also Why does minimizing the MAE lead to forecasting the median and not the mean?). Therefore, you are looking for a model that will yield the *conditional median*, instead of the *conditional mean*.

This is a special case of quantile-regression, specifically for the 50% quantile. Roger Koenker is the main guru for quantile regression; see in particular his book *Quantile Regression*.

There are ways to do quantile regression in Python. This tutorial may be helpful. If you are open to using R, you can use the `quantreg`

package.

**Attribution***Source : Link , Question Author : Giovanni De Gaetano , Answer Author : Stephan Kolassa*