Friday, September 1, 2017

Minimizing a Median

A somewhat odd (to me) question was asked on a forum recently. Assume that you have continuous variables x1,,xN that are subject to some constraints. For simplicity, I'll just write x=(x1,,xN)X. I'm going to assume that X is compact, and so in particular the xi are bounded. The questioner wanted to know how to model the problem of minimizing the median of the values x1,,xN. I don't know why that was the goal, but the formulation is mildly interesting and fairly straightforward, with one wrinkle.

The wrinkle has to do with whether N is odd or even. Suppose that we sort the components of some solution x, resulting in what is sometimes called the "order statistic": x(1)x(2)x(N). For odd N, the median is x(N+12).For even N, it is usually defined as (x(N2)+x(N2+1))/2.

The odd case is easier, so we'll start there. Introduce N new binary variables z1,,zN and a new continuous variable y, which represents the median x value. The objective will be to minimize y. In addition to the constraint xX, we use "big-M" constraints to force y to be at least as large as half the sample (rounding "half" up). Those constraints are: yxiMizi, i=1,,Ni=1Nzi=N12 with the Mi sufficiently large positive constants. The last constraint forces zi=0 for exactly N+12 of the indices i, which in turn forces yxi for N+12 of the xi. Since the objective minimizes y, zi will be 0 for the N+12 smallest of the xi and y will be no larger than the smallest of them. In other words, we are guaranteed that in the optimal solution y=x(N+12), i.e., it is the median of the optimal x.

If N is even and if we are going to use the standard definition of median, we need twice as many added variables (or at least that's the formulation that comes to mind). In addition to the zi, let w1,,wN also be binary variables, and replace y with a pair of continuous variables y1, y2. The objective becomes minimization of their average (y1+y2)/2 subject to the constraints y1xiMizi iy2xiMiwi ii=1Nzi=N21i=1Nwi=N2 where the Mi are as before. The constraints force y1 to be at least as large as N2+1 of the xi and y2 to be at least as large as N2 of them. The minimum objective value will occur when y1=x(N2+1) and y2=x(N2).