0% found this document useful (0 votes)
129 views

MATH2070 Computer Project: Organise Porject Fold

The document provides code examples for importing financial data into MATLAB, calculating stock returns, and performing exploratory data analysis including plotting time series, calculating correlations, and visualizing relationships between returns. The codes cover organizing a project folder, importing data, creating returns, plotting time series, calculating correlation matrices, and identifying minimum and maximum correlations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

MATH2070 Computer Project: Organise Porject Fold

The document provides code examples for importing financial data into MATLAB, calculating stock returns, and performing exploratory data analysis including plotting time series, calculating correlations, and visualizing relationships between returns. The codes cover organizing a project folder, importing data, creating returns, plotting time series, calculating correlation matrices, and identifying minimum and maximum correlations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MATH2070 Computer Project

Table of Contents
Organise Porject Fold .......................................................................................................... 1
Start MATLAB ................................................................................................................... 1
Import Data ........................................................................................................................ 1
Create Returns .................................................................................................................... 2
Create Time Series Plots ...................................................................................................... 2
Get Correlation Coefficient Matrix (plots) ............................................................................... 3

The codes below are just some examples for importting data and exploratory data analysis. Hope they can help you to
write your own codes.If you don't know a function, in console window, type the function's name to get information.

Organise Porject Fold


%Create a folder named MATH2070 (or MATH2090), if you have not done so
yet.
% In MATH2070, create another folder named as Computer Project
% Save data (csv file) and OFM project 2019 (pdf file) into the
folder.

Start MATLAB
%to find out your current working directory by typing pwdin console
window
% >> pwd
%>> 'D:\Usyd\2019\S2\MATH2070\Computer Project'
%path of Wen's working directory.

% If your working directory is not the one data is saved,


% in console window typing
%>> cd 'project fold path'
% Check if the directory has been changed:
%>> pwd

Import Data
Import data as table, matrix, or vectors.

%Import as Table: data object takes more memery, each column has a
name.
%Date will be imported.
%If imported data as Table, name the data as Data_Tbl, say
%Import as matrix, less memery, no names for columns
%Date will not be imported
%If imported as matrix, name the data as data_mt, say

%If imported as vectors, MATLAB will create name for each vector

1
MATH2070 Computer Project

size(Data_Tbl)%1698 rows, 33 columns


%Get varaible names
VarNames=Data_Tbl.Properties.VariableNames;
VarNames

%delete columns 1 and 10 from matrix,


mydata=data_mt;
mydata(:, [1 10])=[];
%delete column from a table:
myTbl=Data_Tbl;
myTbl=removevars(myTbl, 'DowInc');

Create Returns
data_ln=log(mydata);
ret_mt=diff(data_ln);
clear data_ln;
%Save all objects
save Project
%A MATLAB data structure named Project.mat will appear in your fold.
%To open this structure,
load Project.mat

Create Time Series Plots


ts1 = timeseries(mydata(:, 1),1:1698); %convert a column of a matrix
into a time series
ts1.TimeInfo.StartDate = '01-Jan-2013';%Set starting date
ts1.Time = ts1.Time - ts1.Time(1); % Express time relative to the
start date.
ts1.Name = 'Stock xx';
ts1.TimeInfo.Units = 'days';
ts1.TimeInfo.Format = 'mmm/yyyy';

figure
plot(ts1)
title('Price of Stock XX')
xlabel('MM/YYYY')
ylabel('US$')
%Try other stocks

%Plot returns

%convert a column of a matrix into a time series


ts.2TimeInfo.StartDate = '01-Jan-2013';%Set starting date
ts2.Time = ts2.Time - ts2.Time(1); % Express time relative to the
start date.
ts2.Name = 'Stock xx';
ts2.TimeInfo.Units = 'days';
ts2.TimeInfo.Format = 'mmm/yyyy';

figure

2
MATH2070 Computer Project

plot(ts2)
title('Return of Stock XX')
xlabel('MM/YYYY')
ylabel('%')

%Get minimum returns and dates


[retmin, min_ind]=min(ret_mt);
myName=VarNames;
myName([1 10])=[];
ret_min=[myName', retmin', Dates(min_ind)];

Get Correlation Coefficient Matrix (plots)


ret_corr=corr(ret_mt);
%or
%rr=corrplot(ret_mt);

%Convert matrix to table:


ret_corr_Tbl=array2table(ret_corr, 'VariableNames',myName, 'RowName',
myName);
%Export data
writetable(ret_corr_Tbl , 'COrrelationMatrix.xlsx','Sheet',1,'Range','A1', 'WriteRo
%Get minimum corr
[corr_min1, minind1]=min(ret_corr);
[corr_min, corr_minind]=min(min(ret_mt));
[corr1, ind1]=find(ret_corr=corr_min)
corr_min1(corr_minind)
x=ret_corr;
[mm,ii]=min(x(:))
[I_r I_c]=ind2sub(size(x),ii)
[myName(I_r) myName(I_c) x(I_r, I_c)]

%Get max corrret_corr


dum=ret_corr;
for i = 1:size(ret_corr, 1)
dum(i,i)=0
end;

[MM,JJ]=max(dum(:))
[J_r J_c]=ind2sub(size(dum),JJ)
[myName(J_r) myName(J_c) dum(J_r, J_c)]

%Scatter plots
figure
subplot(2,1,1)
scatter(ret_mt(:, J_r), ret_mt(:,J_c))
h=lsline;
h.Color='r';
h.LineStyle='--';
subplot(2,1,2)
scatter(ret_mt(:, I_r), ret_mt(:,I_c))
h=lsline;
h.Color='g';

3
MATH2070 Computer Project

h.LineStyle='--';

Published with MATLAB® R2018a

You might also like