# Matrix Operations

## Matrix Multiplication

A key matrix operation is that of multiplication.

### The product of two vectors

Consider the task of portfolio valuation. This requires the multiplication of the number of shares of each security by the corresponding price per share, then the summation of the results. A simple matrix operation can accomplish this easily. Suppose that:

```   price {1*assets} =
54 21

quantity {assets*1} =
1
2
```

Let value be the product of price and quantity:

```    value = price*quantity
```

In this case:

```
value = 96
```

To compute the value, one multiplies matrix (here, vector) price by matrix (here, vector) quantity.

To understand this process, it is useful to represent each number by a symbol:

```    price =
p1   p2

quantity =
n1
n2

value =
p1*n1 + p2*n2
```

The first number in price is multiplied by the first number in quantity, then the second number in price is multiplied by the second number in quantity. The process continues until the end is reached, at which time all the products are summed.

Rather clearly, this cannot be done unless the number of columns in the first matrix equals the number of rows in the second. Put somewhat differently, the inner dimensions of the two matrices must be the same. This is always required in matrix multiplication and should be checked in advance. Here:

```     price {1*assets} *quantity {assets*1} ===> value {1*1}
```

Note that the information in the curly brackets verifies that the multiplication can take place, since the inner dimensions are the same (assets). Such information also indicates the dimensions of the answer, which is given by the outer dimensions (here: 1 by 1).

In general, the product obtained by multiplying two matrices will have the same number of rows as the first matrix, and the same number of columns as the second. For example:

```     {2*3} times {3*5} ==> {2*5}
{3*2} times {2*4} ==> {3*4}
{1*2} times {2*1} ==> {1*1}
```

The last case is the one in the example. More generally, multiplying a row vector times a column vector always produces a scalar.

To repeat, it is good practice (and often necessary) to think about the dimension of an answer before performing any matrix multiplication. When doing so, one can also check to make certain that the inner dimensions are the same, as is required. The general scheme is:

```    {a*b} times {b*c} will produce {a*c}
```

### The product of a matrix and a vector

When one or more of the matrices to be multiplied is a table, the process is simply one of repeated vector multiplications. Consider, for example, the determination of the value of a portfolio on three different days (Monday, Tuesday, Wednesday):

Here, there are three sets of prices. The Price Table is:

```       Bond  Stock
Mon   54    21
Tue   55    18
Wed   56    27
```

while the Price Matrix is:

```   54    21
55    18
56    27
```

The dimensions of Price are {days*assets} -- in this case, {3*2}.

Now, consider multiplication of Price times quantity, to obtain value:

```      Price {days*assets} *quantity {assets*1} ===> value {days*1}
```

Given the quantity vector q:

```    Bond     1
Stock    2
```

The result is the column vector:

```     96
91
110
```

The first number in the result value is obtained by multiplying the vector in the top row of matrix Price by the column vector quantity, giving the same result as before. The second number in the result is obtained by multiplying the vector in the second row of matrix Price by the column vector quantity, and so on. Using symbols:

```   Price =
p11   p12
p21   p22
p31   p32

quantity =
n1
n2

value =
p11*n1 + p12*n2
p21*n1 + p22*n2
p31*n1 + p32*n2
```

Recall that value is {days*1}. Hence, the associated table is:

```   Mon   96
Tue   91
Wed  110
```

The value of the portfolio was \$96 on Monday, \$91 on Tuesday, and \$110 on Wednesday.

## The product of two matrices

When two tables are multiplied, the process is simply expanded, with each column of the result obtained by using the corresponding column of the second matrix. For example, consider the task of finding the values of two portfolios on each of three days. In this case, Quantity is itself a matrix. In table form:

```         PortA PortB
Bond     1    5
Stock    2    2
```

In matrix form:

```   1    5
2    2
```

The product is a matrix showing the value of each portfolio on each of the three days:

```     Price {days*assets} *quantity {assets*portfolios}
===> Value {days*portfolios}
```

In table form:

```       PortA  PortB
Mon    96    312
Tue    91    311
Wed   110    334
```

### The sum of two matrices

If two matrices have the same dimensions, they may be added together. The result is a new matrix with the same dimensions in which each element is the sum of the corresponding elements of the previous matrices. For example, consider the following tables:

```   portA:
Bond   1
Stock  2

portB:
Bond   5
Stock  2
```

To find the total amounts held in the two portfolios, simply add the corresponding matrices:

```    portAll = portA + portB
```

In table form:

```   portAll:
Bond   6
Stock  4
```

### The sum of a matrix and a scalar

It is also possible to add a constant to every element in a matrix. For example:

```    portPlus = portAll + 5
```

Gives:

```
portPlus:
Bond   11
Stock   9
```

## Matrix Subtraction

Matrix subtraction is like addition. Each element of one matrix is subtracted from the corresponding element of the other. If a scalar is subtracted from a matrix, the former is subtracted from every element of the latter. For example:

```   portA:
Bond   1
Stock  2

portB:
Bond   5
Stock  2

portB - portA:
Bond   4
Stock  0

portB - 1:
Bond   4
Stock  1
```

## Other Element-by-element Operations

Addition and subtraction of matrices operate on an element-by-element basis. In some cases it is desirable to perform multiplication, division or exponentiation in the same manner. We follow the MATLAB conventions, preceding the relevant operator with a dot (period) to indicate that such an element-by-element operation is desired.

### Element-by-element operations with matrices

Here are examples involving vectors:

```   portA:
Bond   1
Stock  2

portB:
Bond   5
Stock  2

portA .* portB:
Bond   5
Stock  4

portA ./ portB:
Bond   0.2
Stock  1.0

portB .^ portA:
Bond    5
Stock   4
```

### Element-by-element operations with a matrix and a scalar

Element-by-element operations can also be performed with a matrix and a scalar. For example:

```   portA .* 5:
Bond    5
Stock  10

portA ./ 5:
Bond   0.2
Stock  0.4

portA .^ 3:
Bond    1
Stock   8
```

## Matrix Inversion

Thus far, we have not discussed matrix division; only array division. There is a matrix construct similar to that of division, and it is central to much of the work of the Analyst. The key ingredient is the use of the inverse of a matrix, to which we now turn.

First, a few preliminaries.

A square matrix has the same number of rows and columns. An identity matrix is a square matrix with ones on the diagonal from upper left to lower right and zeros elsewhere. For example:

```   I =
1 0 0
0 1 0
0 0 1
```

Such a matrix is often denoted I.

The product of an identity matrix (of the right size) and a column vector is the column vector, as can be seen by applying the rules for matrix multiplication. Thus, if:

```   v =
3
4
5

I*v ==> v
```

(read: I times v gives v).

More generally, the product of any matrix M and an identity matrix with the same number of columns as M will be the original matrix:

```   I*M ==> M
```

as can be seen by working through the operations involved in matrix multiplication.

The inverse of a square matrix is a matrix of the same size that, when multiplied by the matrix, gives an identity matrix of the same size. The inverse of a matrix is sometimes written with a "-1" superscript. We use instead the more computer-friendly MATLAB form:

```   inv(M)
```

where M is a square matrix.

By definition:

```    inv(M)*M = I
```

Note that only square matrices can have inverses (although not all do).

To see why matrix inversion is similar to division, consider a {1*1} matrix -- i.e. a scalar -- with a value of 5. The identity matrix of the same size will also be a scalar, in this case the single value 1. From this it follows that the inverse of the original matrix (scalar) will be the reciprocal of its value. Thus:

```   (1/5)*5 = 1
```

Multiplication by the inverse of a matrix is like dividing by the matrix, except this is strictly true only if the matrix is {1*1}.

## Solving Simultaneous Linear Equations

Matrix inversion is often used to solve a set of simultaneous linear equations. Consider a situation in which there are two states of the world ("weather is good", "weather is bad") and two securities (Bond, Stock). Matrix Payoff {states*assets} shows the payments made by each security in each state of the world. Vector quantity {assets*1} shows the composition of a portfolio. Vector result {states*1} shows the payments that will be received from the portfolio in each possible state of the world. Below, we show all three in table form:

```   Payoff:
Bond Stock
good   60   40

quantity:
Bond   1
Stock  2

result = Payoff*quantity:
good   140
```

Thus the portfolio will provide \$140 if the weather is good. If the weather is bad it will only provide \$80.

Now, assume that an investor would like to receive \$240 if the weather is good and \$150 if the weather is bad. The problem is to determine the portfolio (quantity) that will produce the desired payment vector.

Consider the equation for the computation:

```   Payoff*quantity = result
```

Note that Payoff is square, so it is possible to compute its inverse, barring complications to be discussed later. We multiply both sides of the equation by this inverse (a "legal" matrix operation):

```   inv(Payoff)*Payoff*quantity = inv(Payoff)*result
```

But the product of the inverse and the original matrix is the identity matrix, so:

```   I*quantity = inv(Payoff)*result
```

But the product of an identity matrix and a vector is the vector. Thus:

```   quantity = inv(Payoff)*result
```

This is precisely what we want -- an equation for a portfolio (quantity) that will provide the desired set of cash flows (result)!

The three components are shown below, with the resulting values shown in bold:

```   result:
good   240

inv(Payoff):
-0.0056    0.0222
0.0333   -0.0333

quantity:
Bond    2
Stock   3
```

Thus the desired result can be achieved with a portfolio of 2 bonds and 3 stocks.

Any set of simultaneous linear equations for which there is a solution can be solved in this manner. It may seem that the requirement that the matrix of coefficients be square is overly restrictive. However, to solve a set of such equations requires precisely as many equations as there are unknowns, so the matrix of "left-hand sides" (here, Payoff) must have as many rows (equations) as it does columns (variables).

Unfortunately, sometimes this won't work. It is impossible to take the inverse of some matrices, even though they are square. In such cases the matrix in question is said to be singular. In typical investment applications this will occur when a strategy is not truly independent and can be provided with some combination of other included strategies. When this occurs, the programming system being used is likely to complain that it cannot take the needed inverse because the matrix in question is singular (or very nearly so). This is a signal that the economics of the original problem formulation need to be re-examined.

## The Transpose of a Matrix

It is not unusual to find that a matrix is the "wrong way around" for a needed calculation. More precisely, its rows should be columns and its columns should be rows. Happily, there is a standard operation that "turns around" a matrix (or vector).

The transpose of a matrix is, in effect, the matrix rotated in this manner. For example, if M is:

```   1 2 3
4 5 6
```

then M' (read: M-prime or M-transpose) is:

```   1 4
2 5
3 6
```

This is sometimes denoted by appending a "T" as a superscript after M , but we will use the MATLAB version M'.

## Multiple Operations

To facilitate exposition, we have generally restricted our examples to one matrix or array operation. Sometimes we have put the result on the left; and sometimes on the right. Moreover, we have used an arrow when it appeared useful and an equality sign at other times. When writing commands to be executed by a programming system, of course, rather strict rules of syntax must be followed. Generally, the result must be written first, followed by an equality sign, followed by an expression indicating the desired computations. Such expressions can include multiple matrix and/or array operations, if desired. For example:

```   D = inv(A)*(b*c)
```

This would be perfectly legal if the dimensions of A, b and c were appropriate. The sense of the equality sign is that of assignment. Thus the statement really says: "D should be assigned the result obtained by multiplying the inverse of A times the product of b and c."

Statements such as this, which are designed to be operated on by a programming system, are generally written without bold fonts, since such subtleties would be lost on the processor, even if they could be presented to it.