Show that$$\sum_{1\le i<j\le n}\left((x_j-x_i)-(x_j-x_i)^2\right)=\left(\sum_{i=1}^n{x_i}\right)^2-n\sum_{i=1}^n{x_i^2}-\sum_{i=1}^n{(n-2i+1)x_i}\\=-n\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2+\frac1{4n}{\sum_{i=1}^n(n-2i+1)^2}.$$
I wonder how this identity comes up with the first and second recipes? Can anyone explain this in detail? I got this identity when I was reading the problem here. Thanks.
Taking a low tech approach, let our original summation be $\ s_n := u_n - t_n\ $ where $$ u_n := \sum_{1\le i<j\le n} (x_j-x_i), \quad t_n := \sum_{1\le i<j\le n} (x_j-x_i)^2. \tag{1}$$ Also, let $$ v_n := \sum_{1\le i\ne j\le n} x_i\ x_j = \left(\sum_{1\le k\le n} x_k \right)^2 - \sum_{1\le k\le n}x_k^2. \tag{2}$$ Now, $\;u_n = \sum_{i\le k\le n} c_{k,n}\ x_k\ $ where $$ c_{k,n} := (\sum_{1\le i\le k} 1) - (\sum_{k\le j\le n} 1) = k-(n-k+1) = 2k-n-1 \tag{3}$$ which counts how many times $\ k\ $ appears as $\ j\ $ minus the times it appears as $\ i\ $ in equation $(1).$ Thus, $$ u_n = \sum_{1\le k\le n} (2k-n-1)\ x_k = -\sum_{1\le k\le n} (n-2k+1)\ x_k . \tag{4}$$ Now, we get $\ t_n = \sum_{1\le i<j\le n} (x_j^2 + x_i^2 - 2x_jx_i)\ $ by expanding the square in equation $(1)$ and similarly to how we got equation $(3)$, we now get $$ t_n \!=\! \sum_{k=1}^n (k\!+\!(n\!-\!k\!+\!1)) x_k^2 +\! \sum_{1\le i\ne j\le n} x_ix_j \!=\! (n\!+\!1)\!\sum_{k=1}^n x_k^2\!+\! v_n. \tag{5}$$ Combining this with equation $(2)$ we get $$ t_n = n\sum_{1\le k\le n} x_k^2 - \sum_{1\le k\le n}x_k^2. \tag{6}$$ Combining this with equation $(4)$ we get $$ s_n = \left(\sum_{i=1}^n{x_i}\right)^2-n\sum_{i=1}^n{x_i^2}-\sum_{i=1}^n{(n-2i+1)\ x_i}, \tag{7}$$ which is the first identity requested.
Continuing, let $\ y_i = y_{i,n} := x_i -\frac1n\sum_{j=1}^n x_j.\ $ There is a famous formula in statistics $$ t_n = n \sum_{1\le i\le n} \Big(x_i-\frac1n\sum_{j=1}^n x_j\Big)^2 = n \sum_{1\le i\le n} y_i^2. \tag{8}$$ Notice that $$ \sum_{i=1}^n (n-2i+1) = 0. \tag{9}$$ Combining this with equation $(4)$ we get $$ u_n = -\sum_{1\le i\le n} (n-2i+1)\ y_i. \tag{10}$$ Combining this with equation $(8)$ we get $$ s_n = -n \sum_{i=1}^n y_i^2 -\sum_{1\le i\le n} (n-2i+1)\ y_i. \tag{11}$$ Continuing, let $\ z_i = z_{i,n} := y_{i,n} +\frac{n-2i+1}{2n}. \ $ Now $$ z_i^2 = y_i^2 + y_i\frac{n-2i+1}n + \frac{(n-2i+1)^2}{4n^2}. \tag{12}$$ Summing this over $\ i\ $ and multipling by $\ n\ $ gives us $$ n \sum_{i=1}^n z_i^2 \!=\! n \sum_{i=1}^n y_i^2 + \sum_{i=1}^n y_i(n\!-\!2i\!+\!1) \!+\! \frac1{4n}\sum_{i=1}^n (n\!-\!2i\!+\!1)^2. \tag{13}$$ Finally, combining equations $(11)$ and $(13)$ we get $$ s_n = - n \sum_{i=1}^n z_i^2 + \frac1{4n}\sum_{i=1}^n (n-2i+1)^2. \tag{14}$$ which is the second identity since $\ z_i = x_i -\frac1n\sum_{j=1}^n x_j +\frac{n-2i+1}{2n}.$
P.S. The identities and proofs are simplified if we use a non-standard indexing for the $\ x_i.\ $ So suppose we have an indexed set of $\ n\ $ numbers $\ \{x_{-n+1}, x_{-n+3}, \dots, x_{n-3}, x_{n-1}\}.\ $ For $\ n=0\ $ we have just $\{x_0\}.\ $ For $\ n=1\ $ we have the set $\ \{x_{-1}, x_1\}\ $ and so on. It is understood that we will sum from $\ -n+1\ $ to $\ n-1\ $ in steps of $2$. So define our new $$ u_n :=\! \sum_{-n+1\le i<j\le n-1} (x_j\!-\!x_i), \; t_n := \!\sum_{-n+1\le i<j\le n-1} (x_j\!-\!x_i)^2. \tag{15}$$ The first identity is now $$ s_n = u_n - t_n = \Big(\sum_i i\ x_i\Big) + \Big(\sum_i x_i\Big)^2 - n\Big(\sum_i x_i^2\Big). \tag{16}$$ The second identity is now $$ s_n = -n\ \sum_i \Big(x_i-\frac1n\sum_j x_j-\frac{i}{2n}\Big)^2 + \frac{n^2-1}{12}. \tag{17}$$