Lindo Systems
    ! Least Median Regression. (LeastMedianReg.lng)
Given a set of observations, some of which may contain 
substantial errors ( be we do not know which observations),
decide which observations to discard, and then
fit a prediction formula to the observations retained;
! Keywords: Forecasting, Least Median regression, Regression,
  Robust regression, Statistics, Trimming, ;
sets:
  obs: delta, r;
  var: beta;
  oxv( obs, var): X;
endsets
data:
  obs = 1..50;
  var = 1..3;
  dependv = 3;   ! The index of the dependent variable;
  h = 25;        ! Number of observations to retain;
  Bigm = 999999; ! An upper bound on prediction error;
  X =    
23	90	1559	
39	82	33	
67	99	1326	
93	55	170	
93	28	-370	
19	63	1080	
47	45	432	
90	99	229	
82	23	-363	
99	55	119	
55	46	372	
28	70	1111	
63	49	350	
45	28	800	
99	18	-631	
23	46	-693	
55	70	850	
46	49	520	
70	28	-140	
49	18	-130	
28	2	-245	
18	70	1214	
2	10	180	
70	90	21	
10	82	1533	
90	99	1083	
82	18	-455	
99	92	861	
55	77	42	
28	70	1121	
63	10	-432	
45	90	1350	
99	82	651	
23	99	1750	
55	55	550	
46	28	1255	
70	63	560	
49	18	-130	
28	92	1560	
18	77	-1360	
92	43	-60	
77	76	1408	
43	29	157	
76	34	-89	
29	98	1674	
34	49	641	
98	28	423
28	12	-235	
45	28	808	
92	27	-368			
 ;
enddata
! Variables:
   r(i) = prediction error for observation i,
   z = maximum absolute prediction error for
       a set of h observations,
   delta(i) = 1 if observation i is included in
       the error metric.
     ;
min = z;
@sum( obs(i): delta(i)) = h;
! The beta(j) can be + or -;
@for( var( j) : @free( beta(j)));
@for( obs( i):
    @bin( delta(i));
    @free( r(i));
    r(i) = X(i,dependv) - @sum( var( j) | j #ne# dependv: X(i,j)*beta(j));
! |r(i)| <= z if observation i is kept ( delta(i) = 1);
    r(i) <= z + BigM*(1-delta(i));
   -r(i) <= z + BigM*(1-delta(i));
    );