clear all
% Sample size
n=4000;
n
% ng must be greater than the sample size
ng=7000;

% parameters

alpha_0=1;
beta_1=0;
alpha_1=0.5;
phi_0 = 1;
phi_1 = 0.3;
a=zeros(ng,1);
r=zeros(ng,1);
h=(alpha_0/(1-alpha_1-beta_1))*ones(ng,1);

w=zeros(ng,1);

for i=2:ng
    h(i) = alpha_0+alpha_1*a(i-1).^2+beta_1*h(i-1);
    a(i) = sqrt(h(i))*normrnd(0,1);
    r(i) = phi_0+phi_1*r(i-1)+a(i);
end

t=(1:1:ng-n);
a=a(n+1:ng);
r=r(n+1:ng);


% Estimation of AR-GARCH

dist = struct('Name','Gaussian');
FMdlEst = arima('ARLags',1,'Variance',garch('Constant',NaN,'ARCHLags',1,'GARCHLags',1,'Distribution',dist));
estimate(FMdlEst,r)

% Estimation of AR-ARCH

dist = struct('Name','Gaussian');
FMdlEstA = arima('ARLags',1,'Variance',garch('Constant',NaN,'ARCHLags',1,'Distribution',dist));
estimate(FMdlEstA,r)


