This note is about a well-known fact in linear programming: variable splitting and minimizing absolute values lead to complementarity pairs \[\begin{align}& r_i^{\bf +} \cdot r_i^{\bf -} = 0\\ & r_i^{\bf +}, r_i^{\bf -} \ge 0\end{align}\] in the solution. I.e., only one of \(r_i^{\bf +}, r_i^{\bf -}\) can be nonzero. A simple example of this absolute-value modeling technique is implementing an LAD (Least Absolute Deviation) regression model through linear programming.
Let's have a look.
Problem: LAD regression
I created an artificial dataset with a few possible outliers:
---- 25 PARAMETER data x y observation1 0.442 6.448 observation2 3.823 10.020 observation3 6.231 7.789 observation4 5.727 9.704 observation5 4.607 12.430 observation6 8.779 11.618 observation7 9.277 14.677 observation8 5.934 28.955 observation9 2.727 6.060 observation10 8.390 15.843 observation11 7.665 11.440 observation12 5.798 8.444 observation13 9.096 7.256 observation14 7.773 11.288 observation15 4.459 7.596 observation16 1.593 6.750 observation17 3.951 7.491 observation18 2.772 11.990 observation19 1.752 11.374 observation20 4.911 11.558 observation21 5.613 12.916 observation22 6.168 11.604 observation23 7.551 11.726 observation24 5.057 6.750 observation25 2.547 6.266 observation26 8.157 11.052 observation27 2.398 8.139 observation28 5.162 10.734 observation29 8.787 12.720 observation30 2.949 11.679
The outliers will heavily impact standard least-squares regression, but using absolute deviations reduces its impact.
The trick is that we can write \[\left|{\color{darkred}r}_i\right| = {\color{darkred}r}^+_i + {\color{darkred}r}^-_i\] This approach only works if at most one of the pair \(({\color{darkred}r}^+_i, {\color{darkred}r}^-_i)\) is nonzero. This is automatically the case for our LAD model as we will discuss below.
The complete LP model is:
| LAD Model |
|---|
| \[\begin{align}\min\>&{\color{darkred}z}=\sum_i \left({\color{darkred}r}^+_i + {\color{darkred}r}^-_i \right) \\ & {\color{darkblue}y}_i ={\color{darkred}\alpha} +{\color{darkred}\beta}{\color{darkblue}x}_i +{\color{darkred}r}^+_i -{\color{darkred}r}^-_i & \forall i \\ &{\color{darkred}\alpha}, {\color{darkred}\beta}\>{\bf free} \\& {\color{darkred}r}^+_i, {\color{darkred}r}^-_i \ge 0 \end{align}\] |
As usual for statistical models, \({\color{darkblue}x}\) and \({\color{darkblue}y}\) are data (exogenous).
Solution
If we take a look at the solution, we see:
---- 63 VARIABLE a.L = 6.119 intercept VARIABLE b.L = 0.743 slope ---- 63 VARIABLE r.L residuals + — observation2 1.062 observation3 2.956 observation4 0.667 observation5 2.890 observation6 1.020 observation7 1.669 observation8 18.430 observation9 2.084 observation10 3.495 observation11 0.370 observation12 1.980 observation13 5.617 observation14 0.603 observation15 1.834 observation16 0.552 observation17 1.562 observation18 3.813 observation19 3.954 observation20 1.792 observation21 2.629 observation22 0.905 observation24 3.124 observation25 1.745 observation26 1.124 observation27 0.239 observation28 0.782 observation29 0.076 observation30 3.370 ---- 63 VARIABLE z.L = 70.345 sum of absolute deviations
Notice that observations 1 and 23 are missing. The display is skipping zero elements. The residuals are zero for these observations.
Also note the typical "complementarity pattern" in the residuals. Only one of the plus or minus components of each residual can be nonzero.
Why does this work?
For any optimal solution we have the property that \({\color{darkred}r}^+_i \cdot {\color{darkred}r}^-_i = 0\) for all \(i\).
There are two reasons why we see this complementarity property:
- The objective pushes \({\color{darkred}r}^+_i + {\color{darkred}r}^-_i\) down. If both were positive in the optimal solution, we could subtract the smaller from both and improve the objective. I.e., the solution would not be optimal. A nice example of a proof by contradiction.
- This second reason is about how the simplex method works. We can't have both components of the residuals being basic. If we allow that, the basic matrix \(B\) would be singular (not invertible), as the coefficients of \(A\) for these two columns \({\color{darkred}r}^+_i\) and \({\color{darkred}r}^-_i\) are identical except for the sign. To be precise, the columns are \(e_i\) and \(-e_i\), where \(e_i\) is all zeros except at the \(i\)-th position, where it is 1. These two columns are obviously linearly dependent. Since only one of these two columns can be basic, the variable corresponding to the other must be zero (i.e., non-basic). This holds not only for optimal solutions, but for any solution. A caveat is using an interior point method without crossover or a first-order method. Of course, they can still rely on the objective. Also, for completeness, I assume here that the objective row is not part of the matrix \(A\).
- Using the alternative bounding approach for LAD regression \[\begin{align}\min & \sum_i {\color{darkred}t}_i \\ & -{\color{darkred}t}_i \le {\color{darkred}r}_i \le {\color{darkred}t}_i \\ & {\color{darkblue}y}_i ={\color{darkred}\alpha} +{\color{darkred}\beta}{\color{darkblue}x}_i +{\color{darkred}r}_i \\ & {\color{darkred}t}_i \ge 0 \end{align}\] does not have this issue or property.
- Of course, minimizing absolute values happens in applications outside of LAD regression.
- If we were to maximize the absolute values, we would no longer have a convex problem. In that case, we would need heavy machinery such as binary variables or SOS1 sets.
---- 66 VARIABLE a.L = 6.119 intercept VARIABLE b.L = 0.743 slope ---- 66 VARIABLE r.L residuals + — observation1 5.87476E-11 6.103220E-9 observation2 1.062 5.82278E-11 observation3 5.82231E-11 2.956 observation4 5.82299E-11 0.667 observation5 2.890 5.82245E-11 observation6 5.82293E-11 1.020 observation7 1.669 5.82257E-11 observation8 18.430 5.82079E-11 observation9 5.82239E-11 2.084 observation10 3.495 5.82233E-11 observation11 5.82605E-11 0.370 observation12 5.82249E-11 1.980 observation13 5.82197E-11 5.617 observation14 5.82285E-11 0.603 observation15 5.82249E-11 1.834 observation16 5.82236E-11 0.552 observation17 5.82255E-11 1.562 observation18 3.813 5.82236E-11 observation19 3.954 5.82235E-11 observation20 1.792 5.82252E-11 observation21 2.629 5.82248E-11 observation22 0.905 5.82296E-11 observation23 1.262410E-8 5.86630E-11 observation24 5.82226E-11 3.124 observation25 5.82245E-11 1.745 observation26 5.82288E-11 1.124 observation27 0.239 5.82783E-11 observation28 0.782 5.82319E-11 observation29 0.076 5.81763E-11 observation30 3.370 5.82241E-11 ---- 66 VARIABLE z.L = 70.345 sum of absolute deviations
Intermediate solutions
S O L V E S U M M A R Y MODEL lad OBJECTIVE z TYPE LP DIRECTION MINIMIZE SOLVER MINOS FROM LINE 64 **** SOLVER STATUS 2 Iteration Interrupt **** MODEL STATUS 7 Feasible Solution **** OBJECTIVE VALUE 73.2462 RESOURCE USAGE, LIMIT 0.016 10000000000.000 ITERATION COUNT, LIMIT 10 10 ---- 65 VARIABLE a.L = 6.184 intercept VARIABLE b.L = 0.597 slope ---- 65 VARIABLE r.L residuals + - observation2 1.555 observation3 2.113 observation4 0.102 observation5 3.497 observation6 0.195 observation7 2.956 observation8 19.230 observation9 1.751 observation10 4.652 observation11 0.682 observation12 1.200 observation13 4.357 observation14 0.465 observation15 1.249 observation16 0.385 observation17 1.051 observation18 4.152 observation19 4.145 observation20 2.443 observation21 3.382 observation22 1.739 observation23 1.035 observation24 2.452 observation25 1.438 observation27 0.524 observation28 1.470 observation29 1.292 observation30 3.735 ---- 65 VARIABLE z.L = 73.246 sum of absolute deviations
S O L V E S U M M A R Y MODEL lad OBJECTIVE z TYPE LP DIRECTION MINIMIZE SOLVER IPOPT FROM LINE 64 **** SOLVER STATUS 2 Iteration Interrupt **** MODEL STATUS 6 Intermediate Infeasible **** OBJECTIVE VALUE 4.2215 RESOURCE USAGE, LIMIT 0.004 10000000000.000 ITERATION COUNT, LIMIT 2 2 ---- 65 VARIABLE a.L = 0.277 intercept VARIABLE b.L = 0.022 slope ---- 65 VARIABLE r.L residuals + - observation1 0.034 0.072 observation2 0.064 0.038 observation3 0.013 0.126 observation4 0.038 0.067 observation5 0.112 0.011 observation6 0.041 0.063 observation7 0.100 0.015 observation8 0.728 0.020 observation9 0.015 0.118 observation10 0.149 9.213570E-5 observation11 0.049 0.053 observation12 0.021 0.100 observation13 0.004 0.200 observation14 0.045 0.057 observation15 0.020 0.102 observation16 0.029 0.081 observation17 0.022 0.097 observation18 0.130 0.006 observation19 0.129 0.007 observation20 0.084 0.024 observation21 0.109 0.012 observation22 0.069 0.034 observation23 0.055 0.046 observation24 0.010 0.138 observation25 0.017 0.109 observation26 0.038 0.068 observation27 0.043 0.059 observation28 0.063 0.039 observation29 0.061 0.040 observation30 0.118 0.010 ---- 65 VARIABLE z.L = 4.221 sum of absolute deviations
Solver problems
S O L V E S U M M A R Y MODEL lad OBJECTIVE z TYPE LP DIRECTION MINIMIZE SOLVER HIGHS FROM LINE 64 **** SOLVER STATUS 2 Iteration Interrupt **** MODEL STATUS 14 No Solution Returned **** OBJECTIVE VALUE 39.8485 RESOURCE USAGE, LIMIT 0.000 10000000000.000 ITERATION COUNT, LIMIT 10 10 S O L V E S U M M A R Y MODEL lad OBJECTIVE z TYPE LP DIRECTION MINIMIZE SOLVER XPRESS FROM LINE 64 **** SOLVER STATUS 2 Iteration Interrupt **** MODEL STATUS 14 No Solution Returned **** OBJECTIVE VALUE 0.0000 RESOURCE USAGE, LIMIT 0.046 10000000000.000 ITERATION COUNT, LIMIT 10 10
Xpress Python Model
import xpress as xp from io import StringIO import pandas as pd iterlimit = 10 #---------------------------------------------- # data #---------------------------------------------- df = pd.read_table(StringIO(""" i x y observation1 0.44210132 6.44750203 observation2 3.82274024 10.01983898 observation3 6.23060668 7.78926079 observation4 5.72676411 9.70417381 observation5 4.60680179 12.42981755 observation6 8.77921471 11.61833643 observation7 9.27713358 14.67683786 observation8 5.93381615 28.95514017 observation9 2.72686678 6.06008592 observation10 8.38980378 15.84345556 observation11 7.66500308 11.44049644 observation12 5.79829196 8.44430907 observation13 9.09576650 7.25589802 observation14 7.77304183 11.28781064 observation15 4.45932581 7.59624812 observation16 1.59348806 6.75018412 observation17 3.95136687 7.49105201 observation18 2.77230871 11.99033842 observation19 1.75187971 11.37407422 observation20 4.91071592 11.55804245 observation21 5.61335506 12.91631670 observation22 6.16796837 11.60374255 observation23 7.55066563 11.72576766 observation24 5.05663151 6.75010567 observation25 2.54691604 6.26558134 observation26 8.15682045 11.05210464 observation27 2.39781739 8.13913190 observation28 5.16244536 10.73444211 observation29 8.78656221 12.71992827 observation30 2.94928071 11.67903419 """), sep="\\s+") x = df['x'].to_numpy() y = df['y'].to_numpy() #---------------------------------------------- # LP Model #---------------------------------------------- n = len(x) p = xp.problem() rplus = p.addVariables(n, name='rplus') rmin = p.addVariables(n, name='rmin') a = p.addVariable('a', lb=-xp.infinity) b = p.addVariable('b', lb=-xp.infinity) p.addConstraint(rplus[i] - rmin[i] == y[i] - (a + b * x[i]) for i in range(n)) p.setObjective(sum([rplus[i] + rmin[i] for i in range(n)]), sense=xp.ObjSense.MINIMIZE) #---------------------------------------------- # Solve and reporting #---------------------------------------------- p.setControl("LPITERLIMIT", iterlimit) p.lpOptimize() print(f"lpstatus: {p.attributes.lpstatus.name}") print(f"Num infeas: {p.attributes.primalinfeas}") print(f"Num nonopt: {p.attributes.dualinfeas}") sol = p.getLpSol()[0] dfsol = pd.DataFrame({'rplus': sol[0:n], 'rmin': sol[n:2*n]}) print(f"Objective value: {p.attributes.lpobjval:.3f}") print(f"Estimates: a = {sol[2*n]:.3f}, b = {sol[2*n+1]:.3f}") print("Residuals (rplus, rmin):") print(dfsol.round(3).astype(str).replace(['0.00','0.0','0'], ''))
Objective value: 36.198 Estimates: a = 5.812, b = 1.436 Residuals (rplus, rmin): rplus rmin 0 1 2 3 4 5 6 7 14.619 8 3.669 9 10 11 12 13 5.69 14 15 1.351 16 3.997 17 2.196 18 3.045 19 20 21 22 23 24 25 26 27 28 29 1.63
Summary
- The objective will make sure that optimal solutions have this property, and
- The Simplex method will never allow both the negative and the positive components to be nonzero.
References
- Least absolute deviations, https://en.wikipedia.org/wiki/Least_absolute_deviations
Appendix: GAMS Implementation
$ontext LAD (Least Absolute Deviation) Regression LP model using variable splitting $offtext
option lp=highs;
option seed=1234;
*--------------------------------------------------------------------- * data *---------------------------------------------------------------------
set i 'observations' /observation1*observation30/;
parameter data(i,*);
data(i,'x') = uniform(0,10); data(i,'y') = 10+0.25*data(i,'x')+normal(0,3); * for a few observations, generate more wild y data data(i,'y')$(uniform(0,1)<0.1) = 10+0.25*data(i,'x')+normal(0,10); display data;
*--------------------------------------------------------------------- * derived data *---------------------------------------------------------------------
parameters x(i) 'independent variable' y(i) 'dependent variable' ; x(i) = data(i,'x'); y(i) = data(i,'y');
*--------------------------------------------------------------------- * LAD regression LP model *---------------------------------------------------------------------
set pm 'plus/minus' /'+','-'/;
variables a 'intercept' b 'slope' z 'sum of absolute deviations' ; positive variables r(i,pm) 'residuals' ;
equations objective 'objective function' fit(i) 'model to be fitted' ;
objective.. z =e= sum((i,pm), r(i,pm)); fit(i).. y(i) =e= a + b * x(i) + r(i,'+') - r(i,'-');
model lad /all/; solve lad using lp minimizing z; display a.l, b.l, r.l, z.l; |
No comments:
Post a Comment