Application of Matlab for Finance: Individual Coursework
Please upload your soft copy together with the code les to the HUB by
the given deadline.
This coursework is designed to test your ability to apply MATLAB to two real world
nancial applications: portfolio optimization and options pricing. To complete the
coursework successfully, you will need to be comfortable with: linear/matrix algebra,
csv. le import/export, matrix indexing colon operator, for/while loop, simulation,
user-de ned function, datetime object manipulation, and help function.
The output of this coursework should be a 1-3 page report (including gures and
tables) with a brief discussion of the results and a .m le of your matlab program.
NOTE: your matlab le should be divided into subsections using %% iden-
ti er, and you MUST write clear comments in your matlab program to
explain what your code is trying to accomplish.
1
Application of Matlab for Finance: Individual Coursework
Question 1: Portfolio Optimization and Performance Backtesting
In this question, we are going to nd out how to construct a optimal portfolio in a
universe of 30 US stocks and backtest the performance of several portfolio strategies
using historical data.
The dataset equity dataset 30.csv can be downloaded from the HUB. It contains
the daily closing prices (adjusted for stock splits and cash/stock dividends) for 30
blue-chip stocks over past 10 years. The dataset has 31 columns in total, with the rst
column being the date index in ISO format (yyyy-mm-dd) and the rest 30 columns
containing price data for 30 stocks respectively.
(1) Import the csv data le equity dataset 30.csv into Matlab, and extract the
numeric price data into a variable named px mat. px mat should be a T-by-N
matrix where T = 2641 and N = 30.
(2) Calculate the log return series for all 30 stocks according to formula
Ri;t = ln(pi;t) ln(pi;t 1) 8i2[1;30];t2[2;2641]:
Save the resulting stock return matrix as ret mat.
(3) Split the whole sample period into In-Sample (training dataset, from 2005-01-01
to 2012-12-31) and Out-of-Sample (testing dataset, from 2013-01-01 to 2015-
06-30) periods. Use two variables ret mat is and ret mat oos to store the
stock return matrix for In-Sample and Out-of-Sample periods respectively. (
hints: you may want to use datenum and datestr to convert between serial
date number and the string representation of date. To nd the index of the
Application of Matlab for Finance: Individual Coursework
cut-o date, try to use the function find(.). Note: the return matrix is one-
observation short than the price matrix or date vector).
(4) Calculate the historical average daily return for each stock and the historical
covariance matrix by using only the In-Sample dataset. (hints: check the
help pages for mean(A,dim) and cov)
(5) Consider the following 4 portfolios:
{ Benchmark 1/N portfolio: allocate capital equally to each of the 30 stocks.
wi = 130 8i2[1;30]
{ Portfolio 1: Maximize Sharpe ratio (short-selling is allowed).
maxw E[rp] rf
{ Portfolio 2: Maximize Sharpe ratio (no short-selling)
maxw E[rp] rf
Application of Matlab for Finance: Individual Coursework
{ Portfolio 3: Minimize portfolio variance (short-selling is allowed).
Use the historical mean return vector and covariance matrix calculated in part
(4) for the in-sample period, nd the optimal weights for portfolio 1, 2 and 3
respectively.
(6) Assume that we can buy/sell any fraction of shares and ignore the transac-
tion cost associated with rebalancing portfolio daily. Backtest the performances
of benchmark 1/N portfolio and optimized portfolio 1, 2 and 3 based on the
weights using the In-sample dataset. Construct and plot the cumulative return
for the four trategy strategies. Calculate annualized portfolio return, variance
and Sharpe Ratio. Comment on which strategy perform. better. (hints: You
might want to consider a log 10 scale when plotting.)
(7) Next, evaluate the investment strategy based on the in-sample data with out-
sample observations. Does the best performance strategy for the in-sample pe-
riod still outperform. in the out-of-sample period?
(8) Repeat (5) and (6) with out-of-sample data. Pick one trading strategy, comment
on the weights di erence between the weights here versus the weights from (6).
Application of Matlab for Finance: Individual Coursework
Question 2: Simulation Option Pricing
Consider a stock pays no dividends, has an expected return 0:15 per annual with
continuous compounding and an annual volatility of 0:3. Observe today’s price $100
per share with t = 1252 year. And the stock price follows a log-normal process as
below.
ln(St) = ln(S0) + ( 22 ) t+ tp t (1)
St = S0e( 22 ) t+ tp t
(2)
(1) Simulate the path of log price of the stock over 1 year, and plot the stock price
visually (hint: simulate 252 t for t = 1252;:::;1);
(2) Simulate 10;000;000 times for the stock price, calculate today’s price of an Eu-
ropean Call and Put option on this stock with K = 100; r = 0:1; T = 1 year
C(S;T) = max(S K;0) P(S;T) = max(K S;0)
{ Di erent from 1, now t = 1 year, simulate 10;000;000 times for T as now
we want to know di erent possible values of ST at the option mature date;
{ The nal price is the average across the simulated prices;
(3) Create a function perform. the Black-Scholes Formula to determine the above
5
Application of Matlab for Finance: Individual Coursework
options’ price, with user-de ned input in de ne a call or put.
C(T) = S0N(d1) Ke rTN(d2) P(T) = Ke rTN( d2) S0N( d1)
d1 = ln(S0=K) + (r + 2=2)TpT
d2 = d1 pT
(4) Compare results of the option prices based on the simulation and Black-Scholes
Formula