可以参见我的博客:
Matlab中做GARCH Estimation
先看懂matlab中的帮助:
U(t) = sqrt(H(t))*v(t), where v(t) is an i.i.d. sequence ~ N(0,1).
The GARCH(P,Q) coefficients K, A, B are subject to constraints:
(1) K > 0
(2) A(i) >= 0 for i = 1,2,...P
(3) B(i) >= 0 for i = 1,2,...Q
(4) sum(A(i) + B(j)) < 1 for i = 1,2,...P and j = 1,2,...Q
The coefficient vectors A and B are each entered as comma-separated lists. The
number of elements in A and B determines the model orders P and Q, respectively.
When you have entered the desired process parameters, click the SIMULATE button
to create time sequences of length N of innovations U(t) and conditional
variance H(t). The sequence U(t) will then appear in the top plot, and the
conditional volatility (the square root of H(t)) will appear in the bottom plot.
To estimate, and make forecasts from, the GARCH(P,Q) parameters from the U(t)
process you just simulated (i.e., to reverse-engineer the process for
comparison), click the FORECAST button. The parameters K, A, B are then
estimated via Maximum Likelihood and displayed in the corresponding ESTIMATED
frame.
To estimate GARCH parameters, we must, by necessity, also re-construct an
estimate of the H(t) time series. The square root of this re-constructed
estimate is then plotted along with the original conditional volatility you
previously simulated for comparison. The last 100 samples of the lower plot are
reserved for the forecast sequence of conditional volatility based on the
estimated parameters. Close inspection will reveal that the volatility forecast
approaches the unconditional volatility of the GARCH process, which is indicated
as a horizontal line in the lower plot. A vertical line 100 samples from the
right-hand edge of the lower plot indicates the region reserved for the
volatility forecast.
操作图像:
function garchgui()
%GARCHGUI Garch Estimation Demo.
% Author(s): C.F.Garvin, 05-04-98
% Copyright 1995-2002 The MathWorks, Inc.
% $Revision: 1.9 $ $Date: 2002/04/14 21:44:01 $
%Focus garchgui if already open
gobj = findobj('Tag','GARCHGUI');
if ~isempty(gobj)
figure(gobj)
return
end
%Figure spacing parameters
bspc = 5;
bwid = 80;
bhgt = 20;
%Build the figure window
fig = figure('Numbertitle','off','Tag','GARCHGUI',...
'Name','GARCH Estimation Demo...');
figpos = get(fig,'Position');
rgt = figpos(3)-bspc;
top = figpos(4);
%Callbacks
collectparams = [...
'clear all,'...
'pobj = findobj(gcf,''Tag'',''paramp'');'...
'qobj = findobj(gcf,''Tag'',''paramq'');'...
'kobj = findobj(gcf,''Tag'',''paramk'');'...
'nobj = findobj(gcf,''Tag'',''paramn'');'...
'k = str2double(get(kobj,''String''));'...
'pstr = get(pobj,''String'');'...
'qstr = get(qobj,''String'');'...
'n = str2double(get(nobj,''String''));'...
'pstr = ['','' pstr '',''];'...
'qstr = ['','' qstr '',''];'...
'i = find(pstr == '','');'...
'for j = 1:length(i)-1,'...
'p(j) = str2double(pstr(i(j)+1:i(j+1)-1));'...
'end,'...
'i = find(qstr == '','');'...
'for j = 1:length(i)-1,'...
'q(j) = str2double(qstr(i(j)+1:i(j+1)-1));'...
'end,'...
'if isnan(k) | any(isnan(p)) | any(isnan(q)) | isnan(n),'...
'errordlg(''Please enter valid garch parameters.'');'...
'set(findobj(''Tag'',''GARCHGUI''),''Pointer'',''arrow''),'...
'return;'...
'end,'...
'if n < 101,'...
'errordlg(''N must be greater than 100.'');'...
'return,'...
'end,'...
];
simulate = [...
'try,'...
'set(findobj(gcf,''Tag'',''volfor''),''Visible'',''off'');'...
'set(findobj(gcf,''Tag'',''threshold''),''Visible'',''off'');'...
'set(findobj(gcf,''Tag'',''fcline''),''Visible'',''off'');'...
collectparams,...
';[ts,va] = ugarchsim(k,p'',q'',n);'...
'tobj = findobj(gcf,''Tag'',''timeseries'');'...
'set(tobj,''Xdata'',1:n,''Ydata'',ts,''Visible'',''on'');'...
'aobj = findobj(gcf,''Tag'',''volact'');'...
'set(aobj,''Xdata'',1:n,''Ydata'',sqrt(va),''Visible'',''on'');'...
'set(findobj(gcf,''Type'',''axes''),''Xlim'',[-inf inf],''Ylim'',[-inf inf]);'...
'catch,'...
'errordlg(lasterr),'...
'end,'...
'set(findobj(''Type'',''figure''),''Pointer'',''arrow''),'...
];
forecast = [...
'warning off,'...
'set(gcf,''Pointer'',''watch''),'...
'try,'...
collectparams,...
';tsax = findobj(gcf,''Type'',''axes'');'...
'ch = get(tsax,''Children'');'...
'u = get(ch{2},''Ydata'');'...
'[k,a,b] = ugarch(u(:),length(p),length(q));'...
'astr = [];bstr = [];'...
'for i = 1:length(a),'...
'astr = [astr '' '' num2str(a(i),2)];'...
'end,'...
'for i = 1:length(b),'...
'bstr = [bstr '' '' num2str(b(i),2)];'...
'end,'...
'set(findobj(''Tag'',''estk''),''String'',num2str(k,2));'...
'set(findobj(''Tag'',''esta''),''String'',astr);'...
'set(findobj(''Tag'',''estb''),''String'',bstr);'...
'[v,h] = ugarchpred(u(1:n-100)'',k,a,b,100);'...
'fobj = findobj(gcf,''Tag'',''volfor'');'...
'newv = [sqrt(h);sqrt(v)];'...
'set(fobj,''Xdata'',1:n,''Ydata'',newv,''Visible'',''on'');'...
'tobj = findobj(gcf,''Tag'',''threshold'');'...
'sig = sqrt((k / (1 - (sum(a) + sum(b)) ) ));'...
'set(tobj,''Xdata'',1:n,''Ydata'',sig(ones(n,1)),''Visible'',''on'');'...
'lobj = findobj(gcf,''Tag'',''fcline'');'...
'vobj = findobj(gcf,''Tag'',''volact'');'...
'volact = get(vobj,''Ydata'');'...
'set(lobj,''Xdata'',[n-100 n-100],''Ydata'',[min([volact sig]) max([volact sig])],''Visible'',''on'');'...
'catch,'...
'errordlg(lasterr),'...
'end,'...
'set(findobj(''Type'',''figure''),''Pointer'',''arrow''),'...
'warning on,'...
];
helpstring = {...
'GARCH (Generalized Auto-Regressive Conditional Heteroskedastic) Demonstration:';...
'';...
'Within the ACTUAL frame, enter the true GARCH(P,Q) model parameters of the ';...
'process you want to simulate. The parameters you enter correspond to the ';...
'following GARCH(P,Q) model for the conditional variance, H(t), and innovations,';...
'U(t), sequences:';...
'';...
' H(t) = K + A(1)*H(t-1) + A(2)*H(t-2) + ... + A(P)*H(t-P)';...
' + B(1)*U^2(t-1)+ B(2)*U^2(t-2) + ... + B(Q)*U^2(t-Q)';...
'';...
'for time steps t = 1, 2, ... N (N > 100).';...
'';...
'Note that U and H are related:';...
' ';...
' U(t) = sqrt(H(t))*v(t), where v(t) is an i.i.d. sequence ~ N(0,1).';...
' ';...
'The GARCH(P,Q) coefficients K, A, B are subject to constraints:';...
' (1) K > 0';...
' (2) A(i) >= 0 for i = 1,2,...P';...
' (3) B(i) >= 0 for i = 1,2,...Q';...
' (4) sum(A(i) + B(j)) < 1 for i = 1,2,...P and j = 1,2,...Q';...
'';...
'The coefficient vectors A and B are each entered as comma-separated lists. The ';...
'number of elements in A and B determines the model orders P and Q, respectively.';...
'';...
'When you have entered the desired process parameters, click the SIMULATE button';...
'to create time sequences of length N of innovations U(t) and conditional ';...
'variance H(t). The sequence U(t) will then appear in the top plot, and the ';...
'conditional volatility (the square root of H(t)) will appear in the bottom plot.';...
'';...
'To estimate, and make forecasts from, the GARCH(P,Q) parameters from the U(t) ';...
'process you just simulated (i.e., to reverse-engineer the process for ';...
'comparison), click the FORECAST button. The parameters K, A, B are then ';...
'estimated via Maximum Likelihood and displayed in the corresponding ESTIMATED ';...
'frame. ';...
'';...
'To estimate GARCH parameters, we must, by necessity, also re-construct an ';...
'estimate of the H(t) time series. The square root of this re-constructed ';...
'estimate is then plotted along with the original conditional volatility you ';...
'previously simulated for comparison. The last 100 samples of the lower plot are';...
'reserved for the forecast sequence of conditional volatility based on the ';...
'estimated parameters. Close inspection will reveal that the volatility forecast';...
'approaches the unconditional volatility of the GARCH process, which is indicated';...
'as a horizontal line in the lower plot. A vertical line 100 samples from the ';...
'right-hand edge of the lower plot indicates the region reserved for the ';...
'volatility forecast.';...
};
%Store help string for later use
uicontrol('Visible','off','Tag','helpstring','String',helpstring);
garchhelp = [...
'helpwin(get(findobj(''Tag'',''helpstring''),''String''),''GARCH Estimation Demo Help'');'...
];
%Build uicontrols
uicontrol('Enable','off','Position',[rgt-3*bspc-bwid top-6*bspc-9*bhgt 2*bspc+bwid bspc+9*bhgt]);
uicontrol('Style','text','String','Actual','Position',[rgt-2*bspc-bwid top-4*bspc-bhgt bwid/2 bhgt]);
uicontrol('Style','text','String','K:','Horizontalalignment','left',...
'Position',[rgt-2*bspc-bwid top-3*bspc-2*bhgt bwid bhgt]);
uicontrol('Style','edit','Tag','paramk','Position',[rgt-2*bspc-bwid top-2*bspc-3*bhgt bwid bhgt]);
uicontrol('Style','text','String','A:','Horizontalalignment','left',...
'Position',[rgt-2*bspc-bwid top-4*bspc-4*bhgt bwid bhgt]);
uicontrol('Style','edit','Tag','paramp','Position',[rgt-2*bspc-bwid top-3*bspc-5*bhgt bwid bhgt]);
uicontrol('Style','text','String','B:','Horizontalalignment','left',...
'Position',[rgt-2*bspc-bwid top-5*bspc-6*bhgt bwid bhgt]);
uicontrol('Style','edit','Tag','paramq','Position',[rgt-2*bspc-bwid top-4*bspc-7*bhgt bwid bhgt]);
uicontrol('Style','text','String','N:','Horizontalalignment','left',...
'Position',[rgt-2*bspc-bwid top-6*bspc-8*bhgt bwid bhgt]);
uicontrol('Style','edit','Tag','paramn','Position',[rgt-2*bspc-bwid top-5*bspc-9*bhgt bwid bhgt])
uicontrol('Enable','off','Position',[rgt-3*bspc-bwid top-6*bspc-14*bhgt 2*bspc+bwid bspc+4*bhgt]);
uicontrol('Style','text','String','Estimated','Position',[rgt-2*bspc-bwid top-8*bspc-10*bhgt bwid*2/3 bhgt]);
uicontrol('Style','text','String','K:','Position',[rgt-2*bspc-bwid top-9*bspc-11*bhgt bwid/2 bhgt]);
uicontrol('Style','edit','Tag','estk','Position',[rgt-1*bspc-bwid/1.5 top-8*bspc-11*bhgt bwid/2 bhgt]);
uicontrol('Style','text','String','A:','Position',[rgt-2*bspc-bwid top-9*bspc-12*bhgt bwid/2 bhgt]);
uicontrol('Style','edit','Tag','esta','Position',[rgt-1*bspc-bwid/1.5 top-8*bspc-12*bhgt bwid/2 bhgt]);
uicontrol('Style','text','String','B:','Position',[rgt-2*bspc-bwid top-9*bspc-13*bhgt bwid/2 bhgt]);
uicontrol('Style','edit','Tag','estb','Position',[rgt-1*bspc-bwid/1.5 top-8*bspc-13*bhgt bwid/2 bhgt]);
uicontrol('Enable','off','Position',[rgt-3*bspc-bwid top-4*bspc-19*bhgt 2*bspc+bwid bspc+3.7*bhgt]);
uicontrol('String','Simulate','Callback',simulate,...
'Position',[rgt-2*bspc-bwid top-9*bspc-15*bhgt bwid bhgt]);
uicontrol('String','Forecast','Callback',forecast,...
'Position',[rgt-2*bspc-bwid top-10*bspc-16*bhgt bwid bhgt]);
uicontrol('String','Help','Callback',garchhelp,...
'Position',[rgt-2*bspc-bwid top-11*bspc-17*bhgt bwid bhgt]);
%Build subplots and initialize figures with data
subplot(2,1,1)
h = plot(ones(100,1)); %Time series object
set(h,'Tag','timeseries','Visible','off')
title('Time Series','Fontweight','bold')
axpos = get(gca,'Position');
set(gca,'Position',[.075 axpos(2) .71 axpos(4)],'Fontweight','bold')
grid on
subplot(2,1,2)
h = plot(ones(100,1),'-','Color','b'); %Actual volatility object
hold on
set(h,'Tag','volact','Visible','off')
h = plot(ones(100,1),'-','Color','k');
set(h,'Tag','threshold','Visible','off')
h = plot(ones(100,1),'-','Color','r'); %Forecast volatility object
set(h,'Tag','volfor','Visible','off')
h = plot([1 1],[1 1],'-','Color','k');
hold off
set(h,'Tag','fcline','Visible','off');
title('Volatility','Fontweight','bold')
xlabel('Blue = Simulated Red = Forecasted','Fontweight','bold')
axpos = get(gca,'Position');
set(gca,'Position',[.075 axpos(2) .71 axpos(4)],'Fontweight','bold')
grid on
%GUI cleanup
set(findobj(gcf,'Style','edit'),'Backgroundcolor','white','Horizontalalignment','left')
set(findobj(gcf,'Type','uicontrol'),'Units','normal')
[此贴子已经被作者于2009-5-23 17:13:30编辑过]