% USA_Macrodata_US.m % ************* % PART 3: M U L T I - E Q U A T I O N S M O D E L S % 13.3 Simulation Example % ============================================================== % Robert S. Pindyck & Daniel L. Rubinfeld % Econometric Models & Economic Forecast, 4/e % McGraw-Hill, 1998 % ISBN 0-07-050208-0 % % Data to accompany Econometric Models & Economic Forecast, 4/e % (c) Irwin/McGraw-Hill, 1998 % Disk 1 of 1 % ISBN 0-07-848043-4 % ============================================================== clear all; close all; clc; %data ulozena v textovem souboru USAmacro.dat load USAMACRO; USAmacro = USAMACRO; maxl = 4; %maximalni zpozdeni v modelu last = 0; %pocet poslednich vynechanych hodnot t = USAmacro(maxl+1:end-last,1); % Ctvrtleti od 2Q1950-1Q1988 n = length(t); %endogeni promenne C = USAmacro(maxl+1:end-last,2); % Realna agregatni osobni potreba (v cenach roku 1982) [mld.] I = USAmacro(maxl+1:end-last,3); % Realne hrube domaci investice (v cenach roku 1982) [mld.] R = USAmacro(maxl+1:end-last,4); % Urokova sazba 3 mesicnich Treasury bill [% p.a.] Y = USAmacro(maxl+1:end-last,5); % Realny HDP (ocisten o exporty a importy; v cenach 1982) [mld.] %zpozdene endogenni promenne C_1 = USAmacro(maxl+1-1:end-last-1,2); %Zpozdene C(t-1) Y_1 = USAmacro(maxl+1-1:end-last-1,5); %... dY_1 = USAmacro(maxl+1-1:end-last-1,5)-USAmacro(maxl+1-2:end-last-2,5); %Y(t-1)-Y(t-2) R_4 = USAmacro(maxl+1-4:end-last-4,4); sumR_1 = USAmacro(maxl+1-1:end-last-1,4)+USAmacro(maxl+1-2:end-last-2,4); %R(t-1)+R(t-2) %exogenni promenne konst = ones(n,1); G = USAmacro(maxl+1:end-last,6); % Realne vladni vydaje (v cenach roku 1982) [mld.] dM = USAmacro(maxl+1:end-last,7)-USAmacro(maxl+1-1:end-last-1,7); % Zmena realne penezni zasoby (M1) [mld.] %dalsi endogenni promenne dY = Y-Y_1; %Y(t)-Y(t-1) %pomocne exogenni promenne M = USAmacro(maxl+1:end-last,7); %penezni zasoba - agregat M1 %graficke zobrazeni endogennich promennych figure set(gcf,'Name',' Endogenous variables'); subplot(2,2,1) hndl2=plot(t,C); set(hndl2,'LineWidth',2); title('Real Aggregate Personal Consumption'); ylabel('C [ billions of 1982 dolars ]'); xlabel('Time [ quarter ]'); grid subplot(2,2,2) hndl3=plot(t,I); set(hndl3,'LineWidth',2); title('Real Gross Domestic Investment'); ylabel('I [ billions of 1982 dolars ]'); xlabel('Time [ quarter ]'); grid subplot(2,2,3) hndl4=plot(t,R,'r'); set(hndl4,'LineWidth',2); title('Interest Rate on 3-Month Treasury Bills'); ylabel('R [ percent per year ]'); xlabel('Time [ quarter ]'); grid subplot(2,2,4) hndl5=plot(t,Y,'r'); set(hndl5,'LineWidth',2); title('Real GNP (net export and import)'); ylabel('Y [ billions of 1982 dolars ]'); xlabel('Time [ quarter ]'); grid %graficke zobrazeni exogennich promennych figure set(gcf,'Name',' Exogenous variables'); subplot(2,1,1) hndl6=plot(t,G,'r'); set(hndl6,'LineWidth',2); hold off title('Real Gavernment Spending'); ylabel('G [ billions of 1982 dolars'); xlabel('Time [ quarter ]'); grid subplot(2,1,2) hndl7=plot(t,M,'r'); set(hndl7,'LineWidth',2); hold off title('Real Money Stock M1'); ylabel('M [ billions of 1982 dolars ]'); xlabel('Time [ quarter ]'); grid %odhad spotrebni funkce (13.14) C(t) = a1 + a2*Y(t) + a3*C(t-1) disp('*******************'); disp('*Spotrebni funkce*'); disp('*******************'); y = C; yendog = [Y]; %endogenni promenne v rovnici xexog = [konst C_1]; %exogenni promenne v rovnici %vsechny exogenni a zpozdenne endogenni promenne v systemu xall = [konst G dM C_1 Y_1 dY_1 R_4 sumR_1]; res2SLS_C = tsls(y,yendog,xexog,xall); resOLS_C = ols(y,[yendog xexog]); vnames_C = strvcat('Spotreba','Y','konstanta','C_1'); prt_reg(res2SLS_C,vnames_C); prt_reg(resOLS_C,vnames_C); %odhad investicni funkce (13.15) I(t) = b1 + b2*[Y(t-1)-Y(t-2)] + b3*Y(t) + b4*R(t-4) disp('*******************'); disp('*Investicni funkce*'); disp('*******************'); y = I; yendog = [Y]; xexog = [konst dY_1 R_4]; res2SLS_I = tsls(y,yendog,xexog,xall); resOLS_I = ols(y,[yendog xexog]); vnames_I = strvcat('Investice','Y','konstanta','dY_1','R_4'); prt_reg(res2SLS_I,vnames_I); prt_reg(resOLS_I,vnames_I); %odhad rovnice urokove miry (13.16) R(t) = c1 + c2*Y(t) + c3*[Y(t)-Y(t-1)] + c4*[M(t]-M(t-1)] + c5*[R(t-1)+R(t-2)] disp('**********************'); disp('*Rovnice urokove miry*'); disp('**********************'); y = R; yendog = [Y dY]; xexog = [konst dM sumR_1]; res2SLS_R = tsls(y,yendog,xexog,xall); resOLS_R = ols(y,[yendog xexog]); vnames_R = strvcat('Ur. mira','Y','dY','konstanta','dM','sumR_1'); prt_reg(res2SLS_R,vnames_R); prt_reg(resOLS_R,vnames_R); %dopocitani Y res2SLS_Y.yhat = res2SLS_C.yhat + res2SLS_I.yhat + G; resOLS_Y.yhat = resOLS_C.yhat + resOLS_I.yhat + G; %graficke zobrazeni endogennich promennych figure set(gcf,'Name',' Endogenous variables'); subplot(2,2,1) plot(t,[C res2SLS_C.yhat resOLS_C.yhat]); title('Real Aggregate Personal Consumption'); ylabel('C [ billions of 1982 dolars ]'); xlabel('Time [ quarter ]'); legend('actual','predicted 2SLS','predictedOLS'); grid subplot(2,2,2) hndl3=plot(t,[I res2SLS_I.yhat resOLS_I.yhat]); title('Real Gross Domestic Investment'); ylabel('I [ billions of 1982 dolars ]'); xlabel('Time [ quarter ]'); legend('actual','predicted 2SLS','predictedOLS'); grid subplot(2,2,3) plot(t,[R res2SLS_R.yhat resOLS_R.yhat]); title('Interest Rate on 3-Month Treasury Bills'); ylabel('R [ percent per year ]'); xlabel('Time [ quarter ]'); legend('actual','predicted 2SLS','predictedOLS'); grid subplot(2,2,4) plot(t,[Y res2SLS_Y.yhat resOLS_Y.yhat]); title('Real GNP (net export and import)'); ylabel('Y [ billions of 1982 dolars ]'); xlabel('Time [ quarter ]'); legend('actual','predicted 2SLS','predictedOLS'); grid %simulace modelu simBB*simYY = simCC*simXX => simYY = inv(simBB)*simCC*simXX %parametry 2SLS %nacteni parametru (cislovani dle Pindyck-Rubinfeld (a(ij)...i-ta rovnice, j-ty parametr a11 = res2SLS_C.beta(2); a12 = res2SLS_C.beta(1); a13 = res2SLS_C.beta(3); a21 = res2SLS_I.beta(2); a22 = res2SLS_I.beta(3); a23 = res2SLS_I.beta(1); a24 = res2SLS_I.beta(4); a31 = res2SLS_R.beta(3); a32 = res2SLS_R.beta(1); a33 = res2SLS_R.beta(2); a34 = res2SLS_R.beta(4); a35 = res2SLS_R.beta(5); %matice pro prevod na redukovanou formu simBB =[1 0 0 -a12 0 0 1 0 -a23 0 0 0 1 -a32 -a33 -1 -1 0 1 0 0 0 0 -1 1]; simCC = [a11 0 0 a13 0 0 0 0 a21 0 0 0 a22 a24 0 0 a31 a34 0 0 0 0 a35 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 -1]; % deklarace simulovanych promennych sim2SLS_C = zeros(n,1); sim2SLS_I = zeros(n,1); sim2SLS_R = zeros(n,1); sim2SLS_Y = zeros(n,1); sim2SLS_dY= zeros(n,1); %zacate simulace for i=1:n, % definice zpozdenych promennych pro pripad, kdy jeste nebudou k % dispozici prislusne nasimulovane hodnoty if i<2 sim2SLS_C_1(i) = C_1(i); sim2SLS_Y_1(i) = Y_1(i); else sim2SLS_C_1(i) = sim2SLS_C(i-1); sim2SLS_Y_1(i) = sim2SLS_Y(i-1); end if i<3 sim2SLS_dY_1(i) = dY_1(i); sim2SLS_sumR_1(i) = sumR_1(i); else sim2SLS_dY_1(i) = sim2SLS_dY(i-1); sim2SLS_sumR_1(i) = sim2SLS_R(i-1)+sim2SLS_R(i-2); end if i<5 sim2SLS_R_4(i) = R_4(i); else sim2SLS_R_4(i) = sim2SLS_R(i-4); end % vektor predeterminovanych promennych v case i simXX = [1;dM(i);G(i);sim2SLS_C_1(i);sim2SLS_dY_1(i);sim2SLS_R_4(i);sim2SLS_sumR_1(i);sim2SLS_Y_1(i)]; % simulace pres redukovanou formu simYY = inv(simBB)*simCC*simXX; sim2SLS_C(i) = simYY(1); sim2SLS_I(i) = simYY(2); sim2SLS_R(i) = simYY(3); sim2SLS_Y(i) = simYY(4); sim2SLS_dY(i) = simYY(5); end %simulace modelu imBB*simY = simCC*simX => simYY = inv(simBB)*simCC*simXX %parametry OLS %nacteni parametru (cislovani dle Pindyck-Rubinfeld (a(ij)...i-ta rovnice, j-ty parametr a11 = resOLS_C.beta(2); a12 = resOLS_C.beta(1); a13 = resOLS_C.beta(3); a21 = resOLS_I.beta(2); a22 = resOLS_I.beta(3); a23 = resOLS_I.beta(1); a24 = resOLS_I.beta(4); a31 = resOLS_R.beta(3); a32 = resOLS_R.beta(1); a33 = resOLS_R.beta(2); a34 = resOLS_R.beta(4); a35 = resOLS_R.beta(5); %matice pro prevod na redukovanou formu simBB =[1 0 0 -a12 0 0 1 0 -a23 0 0 0 1 -a32 -a33 -1 -1 0 1 0 0 0 0 -1 1]; simCC = [a11 0 0 a13 0 0 0 0 a21 0 0 0 a22 a24 0 0 a31 a34 0 0 0 0 a35 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 -1]; % deklarace simulovanych promennych simOLS_C = zeros(n,1); simOLS_I = zeros(n,1); simOLS_R = zeros(n,1); simOLS_Y = zeros(n,1); simOLS_dY= zeros(n,1); % zacate simulace for i=1:n, % definice zpozdenych promennych pro pripad, kdy jeste nebudou k % dispozici prislusne nasimulovane hodnoty if i<2 simOLS_C_1(i) = C_1(i); simOLS_Y_1(i) = Y_1(i); else simOLS_C_1(i) = simOLS_C(i-1); simOLS_Y_1(i) = simOLS_Y(i-1); end if i<3 simOLS_dY_1(i) = dY_1(i); simOLS_sumR_1(i) = sumR_1(i); else simOLS_dY_1(i) = simOLS_dY(i-1); simOLS_sumR_1(i) = simOLS_R(i-1)+simOLS_R(i-2); end if i<5 simOLS_R_4(i) = R_4(i); else simOLS_R_4(i) = simOLS_R(i-4); end % vektor predeterminovanych promennych v case i simXX = [1;dM(i);G(i);simOLS_C_1(i);simOLS_dY_1(i);simOLS_R_4(i);simOLS_sumR_1(i);simOLS_Y_1(i)]; % simulace pres redukovanou formu simYY = inv(simBB)*simCC*simXX; simOLS_C(i) = simYY(1); simOLS_I(i) = simYY(2); simOLS_R(i) = simYY(3); simOLS_Y(i) = simYY(4); simOLS_dY(i) = simYY(5); end %graficke zobrazeni nasimulovanych endogennich promennych figure set(gcf,'Name',' Endogenous variables'); subplot(2,2,1) plot(t,[C sim2SLS_C simOLS_C]); title('Real Aggregate Personal Consumption'); ylabel('C [ billions of 1982 dolars ]'); xlabel('Time [ quarter ]'); legend('actual','simulated 2SLS','simulated OLS'); grid subplot(2,2,2) hndl3=plot(t,[I sim2SLS_I simOLS_I]); title('Real Gross Domestic Investment'); ylabel('I [ billions of 1982 dolars ]'); xlabel('Time [ quarter ]'); legend('actual','simulated 2SLS','simulated OLS'); grid subplot(2,2,3) plot(t,[R sim2SLS_R simOLS_R]); title('Interest Rate on 3-Month Treasury Bills'); ylabel('R [ percent per year ]'); xlabel('Time [ quarter ]'); legend('actual','simulated 2SLS','simulated OLS'); grid subplot(2,2,4) plot(t,[Y sim2SLS_Y simOLS_Y]); title('Real GNP (net export and import)'); ylabel('Y [ billions of 1982 dolars ]'); xlabel('Time [ quarter ]'); legend('actual','simulated 2SLS','simulated OLS'); grid