Linear regression minimising MAD in sklearn [on hold]












4












$begingroup$


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 $y_j$ be the variate value of the $j$-th observation and $x_{1,j}, dots, x_{n,j}$ be the values of the $n$ covariates of the $j$-th observation. The linear relationship is of the form
$$ y = beta_1 x_1 + dots beta_n x_n;$$
where the coefficients $beta_1, dots, beta_n$ are given by
$$beta_1, dots, beta_n = underset{tildebeta_1, dots, tildebeta_n}{mathrm{argmin}} left( sum_{j = 1}^N left( y_j - tildebeta_1x_{1, j} - dots -tildebeta_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
$$beta_1, dots, beta_n = underset{tildebeta_1, dots, tildebeta_n}{mathrm{argmin}} left( sum_{j = 1}^N left| y_j - tildebeta_1x_{1, j} - dots -tildebeta_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 $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?










share|cite|improve this question









New contributor




Giovanni De Gaetano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by Peter Flom 1 hour ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Peter Flom

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 2




    $begingroup$
    I just nominated this for reopening. Yes, the question is about how to perform a task in sklearn or Python in general. But it needs statistical expertise to understand or answer, which is explicitly on-topic.
    $endgroup$
    – Stephan Kolassa
    38 mins ago










  • $begingroup$
    @StephanKolassa I agree with you - the question should be reopened..
    $endgroup$
    – James Phillips
    6 mins ago
















4












$begingroup$


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 $y_j$ be the variate value of the $j$-th observation and $x_{1,j}, dots, x_{n,j}$ be the values of the $n$ covariates of the $j$-th observation. The linear relationship is of the form
$$ y = beta_1 x_1 + dots beta_n x_n;$$
where the coefficients $beta_1, dots, beta_n$ are given by
$$beta_1, dots, beta_n = underset{tildebeta_1, dots, tildebeta_n}{mathrm{argmin}} left( sum_{j = 1}^N left( y_j - tildebeta_1x_{1, j} - dots -tildebeta_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
$$beta_1, dots, beta_n = underset{tildebeta_1, dots, tildebeta_n}{mathrm{argmin}} left( sum_{j = 1}^N left| y_j - tildebeta_1x_{1, j} - dots -tildebeta_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 $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?










share|cite|improve this question









New contributor




Giovanni De Gaetano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by Peter Flom 1 hour ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Peter Flom

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 2




    $begingroup$
    I just nominated this for reopening. Yes, the question is about how to perform a task in sklearn or Python in general. But it needs statistical expertise to understand or answer, which is explicitly on-topic.
    $endgroup$
    – Stephan Kolassa
    38 mins ago










  • $begingroup$
    @StephanKolassa I agree with you - the question should be reopened..
    $endgroup$
    – James Phillips
    6 mins ago














4












4








4





$begingroup$


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 $y_j$ be the variate value of the $j$-th observation and $x_{1,j}, dots, x_{n,j}$ be the values of the $n$ covariates of the $j$-th observation. The linear relationship is of the form
$$ y = beta_1 x_1 + dots beta_n x_n;$$
where the coefficients $beta_1, dots, beta_n$ are given by
$$beta_1, dots, beta_n = underset{tildebeta_1, dots, tildebeta_n}{mathrm{argmin}} left( sum_{j = 1}^N left( y_j - tildebeta_1x_{1, j} - dots -tildebeta_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
$$beta_1, dots, beta_n = underset{tildebeta_1, dots, tildebeta_n}{mathrm{argmin}} left( sum_{j = 1}^N left| y_j - tildebeta_1x_{1, j} - dots -tildebeta_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 $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?










share|cite|improve this question









New contributor




Giovanni De Gaetano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




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 $y_j$ be the variate value of the $j$-th observation and $x_{1,j}, dots, x_{n,j}$ be the values of the $n$ covariates of the $j$-th observation. The linear relationship is of the form
$$ y = beta_1 x_1 + dots beta_n x_n;$$
where the coefficients $beta_1, dots, beta_n$ are given by
$$beta_1, dots, beta_n = underset{tildebeta_1, dots, tildebeta_n}{mathrm{argmin}} left( sum_{j = 1}^N left( y_j - tildebeta_1x_{1, j} - dots -tildebeta_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
$$beta_1, dots, beta_n = underset{tildebeta_1, dots, tildebeta_n}{mathrm{argmin}} left( sum_{j = 1}^N left| y_j - tildebeta_1x_{1, j} - dots -tildebeta_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 $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?







regression multiple-regression scikit-learn






share|cite|improve this question









New contributor




Giovanni De Gaetano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|cite|improve this question









New contributor




Giovanni De Gaetano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|cite|improve this question




share|cite|improve this question








edited 1 hour ago









Stephan Kolassa

44.2k692161




44.2k692161






New contributor




Giovanni De Gaetano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Giovanni De GaetanoGiovanni De Gaetano

1235




1235




New contributor




Giovanni De Gaetano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Giovanni De Gaetano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Giovanni De Gaetano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by Peter Flom 1 hour ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Peter Flom

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Peter Flom 1 hour ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Peter Flom

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 2




    $begingroup$
    I just nominated this for reopening. Yes, the question is about how to perform a task in sklearn or Python in general. But it needs statistical expertise to understand or answer, which is explicitly on-topic.
    $endgroup$
    – Stephan Kolassa
    38 mins ago










  • $begingroup$
    @StephanKolassa I agree with you - the question should be reopened..
    $endgroup$
    – James Phillips
    6 mins ago














  • 2




    $begingroup$
    I just nominated this for reopening. Yes, the question is about how to perform a task in sklearn or Python in general. But it needs statistical expertise to understand or answer, which is explicitly on-topic.
    $endgroup$
    – Stephan Kolassa
    38 mins ago










  • $begingroup$
    @StephanKolassa I agree with you - the question should be reopened..
    $endgroup$
    – James Phillips
    6 mins ago








2




2




$begingroup$
I just nominated this for reopening. Yes, the question is about how to perform a task in sklearn or Python in general. But it needs statistical expertise to understand or answer, which is explicitly on-topic.
$endgroup$
– Stephan Kolassa
38 mins ago




$begingroup$
I just nominated this for reopening. Yes, the question is about how to perform a task in sklearn or Python in general. But it needs statistical expertise to understand or answer, which is explicitly on-topic.
$endgroup$
– Stephan Kolassa
38 mins ago












$begingroup$
@StephanKolassa I agree with you - the question should be reopened..
$endgroup$
– James Phillips
6 mins ago




$begingroup$
@StephanKolassa I agree with you - the question should be reopened..
$endgroup$
– James Phillips
6 mins ago










1 Answer
1






active

oldest

votes


















6












$begingroup$

The expected MAD is minimized by the median of the distribution (Hanley, 2001, The American Statistician). 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 eponymous book.



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.






share|cite|improve this answer









$endgroup$









  • 2




    $begingroup$
    In python it is available vis statsmodels statsmodels.org/dev/generated/…
    $endgroup$
    – Tim
    2 hours ago










  • $begingroup$
    Thanks! It is an easy way to look at the problem indeed...
    $endgroup$
    – Giovanni De Gaetano
    2 hours ago


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









6












$begingroup$

The expected MAD is minimized by the median of the distribution (Hanley, 2001, The American Statistician). 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 eponymous book.



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.






share|cite|improve this answer









$endgroup$









  • 2




    $begingroup$
    In python it is available vis statsmodels statsmodels.org/dev/generated/…
    $endgroup$
    – Tim
    2 hours ago










  • $begingroup$
    Thanks! It is an easy way to look at the problem indeed...
    $endgroup$
    – Giovanni De Gaetano
    2 hours ago
















6












$begingroup$

The expected MAD is minimized by the median of the distribution (Hanley, 2001, The American Statistician). 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 eponymous book.



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.






share|cite|improve this answer









$endgroup$









  • 2




    $begingroup$
    In python it is available vis statsmodels statsmodels.org/dev/generated/…
    $endgroup$
    – Tim
    2 hours ago










  • $begingroup$
    Thanks! It is an easy way to look at the problem indeed...
    $endgroup$
    – Giovanni De Gaetano
    2 hours ago














6












6








6





$begingroup$

The expected MAD is minimized by the median of the distribution (Hanley, 2001, The American Statistician). 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 eponymous book.



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.






share|cite|improve this answer









$endgroup$



The expected MAD is minimized by the median of the distribution (Hanley, 2001, The American Statistician). 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 eponymous book.



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.







share|cite|improve this answer












share|cite|improve this answer



share|cite|improve this answer










answered 3 hours ago









Stephan KolassaStephan Kolassa

44.2k692161




44.2k692161








  • 2




    $begingroup$
    In python it is available vis statsmodels statsmodels.org/dev/generated/…
    $endgroup$
    – Tim
    2 hours ago










  • $begingroup$
    Thanks! It is an easy way to look at the problem indeed...
    $endgroup$
    – Giovanni De Gaetano
    2 hours ago














  • 2




    $begingroup$
    In python it is available vis statsmodels statsmodels.org/dev/generated/…
    $endgroup$
    – Tim
    2 hours ago










  • $begingroup$
    Thanks! It is an easy way to look at the problem indeed...
    $endgroup$
    – Giovanni De Gaetano
    2 hours ago








2




2




$begingroup$
In python it is available vis statsmodels statsmodels.org/dev/generated/…
$endgroup$
– Tim
2 hours ago




$begingroup$
In python it is available vis statsmodels statsmodels.org/dev/generated/…
$endgroup$
– Tim
2 hours ago












$begingroup$
Thanks! It is an easy way to look at the problem indeed...
$endgroup$
– Giovanni De Gaetano
2 hours ago




$begingroup$
Thanks! It is an easy way to look at the problem indeed...
$endgroup$
– Giovanni De Gaetano
2 hours ago



Popular posts from this blog

الفوسفات في المغرب

Four equal circles intersect: What is the area of the small shaded portion and its height

جامعة ليفربول