Portfolio Variance of a Portfolio of N Assets in Python
np.dot(weights.T,np.dot(covar,weights))# array([[ 0.00064654]])
For 2D numpy arrays, np.dot
is equivalent to matrix multiplication.
For a 2D array np.dotted with a 1D array, np.dot
is equivalent to matrix-vector multiplication.
For 1D arrays, np.dot
is equivalent to the inner product.
For numpy arrays, the *
performs element-wise multiplication (with broadcasting if necessary).
weights.T*np.matrix(covar)*weights#matrix([[ 0.00064654]])
Alternatively, if you convert covar
to a np.matrix
, then *
is equivalent to matrix multiplication.