Derivative of a summation in order to minimize

Consider $f(C)=\sum_{i=1}^{n}(x_{i}-C)^{2}$ so by chain rule the derivative of $f$ is:

$f'(C)=\sum_{i=1}^{n}2(x_{i}-C)(-1)$.

To minimize at a differentiable point we need that $f'(C)=0$ so the above is $0$ when:

$0=-\sum_{i=1}^{n}(2x_{i}-2C)$

Multiplying both sides by $-1$ we get

$0=\sum_{i=1}^{n}(2x_{i}-2C)=2\sum_{i=1}^{n}x_{i}-2\sum_{i=1}^{n}C=2\sum_{i=1}^{n}x_{i}-2C\sum_{i=1}^{n}1=2\sum_{i=1}^{n}x_{i}-2Cn$.

$\sum_{i=1}^{n}1=n$ since $\sum_{i=1}^{n}a_{i}=a_{1}+...+a_{n}$ by definition and we can take $a_{i}=1$ for all $i$.

So

$2Cn=2\sum_{i=1}^{n}x_{i}$

Dividing by $2n$ we get $C=\frac{\sum_{i=1}^{n}x_{i}}{n}$


First step

Think of the sum as a function. To find a minima/maxima for a certain function we need to find it's derivative and set it to 0. And because we have 2 terms in between the parenthesis, we can't just apply the rule $\frac{\partial}{\partial x} x^n = nx^{n-1}$, but instead we apply the chain rule. So that -2 is from the chain rule.

Second step

Let's denote the derivative of the sum as $S_1$, the when $2S_1 = 0$? It's only possible if $S_1 =0$, so we left out the 2.

Fourth step

$C$ is a constant that's independent from $n$ so after every step we just add $C$. If $n$ is the number of steps then we have added $nC$


Step 1: the $-1$ comes from the chain rule.

Step 2: We set the derivative equal to $0$, then divide both sides by $2$. That's why the $2$ goes away.

Step 4: $C + \cdots + C = nC$. ($C$ appears $n$ times on the left.)