Let $r_1,\ldots, r_n$ be real-valued random variables, $\Sigma$ be the covariance matrix, $\mu_i=E(r_i)$, and let $w=(w_1,\ldots,w_n)^\top$ be an arbitrary vector in $\Bbb{R}^n$. Why does $$\mathrm{var}\left(\sum_{i=1}^n w_ir_i\right)=w^\top\Sigma w$$ always hold?
This fact was stated while studying mean-variance portfolio analysis, but was not proved or justified. In this context $r_i$ are the random returns from $n$ assets, and $w_i$ are the "weights" invested on each asset.
I would like to see every detail of the proof, since I am not familiar with covariances, aside from looking up the definition, so it may not be obvious (also my algebra is rusty!).
Feller's two-volume probability textbook calls the "covariance matrix" the "variance", since it's the higher-dimensional analog of the one-dimensional variance, and I'll do that here: $$ \Sigma = \operatorname{var} R = \operatorname{E}((R-\mu)(R-\mu)^\top) \in \mathbb R^{n\times n} \quad \text{where } \mu = \operatorname{E}(R) \in \mathbb R^{n\times 1}. \tag 1 $$ Then we have \begin{align} \operatorname{var}(w^\top R) & = \operatorname{E}(w^\top(R-\mu)(R-\mu)^\top w) \\[10pt] & = w^\top \Big( \operatorname{E}((R-\mu)(R-\mu)^\top) \Big) w \tag2 \\[10pt] & = w^\top\! \operatorname{var} (R)\, w \\[10pt] & = w^\top\Sigma w. \end{align}
Why is $(2)$ true? If $T$ is an $n\times m$ random column vector is $A$ is a constant (i.e. non-random) $k\times n$ matrix and $B$ is a constant $m\times\ell$ matrix, then $$ \operatorname{E}(AT) = A\Big(\operatorname{E}(T)\Big) \text{ and } \operatorname{E}(TB) = \Big( \operatorname{E}(T) \Big) B. \tag 3 $$ Ultimately the proof of that is a matter of linearity of expectation.
So $(3)$ rather than $(2)$ is the heart of the matter.