# Linear regression minimising MAD in sklearn

The standard sklearn linear regression class finds an approximated linear relationship between variate and covariates that minimises the mean squared error (MSE). Specifically, let $$NN$$ be the number of observations and let us ignore the intercept for simplicity. Let $$yjy_j$$ be the variate value of the $$jj$$-th observation and $$x1,j,…,xn,jx_{1,j}, \dots, x_{n,j}$$ be the values of the $$nn$$ covariates of the $$jj$$-th observation. The linear relationship is of the form
$$y=β1x1+…βnxn; y = \beta_1 x_1 + \dots \beta_n x_n;$$
where the coefficients $$β1,…,βn\beta_1, \dots, \beta_n$$ are given by
$$β1,…,βn=argmin˜β1,…,˜βn(N∑j=1(yj−˜β1x1,j−⋯−˜βnxn,j)2).\beta_1, \dots, \beta_n = \underset{\tilde\beta_1, \dots, \tilde\beta_n}{\mathrm{argmin}} \left( \sum_{j = 1}^N \left( y_j - \tilde\beta_1x_{1, j} - \dots -\tilde\beta_nx_{n, j}\right)^2 \right).$$

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|).\beta_1, \dots, \beta_n = \underset{\tilde\beta_1, \dots, \tilde\beta_n}{\mathrm{argmin}} \left( \sum_{j = 1}^N \left| y_j - \tilde\beta_1x_{1, j} - \dots -\tilde\beta_nx_{n, j}\right| \right).$$

I understand that, in sharp contrast to the MSE case, the lack of differentiability of the absolute value function at $$00$$ 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?

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.