Equipped with the portfolio covariance matrix, Equation 11-3 then provides the formula
for the expected portfolio variance.
Equation 11-3. General formula for expected portfolio variance
In Python this all again boils down to a single line of code, making heavy use of NumPy’s
vectorization capabilities. The dot function gives the dot product of two vectors/matrices.
The T or transpose method gives the transpose of a vector or matrix:
In [ 43 ]: np.dot(weights.T, np.dot(rets.cov() * 252 , weights))
# expected portfolio variance
Out[43]: 0.024929484097150213
The (expected) portfolio standard deviation or volatility is then only one square
root away:
In [ 44 ]: np.sqrt(np.dot(weights.T, np.dot(rets.cov() * 252 , weights)))
# expected portfolio standard deviation/volatility
Out[44]: 0.15789073467797346