Inputs:
- Co-variance matrix of stocks.
For example, the co-variance between IBM / FB is 0.1, co-variance IBM / Wells Fargo is 1, co-variance between Wells Fargo / 0.2 etc etc etc
- Weights
I have 30% of my portfolio in IBM and 50% in FB and 20% in Wells Fargo
Question:
How do I calculate variance of the overall portfolio
Letting $x_i$ be the weight of the $i$th stock, so $\sum_i x_i=1$, and letting $(\sigma_{ij})_{i,j=1}^n$ be the covariance matrix, then the variance of the portfolio is $$\sum_{i=1}^n\sum_{j=1}^nx_ix_jσ_{ij}.\tag1$$
This summation can be simplified a bit, since there are repeats; you need to add up the values of $x^2_i\sigma^2_{ii}$ for each $i$ (note $\sigma^2_{ii}$ is the variance of the $i^\text{th}$ stock) and then add up $2x_ix_jσ_{ij}$ for each pair $i<j$.
The reason for this is that if $R$ is the return of the portfolio, and $R_i$ is the return of the $i$th stock, then $$ R=\sum_i x_iR_i $$ so $$ Var(R)=Var(\sum_i x_iR_i) $$ Now, use the rule for the variance of a sum: $$ Var(X+Y)=Var(X)+Var(Y)+2Cov(X,Y) $$ Doing this repeatedly leads to the expression (1).