www.gusucode.com > econ 案例源码程序 matlab代码 > econ/GenerateImpulseResponsesForAVARModelExample.m
%% Generate Impulse Responses for a VAR model % This example shows how to generate impulse responses of an interest rate % shock on real GDP using |vgxproc|. %% % Load the |Data_USEconModel| data set. This example uses two time series: % the logarithm of real GDP, and the real 3-month T-bill rate, both % differenced to be approximately stationary. Suppose that a VAR(4) model % is appropriate to describe the time series. % Copyright 2015 The MathWorks, Inc. load Data_USEconModel DEF = log(DataTable.CPIAUCSL); GDP = log(DataTable.GDP); rGDP = diff(GDP - DEF); % Real GDP is GDP - deflation TB3 = 0.01*DataTable.TB3MS; dDEF = 4*diff(DEF); % Scaling rTB3 = TB3(2:end) - dDEF; % Real interest is deflated Y = [rGDP,rTB3]; %% % Define the forecast horizon. FDates = datenum({'30-Jun-2009'; '30-Sep-2009'; '31-Dec-2009'; '31-Mar-2010'; '30-Jun-2010'; '30-Sep-2010'; '31-Dec-2010'; '31-Mar-2011'; '30-Jun-2011'; '30-Sep-2011'; '31-Dec-2011'; '31-Mar-2012'; '30-Jun-2012'; '30-Sep-2012'; '31-Dec-2012'; '31-Mar-2013'; '30-Jun-2013'; '30-Sep-2013'; '31-Dec-2013'; '31-Mar-2014'; '30-Jun-2014' }); FT = numel(FDates); %% % Fit a VAR(4) model specification: Spec = vgxset('n',2,'nAR',4,'Constant',true); impSpec = vgxvarx(Spec,Y(5:end,:),[],Y(1:4,:)); impSpec = vgxset(impSpec,'Series',... {'Transformed real GDP','Transformed real 3-mo T-bill rate'}); %% % Generate the innovations processes both with and without an impulse (shock): W0 = zeros(FT, 2); % Innovations without a shock W1 = W0; W1(1,2) = sqrt(impSpec.Q(2,2)); % Innovations with a shock %% % Generate the processes with and without the shock: Yimpulse = vgxproc(impSpec,W1,[],Y); % Process with shock Ynoimp = vgxproc(impSpec,W0,[],Y); % Process with no shock %% % Undo the scaling for the GDP processes: Yimp1 = exp(cumsum(Yimpulse(:,1))); % Undo scaling Ynoimp1 = exp(cumsum(Ynoimp(:,1))); %% % Compute and plot the relative difference between the calculated GDPs: RelDiff = (Yimp1 - Ynoimp1) ./ Yimp1; plot(FDates,100*RelDiff);dateaxis('x',12) title(... 'Impact of Interest Rate Shock on Real Gross Domestic Product') ylabel('% Change') %% % The graph shows that an increased interest rate causes a dip in the real % GDP for a short time. Afterwards the real GDP begins to climb again, % reaching its former level in about 1 year.