% SP500.mat is a MATLAB data set with 2 columns. First is closing price
% (adjusted for splits and dividends) from 1/4/1988 - 6/10/2020.
% the S&P 500 index from 01.03.1950 - 09.22.2011.

load SP500.mat;
data = SP500;
p = data(:,1);

p_0 = p(2:end,1);
p_1 = p(1:end-1,1);
r = log (p_0./p_1);
n = size(r,1);
t = (1:1:n);

% maximum likelihood estimators for drift and variance/volatility

mu_hat = mean(r);
sigma2_tilde = (1/(n-1))*sum( (r-mean(r)).^2  );
volat = sqrt(sigma2_tilde);

alpha=0.01;
pd = makedist('Normal','mu',mu_hat,'sigma', volat);
[h,p,ksstat,cv] = kstest(r,pd,alpha);

% If h=1 reject normality and h=0 accept
h

% p-value
p

% Value of the Kolmogorov-Smirnoff statistic
ksstat

% cut-off value
cv


figure(1)
qqplot(r)
grid on
box off

xlabel('Standard Normal Quantiles')
ylabel('Sample Quantiles')
title('QQ Plot Log-returns on S&P500 from 01.04.1988 - 06.10.2020')