clear all
load AAPL.mat;
dd = AAPL;
n=size(dd,1);
date = dd(:,1);

% returns
sp = dd(:,5);
sp_0 = sp(2:n,1);
sp_1 = sp(1:n-1,1);
r = log (sp_0./sp_1);
size(r,1)
% time index
t_ = (1:1:size(r,1));

% Estimation of ARMA-GARCH

distg = struct('Name','Gaussian');
dists = struct('Name','t','DoF',NaN);

% Model 1
ModelToEstg = garch('Constant',NaN,'ARCHLags',1,'GARCHLags',1,'Distribution',distg); 
[EstMdl, EstParamCov, LogL, info] = estimate(ModelToEstg,r);
EstMdl


% Model 2
FMdlEstt= arima('ARLags',1 ,'Variance',garch('Constant',NaN,'ARCHLags',1,'GARCHLags',1,'Distribution',dists));
EstMdlt=estimate(FMdlEstt,r);
EstMdlt





%[rf rmse]=forecast(EstMdl,10,'Y0',r(1:16000));

%m=[rf r(16001:16010)];
% figure(1)
% plot(t_,m(:,1),t,m(:,2))
% box off
% grid off
% xlabel('Time Index')
% ylabel('returns S&P')
% title('Returns S&P')
% 
% figure(2)
% plot(t,w)
% box off
% grid off
% xlabel('Time Index')
% ylabel('Value of Student-t process with v=6 d.f.')
% title('White Noise')
% 
% figure(3)
% normplot(a)
% title('GARCH(1,1)')
% 
% figure(4)
% normplot(w)
% title('Student-t')

