Covariance and correlation of stock returns
We illustrate how mean, covariances and correlations can be computed using Matlab and how correlation can be visually inspected using scatter plots. We use returns of two commonly used stock market indices, NASDAQ-100 and SP-500.
ELEC-C5211 - Johdatus signaalien tilastolliseen mallintamiseen ja paattelyyn / Prof. Esa Ollila
Contents
Load the data
clear all; % read in the data if it is not already in the workspace NDX100 = hist_stock_data('01012017','01012018','^NDX','frequency','d'); SP500 = hist_stock_data('01012017','01012018','^GSPC','frequency','d') g1 = NDX100.AdjClose(2:end)./NDX100.AdjClose(1:(end-1)); % gross return r1 = g1 - 1; % net return g2 = SP500.AdjClose(2:end)./SP500.AdjClose(1:(end-1)); % gross return r2 = g2 - 1; % net return n = length(r1); % sample length r = [r1 r2]; % n x 2 matrix of returns
SP500 = struct with fields: Date: {251×1 cell} Open: [251×1 double] High: [251×1 double] Low: [251×1 double] Close: [251×1 double] AdjClose: [251×1 double] Volume: [251×1 double] Ticker: '^GSPC'
Plot the data
xlist = cell(1,5); for i=1:5 xlist{i} = NDX100.Date{40*i}; end figure(1); clf subplot(2,1,1); plot(2:(n+1),r1,'LineWidth',2.5) title('NASDAQ-100 daily net returns'); xticks([40:40:220]); axis tight; grid on; hold on; plot([2 (n+1)],[0 0],'--','LineWidth',2.5) xticklabels(xlist) set(gca,'LineWidth',3,'FontSize',16); ax = axis; subplot(2,1,2); plot(2:(n+1),r2,'LineWidth',2.5) title('SP-500 daily net returns'); xticks([40:40:220]); axis tight; grid on; hold on; plot([2 (n+1)],[0 0],'--','LineWidth',2.5) xticklabels(xlist) set(gca,'LineWidth',3,'FontSize',16); axis(ax);

What does the plot tell you about the risk (standard deviations) of these two market indices?
Compute the mean returns and covariance/correlation matrix of returns
mu = mean(r) % estimated mean returns Sigma = cov(r) % covariance matrix Rho = corr(r) % correlation matrix risks = sqrt(diag(Sigma)) % standard deviations
mu = 0.0011 0.0007 Sigma = 1.0e-04 * 0.4177 0.2162 0.2162 0.1757 Rho = 1.0000 0.7982 0.7982 1.0000 risks = 0.0065 0.0042
Q: What can you infer about correlatedness of SP-500 and NASDAQ-100?
scatter plots: visual inspection of correlation between two variables
figure(2); clf plot(r1,r2,'o') set(gca,'LineWidth',3,'FontSize',16); xlabel('$r_1$ (Nasdaq-100)','FontSize',22,'Interpreter','Latex') ylabel('$r_2$ (SP 500)','FontSize',22,'Interpreter','Latex') grid on

Q: Can you already infer the degree and type of correlation from this figure?