Matrix Algebra and Multiplication Using Excel

advertisement
Matrix Algebra and Multiplication Using
Excel
by Jim Finnegan, CFA
The previous “Back to Basics” column (Sept/Oct 2004) introduced
matrix algebra as a powerful tool to simplify and easily perform
calculations, such as volatility, for a portfolio of N risky assets. The
column defined the basic rules for matrix algebra and illustrated the
mechanics of matrix multiplication by manually solving for the
volatility of a four-asset portfolio ( P) where each asset had an equal
weight (w i) and the variance of each asset’s excess returns plus
covariance of each asset’s excess returns with all other assets were
given in a variance-covariance matrix. [For a quick review, CLICK
HERE]
This column continues with the same example and shows how using
Excel can greatly facilitate all the calculations involved when two
matrices are multiplied. Any computer spreadsheet provides an
excellent visual environment for laying out two-dimensional (row and
column) matrices. And most (like Excel) provide built-in functions
that automatically perform matrix algebra calculations such as
transposing, multiplying or inverting a matrix. The trick is to correctly
define the inputs so that you don’t end up with an error message, or
worse, a result that is incorrect.
The basic inputs to the example are as follows. W is a 4-x-1 (4 rows x
1 column) matrix containing the relative weights (w) of each asset (A,
B, C and D) in the portfolio.
V is a 4 x 4 (4 rows x 4 columns) matrix containing the variance of
excess returns for each asset (e.g.,  2 A) and the covariance of excess
returns for each asset with every other asset (e.g.,  2 AB for the covariance of excess
returns between assets A and B).
The matrix algebra formula for calculating the volatility of this four-asset portfolio ( P)
is:
The term W T is a new matrix which is the transpose of the matrix W. It is this equation
we will solve in Excel using the spreadsheet’s matrix algebra capabilities – specifically
the “=TRANSPOSE()” and the “=MMULT()” functions.
The Basic Steps
Remember from the last column (rules for matrix multiplication) that (1) process is not
symmetrical (A . B does not equal B . A with rare exceptions), and (2) when two matrices
are multiplied, their product is a new matrix with the number of rows equal to the first
matrix in the multiplication and the number of columns equal to the second matrix in the
multiplication.
So we begin by multiplying W T times V. Ah, but first we have to determine (by
transposing W) what W T equals. W, as shown in Figure 1, is a 4 x 1 (4 rows x 1
column) matrix. So we know its transpose (W T) will be a 1 x 4 matrix. Select any empty
cell and use your cursor to highlight three more cells to the right so that you have
highlighted a 1 row x 4 column space. Enter the formula “=TRANSPOSE(” into the
active cell of this range. You will see a smart tag appear asking for an array input.
FIGURE 1
Next, select the range of cells that contain the values of W for the value of the array
argument that will be the input to the TRANSPOSE calculation (Figure 2).
FIGURE 2
Finally (and this is the tricky part), hold down the SHIFT and CTRL keys on your
keyboard while pressing the Enter key to complete the calculation. You must hold down
SHIFT and CTRL to tell Excel you want the results calculated as a new array (i.e.,
matrix) in the 1 row x 4 column region you initially highlighted (Figure 3). If you don’t
do this, you will only get a result in the active cell of this range.
FIGURE 3
Okay, now we have the value of W T. Multiply W T times the matrix V. We know W T
is a 1-x-4 matrix and V is a 4-x-4 matrix. Hence their product (call it W T.V) will be a 1x-4 (1 row x 4 columns) matrix. So begin by selecting an empty cell and highlighting
three more cells to the right to create a highlighted area of 1 row x 4 columns (Figure 4).
Then, in the active cell of this range, type “=MMULT(”. As was the case with the
transpose function, several smart tags will appear, asking for the array arguments for this
multiplication.
FIGURE 4
Next, select the range of cells that contain the values of W T (for array 1) and V (for
array 2) as the arguments that will be the input to the MMULT calculation (Figure 5).
FIGURE 5
Finally, (you are going to get sick of me emphasizing this – but it is critical) hold down
the SHIFT and CTRL keys on your keyboard while pressing the Enter key to complete
the calculation. You must hold down SHIFT and CTRL to tell Excel you want the results
calculated as a new array (i.e., matrix) in the 1 row x 4 column region you initially
highlighted (Figure 6). If you don’t do this, you will only get a result in the active cell of
this range.
FIGURE 6
We’ve now calculated a matrix which is the product of W T times V. As a final step to
calculate  P, we need to multiply this term by the matrix W (in this order). W T .V is a
1-x-4 matrix and W is a 4-x-1 matrix. The product of these two will be a 1-x-1 (1 row x 1
column) matrix, or a single cell, on the spreadsheet. Select an empty single cell and enter
the MMULT function (Figure 7).
FIGURE 7
Next, select the range of cells that contain the values of W T .V (for array 1) and W (for
array 2) as the arguments that will be the input to the MMULT calculation (Figure 8).
FIGURE 8
Finally, (last time, I promise) hold down the SHIFT and CTRL keys on your keyboard
while pressing the Enter key to complete the calculation (Figure 9). You can take the
square root of the result yourself to arrive at the value of  P.
FIGURE 9
Like most functions in Excel, matrix functions can be “nested” into a single formula if
you want to avoid the step-by-step approach we just illustrated. Figure 10 shows the onestep calculation formula.
FIGURE 10
Closing Thoughts
You probably noticed that Excel adds curly brackets – { and }– around the TRANSPOSE
and MMULT formulas you enter (Figure 11). The brackets are inserted automatically as
part of that SHIFT and CTRL press Enter step I harped on. These brackets indicate that
the formula is an array calculation and the result in each cell is a member of the array.
For example, try clearing the contents of just one cell in one of the arrays you calculated
for W T or W T . V. Excel won’t let you because that cell is not alone – it is a single
element of an array you defined.
FIGURE 11
CLICK HERE to download the Excel spreadsheet used in this column
Download