%% Infer EGARCH Model Conditional Variances  
% Infer conditional variances from an EGARCH(1,1) model with known
% coefficients. When you use, and then do not use presample data, compare
% the results from |infer|.

% Copyright 2015 The MathWorks, Inc.


%% 
% Specify an EGARCH(1,1) model with known parameters. Simulate 101 conditional
% variances and responses (innovations) from the model. Set aside the first
% observation from each series to use as presample data. 
Mdl = egarch('Constant',0.001,'GARCH',0.8,...
               'ARCH',0.15,'Leverage',-0.1);

rng default; % For reproducibility
[vS,yS] = simulate(Mdl,101);
y0 = yS(1); 
v0 = vS(1); 
y = yS(2:end);
v = vS(2:end);

figure
subplot(2,1,1)
plot(v)
title('Conditional Variances')
subplot(2,1,2)
plot(y)
title('Innovations')     

%% 
% Infer the conditional variances of |y| without using any presample data.
% Compare them to the known (simulated) conditional variances. 
vI = infer(Mdl,y);

figure
plot(1:100,v,'r','LineWidth',2)
hold on
plot(1:100,vI,'k:','LineWidth',1.5)
legend('Simulated','Inferred','Location','NorthEast')
title('Inferred Conditional Variances - No Presamples')
hold off    

%%
% Notice the transient response (discrepancy) in the early time periods
% due to the absence of presample data.  

%% 
% Infer conditional variances using the set-aside presample innovation,
% |y0|. Compare them to the known (simulated) conditional variances. 
vE = infer(Mdl,y,'E0',y0);

figure
plot(1:100,v,'r','LineWidth',2)
hold on
plot(1:100,vE,'k:','LineWidth',1.5)
legend('Simulated','Inferred','Location','NorthEast')
title('Inferred Conditional Variances - Presample E')
hold off    

%%
% There is a slightly reduced transient response in the early time periods.  

%% 
% Infer conditional variances using the set-aside presample variance, |v0|.
% Compare them to the known (simulated) conditional variances. 
vO = infer(Mdl,y,'V0',v0);

figure
plot(v)
plot(1:100,v,'r','LineWidth',2)
hold on
plot(1:100,vO,'k:','LineWidth',1.5)
legend('Simulated','Inferred','Location','NorthEast')
title('Inferred Conditional Variances - Presample V')
hold off    

%%
% The transient response is almost eliminated.  

%% 
% Infer conditional variances using both the presample innovation and conditional
% variance. Compare them to the known (simulated) conditional variances. 
vEO = infer(Mdl,y,'E0',y0,'V0',v0);

figure
plot(v)
plot(1:100,v,'r','LineWidth',2)
hold on
plot(1:100,vEO,'k:','LineWidth',1.5)
legend('Simulated','Inferred','Location','NorthEast')
title('Inferred Conditional Variances - Presamples')
hold off    

%%
% When you use sufficient presample innovations and conditional variances,
% the inferred conditional variances are exact (there is no transient
% response).
