Suppose you have a portfolio of stocks with returns R_1, R_2, …, R_n, each with weights x_1, x_2, …, x_n respectively. The portfolio is the sum of the returns multiplied by their respective weights.

R_{portfolio} = x_1R_1 + x_2R_2 + ... + x_nR_n

From probability theory, we know that

Var(R_{portfolio}) = Var(x_1R_1 + x_2R_2 + ... + x_nR_n) =

Var(x_1R_1) + Var(x_2R_2) + ... + Var(x_nR_n) +

Cov(x_1R_1, x_2R_2) + ... + Cov(x_1R_1, x_nR_n) +

… + Cov(x_2R_2, x_1R_1) + ... + Cov(x_2R_2, x_nR_n) +

… + Cov(x_{n-1}R_{n-1}, x_nR_n)

=

x_1^2Var(R_1) + x_2^2Var(R_2) + ... + x_n^2Var(R_n) +

x_1x_2Cov(R_1, R_2) + ... + x_1x_nCov(R_1, R_n) +

… + x_2x_1Cov (R_2, R_1) + ... + x_2x_nCov(R_2, R_n) +

… + x_{n-1}x_nCov(R_{n-1}, R_n)

For an equally-weighted portfolio, x_1 = x_2 = ... = x_n = 1/n, and so above becomes

(1/n)^2Var(R_1) + (1/n)^2Var(R_2) + ... + (1/n)^2Var(R_n) +

(1/n)^2Cov(R_1, R_2) + ... + (1/n)^2Cov(R_1, R_n) + ... +

(1/n)^2Cov(R_2, R_1) + ... + (1/n)^2Cov(R_2, R_n) +

… + (1/n)^2Cov(R_{n-1}, R_n)

= (1/n)^2(Var(R_1) + Var(R_2) + ... + Var(R_n)) +

(1/n)^2(Cov(R_1, R_2) + ... + Cov(R_{n-1}, R_n))

Now here is the trick to the formula (according to a Corporate Finance book, i wish i was clever enough to find this on my own)

There are a total of n^2 terms, n of which are variance terms and the rest (n^2 - n) are covariance terms. We therefore have:

n(1/n)^2((Var(R_1) + Var(R_2) + ... + Var(R_n))/n) +

(n^2 - n)(1/n)^2((Cov(R_1, R_2) + ... + Cov(R_{n-1}, R_n))/(n^2 -n)

(we are dividing the first term by n because there are n variance terms, and the second term by n^2 - n because there are these many covariance terms. also, don’t forget to multiply by the same amount so that the number doesn’t change)

Because ((Var(R_1) + Var(R_2) + ... + Var(R_n))/n is just the average variance of individual stocks, and ((Cov(R_1, R_2) + ... + Cov(R_{n-1}, R_n))/(n^2 -n) is just the average covariance between any pair of different stocks, the formula becomes

n(1/n)^2(Average variance of individual stocks) + (n^2 - n)(1/n)^2(Average covariance of any pair of different stocks)

n(1/n)^2 = 1/n, and (n^2 - n)(1/n)^2 = 1 - 1/n

so we finally arrive at the famous formula for an equally-weighted portfolio:

Var(R_{equallyweightedportfolio}) = (1/n)(Average variance of individual stocks) + (1 - 1/n)(Average covariance of any pair of different stocks)

If n increases to infinity, the first term goes to zero and the second term approaches 1. That is, as n increases, the contribution of individual stock variance decreases.

edit: fixing Mathjax

edit: for some reason the mathjax cuts off when the formula is too long…

edit: i’m trying to make it show all the formula cutoff by mathjax, but mathjax is turning it into bullet points and i don’t know why.