Why does $\operatorname{var}\sum_{i=1}^n w_ir_i=w^\top\Sigma w$ hold? 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!).
 A: 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.
A: \begin{align}
\mathrm{var}\bigg(\sum_{i=1}^n w_ir_i\bigg) &=E\left(\bigg(\sum_{i=1}^nw_ir_i-E\bigg(\sum_{i=1}^nw_ir_i\bigg)\bigg)^2\right)\qquad\text{defn of variance}\\
&=E\left(\bigg(\sum_{i=1}^nw_ir_i-\sum_{i=1}^nw_iE(r_i)\bigg)^2\right)\qquad\text{linearity}\\
&=E\left(\bigg(\sum_{i=1}^n(w_ir_i-w_i\mu_i)\bigg)^2\right)\qquad\text{combine sums}\\
&=E\bigg(\sum_{i=1}^n\sum_{j=1}^nw_ir_iw_jr_j-w_i\mu_iw_jr_j+w_ir_iw_j\mu_j-w_i\mu_iw_j\mu_j\bigg)\quad(\ast)\\
&=E\bigg(\sum_{i=1}^n\sum_{j=1}^nw_iw_j(r_ir_j-\mu_ir_j+r_i\mu_j-\mu_i\mu_j)\bigg)\qquad\text{factor out }w_iw_j\\
&=\sum_{i=1}^n\sum_{j=1}^nw_iw_jE(r_ir_j-\mu_ir_j+r_i\mu_j-\mu_i\mu_j)\qquad\text{linearity}\\
&=\sum_{i=1}^n\sum_{j=1}^nw_iw_j\big\{E(r_ir_j)-\mu_iE(r_j)+\mu_jE(r_i)-\mu_i\mu_j\big\}\qquad\text{linearity}\\
&=\sum_{i=1}^n\sum_{j=1}^nw_iw_j\big\{E(r_ir_j)-E(r_i)E(r_j)\big\}\qquad`-t+t=0\text{'}\\
&=\sum_{i=1}^n\sum_{j=1}^nw_iw_j\cdot\mathrm{cov}(r_i,r_j)\qquad\text{defn of covariance}\\
&=w\Sigma w^\top\\
\end{align}
