BE368 - Lab Class 3
BE368 - Lab Class 3
Work through the following exercises using Matlab. Remember that you can type
and run all of your commands in a new script file in the Editor, making it easier to
correct mistakes and reuse previous commands. To run a specific command or set of
commands you have written in the Editor, simply select the code you want to run with
the cursor, then right-click and then select ‘Evaluate Selection’.
For the lab class this week, we will use the data contained in the Excel file named
BE368 - lab class 3 data.xlsx that you can download from the course’s Moodle page.
This file contains five years of data from 30/09/2015 to 30/09/2020 for the prices of
Apple (AAPL) and Microsoft (MSFT) stocks, with one price value for each working day.
1. Start by importing the data contained in the Excel file into Matlab using the
Import tool in Matlab. Import the complete dataset into a table object. Now
create a timetable version of the dataset named data tt from the table.
2. Using the rules for indexing timetable objects, create the following objects using
your timetable data tt:
3. Next we will use the Apple and Microsoft price data to calculate returns (i.e. %
price changes) for these two stocks. The quickest way to do this is using the
Matlab function price2ret(varname), which converts price data stored in the
vector/matrix variable named varname to returns.
(a) Create a numeric matrix version of the dataset named data pr containing all
the data for both Apple and Microsoft’s stock prices.
(b) Use the ‘price2ret’ function to convert the prices in data pr to returns. Note
that the ‘price2ret’ function computes decimal returns (i.e. a value of 0.01
represents a 1% return) and so you will also need to multiply the return values
obtained by 100 if you prefer percentage returns (so a value of 1 represents a
1% return). Save the returns into a matrix named data ret.
1
(c) Note that when we compute returns, we loose one observation at the start of
the sample period because we cannot compute the change in price when we
do not know the previous value. Create a datetime vector named tmp dates
that contains all of the dates in data tt except the first date. Dates are stored
in the Properties of a timetable object with the name RowTimes, so we can
index them with .Properties.RowTimes.
(d) Create a cell array named tmp names that contains the variable names in
data tt.
(e) Create a new timetable object containing the returns using the following com-
mand: data ret tt = array2timetable(data ret, ’RowTimes’, tmp dates,
’VariableNames’, tmp names);
4. In this final question we will perform some basic numerical and graphical analysis
of the data for Apple and Microsoft.
(a) Compute the sample means of stock returns for Apple and Microsoft using
all observations.
(b) What was the standard deviation of returns for Microsoft during 2016? What
about during 2019?
(c) During September 2016, what was the largest single daily return for Apple
stock? Over the same time period, was the largest return for Microsoft larger
or smaller than this value?
(d) Create a figure that plots the price of Apple stock over the complete sample
period using the ‘plot’ command.
(e) Use the ylabel(label) and title(title) commands to add a suitable title
and label to the vertical axis of your figure. If you are not sure how to do
this, search the Matlab user documentation for title and ylabel.
(f) Create a new figure and plot the returns for both Apple and Microsoft on the
same figure. Add appropriate axis labels and a title as before. Now we also
need to add a legend to identify which line corresponds to which stock price.
We can do this using the ‘legend’ command and a cell array containing the
variable names (which you should already have from Q3 (d)). See the help
page for the ‘legend’ function to see how it works.