TITLE: Why are these two summations in the calculation of the variance equal?
QUESTION [5 upvotes]: Background: The screenshot below is the book solution to a 1st year probability question (Sheldon Ross self test 7.12). I understand everything except the last equality.
My Question: Is the red box equal to the blue box below? Can you show me step by step how to do it? I'm guessing there's some identity that will make it easier...
My Attempt: Unfortunately in lieu of being able to solve the question using math I put it in python and got different results for the red and blue box... but my script could be wrong. Thanks for your help.
n = 5

redBox = 0
for i in range(1,n):
    for j in range(i+1,n+1):
        redBox += (i-1)*(j-n)
redBox = 2*redBox / ((n-2)**2 * (n-1))

blueBox = 0
for i in range(1,n):
    blueBox += (i-1)*(n-i)*(n-i-1)
blueBox = -blueBox / ((n-2)*(n-1)**2)

print(redBox,blueBox)

and for $n=5$ I get $\text{red box} = -0.277$ vs $\text{blue box} = -0.20833$.
Thanks.

Book solution

REPLY [2 votes]: At a first glance, the sum in the red box looks worse than it is. To get to the blue box you need only to collect common factors and use the formula

$\sum_{k=1}^{N}k = \frac{N(N+1)}{2}$

$$2\sum_{i=1}^{n-1}\sum_{j=i+1}^n \frac{\color{green}{(i-1)}\cdot (j-n)}{\color{blue}{(n-2)(n-1)^2}}= \frac{2}{\color{blue}{(n-2)(n-1)^2}}\sum_{i=1}^{n-1}\color{green}{(i-1)}\boxed{\sum_{j=i+1}^n (j-n)}$$
For the "boxed" sum you may just write down some terms starting with $j=n$ to realize that this is nothing but

$\boxed{\sum_{j=i+1}^n (j-n)} = -(0+1+\cdots +n - (i+1)) = -\sum_{k=1}^{N}k = -\frac{N(N+1)}{2}$ with $N = n-(i+1)=n-i-1$. 

So,
$$\boxed{\sum_{j=i+1}^n (j-n) = -\frac{(n-i-1)(n-i)}{2} }$$
Now, just "plug in" the box and rearrange a bit:
$$\frac{\color{blue}{2}}{(n-2)(n-1)^2}\sum_{i=1}^{n-1}(i-1)\boxed{\sum_{j=i+1}^n (j-n)} = $$ $$\frac{\color{blue}{2}}{(n-2)(n-1)^2}\sum_{i=1}^{n-1}(i-1)\left(\color{blue}{-}\frac{(n-i-1)(n-i)}{\color{blue}{2}} \right) = $$ $$\color{blue}{-}\frac{1}{(n-2)(n-1)^2}\sum_{i=1}^{n-1}(i-1)(n-i)(n-i-1)$$