Cody

Problem 45851. Least Absolute Deviations (L1-norm) line fit - degree 1

Solution 2962069

Submitted on 18 Sep 2020 by Rafael S.T. Vieira
  • Size: 37
  • This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
X=[-2.8 -4.8 0.9 2.3 3.6 -4.2 -0.7 3.0 0.1 -1.0]'; Y=[12.0 6.5 -1.6 -2.1 -2.6 0.1 -1.1 -2.4 -1.3 -1.0]'; P=LADfit(X,Y); % P_correct = [-10/27 -19/15] res=sum(abs(Y-polyval(P_lad,X))); % Optimal residual = 1007/54 assert(res<19)

ans = []

2   Pass
X=[1 2 3 4 5]'; Y=[1 2 3 4 3]'; P=LADfit(X,Y); % P_correct = [1 0] res=sum(abs(Y-polyval(P_lad,X))); % Optimal residual = 2.0 assert(res<2.1)

ans = []

3   Pass
X=[1 2 3 4 5]'; Y=[1 0 0 0 0]'; P=LADfit(X,Y); % P_correct = [0 0] res=sum(abs(Y-polyval(P_lad,X))); % Optimal residual = 1.0 assert(res<1.01)

ans = []

4   Pass
n=10; % Nr of points no=2; % Nr of outliers max_res = 0; max_ratio = 0; for cycle=1:10 rng('shuffle'); P=(rand(2,1)-0.5)*10; % Generate model X=(rand(n,1)-0.5)*2; % Generate X Y=polyval(P,X)+(rand(n,1)-0.5); % Generate Y and add noise Y(1:no)=Y(1:no)+(rand(no,1)-0.5)*10; % Add outliers P_lad=LADfit(X,Y); P_lin=polyfit(X,Y,1); Y_lad=polyval(P_lad,X); res_lad=sum(abs(Y-Y_lad)); Y_lin=polyval(P_lin,X); res_lin=sum(abs(Y-Y_lin)); max_res=max(max_res,res_lad); max_ratio=max(max_ratio,res_lad/res_lin); end assert(max_res<12.0 & max_ratio<0.99); % Should be always better than L-2 and under 12.0

ans = [] ans = [] ans = [] ans = [] ans = [] Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 6.142775 ans = [] ans = [] ans = [] ans = [] ans = []