# Is there a way using matrix algebra to add portfolios to a covariance matrix of assets?

What I want to do is the following:

Let's say I have two assets 1 and 2, and have a 2x2 covariance matrix.

Then I have two portfolios A and B made of weights from assets 1 and 2.

What I would like to do is create a 4x4 covariance matrix of assets 1 and 2 and portfolios A and B.

I know how to calculate the covariance of the portfolios to the assets, I'm interested if there's a 'shortcut' to creating the 4x4 matrix using matrix algebra vs. building it from parts.

If your two assets are denoted by random variables $$X_1$$, $$X_2$$, with 2x2 covariance matrix $$\mathbf{Q}$$ and the portfolios:

$$Z_1 = w_{11} X_1 + w_{12} X_2$$ $$Z_2 = w_{21} X_1 + w_{22} X_2$$

Then,

$$Cov(Z_1, X_1) = w_{11}Cov(X_1,X_1) + w_{12} Cov(X_2, X_1)$$ , etc.

In matrix algebra:

$$\mathbf{Z} = \mathbf{W} \mathbf{X}$$

The 4x4 covariance matrix, is:

$$\begin{bmatrix} \mathbf{Q} & \mathbf{QW^T} \\ \mathbf{WQ} & \mathbf{WQW^T} \\ \end{bmatrix}$$

Where W is the identity matrix you can verify this reduces to your intuition.

• Appreciate it! How would you go about building the last part in code? In R for example Feb 2, 2021 at 21:52
• in pythons numpy you would just create the Q and W matrices, then uses a matrix multiplication method and then use a block method to piece them together. Look at numpy.array, numpy.matmul, and numpy.block
– Attack68
Feb 2, 2021 at 21:54