least absolute deviation when we have data set
이전 댓글 표시
I have this data
x = (1:10)';
y = 10 - 2*x + randn(10,1);
y(10) = 0;
how can I use least absolute value regression?
채택된 답변
추가 답변 (1개)
Bruno Luong
2020년 8월 22일
편집: Bruno Luong
2020년 8월 22일
% Test data
x = (1:10)';
y = 10 - 2*x + randn(10,1);
y(10) = 0;
order = 1; % polynomial order
M = x(:).^(0:order);
m = size(M,2);
n = length(x);
Aeq = [M, speye(n,n), -speye(n,n)];
beq = y(:);
c = [zeros(1,m) ones(1,2*n)]';
%
LB = [-inf(1,m) zeros(1,2*n)]';
% no upper bounds at all.
UB = [];
sol = linprog(c, [], [], Aeq, beq, LB, UB);
Pest = sol(m:-1:1); % here is the polynomial
% Check
clf(figure(1));
plot(x, y, 'or', x, polyval(Pest,x), 'b');

댓글 수: 3
Terry nichols
2020년 12월 22일
편집: Terry nichols
2020년 12월 22일
I have 1 comment, 1 question, and 1 interestig result
(actually I have many qustions, but I'll just keep it to one for now)
1) Nice piece of work! I tried it for my problem and it worked seamlessly. The L1 norm just blew through all of the outliers. Although I am still trying to understand how you calcualted the values that need to get passed to linprog.m (my long list of questions).
2) I am totally new to the ways of linear programming, so I am wondering how come you have no inequality constraints? I am guessing you are saying the solution must adhere to the objective function, precisely?
3) I tested your code for a quadratic using both the L2 (polyfit, polyval) and L1 norm provided in your code and got the following.

Bruno Luong
2020년 12월 22일
편집: Bruno Luong
2020년 12월 22일
"2) I am totally new to the ways of linear programming, so I am wondering how come you have no inequality constraints? I am guessing you are saying the solution must adhere to the objective function, precisely? "
Because I don't need it. I formulate the problem as
M*P - u + v = y
where u and v a extra variables, they meant to be positive
v =( M*P - y) = u
so
argmin (u + v) is sum(abs( M*P - y)) is L1 norm of the fit.
I could formulate with inequality but they are equivalent. There is no unique way to formulate LP, as long as it does what we want.
And as comment; all LP can be showed to be equivalent to a "canonical form" where all the inequalities are replaced by only linear equalities + positive bounds
argmin f'*x
A*x = b
x >= 0
Terry nichols
2020년 12월 22일
편집: Terry nichols
2020년 12월 25일
Thanks much for all of your help!
카테고리
도움말 센터 및 File Exchange에서 Multiple Linear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!