new; @CIV_UR GNP96 48.1-87.4C@ let x[160,2]= 3.73333333333 1546.6 3.66666666667 1571.1 3.76666666667 1577.6 3.83333333333 1580.5 4.66666666667 1558.2 5.86666666667 1553.6 6.7 1570.7 6.96666666667 1553.9 6.4 1618.4 5.56666666667 1667.2 4.63333333333 1733.1 4.23333333333 1763.9 3.5 1782.9 3.1 1814.9 3.16666666667 1851.6 3.36666666667 1855.8 3.06666666667 1876.7 2.96666666667 1878.2 3.23333333333 1889.9 2.83333333333 1951.9 2.7 1987.4 2.56666666667 2004.3 2.73333333333 1990.2 3.7 1958.6 5.26666666667 1949.7 5.8 1952.6 5.96666666667 1973.7 5.33333333333 2014.1 4.73333333333 2071.6 4.4 2104.3 4.1 2132.4 4.23333333333 2143.9 4.03333333333 2136.4 4.2 2152.8 4.13333333333 2150.8 4.13333333333 2184.1 3.93333333333 2198.8 4.1 2195 4.23333333333 2215.5 4.93333333333 2189.2 6.3 2131 7.36666666667 2143.6 7.33333333333 2190.9 6.36666666667 2239.7 5.83333333333 2286.2 5.1 2345.5 5.26666666667 2345.5 5.6 2354.1 5.13333333333 2405.4 5.23333333333 2393.9 5.53333333333 2398.9 6.26666666667 2369.3 6.8 2383.7 7 2427.1 6.76666666667 2467.2 6.2 2517.5 5.63333333333 2561 5.53333333333 2590.3 5.56666666667 2615.7 5.53333333333 2625.1 5.76666666667 2654.8 5.73333333333 2688.2 5.5 2739.8 5.56666666667 2760.3 5.46666666667 2823.2 5.2 2855.7 5 2894.7 4.96666666667 2900.5 4.9 2974 4.66666666667 3014.6 4.36666666667 3073.6 4.1 3144.5 3.86666666667 3222.6 3.83333333333 3234.8 3.76666666667 3254.7 3.7 3283.7 3.83333333333 3313.4 3.83333333333 3310.7 3.8 3336.6 3.9 3360.8 3.73333333333 3429.2 3.56666666667 3488.3 3.53333333333 3513.4 3.4 3528.1 3.4 3582.2 3.43333333333 3590.6 3.56666666667 3610.3 3.56666666667 3593.3 4.16666666667 3589.1 4.76666666667 3597.4 5.16666666667 3628.3 5.83333333333 3587.6 5.93333333333 3691.3 5.9 3712.8 6.03333333333 3738.4 5.93333333333 3749.2 5.76666666667 3823.4 5.7 3910 5.56666666667 3950.7 5.36666666667 4018.7 4.93333333333 4125 4.93333333333 4168.3 4.8 4158 4.76666666667 4192.5 5.13333333333 4168.1 5.2 4176.5 5.63333333333 4126.5 6.6 4098 8.26666666667 4040.1 8.86666666667 4075.6 8.46666666667 4148.4 8.3 4206.7 7.73333333333 4304.2 7.56666666667 4341.2 7.73333333333 4362 7.76666666667 4398.4 7.5 4457.6 7.13333333333 4535.9 6.9 4616.4 6.66666666667 4616.6 6.33333333333 4636 6 4804.8 6.03333333333 4854.6 5.9 4925.8 5.86666666667 4939.6 5.7 4949.3 5.86666666667 4995.6 5.96666666667 5011.4 6.3 5028.8 7.33333333333 4922.5 7.66666666667 4911.3 7.4 4986.3 7.43333333333 5086.4 7.4 5048.1 7.4 5110.5 8.23333333333 5056.8 8.83333333333 4969.4 9.43333333333 4996.9 9.9 4963.4 10.6666666667 4964.8 10.3666666667 5021.5 10.1333333333 5142.2 9.36666666667 5233.9 8.53333333333 5342 7.86666666667 5452.6 7.43333333333 5544.3 7.43333333333 5591.1 7.3 5627.1 7.23333333333 5664.3 7.3 5710.9 7.2 5788.6 7.03333333333 5839.6 7.03333333333 5887.3 7.16666666667 5901.9 6.96666666667 5959 6.83333333333 5981.7 6.6 6027.6 6.26666666667 6095.8 6 6145.8 5.83333333333 6254.1 ; x; {resultIRF_D,resultIRF_L,resultFEVD_D,resultFEVD_L,u,e,HD} = BQVAR(x,8,20,0,0); library pgraph; xy(seqa(1,1,rows(resultIRF_D)),resultIRF_D); @resultIRF_L,resultFEVD_D,resultFEVD_L,u,e,HD} = BQVAR(x,8,2,0,0);@ /* BLANCHARD-QUAH DECOMPOSITION */ /* Author: Alan G. Isaac mailto: aisaac AT american DOT edu Essentially translates bqvar.m by Martin W Johansson http://www.nek.lu.se/nekmwj/MATLAB/bqvar.m I did this translation in a hurry, so please send suggestions. Written: 5 April 2004 Last Modified: 7 April 2004 Purpose: Perform the Blanchard & Quah (1989 AER) decompostion of a unrestricted VAR and calculate the implied impulse-response functions (IRFs), variance decompostions (FEVDs), and (someday) historical decompositions (HDs). Reference: Blanchard & Quah (1989), "The dynamic effects of aggregate demand and supply disturbances", American Economic Review, 79:4, 654-673. Syntax: {resultIRF_D,resultIRF_L,resultFEVD_D,resultFEVD_L,u,e,HD} = BQVAR(x,p,steps,dcomp,selector) INPUTS: x = T*k data matrix (where T=#obs, k=#variables) p = Number of lags in unrestricted VAR steps = Number of steps in IRFs and FEVDs dcomp = 0,1,2 determines the determinstic components 0 = No deterministic trends 1 = Intercept (recommended default) 2 = Intercept and linear trend selector = 1*k matrix of zeros and ones 0: variable should be excluded during HD 1: variable should be included during HD (Note: set selector = 0 to skip historical decomp) OUTPUTS: resultIRF_D = IRFs in first diff. resultIRF_L = IRFs in levels (i.e. accumulated). resultFEVD_D = FEVD in first diff. resultFEVD_L = FEVD in levels (i.e. accumulated). u = structural shocks. e = reduced form shocks. HD = The historical decomposition. Martin W. Johansson provides the following NOTES: The function basically performs a LT Cholesky decompostion of the long-run VMA matrix. Since the factorisation is LT this means that the only shock which may have long-run effects on the first variable is the first shock while the first and second shock may have long-run effects on the second variable. The first, second and third shock may have long-run effects on the third variable etc, etc... The IRFs and FEVD are organised in 'variablewise'. Thus the first block of k columns holds the IRFs/FEVDs of the first variable to shock 1...k. The next block of k columns holds the IRFs/FEVDs of the second variable to shock 1...k. Finally the orthogonalized (reduced form) residuals are stored in result.u (result.e) */ /* In newer GAUSS may want to change to a results structure: %Syntax: result = BQVAR(x,p,steps,dtrend,selector) %results structure: %result.IRF_D = IRFs in first diff. %result.IRF_L = IRFs in levels (i.e. accumulated). %result.FEVD_D = FEVD in first diff. %result.FEVD_L = FEVD in levels (i.e. accumulated). %result.u = structural shocks. %result.e = reduced form shocks. %result.HD = The historical decomposition. */ proc (7) = BQVAR(x,p,steps,dcomp,selector); local xp,xlag,xun,k,nobs,intercept,lintrend,Bols,ndf,e,sigma,Btrim,bt,const,trprmtr,S,sumBtrim; local C1,invC1,G,capB,u,J,A,imp_resp,imp_add,imp_resp_D,imp_resp_L,fevd_D,fevd_L,fev; local sum_fevd_D,sum_fevd_L,w_L,irf,imp,IMP_TRACEcum,imp_trace,theta,w_D; local resultIRF_D,resultIRF_L,resultFEVD_D,resultFEVD_L,HD; @returns@ /* Prepare Data */ xp=x[1:p,.]; @store original data; add efficiency by storing only the p trimmed rows@ {x,xlag}=varlags(x,p); k=cols(x); nobs=rows(x); intercept=ones(nobs,1); lintrend=seqa(1,1,nobs); if dcomp==1; xlag=intercept~xlag; endif; if dcomp==2; xlag=intercept~lintrend~xlag; endif; /* Start by estimating unrestricted VAR(p) */ Bols=x/xlag; @ coefficient matrix Bols has k columns, one for each equation @ e=x-xlag*Bols; ndf=nobs-cols(Bols); sigma=(e'e)/ndf; @VCV according to Eviews 4 p507.@ /* Trim off the coefficients on intercept and lintrend: so Btrim still has k column but only p*k rows */ Btrim=trimr(Bols,dcomp,0); bt=Btrim'; @ so bt has p*k columns and k rows @ if dcomp>=1; const=Bols[1,.]; endif; if dcomp==2; trprmtr=Bols[2,.]; endif; /* Impose long-run restrictions */ S=chol(sigma)'; @fails to use this!??!@ if p==1; sumBtrim=Btrim; else; sumBtrim=sumc(reshape(Btrim,p,k*k)); @careful: GAUSS reshapes by row; Matlab by column@ sumBtrim=reshape(sumBtrim,k,k); endif; C1=inv(eye(k)-sumBtrim)'; invC1=inv(C1); @inverse of inverse??@ G=chol(C1*sigma*C1')'; capB=invC1*G; u=(inv(capB)*e')'; /* Calculating the IRFs */ J=eye(k)~zeros(k,k*p-k); A=bt|(eye((p-1)*k)~zeros((p-1)*k,k)); imp_resp={}; for i(0,nobs-1,1); theta=J*A^i*J'; imp_add=theta*capB; imp_add=vec(imp_add')'; imp_resp=imp_resp|imp_add; endfor; /* Defining IRFs in levels and first diff */ imp_resp_D=imp_resp; imp_resp_L=cumsumc(imp_resp); /* FEVD in differences */ fevd_D=cumsumc(imp_resp_D^2); @sum_fevd_D=repmat(fevd_D, [k 1]); Matlab version @ sum_fevd_D=ones(k,1) .*. fevd_D; sum_fevd_D=sumc((sum_fevd_D.*(eye(k) .*. ones(nobs,k)))'); sum_fevd_D=reshape(sum_fevd_D',k,nobs)'; sum_fevd_D=sum_fevd_D .*. ones(1,k); w_D=100*fevd_D./sum_fevd_D; /* FEVD in levels */ fevd_L=cumsumc(imp_resp_L^2); @sum_fevd_L=repmat(fevd_L, [k 1]); Matlab version@ sum_fevd_L=ones(k,1) .*. fevd_L; sum_fevd_L=sumc((sum_fevd_L.*(eye(k) .*. ones(nobs,k)))'); sum_fevd_L=reshape(sum_fevd_L',k,nobs)'; sum_fevd_L=sum_fevd_L .*. ones(1,k); w_L=100*fevd_L./sum_fevd_L; @ Setting up results structure @ resultIRF_D=imp_resp_D[1:steps,.]; resultIRF_L=imp_resp_L[1:steps,.]; resultFEVD_D=w_D[1:steps,.]; resultFEVD_L=w_L[1:steps,.]; HD={}; @not implemented yet@ retp(resultIRF_D,resultIRF_L,resultFEVD_D,resultFEVD_L,u,e,HD); endp; /********************** PROC VARLAGS ***************************** ** author: Alan G. Isaac ** last update: 5 Dec 95 previous: 15 June 94 ** FORMAT ** { x,xlags } = varlags(var,lags) ** INPUT ** var - T x K matrix ** lags - scalar, number of lags of var (a positive integer) ** OUTPUT ** x - (T - lags) x K matrix, the last T-lags rows of var ** xlags - (T - lags) x lags*cols(var) matrix, ** being the 1st through lags-th ** values of var corresponding to the values in x ** i.e, the appropriate rows of x(-1)~x(-2)~etc. ** GLOBAL VARIABLES: none **********************************************************************/ proc(2)=varlags(var,lags); local xlags; xlags = shiftr((ones(1,lags) .*. var)',seqa(1-lags,1,lags) .*. ones(cols(var),1),miss(0,0))'; retp(trimr(var,lags,0),trimr(xlags,0,lags)); endp;