% This code generates GARCH(1,1) data
% n is the desired sample size.  ng data points are generated and all
% are discarded, except the last n.
clear all;
n=7000;

% ng must be greater than the sample size

ng=10000;
mu=1; %Offset
alpha_0=2; %constant
alpha_1=0.3;
beta_1=0.1;
a=zeros(ng,1);
h=alpha_0*ones(ng,1);
w=zeros(ng,1);

% process iis generated with Gaussian White noise

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

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

% Garch estimation
%dist = struct('Name','t','DoF',NaN);
dist = struct('Name','Gaussian');
ModelToEst = garch('Offset',NaN,'Constant',NaN,'ARCHLags',1,'GARCHLags',1,'Distribution',dist); 
[EstMdl, EstParamCov, LogL, info] = estimate(ModelToEst,a);
EstMdl
EstParamCov
LogL
info


figure(1)
plot(t,a)
box off
grid off
xlabel('Time Index')
ylabel('Value of GARCH(1,1) process')
title('GARCH(1,1)')


figure(2)
normplot(a)
title('GARCH(1,1)')



