Application of Matlab for Finance: Individual Coursework

profilefanbing910724
MATLAB_CW_1718.pdf

Application of Matlab for Finance: Individual Coursework

Release Date: Monday, 11th September, 2017

Due Date: 17:30 PM Friday, 3rd November , 2017

Please upload your soft copy together with the code files to the HUB by

the given deadline.

This coursework is designed to test your ability to apply MATLAB to two real world

financial applications: portfolio optimization and options pricing. To complete the

coursework successfully, you will need to be comfortable with: linear/matrix algebra,

csv.file import/export, matrix indexing & colon operator, for/while loop, simulation,

user-defined function, datetime object manipulation, and help function.

The output of this coursework should be a 1-3 page report (including figures and

tables) with a brief discussion of the results and a .m file of your matlab program.

NOTE: your matlab file should be divided into subsections using %% iden-

tifier, 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 find 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 first

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 file 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) ∀i ∈ [1, 30], t ∈ [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 find the index of the

2

Application of Matlab for Finance: Individual Coursework

cut-off 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 = 1

30 ∀i ∈ [1, 30]

– Portfolio 1: Maximize Sharpe ratio (short-selling is allowed).

max w

E[rp] − rf σp

s.t. N∑ i

wi = 1

– Portfolio 2: Maximize Sharpe ratio (no short-selling)

max w

E[rp] − rf σp

s.t.

N∑ i

wi = 1 and wi > 0 ∀i ∈ [1, 30]

3

Application of Matlab for Finance: Individual Coursework

– Portfolio 3: Minimize portfolio variance (short-selling is allowed).

min w

σ2p

s.t.

N∑ i

wi = 1

Use the historical mean return vector and covariance matrix calculated in part

(4) for the in-sample period, find 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 difference between the weights here versus the weights from (6).

4

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 = 1 252

year. And the stock price follows a log-normal process as

below.

ln(St) = ln(S0) + (µ− σ2

2 )∆t + σ�t

√ ∆t (1)

St = S0e (µ−σ

2

2 )∆t+σ�t

√ ∆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 = 1

252 , ..., 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)

– Different from 1, now ∆t = 1 year, simulate 10, 000, 000 times for �T as now

we want to know different possible values of ST at the option mature date;

– The final 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-defined input in define 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)T

σ √ T

d2 = d1 −σ √ T

(4) Compare results of the option prices based on the simulation and Black-Scholes

Formula

6