You are on page 1of 11

Teaching Note/Portfolio Optimization/Dr.

Bulent Aybar

PORTFOLIO OPTIMIZATION BY USING EXCEL SOLVER

1. Create a spreadsheet including asset prices for the chosen frequency. In our case asset prices were
sampled in monthly intervals. In other words, you downloaded monthly prices.
2. We will convert prices into returns. In order to calculate returns we will use formula
R=LN(PRICEt/PRICEt-1)1.

It is ok to you use Pt-Pt-1/Pt-1 . The formula LN(Pt/Pt-1) is used to calculate compounded returns. The difference between these
two methods are more subtle than it looks on the surface. The logarithmic returns aggregate more accurately than simple returns.
I can show this with a simple example. Assume that price of a stock goes up from $50 to $100 in the first year, and then drops
back to $50 in the second year. In this case the simple return in first year is 100% (P1-P0/P0=100-50/50) and -50% in the second
year. The average return over two years R1+R2/2=(100%-50%)/2=25% which is obviously not correct. In contrast, R1=
ln(100/50)=0.6931 and R2= ln(50/100)=-06931 , aggregates correctlt and R1+R2/2=0 as it should be. For an interesting article
about the downside of using logarithmic returns see http://papers.ssrn.com/sol3/papers.cfm?abstract_id=1549328

Teaching Note/Portfolio Optimization/Dr.Bulent Aybar

The screen shot above shows the prices or index levels for six assets/indices. As you see the first
observation is as of 10/3/2005. To calculate Gold return series we will simply type +LN(B3/B2)
in an empty cell (say J2) and then copy this cell from J3 to J61. The column will give us monthly
returns on Gold from 10/03/2005 to 10/1/2010. We will repeat this for all the assets until we
have monthly returns for all the assets in the portfolio.
The return series should look as follows:

3. After we calculate the returns we should calculate the average (mean) return for each asset. We
can calculate mean returns by using function @AVERAGE. The arguments in the function are
the first and last observations for each return series. For instance, to calculate mean return for
Gold series, we will use @AVERAGE(J2:J61). We can write the formula on cell J63 and press
enter. To calculate the mean returns for other assets, all we have to do is copy cell J63 through
K63 to O63. In cells J63 through O63 should have your monthly mean (average) returns.
4. The third step is to calculate the variance-covariance matrix. Variance-Covariance matrix is
necessary to calculate the portfolio variance. This step involves a few sub steps. When we have
only two assets, let say X and Y, the covariance is defined as :
N

(Xi
COV ( X , Y )

X )(Yi Y )

i 1

N 1

When there are more than two assets, covariance can best be calculated by using matrix algebra. Excel
allows us matrix manuplations with a few functions such as @MMULT() and TRANSPOSE().
2

Teaching Note/Portfolio Optimization/Dr.Bulent Aybar

As you can see in the formula above, covariance calculation involves difference between the
observation X and the average of observations (given as X-hat). In order to calculate the covariance
matrix, we will create a new series and we will name it ExcessReturn. We obtain excess return
series simply by subtracting the average of the series from each return observation. For instance, the
mean of GOLD returns is 0.0170 (this is calculated in cell J63). We subtract 0.0170 from all the
observations in column J3 through J61. The following is a screen shot of excess return matrix created
by using the procedure above.

Once we create the excess return matrix, we will do something that is very handy. This is a little
secret kept in excel that will simplify our next few steps. Simply highlight the area from R2 to
W62. This is the area where we have all the excess returns stored. After you highlight the area, go
to the upper right corner of the menu tab, and type EXCESSRETURNS in the corner. The screen
shot below shows how you should do it. Once you type EXCESSRETURNS, hit enter. This step
named our matrix EXCESSRETURN.

Teaching Note/Portfolio Optimization/Dr.Bulent Aybar

Now on, we do not need to type cell references to manuplate the matrix.
Once we name the matrix, we are ready to calculate the Variance Covariance Matrix: Go to an
empty cell in the spreadsheet, say cell Z2, and copy and paste asset labels from Z2 to AE2. Then
use paste/transpose and copy the labels in cells Y3 to Y8.
Bring your cursor to cell Z3 and type the formula :
=mmult(transpose(excessreturns),excessreturns)/59
Note that the 59 in the formula is the number of observations (in our case 5 years of 12 monthly
returns=60) less 1, or N-1, which is the denominator in the covariance formula.
and hit return. You should get #VALUE in cell Z3. Do not panic! Highlight the area under the
asset labels including the formula cell that you created above, bring your curser in the formula
window at the end of the typed formula and simulataneously press Ctrl+Shift+Enter. Now you
should have all the cells to be filled with the covariances among the assets.

Teaching Note/Portfolio Optimization/Dr.Bulent Aybar

5. After we create the Variance-Covariance formula we can calculate the Portfolio Variance. At this
step, we will calculate both portfolio return and portfolio variance. Note that in order to calculate
portfolio return and variance we need the weight of each asset in the portfolio. We will start with
an arbitrary allocation, and include each asset at an equal proportion in the portfolio. Since we
have six assets, each asset will have 1/6 th or 16.67% weight to start with. We will optimize the
weights later.
6. To calculate portfolio return, first copy average asset returns from cells J64:O64. Use copy, paste
special/values/transpose to copy these average (mean) asset returns to cells AH3:AH8. Then, we
will create a column for weights by type +1/6 in cells AI3 throug AI8. These for the moment will
represent the weight assigned to each asset. In adjacent column, simply type 1 in each cell, and
call the column ONES. Also create additional column labels CONSTRAINT PORTRETURN
and PORTVAR. You should have something like the following:

Teaching Note/Portfolio Optimization/Dr.Bulent Aybar

Now we can use matrix multiplication function to calculate portfolio return in column AL (my
label is truncated). Before we do this lets use the naming advantage by highligting column
AH3:AH8 and calling it MEANS, highlighting column AI3:AI8 and calling it WEIGHTS, and
AJ3: AJ8 calling it ONES. Once we do this we are ready to do the calculations:
Bring your cursor to cell AL3 and type =mmult(transpose(weights),means). After you hit enter,
you will see #VALUE, but after you bring cursor in the window and press Ctrl+Shift+Enter, you
should see the portfolio return calculated as 0.00751246.
To calculate constraint, which stipulates that the weights assigned to the assets cannot exceed 1,
we need to type = mmult(transpose(weights),ones) and Ctrl+Shift+Enter. You should get 1 in
cell AK3.
To calculate portfolio variance, we will name variance covariance matrix COV. To do this
highlight the cells Z3: AE8, and type COV in the menu window. After we name the matrix, we
can calculate the portfolio variance in cell AM3 as:
=mmult(mmult(transpose(weights),cov),weights)
Note that in the formula we use two matrix multiplication. First one multiplies transpose of
weights with the covariance matrix, the second one multiplies this resulting matrix with vector of
weights. We should get the portfolio variance in the cell AM3. Dont forget Ctrl+Shift+Enter to
calculate the variance.
6

Teaching Note/Portfolio Optimization/Dr.Bulent Aybar

7. Now, we can use Solver to optimize the weights. SOLVER is an excel add-in package. To find
solver, click on tools on the menu bar. If you see solver in the menu (it should be on the far right
corner), then it is installed and ready to be used. If you don't find solver, then click on tools, AddIns, then check Solver, and click OK. This should give you the capability to use solver. We need
to add one more column right next to PORTVAR, and calculate the portfolio standard deviation
as PORTSTD=(PORTVAR)^0.5
We will minimize PORTSTD by changing the weights.

Teaching Note/Portfolio Optimization/Dr.Bulent Aybar

After we launch the Solver, we see the Solver dialog box/menu above. We set the target cell to
minimize. This is the cell that we have Portfolio Standard Deviation stored. We will minimize
portfolio standard deviation by changing portfolio weights. We specify the range of cells our
initial weights are entered (remember we named the range of cells as WEIGHTS, therefore you
see the WEIGHTS in the window. If we had not named the sales, we would have seen the cell
references in the window.
We enter the constraint we created in cell AK3. We can add more constraints as we will do
shortly. But at this point we are ready to run Solver to calculate the optimum weights minimizing
the portfolio std. After we run Solver, copy and paste the weights as values into another cell
range, as we will repeat this step several times until we get the efficient frontier.

Teaching Note/Portfolio Optimization/Dr.Bulent Aybar

As you see in the above screen shot, new optimum weights were calculated. We create a table
including porfolio return, std, and weights all stored in the same row. We should copy and paste
values from AL3, AN3 and AI3:AI8.
We can create the efficient frontier by minimizing portfolio STD and setting varying portfolio
return levels as additional constraints. For instance in the next iteration of Solver, we can add
AL=0.005 as an additional constraint and run solver once more.

Teaching Note/Portfolio Optimization/Dr.Bulent Aybar

As you see in the Solver dialogue box, in the second iteration we added a new constraint, which is
a given portfolio return, in this case arbitrarily chosen 0.005. Solver produced a new set of
weights for this level of return that would minimize the standard deviation of the portfolio. After
repeating this process for other levels of return, we can create the efficient frontier.
The process produced the following return-risk combinations:

PORTRETURN

PORTSTD

0.0030
0.0050
0.0075
0.0100
0.0125

0.0268
0.0275
0.0302
0.0344
0.0398

GOLD

SP500

MSCI_EM

MSCI_EAFE

40.03%
45.18%
51.59%
58.00%
64.41%

60.17%
57.71%
54.67%
51.62%
48.57%

-51.93%
-41.03%
-27.49%
-13.94%
-0.39%

-19.02%
-32.71%
-49.73%
-66.75%
-83.77%

ISHARES_CHINA

FTSE100

4.79%
6.66%
8.99%
11.32%
13.65%

65.97%
64.18%
61.96%
59.74%
57.53%

Plotting Portfolio Returns (Y axis) against Portfolio Standard Deviation, produces the following
scatter diagram:
10

Teaching Note/Portfolio Optimization/Dr.Bulent Aybar

11

You might also like