You are on page 1of 27

FINANCE 441

- Investments Matthew Ringgenberg


Fall 2012
Using Excel to Calculate
Markowitz Optimized Portfolios*
*Citation: these notes were prepared using publicly available information including Bodie, Kane, and
Marcus (9th edition) and lecture notes created by Eric Zivot (http://faculty.washington.edu/ezivot/).

Outline
Markowitz Optimization
Review of matrix algebra
Markowitz with matrix algebra

Maximizing the Sharpe Ratio

We want to solve the following maximization


problem:

=

. . = 1

The solution to this problem is given by equation


7.13 in Bodie, Kane, and Marcus (9th ed)
3

Review of Matrix Algebra: Definitions


First, lets define a matrix:
11
21
=

And a vector:

1
2
=

1
2

Here, matrix has


a dimension of n
rows x m columns

The vector has


a dimension of n
rows x 1 column
4

Review of Matrix Algebra: Notation


A bold capital letter is a matrix (i.e., )

Italic capital letters refer to elements in a matrix


(i.e., is the element in row n column m)

A bold lower case letter is a vector (i.e., )

Italic lower case letters refer to elements in a vector


(i.e., is the element in row n)

Review of Matrix Algebra: Addition and Subtraction


To add matrices we add each element:
=

4
7

3
5
, =
1
2

2
4

7
4+3 5+2
+ =
=
8
7+1 2+4

7
6

To subtract matrices we subtract each element:


1 3
43 52
=
=
6 2
71 24

Note: for addition and subtraction, the matrices must have


the same dimension
6

Review of Matrix Algebra: Transpose and Inverse


The transpose of a matrix (denoted by )
interchanges the rows and columns:
1 4
1 2 3
=
then = 2 5
4 5 6
3 6
The inverse of a matrix (denoted by -1) is like the
reciprocal of a number
i.e., the reciprocal of 8 is 1/8
The inverse of is
When you multiply any matrix by its inverse you get the
identity matrix, which is like getting 8 * 1/8 = 1

Review of Matrix Algebra: Scalar Multiplication


We can also multiply matrices
Scalar multiplication
Matrix multiplication
Scalar multiplication is when we multiply each
element in a matrix by a number
4 5
=
= 4
7 2
We can multiply matrix by the scalar, c
44
c =
74

16
54
=
28
24

20
8

Review of Matrix Algebra: Matrix Multiplication


Matrix multiplication is when we multiply a matrix
by a matrix
It can only happen when the dimensions are
correct: the number of columns in must equal
the number of rows in
4
= 7
2

5
3
2 , =
1
3

2 5
7 8

Review of Matrix Algebra: Matrix Multiplication


We multiply the elements in the first row of by
the elements in the first column of and add
them together
4 5
=

43+51
= 73+21
23+31

3 2 5
7 2
1 7 8
2 3
42+57 45+58
72+27 75+28
22+37 25+38
17 43 60
23 28 51
9 25 34

10

Using Matrix Algebra to Create Optimal Portfolios


Lets develop a portfolio with three risky assets
Well define the return vector, r, and the weight

vector, w
=

We can calculate the expected returns:


[ ]

[] = [ ] = =

[ ]

11

Using Matrix Algebra to Create Optimal Portfolios


We can also calculate the variance of returns:
cov( , )
() = cov( , )
cov( , )
2
=

cov( , )
cov( , )
cov( , )

=
2

cov( , )
cov( , )
cov( , )

12

Using Matrix Algebra to Create Optimal Portfolios


What is the expected return on the portfolio?
Using regular algebra we have:
= + +

Using matrix algebra we have:

= = [ ] = + +

13

Using Matrix Algebra to Create Optimal Portfolios


What about the variance of the portfolio?
Last class we saw (for 3 assets) it was:

= w + w + w + 2wA wB A, B + 2wA wC A,C + 2wB wC B ,C


2
p

2
A

2
A

2
B

2
B

2
C

2
C

Using matrix algebra, its much cleaner:


2 = ww = [

2
]

14

Using Matrix Algebra to Create Optimal Portfolios


Remember:

We want to form a risky portfolio that either:

1. Maximizes expected return for a given level


of risk
2. Or, minimizes risk for a given level of return

15

Using Matrix Algebra to Create Optimal Portfolios


In

practice, we usually use the second


method:

Minimize risk for a given level of return

In matrix notation, our optimization problem is:


min 2 = ww s.t.

= w = ,0

and

w1 = 1
16

Using Matrix Algebra to Create Optimal Portfolios

To solve this problem, we use Lagrange Multipliers

Well skip over the Lagrange multiplier part of the


calculation (although its relatively easy to do)

Instead, well go straight to the solution of the


problem (in matrix notation) and well plug it into
Excel

If we specify a target rate of return, the solution (the


optimal weight vector z) is given by:

zx = A1
x b0

17

Using Matrix Algebra to Create Optimal Portfolios

zx = A1
where:
x b0
Ax is a matrix that has 2 the covariance matrix
in the top left and adds two more rows & columns
The

2nd to last column and row contain the expected


return for each asset ()
22 2 2
The last column & row
2 22 2
contains a 1 for each
2
Ax = 2
2
2

asset and a zero

0
otherwise
1
1
1
0

1
1
1
0
0

18

Using Matrix Algebra to Create Optimal Portfolios


where:

zx =

A1
x b0

zx is a vector and the first n rows contain


the optimal weights for our n risky assets
b0 is vector that contains a zero for each
of the n risky assets (in this case, three
zeros for three assets) and then the
second to last row is our target return,
,0 , and the last row is a 1

zx =
4
5
0
0
b0 = 0
,0
1

19

Using Matrix Algebra to Create Optimal Portfolios

The vector zx gives us the optimal weights that


determine the most efficient portfolio, given our
target rate of return

In other words, it gives us a portfolio on the frontier


zx = A1
x b0

Note: if we calculate zx for different target rates of


return, we can trace out the efficient frontier

20

Using Matrix Algebra to Create Optimal Portfolios

But, what if we want the best possible portfolio on


the frontier?

Remember: the optimal portfolio is tangent to the CAL

This tangency portfolio is given by:

1 ( 1)
=
1 1 ( 1)
21

Calculating the Efficient Frontier


Solving for frontier portfolios in Excel is easy:
1. Pick a target expected return, ,0
2. Type Ax and b0 into Excel

3. Compute the inverse of Ax (i.e., compute A1


x )

Use the function =MINVERSE() & hit ctr+shift+enter

4. Multiply A1
x b0 to get the optimal weights, zx

Use the function =MMULT() & hit ctr+shift+enter


See Example Markowitz with Matrix Algebra in Excel.xlsx
The bullet numbers correspond to the steps in the Excel file
22

Calculating the Efficient Frontier

As you pick different target rates of return (,0 ) youll


get different portfolios on the efficient frontier
In fact, you can trace-out the frontier by calculating zx
for many different target returns
It turns out, you can also trace-out the frontier by
combining any two frontier portfolios

If you calculate two frontier portfolios, you can trace-out


the rest of the frontier by creating a portfolio of these two
frontier portfolios and varying the portfolio weights

i.e., put 100% in 1, 0% in the other, 90% / 10%, etc.


23

Calculating the Optimal (Tangency) Portfolio


What if you want the best frontier portfolio?
Solving for the optimal portfolio in Excel is easy too:
7. Type in (the covariance matrix), a row vector of
1s, and (the expected return vector - rf)
8. Compute the inverse of (i.e., compute 1 )

Use the function =MINVERSE() & hit ctr+shift+enter

See Example Markowitz with Matrix Algebra in Excel.xlsx

24

Calculating the Optimal (Tangency) Portfolio


9. Calculate the numerator by multiplying 1 and
the vector ( )
Use the function =MMULT() & hit ctr+shift+enter

10.Calculate the denominator by multiplying 1


and the vector ( ) and then take the
vector of 1s (transposed) and multiply it by the
solution of 1 ( )
11.Finally, divide the numerator and the
denominator

25

Example: using Matrix Algebra to Create Optimal Portfolios

You can invest in 3 possible risky assets and you


calculate the expected return (), standard deviation
() and covariance matrix ()
Stock i i
A
8.9%
B
12.7%
C
5.9%

i
0.10
0.16
0.14

Covariance Matrix ()
A
B
C
0.0100 0.0020 0.0010
A
B
0.0020 0.0256 0.0030
C
0.0010 0.0030 0.0196

26

Example: using Matrix Algebra to Create Optimal Portfolios

What are the portfolio weights for the efficient portfolio


that has a target expected return of 9%?
WA = 56.53%
WB = 20.65%
WC = 22.82%
What are the optimal (tangency) portfolio weights?
WA = 56.38%
WB = 30.20%
WC = 13.42%
Which portfolio has the better Sharpe ratio and why?

27

You might also like