# Variance of a portfolio

In this blog post the author mentions the following:

[... Given several arrays holding returns for a portfolio], one should calculate the standard deviation via the following function call:

(1) $\sigma= \sqrt{w^{T}Cw}$, where $C$ is the covariance matrix of the returns $R = p^Tw$ as measured by numpy

This is in contrast to this other formula which should not be used:

(2) $\sigma = \text{std}\left(R^T.w\right)$

my question is why?

I think the same question can be rephrased as follows:

$\sigma^2$ should be calculated as:

(3) $\sigma^2 = \sum_i w_i^2 \sigma_{i}^2 + \sum_i \sum_{j \neq i} w_i w_j \sigma_i \sigma_j \rho_{ij}$ where $\rho_{ij}$ is the Pearson product-moment correlation between the returns on assets ''i'' and ''j''

and not as:

(4) $\sigma^2 = \sum_i w_i^2 \sigma_{i}^2$

again, why?

# Trying to derive the formula

Property 3 here reads: $\operatorname{cov}(\mathbf{A X} + \mathbf{a}) = \mathbf{A}\, \operatorname{cov}(\mathbf{X})\, \mathbf{A^{\rm T}}$

Now, let's consider $\mathbf{A} = w$ and $\mathbf{X}=p$. Both are $(N,1)$ matrices, with $N$ being the number of assets in the portfolio. If I do the math above I end up with multiplying matrices of the following dimensions:

$(N,1) \;x \;(N,N) \;x \;(1,N)$

which results in a new matrix $(N,N)$. If I then do the square root of this matrix result I don't end up with a scalar quantity as I presume one does in in Eqs. (1) an (3). What am I missing?

• Look at property 3 of en.wikipedia.org/wiki/Covariance_matrix#Properties . Apply it to $R = w^Tp$, with $w^T$ playing the role of A and p playing the role of X. – Mark L. Stone Jan 18 '16 at 0:34
• Thanks @MarkL.Stone That's great, although I still fail to see how one ends up with a scalar from property 3. (I edited the Q above to clarify specifically what I am looking for). – Josh Jan 18 '16 at 2:41
• Let's say that p is n by 1. then the covariance in question will be 1 by n times n by n times n by 1, which comes out to a 1 1 by 1 covariance matrix, which is the variance. Taking the square root produces the standard deviation. – Mark L. Stone Jan 18 '16 at 3:15
• Typo in preceding comment: That should be "1 by 1 covariance", not 1 1 by 1 "covariance" – Mark L. Stone Jan 18 '16 at 3:37
• Thanks @MarkL.Stone I have updated the question to expand your comments. I think I almost follow you, but somehow I am still missing something. – Josh Jan 18 '16 at 15:27

This is a compilation of comments resulting in the OP's understanding.

Property 3 of the covariance matrix shows how to calculate the covariance of a {matrix (or vector)} times {a vector having known covariance}: $\operatorname{cov}(\mathbf{A X} + \mathbf{a}) = \mathbf{A}\, \operatorname{cov}(\mathbf{X})\, \mathbf{A^{\rm T}}$.

Since $R = p^Tw$, we can replace $A=w^T$ and $X=p$ above, ending up with:

$\text{cov}(R) = w^T \text{cov}\left(p\right) w$

and since $cov(p) = C$, we have:

$\text{cov}(R) = w^T C w$

Now, if R is N x 1, then $C$ is N x N, and $w$ is N x 1, we end up with the following dimensions for $cov(R)$:

1 x N times N x N times N x 1 = 1 x 1.

This 1 x 1 covariance matrix is also known as the variance. Taking the square root provides the standard deviation.

Note that equation (2) in the posted question does not account for non-zero correlation (covariance) among the components of p, and so is not valid unless all correlations across components are zero, in which case the covariance "property 3" calculation reduces to equation (2) as a special case.

• As a practical matter, there is HUGE cross-sectional correlation in returns (eg. stocks in the same industry tend to be correlated; there's even correlation by size, book to market ratio, etc...). Ignoring cross-sectional covariance terms would be a mistake of epic proportions. – Matthew Gunn Apr 27 '16 at 4:34