Code covered by the BSD License  

Highlights from
Financial Seminar Demos

from Financial Seminar Demos by Michael Weidman
Demos commonly used at The MathWorks financial modeling seminars.

garchFXdemo
function garchFXdemo
% GARCH(1,1) Foreign Exchange (FX) Simulation/Estimation Demo.
%
% Simulate the FX returns of the Deutchemark/British Pound as a GARCH(1,1) 
% model. Then, given the simulation results, estimate the coefficients that
% generated the simulated returns via Maximum Likelihood Estimation (MLE).
%
% The sample autocorrelation function (ACF) for both the simulated and 
% estimated processes are shown in an effort to highlight pre-estimation
% and post-estimation diagnostic tools available in the GARCH Toolbox.
%
% Also, a MATLAB 2-D contour plot and a 3-D surface plot of the MLE 
% log-likelihood objective function are displayed.
%
% Note that the coefficients of the FX model are based on the 1996 Bollerslev
% and Ghysels data set available at the Journal of Business & Economic 
% Statistics archive. The original data set consists of 1974 daily observations 
% of the percentage nominal returns for the DEM/GBP exchange rate, and has been 
% used recently as a benchmark data set for testing GARCH modeling software.
%
% In what follows, we fit a simulated FX return series to the GARCH Toolbox
% default model with the following parameters:
%
%    y(t)  = -0.006 + e(t)
%    h(t)  =  0.01 + 0.8h(t-1) + 0.15e^2(t-1)
%
% where 
%
%    y(t) = FX return series modeled as constant plus additive GARCH(1,1) noise
%    e(t) = FX innovations (i.e., residuals) noise process
%    h(t) = conditional variance of e(t) modeled as a GARCH(1,1) model
%

%
% Set the callback string that gets evaluated when we click on any axis
% of a multiple-tiered MATLAB figure window.
%

callbackString = strcat('currentObjects  =  copyobj(gca , gcf);' , ...
                        'figureHandle    =  figure(''NumberTitle'' , ''off'' , ''Name'' , get(get(gca , ''Title'') , ''String''));' , ...
                        'set(currentObjects , ''Parent'' , figureHandle), set(gca , ''Position'' , [0.13 0.11 0.775 0.815] , ''ButtonDownFcn'' , '''')');
%
% Initialize the coefficients of a GARCH(1,1) model based on the 1996 
% Bollerslev & Ghysels data set. 
%
% The coefficients used here are not exactly the same as the accepted 
% benchmark, but are very close. Thus, the simulated processes based upon 
% the following specification is a good approximation to real-world 
% volatility processes.
%

spec  =  garchset('C' , -0.006 , 'K' , 0.01 , 'GARCH' , 0.80 , 'ARCH' , 0.15);

%
% Now simulate the noise, conditional standard deviation, and observed 
% return processes given the FX specification. Note that we have chosen to 
% simulate 2000 daily observation, or about 8 years of daily FX data.
%

[simulatedResiduals, simulatedSigmas , simulatedReturns]  =  garchsim(spec, 2000);

%
% Now that we have simulated the GARCH(1,1) model, let's estimate the model 
% coefficients that generated the simulated return series. In other words, let's
% reverse-engineer the simulation process via the estimation process.
%

[coefficients, errors, LLF, estimatedResiduals, estimatedSigmas] = garchfit(simulatedReturns);

%
% Display the results just for comparison.
%

garchdisp(coefficients, errors)

%
% Reconstruct the observed FX return series given the estimated residuals and
% knowledge that the simulated return series is modeled as a simple constant plus
% additive GARCH(1,1) noise (i.e., y(t) = C + e(t)).
%

estimatedReturns  =  garchget(coefficients,'C')  +  estimatedResiduals;

%
% Now that all the simulation/estimation is complete, let's graphically 
% analyze the results.
%
% The following 4 MATLAB Figure Windows will appear:
%
%    (1) A tiered plot with 3 rows and 2 columns:
%
%        The first column displays the simulated noise, conditional standard 
%        deviation, and observed return FX processes. These 3 graphs in the 
%        first column are all in BLUE, indicating SIMULATION results. 
%
%        The second column is identical to the first column, except that all 
%        graphs are in RED, indicating ESTIMATION results.
%
%        Notice how closely each estimated results is to it's simulated counterpart.
%
%    (2) A tiered plot with 2 rows and 2 columns: 
%
%        The first column displays the autocorrelation function (ACF) of the 
%        simulated FX returns in the top graph (showing lack of correlation or 
%        persistence in the return series) and the ACF of the squared simulated 
%        FX returns in the bottom graph (showing correlation or persistence in 
%        the variance process, and thus the need for GARCH modeling). Again, 
%        notice that the graphs in the first column are related to SIMULATION
%        data, and thus appear in BLUE. The first column represents some simple
%        pre-estimation diagnostic tests.
%
%        The second column displays the ACF's of the standardized residuals (i.e., 
%        the estimated FX residuals dividing by the corresponding standard deviation
%        to access the underlying independent, identically distributed Gaussian 
%        noise process) in the top graph. The bottom graph in the second column 
%        is the sample ACF of the estimated standardized residuals squared. Again,
%        notice that the graphs in the second column are related to ESTIMATION
%        data, and thus appear in RED. The second column represents some simple
%        post-estimation diagnostic tests.
%
%        Notice that both RED graphs in the second column (i.e., ESTIMATION) have
%        most (if not all) ACF values inside the 95% confidence bands, indicating
%        that most of the original correlation in the squared FX return series 
%        has been accounted for by the GARCH(1,1) variance model.
%
%    (3) A contour plot of the log-likelihood function in the plane of the
%        GARCH(1,1) h(t-1) and e^2(t-1) coefficients. To create this plot, we 
%        evaluate the log-likelihood objective function on a grid in the plane
%        of the h(t-1) and e^2(t-1) coefficients, holding the model constants
%        'K' and 'C' fixed at the estimated values. 
%
%    (4) A 3-D surface plot of the log-likelihood function created from the 
%        same data used to the contour plot in Figure (3) above.
%
% NOTES:
%
%    (1) The SIMULATION results shown in Figures (1) and (2) above are shown
%        in BLUE. The ESTIMATION results shown in Figures (1) and (2) are
%        shown in RED.
%
%    (2) All of the tiered plots in Figures (1) and (2) have a "zoom-in" feature: 
%
%          When the user clicks on any individual axis, the graph just
%          clicked on will then appear in a new figure window for
%          closer inspection.
%
%    (3) Please note that Figures (1) and (2) are actually generated on the fly
%        using data generated when a user runs this function. In contrast,
%        Figures (3) and (4) are based on previously stored enumerative search 
%        results because it would take too long to create the data on the fly. 
%        Thus, if any of the parameters of the model are changed, then the stored
%        data needed to generate the data in Figures (3) and (4) will need to be
%        updated as well.
%

%
% Create the MATLAB Figure Window to house the 3-by-2 tiered plot described above.
%

figure ('Name'        , 'GARCH(1,1) FX Demo: Innovations, Conditional Standard Deviations, Returns' , ...
        'NumberTitle' , 'Off' , 'MenuBar' , 'none' , 'WindowButtonDownFcn' , callbackString)

%
% Plot the 3 graphs of the SIMULATED noise, conditional standard deviation, 
% and observed return FX processes in the first column, color-coded in BLUE.
%

subplot(3,2,1), plot(simulatedResiduals , 'blue'), grid('on'), ylabel('Innovation')        , title('Simulated Innovations')
subplot(3,2,3), plot(simulatedSigmas    , 'blue'), grid('on'), ylabel('Standard Deviation'), title('Simulated Standard Deviations')
subplot(3,2,5), plot(simulatedReturns   , 'blue'), grid('on'), ylabel('Return')            , title('Simulated Returns')

%
% Plot the 3 graphs of the ESTIMATED noise, conditional standard deviation, 
% and observed return FX processes in the second column, color-coded in RED.
%

subplot(3,2,2), plot(estimatedResiduals , 'Red'), grid('on'), ylabel('Innovation')        , title('Estimated Innovations')
subplot(3,2,4), plot(estimatedSigmas    , 'Red'), grid('on'), ylabel('Standard Deviation'), title('Estimated Standard Deviations')
subplot(3,2,6), plot(estimatedReturns   , 'Red'), grid('on'), ylabel('Return')            , title('Estimated Returns')

%
% Create the MATLAB Figure Window to house the 2-by-2 tiered ACF plots described above.
%

figure ('Name'        , 'GARCH(1,1) FX Demo: Sample Autocorrelation Functions' , ...
        'NumberTitle' , 'Off' , 'MenuBar' , 'none' , 'WindowButtonDownFcn' , callbackString)

%
% Plot the 2 graphs of the ACF of the SIMULATED FX returns and the
% squared simulated FX returns. 
%
% The top plot should only by chance fall outside the 95% confidence bands, 
% indicating that very little correlation exists in the raw FX return series. 
% The bottom plot should consistently fall outside the 95% confidence bands, 
% indicating that potentially significant correlation exists in the squared 
% FX return series. This, in turn, indicates some level of persistence in 
% the variance process that can be identified by a GARCH model. 
%
% These ACF plots are also color-coded in BLUE.
%

subplot(2,2,1) , autocorr(simulatedReturns)    , xlabel(''), ylabel('Sample ACF'), title('ACF of Simulated Returns')
set(findobj(gca , 'Type' , 'Line') , 'Color' , 'Blue')
subplot(2,2,3) , autocorr(simulatedReturns.^2) , xlabel(''), ylabel('Sample ACF'), title('ACF of Simulated Returns Squared')
set(findobj(gca , 'Type' , 'Line') , 'Color' , 'Blue')

%
% Plot the 2 graphs of the ACF of the ESTIMATED FX return process and it's
% squared values. At this point, we standardize the residuals by dividing 
% each estimated return by it's corresponding estimated conditional 
% standard deviation to access the underlying independent, identically 
% distributed Gaussian residuals.
%
% As before, the top plot should only by chance fall outside the 95% confidence 
% bands, again indicating that very little correlation exists in the standardized
% residuals. However, in contrast to the simulation counterpart, the bottom plot 
% of the ACF of the squared, standardized residuals should now fall inside the
% the 95% confidence bands, indicating that most (if not all) of the original 
% correlation in the squared FX return series has been accounted for by the 
% GARCH(1,1) variance model. 
%
% These ACF plots are color-coded in RED.
%

subplot(2,2,2) , autocorr(estimatedResiduals./estimatedSigmas) 
xlabel ('')    , ylabel('Sample ACF'), title('ACF of Estimated Standardized Residuals')
set(findobj(gca, 'Type', 'Line'), 'Color', 'Red')

subplot(2,2,4) , autocorr((estimatedResiduals./estimatedSigmas).^2)
xlabel ('')    , ylabel('Sample ACF'), title('ACF of Estimated Standardized Residuals Squared')
set(findobj(gca, 'Type', 'Line'), 'Color', 'Red')

%
% Plot the Maximum Likelihood Estimation surface & contours.
%
% The following command was used to generate the contour & surface data:
%
% [LLF, X, Y] = garch11grid(simulatedReturns, coefficients.C, coefficients.K, [0.82:0.0005:0.85], [0.11:0.0005:0.14]);
%

if exist('garchFXdemo.mat' , 'file')

   load   ('garchFXdemo.mat')

   figure ('Name' , 'Maximum Likelihood Estimation Contours' , 'NumberTitle' , 'off')
   contour(X , Y , LLF, 200);
   xlabel ('GARCH Coefficient'), ylabel('ARCH Coefficient')
   title  ('Log-Likelihood Contours')
   colorbar

   figure ('Name' , 'Maximum Likelihood Estimation Surface' , 'NumberTitle' , 'off')
   surf(X, Y, LLF)
   axisLimits  =  axis;
   axis([X(1) X(end) Y(1) Y(end) axisLimits(5) axisLimits(6)])
   xlabel ('GARCH Coefficient'), ylabel('ARCH Coefficient'), zlabel('Log-Likelihood')
   title  ('Log-Likelihood Surface')

end

%
% The following commands simply tile the first 2 Figure windows in an order
% convenient for explanation in front of an audience.
%

figure(2)
figure(1)

Contact us at files@mathworks.com