The explanations in the sections that follow should help refresh your skills for using matrix
algebra and using MATLAB^{®} functions.

In addition, *Macro-Investment Analysis* by William Sharpe also provides an
excellent explanation of matrix algebra operations using MATLAB. It is available on the web at:

**Tip**

When you are setting up a problem, it helps to "talk through" the units and dimensions associated with each input and output matrix. In the example under Multiplying Matrices, one input matrix has five days' closing prices for three stocks, the other input matrix has shares of three stocks in two portfolios, and the output matrix therefore has five days' closing values for two portfolios. It also helps to name variables using descriptive terms.

Matrix addition and subtraction operate element-by-element. The two input matrices must have the same dimensions. The result is a new matrix of the same dimensions where each element is the sum or difference of each corresponding input element. For example, consider combining portfolios of different quantities of the same stocks (“shares of stocks A, B, and C [the rows] in portfolios P and Q [the columns] plus shares of A, B, and C in portfolios R and S”).

Portfolios_PQ = [100 200 500 400 300 150]; Portfolios_RS = [175 125 200 200 100 500]; NewPortfolios = Portfolios_PQ + Portfolios_RS

NewPortfolios = 275 325 700 600 400 650

Adding or subtracting a scalar and a matrix is allowed and also operates element-by-element.

SmallerPortf = NewPortfolios-10

SmallerPortf = 265.00 315.00 690.00 590.00 390.00 640.00

Matrix multiplication does *not* operate element-by-element. It
operates according to the rules of linear algebra. In multiplying matrices, it helps
to remember this key rule: the inner dimensions must be the same. That is, if the
first matrix is *m*-by-`3`

, the second must be
`3`

-by-*n*. The resulting matrix is
*m*-by-*n*. It also helps to “talk
through” the units of each matrix, as mentioned in Analyze Sets of Numbers Using Matrix Functions.

Matrix multiplication also is *not* commutative; that is, it is
not independent of order. A*B does *not* equal B*A. The dimension
rule illustrates this property. If A is `1`

-by-`3`

matrix and B is `3`

-by-`1`

matrix, A*B yields a
scalar (`1`

-by-`1`

) matrix but B*A yields a
`3`

-by-`3`

matrix.

Vector multiplication follows the same rules and helps illustrate the principles. For example, a stock portfolio has three different stocks and their closing prices today are:

ClosePrices = [42.5 15 78.875]

The portfolio contains these numbers of shares of each stock.

NumShares = [100 500 300]

To find the value of the portfolio, multiply the vectors

PortfValue = ClosePrices * NumShares

which yields:

PortfValue = 3.5413e+004

The vectors are `1`

-by-`3`

and
`3`

-by-`1`

; the resulting vector is
`1`

-by-`1`

, a scalar. Multiplying these
vectors thus means multiplying each closing price by its respective number of
shares and summing the result.

To illustrate order dependence, switch the order of the vectors

Values = NumShares * ClosePrices

Values = 1.0e+004 * 0.4250 0.1500 0.7887 2.1250 0.7500 3.9438 1.2750 0.4500 2.3663

which shows the closing values of 100, 500, and 300 shares of each stock, not the portfolio value, and meaningless for this example.

In matrix algebra, if *X* and *Y* are
vectors of the same length

$$\begin{array}{c}Y=\left[{y}_{1},{y}_{2},\dots ,{y}_{n}\right]\\ X=\left[{x}_{1},{x}_{2},\dots ,{x}_{n}\right]\end{array}$$

then the dot product

$$X\xb7Y={x}_{1}{y}_{1}+{x}_{2}{y}_{2}+\dots +{x}_{n}{y}_{n}$$

is the scalar product of the two vectors. It is an exception to the
commutative rule. To compute the dot product in MATLAB, use `sum`

`(X .* Y)`

or `sum(Y .* X)`

. Be sure that the two vectors have the same
dimensions. To illustrate, use the previous vectors.

Value = sum(NumShares .* ClosePrices')

Value = 3.5413e+004

Value = sum(ClosePrices .* NumShares')

Value = 3.5413e+004

As expected, the value in these cases matches the
`PortfValue`

computed previously.

Multiplying vectors and matrices follows the matrix multiplication rules and process. For example, a portfolio matrix contains closing prices for a week. A second matrix (vector) contains the stock quantities in the portfolio.

WeekClosePr = [42.5 15 78.875 42.125 15.5 78.75 42.125 15.125 79 42.625 15.25 78.875 43 15.25 78.625]; PortQuan = [100 500 300];

To see the closing portfolio value for each day, simply multiply

WeekPortValue = WeekClosePr * PortQuan

WeekPortValue = 1.0e+004 * 3.5412 3.5587 3.5475 3.5550 3.5513

The prices matrix is `5`

-by-`3`

, the
quantity matrix (vector) is `3`

-by-`1`

, so the
resulting matrix (vector) is
`5`

-by-`1`

.

Matrix multiplication also follows the rules of matrix algebra. In matrix
algebra notation, if *A* is an
*m*-by-*n* matrix and
*B* is an
*n*-by-*p* matrix

$$A=\left[\begin{array}{cccc}{a}_{11}& {a}_{12}& \cdots & {a}_{1n}\\ \vdots & \vdots & & \vdots \\ {a}_{i1}& {a}_{i2}& \cdots & {a}_{in}\\ \vdots & \vdots & & \vdots \\ {a}_{m1}& {a}_{m2}& \cdots & {a}_{mn}\end{array}\right],\text{}B=\left[\begin{array}{ccccc}{b}_{11}& \cdots & {b}_{1j}& \cdots & {b}_{1p}\\ {b}_{21}& \cdots & {b}_{2j}& \cdots & {b}_{2p}\\ \vdots & & \vdots & & \vdots \\ {b}_{n1}& \cdots & {b}_{nj}& \cdots & {b}_{np}\end{array}\right]$$

then *C = A***B* is an
*m*-by-*p* matrix; and the element
*c*_{ij} in the
*i*th row and *j*th column of
*C* is

$${c}_{ij}={a}_{i1}{b}_{1j}+{a}_{i2}{b}_{12}+\dots +{a}_{in}{b}_{nj}.$$

To illustrate, assume that there are two portfolios of the same three stocks previously mentioned but with different quantities.

Portfolios = [100 200 500 400 300 150];

Multiplying the `5`

-by-`3`

week's closing
prices matrix by the `3`

-by-`2`

portfolios
matrix yields a `5`

-by-`2`

matrix showing each
day's closing value for both portfolios.

PortfolioValues = WeekClosePr * Portfolios

PortfolioValues = 1.0e+004 * 3.5412 2.6331 3.5587 2.6437 3.5475 2.6325 3.5550 2.6456 3.5513 2.6494

Monday's values result from multiplying each Monday closing price by its respective number of shares and summing the result for the first portfolio, then doing the same for the second portfolio. Tuesday's values result from multiplying each Tuesday closing price by its respective number of shares and summing the result for the first portfolio, then doing the same for the second portfolio. And so on, through the rest of the week. With one simple command, MATLAB quickly performs many calculations.

Multiplying a matrix by a scalar is an exception to the dimension and commutative rules. It just operates element-by-element.

Portfolios = [100 200 500 400 300 150]; DoublePort = Portfolios * 2

DoublePort = 200 400 1000 800 600 300

Matrix division is useful primarily for solving equations, and especially for
solving simultaneous linear equations (see Solving Simultaneous Linear Equations). For example, you want to solve
for *X* in * A***X* =
*B.*

In ordinary algebra, you would divide both sides of the equation by
*A*, and *X* would equal
*B/A*. However, since matrix algebra is not commutative
*(A***X* ≠
*X***A)*, different processes apply. In
formal matrix algebra, the solution involves matrix inversion. MATLAB, however, simplifies the process by providing two matrix division
symbols, left and right (`\`

and `/`

). In
general,

`X = A\B`

solves for `X`

in
`A`

*`X = B`

and

`X = B/A`

solves for `X`

in
`X`

*`A = B`

.

In general, matrix `A`

must be a nonsingular square matrix; that
is, it must be invertible and it must have the same number of rows and columns.
(Generally, a matrix is invertible if the matrix times its inverse equals the
identity matrix. To understand the theory and proofs, consult a textbook on linear
algebra such as *Elementary Linear Algebra* by Hill listed in
Bibliography.) MATLAB gives a warning message if the matrix is singular or nearly so.

Matrix division is especially useful in solving simultaneous linear equations. Consider this problem: Given two portfolios of mortgage-based instruments, each with certain yields depending on the prime rate, how do you weight the portfolios to achieve certain annual cash flows? The answer involves solving two linear equations.

A linear equation is any equation of the form

$${a}_{1}x+{a}_{2}y=b,$$

where *a*_{1},
*a*_{2}, and *b* are
constants (with *a*_{1} and
*a*_{2} not both 0), and
*x* and *y* are variables. (It is a linear
equation because it describes a line in the *xy*-plane. For
example, the equation 2*x* + *y* = 8 describes
a line such that if *x* = 2, then *y* =
4.)

A system of linear equations is a set of linear equations that you usually want to
solve at the same time; that is, simultaneously. A basic principle for exact answers
in solving simultaneous linear equations requires that there be as many equations as
there are unknowns. To get exact answers for *x* and
*y*, there must be two equations. For example, to solve for
*x* and *y* in the system of linear
equations

$$\begin{array}{c}2x+y=13\\ x-3y=-18,\end{array}$$

there must be two equations, which there are. Matrix algebra represents this
system as an equation involving three matrices: *A* for the
left-side constants, *X* for the variables, and
*B* for the right-side constants

$$A=\left[\begin{array}{cc}2& 1\\ 1& -3\end{array}\right]\text{,}X=\left[\begin{array}{c}x\\ y\end{array}\right]\text{,}B=\left[\begin{array}{c}13\\ -18\end{array}\right],$$

where *A***X* =
*B*.

Solving the system simultaneously means solving for *X*. Using
MATLAB,

A = [2 1 1 -3]; B = [13 -18];

X = A \ B

solves for `X`

in `A * X = B`

.

X = [3 7]

So *x* = 3 and *y* = 7 in this example. In
general, you can use matrix algebra to solve any system of linear equations such
as

$$\begin{array}{c}{a}_{11}{x}_{1}+{a}_{12}{x}_{2}+\dots +{a}_{1n}{x}_{n}={b}_{1}\\ {a}_{21}{x}_{1}+{a}_{22}{x}_{2}+\dots +{a}_{2n}{x}_{n}={b}_{2}\\ \vdots \\ {a}_{m1}{x}_{1}+{a}_{m2}{x}_{2}+\dots +{a}_{mn}{x}_{n}={b}_{m}\end{array}$$

by representing them as matrices

$$A=\left[\begin{array}{cccc}{a}_{11}& {a}_{12}& \cdots & {a}_{1n}\\ {a}_{21}& {a}_{22}& \cdots & {a}_{2n}\\ \vdots & \vdots & & \vdots \\ {a}_{m1}& {a}_{m2}& \cdots & {a}_{mn}\end{array}\right],\text{}X=\left[\begin{array}{c}{x}_{1}\\ {x}_{2}\\ \vdots \\ {x}_{n}\end{array}\right],\text{}B=\left[\begin{array}{c}{b}_{1}\\ {b}_{2}\\ \vdots \\ {b}_{m}\end{array}\right]$$

and solving for *X* in *A***X
*= *B*.

To illustrate, consider this situation. There are two portfolios of mortgage-based instruments, M1 and M2. They have current annual cash payments of $100 and $70 per unit, respectively, based on today's prime rate. If the prime rate moves down one percentage point, their payments would be $80 and $40. An investor holds 10 units of M1 and 20 units of M2. The investor's receipts equal cash payments times units, or R = C * U, for each prime-rate scenario. As word equations:

M1 | M2 | |

Prime flat: | $100 * 10 units | + $70 * 20 units = $2400 receipts |

Prime down: | $80 * 10 units | + $40 * 20 units = $1600 receipts |

As MATLAB matrices:

Cash = [100 70 80 40]; Units = [10 20]; Receipts = Cash * Units

Receipts = 2400 1600

Now the investor asks this question: Given these two portfolios and their characteristics, how many units of each should they hold to receive $7000 if the prime rate stays flat and $5000 if the prime drops one percentage point? Find the answer by solving two linear equations.

M1 | M2 | |

Prime flat: | $100 * | + $70 * |

Prime down: | $80 * | + $40 * |

In other words, solve for U (units) in the equation R (receipts) = C (cash) * U (units). Using MATLAB left division

Cash = [100 70 80 40]; Receipts = [7000 5000]; Units = Cash \ Receipts

Units = 43.7500 37.5000

The investor should hold 43.75 units of portfolio M1 and 37.5 units of portfolio M2 to achieve the annual receipts desired.

Finally, element-by-element arithmetic operations are called operations. To
indicate a MATLAB array operation, precede the operator with a period
(`.`

). Addition and subtraction, and matrix multiplication and
division by a scalar, are already array operations so no period is necessary. When
using array operations on two matrices, the dimensions of the matrices must be the
same. For example, given vectors of stock dividends and closing prices

Dividends = [1.90 0.40 1.56 4.50]; Prices = [25.625 17.75 26.125 60.50]; Yields = Dividends ./ Prices

Yields = 0.0741 0.0225 0.0597 0.0744