Find $y$ that minimizes a sum of squares

155 Views Asked by At

Let $x_1, x_2, \dots, x_n$ be $n$ positive, distinct real numbers such that $x_1 < x_2 < \cdots < x_n$. I'd like to find some $y \in \{ x_1, x_2, \dots, x_n \}$ such that

$$\sum_{x_i \le y} \left(x_i - \frac{1}{L}\sum_{x_j \le y} x_j\right)^2 + \sum_{x_i > y} \left(x_i - \frac{1}{R}\sum_{x_j > y} x_j\right)^2$$

is minimized. Here, $L$ is the total number of $x_i$'s such that $x_i \le y$, and $R$ is the total number of $x_i$'s such that $x_i > y$.

I apologize if this is a trivial question, but I couldn't figure it out. I thought the answer might be mean or median of $\{ x_1, x_2 \dots, x_n \}$, but some simulations proved my idea to be wrong.

1

There are 1 best solutions below

0
On

We can formulate a continuous version

$$ F(y) = \int_{0}^{y} \left(f(x)-\frac{\int_{0}^{x} f(\zeta) \, d\zeta}{L}\right)^2 \, dx+\int_{y}^{x_n} \left(f(x)-\frac{\int_{y}^{x} f(\zeta) \, d\zeta}{R}\right)^2 \, dx $$

and then specify $f(x)$. For instance, for $f(x) = x$ we have

$$ F(y) = \frac{y^5}{20 L^2}-\frac{y^4}{4 L}-\frac{20 R^2 \left(y^3-125\right)+15 R \left(y^2-25\right)^2+\left(8 y^2+45 y+75\right) (y-5)^3}{60 R^2}+\frac{y^3}{3} $$

now assuming $x_n=5,L=R=1$

enter image description here

Follows a MATHEMATICA script which is a discrete version

n = 50;
dx = 5/n;
X = Table[(dx k), {k, 0, n}];
U = Table[(dx k), {k, 0, n}];
resy = Table[Sum[(X[[j]] - 1/L Sum[X[[k]] dx, {k, 1, j}])^2 dx, {j, 1, nu}] + Sum[(X[[j]] - 1/R Sum[X[[k]] dx, {k, nu + 1, j}])^2 dx, {j, nu + 1, n + 1}], {nu, 1, n + 1}] /. {L -> 1, R -> 1};
res = {U, resy} // Transpose;
ListPlot[res ]

enter image description here

NOTE

dxwas introduced in the script to have comparable results.