├── .DS_Store ├── CM_Bayesian Macro - Matlab - Nimark ├── .DS_Store ├── DSGEMH.m ├── DSGE_SA.m ├── Lecture_1_Matlab_R_E_Solution_Methods │ ├── REsolutionmethods.m │ ├── Upload Class I │ │ ├── REsolutionmethods.m │ │ ├── qzdiv.m │ │ ├── qzdivct.m │ │ ├── qzswitch.m │ │ └── reorder.m │ ├── qzdiv.m │ ├── qzdivct.m │ ├── qzswitch.m │ └── reorder.m ├── Lecture_2_Kalman_App │ ├── Kalman_filter_app.m │ ├── LL_state_space.m │ ├── ML DSGE │ │ ├── DSGE_SA.asv │ │ ├── DSGE_SA.m │ │ ├── LLDSGE.m │ │ ├── REsolutionmethods.asv │ │ ├── REsolutionmethods.m │ │ ├── Z.mat │ │ ├── hpfilter.m │ │ ├── lastxopt.mat │ │ ├── qzdiv.m │ │ ├── qzdivct.m │ │ ├── qzswitch.m │ │ ├── reorder.m │ │ ├── simannb.m │ │ └── smooth.m │ ├── UCmodel.m │ ├── USdata.mat │ ├── dist_plot.m │ ├── distplot.m │ ├── kalman_f.m │ ├── kalman_filter.asv │ ├── kalman_filter_sim.m │ ├── lastxopt.mat │ ├── sim.m │ ├── sim_smooth.m │ └── smooth.m ├── Lecture_3_ DSGE_M-H │ ├── DSGEMH.m │ ├── DSGE_SS.m │ ├── LLDSGE.m │ ├── LL_state_space.m │ ├── LOGPRIOR.m │ ├── Plot_prob_intervals.m │ ├── Z.mat │ ├── convcheck.m │ ├── hpfilter.m │ ├── lastxopt.mat │ ├── log_prior_DSGE.m │ ├── lpdfBeta.m │ ├── lpdfGamma.asv │ ├── lpdfGamma.m │ ├── lpdfInvGamma.m │ ├── lpdfNormal.m │ ├── lpdfTruncNormal.asv │ ├── lpdfTruncNormal.m │ ├── norm_rnd.m │ ├── plotpost.asv │ └── plotpost.m ├── Lecture_4_Bayesian_Analysis │ ├── DSGE M-H │ │ ├── DSGEMH_main.m │ │ ├── DSGE_SS.m │ │ ├── LLDSGE.m │ │ ├── LL_state_space.m │ │ ├── LOGPRIOR.m │ │ ├── Plot_prob_intervals.m │ │ ├── Z.mat │ │ ├── bb_.mat │ │ ├── bb_ML.mat │ │ ├── bb_mle.mat │ │ ├── bb_prior.mat │ │ ├── convcheck.m │ │ ├── hpfilter.m │ │ ├── kalman_filter.m │ │ ├── lastxopt.mat │ │ ├── log_prior_DSGE.asv │ │ ├── log_prior_DSGE.m │ │ ├── lpdfBeta.m │ │ ├── lpdfGamma.asv │ │ ├── lpdfGamma.m │ │ ├── lpdfInvGamma.m │ │ ├── lpdfNormal.m │ │ ├── lpdfTruncNormal.asv │ │ ├── lpdfTruncNormal.m │ │ ├── norm_rnd.m │ │ ├── plot_vardecomp.m │ │ ├── plotpost.asv │ │ └── plotpost.m │ └── M-H_SVAR │ │ ├── LLSVAR.m │ │ ├── SVARMH.m │ │ ├── convcheck.m │ │ ├── hpfilter.m │ │ ├── inf-ff.mat │ │ ├── norm_rnd.m │ │ └── plotpost.m ├── Lecture_5_Heterogeneous_info │ ├── DHOE_LL.m │ ├── DHOE_sim.m │ ├── DHOE_solve.m │ ├── Kalman.m │ ├── MH_DHOE_main.m │ ├── Z_f.mat │ ├── Z_p.mat │ ├── iterdlyap.m │ ├── lpdfBeta.m │ └── norm_rnd.m ├── Lorenz No regimes 25 prior │ ├── $matlab_temp_file$4d9a0bf9 │ ├── $matlab_temp_file$509d11e3 │ ├── $matlab_temp_file$509d7276 │ ├── $matlab_temp_file$5102f2ac │ ├── AdapMetAlgManBD.m │ ├── CPId.mat │ ├── CPIdisp.mat │ ├── CPIsurv.mat │ ├── CleanData.m │ ├── CountFact.asv │ ├── CountFact.m │ ├── DMbad.mat │ ├── DMbadraw.mat │ ├── DMgood.mat │ ├── DMgoodraw.mat │ ├── DMtransform.asv │ ├── DMtransform.m │ ├── DataGraph.m │ ├── DispCovPlot.asv │ ├── DispCovPlot.m │ ├── FFunds.mat │ ├── GDPsurv.mat │ ├── IntroFigures.m │ ├── Kalman.m │ ├── KalmanSimSmooth.m │ ├── Lorenz.m │ ├── MBD1sidedFit.asv │ ├── MBD1sidedFit.m │ ├── MBDLL.asv │ ├── MBDLL.m │ ├── MBDLL_Lorenz.m │ ├── MBD_Gibbs.asv │ ├── MBD_Gibbs.m │ ├── MBDmain.asv │ ├── MBDmain.m │ ├── MBDplotter.m │ ├── MBDsimsmooth.asv │ ├── MBDsimsmooth.m │ ├── MBDsimulator.m │ ├── MCMC_25_prior.mat │ ├── MCMC_Lorenz_25_prior.mat │ ├── MOAFLorenz.asv │ ├── MOAFLorenz.m │ ├── NGDPactual.mat │ ├── NGDPd.mat │ ├── NGDPdisp.mat │ ├── NGDPsurv.mat │ ├── Qdoms.mat │ ├── STMCMC.mat │ ├── ST_25_prior.mat │ ├── SVLorenz.asv │ ├── SVLorenz.m │ ├── StartST.mat │ ├── StartST2.mat │ ├── Untitled.m │ ├── benchMCMC.mat │ ├── beta_pdf.m │ ├── bindec.m │ ├── binom_dist.m │ ├── binvec2dec.m │ ├── dCPI.mat │ ├── dTFP.mat │ ├── dec2binvec.m │ ├── doubleo.m │ ├── hpfilter.m │ ├── hs_err_pid10000.log │ ├── hs_err_pid4236.log │ ├── hs_err_pid5504.log │ ├── hs_err_pid7752.log │ ├── hs_err_pid9560.log │ ├── kfilter.m │ ├── lastMCMC.mat │ ├── lldraw.mat │ ├── llmax.mat │ ├── logGDP.mat │ ├── logdet.m │ ├── logpriorMBD.m │ ├── norm_cdf.m │ ├── norm_pdf.m │ ├── norm_rnd.m │ ├── normlt_rnd.m │ ├── normrt_rnd.m │ ├── normt_rnd.m │ ├── pathdef.m │ ├── postmax.mat │ ├── priordraw.mat │ ├── recessionbars.m │ ├── sim_est_lorenzoni.m │ ├── simannb.m │ ├── st.fig │ ├── stdn_cdf.m │ ├── stdn_pdf.m │ ├── vscale.mat │ ├── xmax.mat │ └── xmaxMarch23.mat ├── REsolutionmethods.m ├── UCmodel.m └── simannb.m ├── CM_Bayesian Macro - Slides - Nimark ├── .DS_Store ├── Lecture_1_Macro_models_data.pdf ├── Lecture_2_SSS.pdf ├── Lecture_3_Slides_DSGE_MH.pdf ├── Lecture_4_Slides_Bayesian_Analysis 2.pdf ├── Lecture_4_Slides_Bayesian_Analysis.pdf └── Lecture_5_Slides_News_Noise.pdf ├── README.md └── RF_Bayesian Macro - Student Reading Material - Nimark ├── Lecture 1 Modern Macro DGP Reading Material ├── Lecture Notes Solving RE models.pdf ├── Lucas 1976.pdf ├── Soderlind EER 1999.pdf └── The Projection Theorem.pdf ├── Lecture 2 State Space Models Reading Material └── Lecture Notes Kalman Filter.pdf ├── Lecture 3 Bayesian Estimation Reading Material ├── an-schorfheide_ER06.pdf └── bayesian_macro.pdf └── Lecture 5 News and Noise Reading Material ├── Barsky and Sims AER 2012.pdf ├── Blanchard LHullier and Lorenzoni 2013.pdf ├── DHOE Nimark 2017.pdf ├── Enders_Kleeman_Mueller_2014.pdf ├── Lorenzoni AER 2009.pdf └── Nimark AER 2014.pdf /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/.DS_Store -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/.DS_Store -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/DSGE_SA.m: -------------------------------------------------------------------------------- 1 | % Set up and estimate miniture DSGE model 2 | clc 3 | clear all 4 | close all 5 | global Z 6 | load('Z'); %load data 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | % Initial values of structural parameters 9 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 10 | r=0.95; %productivity persistence 11 | g=5; %relative risk aversion (gamma) 12 | d=0.75; %Calvo parameter (price stickyness) 13 | b=0.99; %discount factor 14 | k=((1-d)*(1-d*b))/d; %slope of Phillips curve 15 | f=1.5;% coefficient on inflation in Taylor rule (phi) 16 | sigx=0.1;% s.d. prod shock 17 | sigy=0.11;% s.d. demand shock 18 | sigp=0.1;% s.d. cost push shock 19 | sigr=0.1;% s.d. cost push shock 20 | 21 | names={'r' 'g' 'd' 'b' 'f' 'sigx' 'sigy' 'sigp' 'sigr'}; 22 | theta=[r,g,d,b,f,sigx,sigy,sigp,sigr]';%Starting value for parameter vector 23 | LB=[0,0,0,0,1,zeros(1,4);]';%Lower bound for parameter vector 24 | UB=[1,10,1,1,5,1*ones(1,4);]';%Upper bound for parameter vector 25 | x=theta; 26 | 27 | % x=THETA; 28 | % initial temperature 29 | sa_t= 5; 30 | % temperature reduction factor 31 | sa_rt=.3; 32 | % number of draws before the change in temperature 33 | sa_nt=5; 34 | % number of draws before a step size adjustment 35 | sa_ns=5; 36 | % warning off all; 37 | 38 | [xhat]=simannb( 'LLDSGE', x, LB, UB, sa_t, sa_rt, sa_nt, sa_ns, 1); 39 | 40 | %-------------------------------------------------------------------------- 41 | 42 | theta=xhat; 43 | r=theta(1); %productivity persistence 44 | g=theta(2); %relative risk aversion 45 | d=theta(3); %Calvo parameter 46 | b=theta(4); %discount factor 47 | k=((1-d)*(1-d*b))/d; %slope of Phillips curve 48 | f=theta(5);% coefficient on inflation in Taylor rule 49 | sigx=theta(6);% s.d. prod shock 50 | sigy=theta(7);% s.d. demand shock 51 | sigp=theta(8);% s.d. cost push shock 52 | sigr=theta(9);% s.d. measurement error interest rate 53 | 54 | c=g-k*r-2*g*r+k*f+g*(r^2); 55 | A1=[1,-f,0; 56 | 0,1,-k; 57 | (1/g),0,1;]; 58 | R1=[sigr,0,0;0,sigp,0;0,0,sigy;]; 59 | SIGvv=[zeros(3,1), A1\R1;]; 60 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 61 | % Put model in state space form 62 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 63 | A=r; 64 | C=[sigx,zeros(1,3);]; 65 | D=[f*(k*(1-r))/-c;(k*(1-r))/-c;((-k*g*(f-r))/-c)]; 66 | 67 | [X]=smooth(A,C,D,0,SIGvv,Z); 68 | sampleDates=[1981.5:.25:2010.5]; 69 | subplot(2,1,1,'fontsize',18);plot(sampleDates,Z','linewidth',2); 70 | legend('FFR','Inflation','Output') 71 | hold on; 72 | subplot(2,1,2,'fontsize',18); plot(sampleDates,X(:,2:end),'linewidth',2); 73 | legend('Smoothed Estimate of Productivity') 74 | 75 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_1_Matlab_R_E_Solution_Methods/Upload Class I/qzdiv.m: -------------------------------------------------------------------------------- 1 | function [A,B,Q,Z,v] = qzdiv(stake,A,B,Q,Z,v) 2 | %function [A,B,Q,Z,v] = qzdiv(stake,A,B,Q,Z,v) 3 | % 4 | % Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them 5 | % so that all cases of abs(B(i,i)/A(i,i))>stake are in lower right 6 | % corner, while preserving U.T. and orthonormal properties and Q'AZ' and 7 | % Q'BZ'. The columns of v are sorted correspondingly. 8 | % 9 | % by Christopher A. Sims 10 | % modified (to add v to input and output) 7/27/00 11 | vin = nargin==6; 12 | if ~vin, v=[]; end; 13 | [n jnk] = size(A); 14 | root = abs([diag(A) diag(B)]); 15 | root(:,1) = root(:,1)-(root(:,1)<1.e-13).*(root(:,1)+root(:,2)); 16 | root(:,2) = root(:,2)./root(:,1); 17 | for i = n:-1:1 18 | m=0; 19 | for j=i:-1:1 20 | if (root(j,2) > stake | root(j,2) < -.1) 21 | m=j; 22 | break 23 | end 24 | end 25 | if (m==0) 26 | return 27 | end 28 | for k=m:1:i-1 29 | [A B Q Z] = qzswitch(k,A,B,Q,Z); 30 | tmp = root(k,2); 31 | root(k,2) = root(k+1,2); 32 | root(k+1,2) = tmp; 33 | if vin 34 | tmp=v(:,k); 35 | v(:,k)=v(:,k+1); 36 | v(:,k+1)=tmp; 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_1_Matlab_R_E_Solution_Methods/Upload Class I/qzdivct.m: -------------------------------------------------------------------------------- 1 | function [A,B,Q,Z] = qzdivct(stake,A,B,Q,Z) 2 | %function [A,B,Q,Z] = qzdivct(stake,A,B,Q,Z) 3 | % 4 | % Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them 5 | % so that all cases of real(B(i,i)/A(i,i))>stake are in lower right 6 | % corner, while preserving U.T. and orthonormal properties and Q'AZ' and 7 | % Q'BZ'. abs(A(i,i))<1e-11 is interpreted as a zero and as generating 8 | % an infinitely positive real part of the ratio. All i's for which this 9 | % criterion are satisfied are grouped together in the lower right corner 10 | % of the lower right corner, with the non-zero roots above them. This 11 | % version differs from 12 | % qzdiv in that it works on the real part's value, as is appropriate for 13 | % continuous time models, instead of on the absolute value, as is 14 | % appropriate for discrete time models. 15 | % 16 | realsmall=sqrt(eps)*10; 17 | %realsmall=1e-3; 18 | [n jnk] = size(A); 19 | root = [diag(A) diag(B)]; 20 | % first sort on the non-zero root criterion 21 | xdown0 = abs(root(:,1)) stake)); 23 | for i = n:-1:1 24 | m=0; 25 | for j=i:-1:1 26 | if xdown0(j) 27 | m=j; 28 | break 29 | end 30 | end 31 | if (m==0) 32 | break 33 | end 34 | for k=m:1:i-1 35 | [A B Q Z] = qzswitch(k,A,B,Q,Z); 36 | root=[diag(A) diag(B)]; 37 | xdown0(k:k+1)=flipud(xdown0(k:k+1)); 38 | xdown(k:k+1)=flipud(xdown(k:k+1)); 39 | if any(xdown(k:k+1)~=(xdown0(k:k+1) | (real(root(k:k+1,2)./(xdown0(k:k+1)+root(k:k+1,1)))) > stake)) 40 | disp('xdown shift during 0 pack at i,k:') 41 | disp([i k]) 42 | end 43 | end 44 | end 45 | % now repeat, using the stake criterion 46 | for i = n:-1:1 47 | m=0; 48 | for j=i:-1:1 49 | if xdown(j) 50 | m=j; 51 | break 52 | end 53 | end 54 | if (m==0) 55 | return 56 | end 57 | for k=m:1:i-1 58 | gevOld=root(k:k+1,:); 59 | [A B Q Z] = qzswitch(k,A,B,Q,Z); 60 | root=[diag(A) diag(B)]; 61 | xdown0(k:k+1)=flipud(xdown0(k:k+1)); 62 | xdown(k:k+1)=flipud(xdown(k:k+1)); 63 | if any(xdown(k:k+1)~=(xdown0(k:k+1) | (real(root(k:k+1,2)./(xdown0(k:k+1)+root(k:k+1,1)))) > stake)) 64 | disp('xdown shift during pos pack at i,k:') 65 | disp([i k]) 66 | gev=root(k:k+1,:); 67 | [gevOld gevOld(:,1).\gevOld(:,2);gev gev(:,1).\gev(:,2)] 68 | end 69 | end 70 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_1_Matlab_R_E_Solution_Methods/Upload Class I/qzswitch.m: -------------------------------------------------------------------------------- 1 | function [A,B,Q,Z] = qzswitch(i,A,B,Q,Z) 2 | %function [A,B,Q,Z] = qzswitch(i,A,B,Q,Z) 3 | % 4 | % Takes U.T. matrices A, B, orthonormal matrices Q,Z, interchanges 5 | % diagonal elements i and i+1 of both A and B, while maintaining 6 | % Q'AZ' and Q'BZ' unchanged. If diagonal elements of A and B 7 | % are zero at matching positions, the returned A will have zeros at both 8 | % positions on the diagonal. This is natural behavior if this routine is used 9 | % to drive all zeros on the diagonal of A to the lower right, but in this case 10 | % the qz transformation is not unique and it is not possible simply to switch 11 | % the positions of the diagonal elements of both A and B. 12 | realsmall=sqrt(eps)*10; 13 | %realsmall=1e-3; 14 | a = A(i,i); d = B(i,i); b = A(i,i+1); e = B(i,i+1); 15 | c = A(i+1,i+1); f = B(i+1,i+1); 16 | % A(i:i+1,i:i+1)=[a b; 0 c]; 17 | % B(i:i+1,i:i+1)=[d e; 0 f]; 18 | if (abs(c)abs(s(i,i)*t(i+1,i+1)); 15 | [s,t,q,z] = qzswitch(i,s,t,q,z); 16 | if ~(i==1);i = i-2;end 17 | end 18 | i=i+1; 19 | end 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_1_Matlab_R_E_Solution_Methods/qzdiv.m: -------------------------------------------------------------------------------- 1 | function [A,B,Q,Z,v] = qzdiv(stake,A,B,Q,Z,v) 2 | %function [A,B,Q,Z,v] = qzdiv(stake,A,B,Q,Z,v) 3 | % 4 | % Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them 5 | % so that all cases of abs(B(i,i)/A(i,i))>stake are in lower right 6 | % corner, while preserving U.T. and orthonormal properties and Q'AZ' and 7 | % Q'BZ'. The columns of v are sorted correspondingly. 8 | % 9 | % by Christopher A. Sims 10 | % modified (to add v to input and output) 7/27/00 11 | vin = nargin==6; 12 | if ~vin, v=[]; end; 13 | [n jnk] = size(A); 14 | root = abs([diag(A) diag(B)]); 15 | root(:,1) = root(:,1)-(root(:,1)<1.e-13).*(root(:,1)+root(:,2)); 16 | root(:,2) = root(:,2)./root(:,1); 17 | for i = n:-1:1 18 | m=0; 19 | for j=i:-1:1 20 | if (root(j,2) > stake | root(j,2) < -.1) 21 | m=j; 22 | break 23 | end 24 | end 25 | if (m==0) 26 | return 27 | end 28 | for k=m:1:i-1 29 | [A B Q Z] = qzswitch(k,A,B,Q,Z); 30 | tmp = root(k,2); 31 | root(k,2) = root(k+1,2); 32 | root(k+1,2) = tmp; 33 | if vin 34 | tmp=v(:,k); 35 | v(:,k)=v(:,k+1); 36 | v(:,k+1)=tmp; 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_1_Matlab_R_E_Solution_Methods/qzdivct.m: -------------------------------------------------------------------------------- 1 | function [A,B,Q,Z] = qzdivct(stake,A,B,Q,Z) 2 | %function [A,B,Q,Z] = qzdivct(stake,A,B,Q,Z) 3 | % 4 | % Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them 5 | % so that all cases of real(B(i,i)/A(i,i))>stake are in lower right 6 | % corner, while preserving U.T. and orthonormal properties and Q'AZ' and 7 | % Q'BZ'. abs(A(i,i))<1e-11 is interpreted as a zero and as generating 8 | % an infinitely positive real part of the ratio. All i's for which this 9 | % criterion are satisfied are grouped together in the lower right corner 10 | % of the lower right corner, with the non-zero roots above them. This 11 | % version differs from 12 | % qzdiv in that it works on the real part's value, as is appropriate for 13 | % continuous time models, instead of on the absolute value, as is 14 | % appropriate for discrete time models. 15 | % 16 | realsmall=sqrt(eps)*10; 17 | %realsmall=1e-3; 18 | [n jnk] = size(A); 19 | root = [diag(A) diag(B)]; 20 | % first sort on the non-zero root criterion 21 | xdown0 = abs(root(:,1)) stake)); 23 | for i = n:-1:1 24 | m=0; 25 | for j=i:-1:1 26 | if xdown0(j) 27 | m=j; 28 | break 29 | end 30 | end 31 | if (m==0) 32 | break 33 | end 34 | for k=m:1:i-1 35 | [A B Q Z] = qzswitch(k,A,B,Q,Z); 36 | root=[diag(A) diag(B)]; 37 | xdown0(k:k+1)=flipud(xdown0(k:k+1)); 38 | xdown(k:k+1)=flipud(xdown(k:k+1)); 39 | if any(xdown(k:k+1)~=(xdown0(k:k+1) | (real(root(k:k+1,2)./(xdown0(k:k+1)+root(k:k+1,1)))) > stake)) 40 | disp('xdown shift during 0 pack at i,k:') 41 | disp([i k]) 42 | end 43 | end 44 | end 45 | % now repeat, using the stake criterion 46 | for i = n:-1:1 47 | m=0; 48 | for j=i:-1:1 49 | if xdown(j) 50 | m=j; 51 | break 52 | end 53 | end 54 | if (m==0) 55 | return 56 | end 57 | for k=m:1:i-1 58 | gevOld=root(k:k+1,:); 59 | [A B Q Z] = qzswitch(k,A,B,Q,Z); 60 | root=[diag(A) diag(B)]; 61 | xdown0(k:k+1)=flipud(xdown0(k:k+1)); 62 | xdown(k:k+1)=flipud(xdown(k:k+1)); 63 | if any(xdown(k:k+1)~=(xdown0(k:k+1) | (real(root(k:k+1,2)./(xdown0(k:k+1)+root(k:k+1,1)))) > stake)) 64 | disp('xdown shift during pos pack at i,k:') 65 | disp([i k]) 66 | gev=root(k:k+1,:); 67 | [gevOld gevOld(:,1).\gevOld(:,2);gev gev(:,1).\gev(:,2)] 68 | end 69 | end 70 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_1_Matlab_R_E_Solution_Methods/qzswitch.m: -------------------------------------------------------------------------------- 1 | function [A,B,Q,Z] = qzswitch(i,A,B,Q,Z) 2 | %function [A,B,Q,Z] = qzswitch(i,A,B,Q,Z) 3 | % 4 | % Takes U.T. matrices A, B, orthonormal matrices Q,Z, interchanges 5 | % diagonal elements i and i+1 of both A and B, while maintaining 6 | % Q'AZ' and Q'BZ' unchanged. If diagonal elements of A and B 7 | % are zero at matching positions, the returned A will have zeros at both 8 | % positions on the diagonal. This is natural behavior if this routine is used 9 | % to drive all zeros on the diagonal of A to the lower right, but in this case 10 | % the qz transformation is not unique and it is not possible simply to switch 11 | % the positions of the diagonal elements of both A and B. 12 | realsmall=sqrt(eps)*10; 13 | %realsmall=1e-3; 14 | a = A(i,i); d = B(i,i); b = A(i,i+1); e = B(i,i+1); 15 | c = A(i+1,i+1); f = B(i+1,i+1); 16 | % A(i:i+1,i:i+1)=[a b; 0 c]; 17 | % B(i:i+1,i:i+1)=[d e; 0 f]; 18 | if (abs(c)abs(s(i,i)*t(i+1,i+1)); 15 | [s,t,q,z] = qzswitch(i,s,t,q,z); 16 | if ~(i==1);i = i-2;end 17 | end 18 | i=i+1; 19 | end 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/Kalman_filter_app.m: -------------------------------------------------------------------------------- 1 | % Kalman filter applications 2 | % Code to play around with Kalman filter for state space system defined as 3 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 4 | % X[t] = AX[t-1] + Cu[t] 5 | % 6 | % Z[t] = DX[t] + Ru[t] 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | clear all 9 | clc 10 | close all 11 | 12 | periods=200; 13 | 14 | 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | %Examples 17 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 18 | 19 | % % %Scalar case 20 | % A=0.95; 21 | % C=1; 22 | % D=1; 23 | % R=.5; 24 | 25 | 26 | %Bivariate state, scalar signal 27 | % A=[0.99, 0.1; -.3,0.5;]; 28 | % C=eye(2); 29 | % D=[1 1]; 30 | % R=1; 31 | 32 | % %Bivariate state, two dimensional signal 33 | % A=[0.99, 0.1; -.3,0.5;]; 34 | % C=eye(2); 35 | % D=eye(2); 36 | % R=eye(2); 37 | 38 | % %Bivariate state, two dimensional signal 39 | A=eye(9); 40 | C=eye(9); 41 | D=eye(9); 42 | R=10*eye(9); 43 | 44 | [K,P,Kss,Pss,Z]=kalman_filter_sim(A,C,D,R,periods); 45 | 46 | [LL]=LL_state_space(A,C,D,R,Z) -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/LL_state_space.m: -------------------------------------------------------------------------------- 1 | function [LL]=LL_state_space(A,C,D,R,Z) 2 | 3 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 4 | % Computes log likelihood for state space system 5 | % X[t] = AX[t-1] + Cu[t] 6 | % 7 | % Z[t] = DX[t] + Ru[t] 8 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 9 | 10 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 11 | % Set initial values for Kalman filter etc 12 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 13 | T=length(Z); 14 | LL=0; 15 | if max(abs(eig(A)))<1 16 | P0=dlyap(A,C*C'); 17 | else 18 | P0=0; 19 | for j=1:100; 20 | P0=A*P0*A'+C*C'; 21 | end 22 | end 23 | dimZ=length(D(:,1)); 24 | Xfilt=A(:,1)*0; %initial value for the filtered state. 25 | %Compute recursive likleihood using the Kalman filter 26 | for tt=1:T 27 | Ztilde=Z(:,tt)-D*A*Xfilt; 28 | Omega=(D*A)*P0*(D*A)'+(D*C+R)*(D*C+R)'; 29 | Omegainv=eye(dimZ)/Omega; 30 | K=(A*P0*(D*A)'+C*C'*D'+C*R')*Omegainv; 31 | Xfilt=A*Xfilt+K*Ztilde; 32 | P1=A*P0*A'+C*C'; 33 | P0=P1-K*Omega*K'; 34 | 35 | LL = LL - 0.5*dimZ*log(2*pi) - 0.5*(log(det(Omega)) + Ztilde'*Omegainv*Ztilde); 36 | end 37 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/ML DSGE/DSGE_SA.asv: -------------------------------------------------------------------------------- 1 | % Set up and estimate miniture DSGE model 2 | clc 3 | clear all 4 | close all 5 | global Z 6 | load('Z'); %load data 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | % Initial values of structural parameters 9 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 10 | r=0.95; %productivity persistence 11 | g=5; %relative risk aversion 12 | d=0.75; %Calvo parameter 13 | b=0.99; %discount factor 14 | k=((1-d)*(1-d*b))/d; %slope of Phillips curve 15 | f=1.5;% coefficient on inflation in Taylor rule 16 | sigx=0.1;% s.d. prod shock 17 | sigy=0.11;% s.d. demand shock 18 | sigp=0.1;% s.d. cost push shock 19 | sigr=0.1;% s.d. cost push shock 20 | 21 | 22 | theta=[r,g,d,b,f,sigx,sigy,sigp,sigr]';%Starting value for parameter vector 23 | LB=[0,0,0,0,1,zeros(1,4);]';%Lower bound for parameter vector 24 | UB=[1,10,1,1,5,1*ones(1,4);]';%Upper bound for parameter vector 25 | x=theta; 26 | 27 | % x=THETA; 28 | sa_t= 5; 29 | sa_rt=.3; 30 | sa_nt=5; 31 | sa_ns=5; 32 | % warning off all; 33 | 34 | [xhat]=simannb( 'LLDSGE', x, LB, UB, sa_t, sa_rt, sa_nt, sa_ns, 1); 35 | 36 | %-------------------------------------------------------------------------- 37 | 38 | theta=xhat; 39 | r=theta(1); %productivity persistence 40 | g=theta(2); %relative risk aversion 41 | d=theta(3); %Calvo parameter 42 | b=theta(4); %discount factor 43 | k=((1-d)*(1-d*b))/d; %slope of Phillips curve 44 | f=theta(5);% coefficient on inflation in Taylor rule 45 | sigx=theta(6);% s.d. prod shock 46 | sigy=theta(7);% s.d. demand shock 47 | sigp=theta(8);% s.d. cost push shock 48 | sigr=theta(9);% s.d. measurement error interest rate 49 | 50 | c=g-k*r-2*g*r+k*f+g*(r^2); 51 | A1=[1,-f,0; 52 | 0,1,-k; 53 | (1/g),0,1;]; 54 | R1=[sigr,0,0;0,sigp,0;0,0,sigy;]; 55 | SIGvv=[zeros(3,1), A1\R1;]; 56 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 57 | % Put model in state space form 58 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 59 | A=r; 60 | C=[sigx,zeros(1,3);]; 61 | D=[f*(k*(1-r))/-c;(k*(1-r))/-c;((-k*g*(f-r))/-c)]; 62 | 63 | [X]=smooth(A,C,D,0,SIGvv,Z); 64 | sampleDates=[1981.5:.25:2010.5]; 65 | subplot(2,1,1,'fontsize',18);plot(sampleDates,Z','linewidth',2); 66 | legend('FFR','Inflation','Output') 67 | hold on; 68 | subplot(2,1,2,'fontsize',18); plot(sampleDates,X(:,2:end),'linewidth',2); 69 | legend('Smoothed Estimate of Productivity') 70 | 71 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/ML DSGE/DSGE_SA.m: -------------------------------------------------------------------------------- 1 | % Set up and estimate miniture DSGE model 2 | clc 3 | clear all 4 | close all 5 | global Z 6 | load('Z'); %load data 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | % Initial values of structural parameters 9 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 10 | r=0.95; %productivity persistence 11 | g=5; %relative risk aversion 12 | d=0.75; %Calvo parameter 13 | b=0.99; %discount factor 14 | k=((1-d)*(1-d*b))/d; %slope of Phillips curve 15 | f=1.5;% coefficient on inflation in Taylor rule 16 | sigx=0.1;% s.d. prod shock 17 | sigy=0.11;% s.d. demand shock 18 | sigp=0.1;% s.d. cost push shock 19 | sigr=0.1;% s.d. cost push shock 20 | 21 | 22 | theta=[r,g,d,b,f,sigx,sigy,sigp,sigr]';%Starting value for parameter vector 23 | LB=[0,0,0,0,1,zeros(1,4);]';%Lower bound for parameter vector 24 | UB=[1,10,1,1,5,1*ones(1,4);]';%Upper bound for parameter vector 25 | x=theta; 26 | 27 | % x=THETA; 28 | sa_t= 5; % initial temperature 29 | sa_rt=.3; % temperature reduction factor 30 | sa_nt=5; % number of draws before the change in tempreature 31 | sa_ns=5; % number of draws before a step size adjustement (making the grid finer) 32 | % warning off all; 33 | 34 | [xhat]=simannb( 'LLDSGE', x, LB, UB, sa_t, sa_rt, sa_nt, sa_ns, 1); 35 | 36 | %-------------------------------------------------------------------------- 37 | 38 | theta=xhat; 39 | r=theta(1); %productivity persistence 40 | g=theta(2); %relative risk aversion 41 | d=theta(3); %Calvo parameter 42 | b=theta(4); %discount factor 43 | k=((1-d)*(1-d*b))/d; %slope of Phillips curve 44 | f=theta(5);% coefficient on inflation in Taylor rule 45 | sigx=theta(6);% s.d. prod shock 46 | sigy=theta(7);% s.d. demand shock 47 | sigp=theta(8);% s.d. cost push shock 48 | sigr=theta(9);% s.d. measurement error interest rate 49 | 50 | c=g-k*r-2*g*r+k*f+g*(r^2); 51 | A1=[1,-f,0; 52 | 0,1,-k; 53 | (1/g),0,1;]; 54 | R1=[sigr,0,0;0,sigp,0;0,0,sigy;]; 55 | SIGvv=[zeros(3,1), A1\R1;]; 56 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 57 | % Put model in state space form 58 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 59 | A=r; 60 | C=[sigx,zeros(1,3);]; 61 | D=[f*(k*(1-r))/-c;(k*(1-r))/-c;((-k*g*(f-r))/-c)]; 62 | 63 | [X]=smooth(A,C,D,0,SIGvv,Z); 64 | sampleDates=[1981.5:.25:2010.5]; 65 | subplot(2,1,1,'fontsize',18);plot(sampleDates,Z','linewidth',2); 66 | legend('FFR','Inflation','Output') 67 | hold on; 68 | subplot(2,1,2,'fontsize',18); plot(sampleDates,X(:,2:end),'linewidth',2); 69 | legend('Smoothed Estimate of Productivity') 70 | 71 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/ML DSGE/LLDSGE.m: -------------------------------------------------------------------------------- 1 | function [LL]=LLDSGE(theta) 2 | global Z 3 | 4 | r=theta(1); %productivity persistence 5 | g=theta(2); %relative risk aversion 6 | d=theta(3); %Calvo parameter 7 | b=theta(4); %discount factor 8 | k=((1-d)*(1-d*b))/d; %slope of Phillips curve 9 | f=theta(5);% coefficient on inflation in Taylor rule 10 | sigx=theta(6);% s.d. prod shock 11 | sigy=theta(7);% s.d. demand shock 12 | sigp=theta(8);% s.d. cost push shock 13 | sigr=theta(9);% s.d. cost push shock 14 | 15 | c=g-k*r-2*g*r+k*f+g*(r^2); 16 | 17 | A1=[1,-f,0; 18 | 0,1,-k; 19 | (1/g),0,1;]; 20 | R1=[sigr,0,0;0,sigp,0;0,0,sigy;]; 21 | R=A1\R1; 22 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 23 | % Put model in state space form 24 | % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 25 | A=r; 26 | C=sigx; 27 | D=[f*((k*(1-r))/-c);(k*(1-r))/-c;(-k*g*(f-r))/-c;]; 28 | 29 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 30 | % Set initial values for Kalman filter etc 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 32 | 33 | n=length(D(:,1)); %dimension of observables; 34 | T=length(Z); 35 | LL=0; 36 | P= inv(1-r^2)*sigx^2;%Initial uncertainty equal to unconditional variance of state 37 | Xfilt=0; %initial value for the filtered state. 38 | % RR=SIGvv*SIGvv'; 39 | RR=R*R'; 40 | QQ=C*C'; 41 | 42 | 43 | %Compute recursive likleihood using the Kalman filter 44 | for tt=1:T 45 | a=Z(:,tt)-D*Xfilt;%These are the innovations (i.e. Ztilde) 46 | Omega=D*P*D'+RR; 47 | Omegainv=inv(Omega); 48 | K=P*D'*Omegainv; 49 | Xfilt=A*Xfilt+A*K*a; 50 | P = A*(P-P*D'*Omegainv*D*P)*A' + QQ; 51 | LL = LL - 0.5*(log(det(Omega)) + a'*Omegainv*a); 52 | end 53 | if imag(LL)~=0 54 | LL=-9e+200; 55 | end 56 | 57 | LL=-LL;%Because we are minimizing 58 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/ML DSGE/Z.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/ML DSGE/Z.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/ML DSGE/hpfilter.m: -------------------------------------------------------------------------------- 1 | function [s]=hpfilter(y,w) 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | % Author: Ivailo Izvorski, 4 | % Department of Economics 5 | % Yale University. 6 | % izvorski@econ.yale.edu 7 | % This code has been used and seems to be free of error. 8 | % However, it carries no explicit or implicit guarantee. 9 | % 10 | % function [s]=hpfilter(y,w) 11 | % Hondrick Prescott filter where: 12 | % w - smoothing parameter; w=1600 for quarterly data 13 | % y - the original series that has to be smoothed 14 | % s - the filtered series 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | if size(y,1)stake are in lower right 6 | % corner, while preserving U.T. and orthonormal properties and Q'AZ' and 7 | % Q'BZ'. The columns of v are sorted correspondingly. 8 | % 9 | % by Christopher A. Sims 10 | % modified (to add v to input and output) 7/27/00 11 | vin = nargin==6; 12 | if ~vin, v=[]; end; 13 | [n jnk] = size(A); 14 | root = abs([diag(A) diag(B)]); 15 | root(:,1) = root(:,1)-(root(:,1)<1.e-13).*(root(:,1)+root(:,2)); 16 | root(:,2) = root(:,2)./root(:,1); 17 | for i = n:-1:1 18 | m=0; 19 | for j=i:-1:1 20 | if (root(j,2) > stake | root(j,2) < -.1) 21 | m=j; 22 | break 23 | end 24 | end 25 | if (m==0) 26 | return 27 | end 28 | for k=m:1:i-1 29 | [A B Q Z] = qzswitch(k,A,B,Q,Z); 30 | tmp = root(k,2); 31 | root(k,2) = root(k+1,2); 32 | root(k+1,2) = tmp; 33 | if vin 34 | tmp=v(:,k); 35 | v(:,k)=v(:,k+1); 36 | v(:,k+1)=tmp; 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/ML DSGE/qzdivct.m: -------------------------------------------------------------------------------- 1 | function [A,B,Q,Z] = qzdivct(stake,A,B,Q,Z) 2 | %function [A,B,Q,Z] = qzdivct(stake,A,B,Q,Z) 3 | % 4 | % Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them 5 | % so that all cases of real(B(i,i)/A(i,i))>stake are in lower right 6 | % corner, while preserving U.T. and orthonormal properties and Q'AZ' and 7 | % Q'BZ'. abs(A(i,i))<1e-11 is interpreted as a zero and as generating 8 | % an infinitely positive real part of the ratio. All i's for which this 9 | % criterion are satisfied are grouped together in the lower right corner 10 | % of the lower right corner, with the non-zero roots above them. This 11 | % version differs from 12 | % qzdiv in that it works on the real part's value, as is appropriate for 13 | % continuous time models, instead of on the absolute value, as is 14 | % appropriate for discrete time models. 15 | % 16 | realsmall=sqrt(eps)*10; 17 | %realsmall=1e-3; 18 | [n jnk] = size(A); 19 | root = [diag(A) diag(B)]; 20 | % first sort on the non-zero root criterion 21 | xdown0 = abs(root(:,1)) stake)); 23 | for i = n:-1:1 24 | m=0; 25 | for j=i:-1:1 26 | if xdown0(j) 27 | m=j; 28 | break 29 | end 30 | end 31 | if (m==0) 32 | break 33 | end 34 | for k=m:1:i-1 35 | [A B Q Z] = qzswitch(k,A,B,Q,Z); 36 | root=[diag(A) diag(B)]; 37 | xdown0(k:k+1)=flipud(xdown0(k:k+1)); 38 | xdown(k:k+1)=flipud(xdown(k:k+1)); 39 | if any(xdown(k:k+1)~=(xdown0(k:k+1) | (real(root(k:k+1,2)./(xdown0(k:k+1)+root(k:k+1,1)))) > stake)) 40 | disp('xdown shift during 0 pack at i,k:') 41 | disp([i k]) 42 | end 43 | end 44 | end 45 | % now repeat, using the stake criterion 46 | for i = n:-1:1 47 | m=0; 48 | for j=i:-1:1 49 | if xdown(j) 50 | m=j; 51 | break 52 | end 53 | end 54 | if (m==0) 55 | return 56 | end 57 | for k=m:1:i-1 58 | gevOld=root(k:k+1,:); 59 | [A B Q Z] = qzswitch(k,A,B,Q,Z); 60 | root=[diag(A) diag(B)]; 61 | xdown0(k:k+1)=flipud(xdown0(k:k+1)); 62 | xdown(k:k+1)=flipud(xdown(k:k+1)); 63 | if any(xdown(k:k+1)~=(xdown0(k:k+1) | (real(root(k:k+1,2)./(xdown0(k:k+1)+root(k:k+1,1)))) > stake)) 64 | disp('xdown shift during pos pack at i,k:') 65 | disp([i k]) 66 | gev=root(k:k+1,:); 67 | [gevOld gevOld(:,1).\gevOld(:,2);gev gev(:,1).\gev(:,2)] 68 | end 69 | end 70 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/ML DSGE/qzswitch.m: -------------------------------------------------------------------------------- 1 | function [A,B,Q,Z] = qzswitch(i,A,B,Q,Z) 2 | %function [A,B,Q,Z] = qzswitch(i,A,B,Q,Z) 3 | % 4 | % Takes U.T. matrices A, B, orthonormal matrices Q,Z, interchanges 5 | % diagonal elements i and i+1 of both A and B, while maintaining 6 | % Q'AZ' and Q'BZ' unchanged. If diagonal elements of A and B 7 | % are zero at matching positions, the returned A will have zeros at both 8 | % positions on the diagonal. This is natural behavior if this routine is used 9 | % to drive all zeros on the diagonal of A to the lower right, but in this case 10 | % the qz transformation is not unique and it is not possible simply to switch 11 | % the positions of the diagonal elements of both A and B. 12 | realsmall=sqrt(eps)*10; 13 | %realsmall=1e-3; 14 | a = A(i,i); d = B(i,i); b = A(i,i+1); e = B(i,i+1); 15 | c = A(i+1,i+1); f = B(i+1,i+1); 16 | % A(i:i+1,i:i+1)=[a b; 0 c]; 17 | % B(i:i+1,i:i+1)=[d e; 0 f]; 18 | if (abs(c)abs(s(i,i)*t(i+1,i+1)); 15 | [s,t,q,z] = qzswitch(i,s,t,q,z); 16 | if ~(i==1);i = i-2;end 17 | end 18 | i=i+1; 19 | end 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/ML DSGE/smooth.m: -------------------------------------------------------------------------------- 1 | function [X]=smooth(A,C,D1,D2,R,Z) 2 | if D2==0; 3 | D2=D1*0; 4 | end 5 | % Kalman simulation smoother 6 | % Adapted from Durbin and |Koopman (2002) by K Nimark 7 | 8 | % Xt = A*Xt-1 +C*ut 9 | % 10 | % Zt= D1*Xt + D2*Xt-1 + R*ut 11 | 12 | 13 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 14 | %Define a ancilliary variables, predefine matrices etc 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | T=length(Z); 17 | dimX=length(A); %dimension of state 18 | dimS=length(R(1,:)); %dimension of shocks; 19 | dimZ=length(D1(:,1)); %dimension of observables; 20 | 21 | CC=C*C'; 22 | if abs(max(eig(A)))>=1; 23 | P0=CC*1000; 24 | else 25 | P0=reshape(inv(eye(dimX*dimX)-kron(A,A))*CC(:),dimX,dimX); 26 | end 27 | Xhat=zeros(dimX,T+1); 28 | PP0=zeros(dimX,dimX,T); 29 | PP1=zeros(dimX,dimX,T); 30 | 31 | 32 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 33 | % forward recursion 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 35 | for tt=1:T 36 | Ztilde=Z(:,tt)-D1*A*Xhat(:,tt)-D2*Xhat(:,tt); 37 | Omega=(D1*A+D2)*P0*(D1*A+D2)'+(D1*C+R)*(D1*C+R)'; 38 | Omegainv=eye(dimZ)/Omega; 39 | K=(A*P0*(D1*A+D2)'+C*C'*D1'+C*R')*Omegainv; 40 | Xhat(:,tt+1)=A*Xhat(:,tt)+K*Ztilde; 41 | P1=A*P0*A'+C*C'; 42 | P0=P1-K*Omega*K'; 43 | P1=A*P0*A'+C*C'; 44 | PP0(:,:,tt)=P0; 45 | PP1(:,:,tt)=P1; 46 | end 47 | 48 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 49 | %construct what is needed for last step 50 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 51 | 52 | Xtilde=A*Xhat(:,1:end); 53 | Xhat=Xhat(:,1:end); 54 | 55 | Xsm=Xhat(:,1:end)*0; 56 | Xsm(:,T+1)=Xhat(:,end); 57 | 58 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 59 | %backward recursion 60 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 61 | 62 | for tt=T:-1:1 63 | J=PP0(:,:,tt)*A'*inv(PP1(:,:,tt)+eye(dimX)*1e-8); 64 | Xsm(:,tt)=Xhat(:,tt)+J*(Xsm(:,tt+1)-Xtilde(:,tt+1)); 65 | end 66 | 67 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 68 | %spit out 69 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 70 | X=Xsm; 71 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/UCmodel.m: -------------------------------------------------------------------------------- 1 | %Finds maximum likelihood estiamte of parameters for univariate UC model 2 | %with unit root 3 | % tau[t] = tau[t-1] + Cu[t] 4 | % 5 | % pi[t] = tau[t] + Ru[t] 6 | % 7 | clear all; 8 | clc 9 | close all 10 | 11 | load USdata 12 | 13 | Z=USdata(1,:); 14 | 15 | %Scalar UC values for A and C in state space system 16 | A=1; 17 | D=1; 18 | 19 | dimX=size(A,1); 20 | dimZ=length(D(:,1)); 21 | 22 | % periods=1000; 23 | randn('seed', 12345); 24 | 25 | % build the grid for sigma_eps (state) and sigma_eta (measurement) 26 | Cgrid=[0.0001:0.0001:0.01]; 27 | Rgrid=[0.0001:0.001:0.01]; 28 | dimC=length(Cgrid); 29 | dimR=length(Rgrid); 30 | 31 | T=length(Z); 32 | 33 | Grid=zeros(dimC,dimR); 34 | 35 | for c=1:dimC; 36 | 37 | % for each point of the grid (combination of sigma_eps, sigma_eta)... 38 | for r=1:dimR; 39 | C=Cgrid(c); % first guess for sigma_eps 40 | R=Rgrid(r); % first guess for sigma_eta 41 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 42 | %Compute Kalman filter equations 43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 44 | P=zeros(dimX,dimX,T+1); 45 | K=zeros(dimX,dimZ,T); 46 | Xtt=zeros(1,T+1); 47 | LL=0; 48 | P(:,:,1)=C*C'/(1-0.9*A^2); 49 | 50 | % ... compute the whole series of the unobserved state 51 | for t=1:T; 52 | P(:,:,t+1)=A*(P(:,:,t)-(P(:,:,t)*D'/(D*P(:,:,t)*D'+R*R'))*D*P(:,:,t))*A'+C*C'; 53 | K(:,:,t)= P(:,:,t+1)*D'/(D*P(:,:,t+1)*D'+R*R'); 54 | Xtt(:,t+1)=A*Xtt(:,t)+K(:,:,t)*(Z(:,t)-D*A*Xtt(:,t)); 55 | Ztilda=(Z(:,t)-D*A*Xtt(:,t)); 56 | Omega=D*P(:,:,t+1)*D'+R*R'; 57 | % sum the likelihood related to every t 58 | LL=LL-0.5*(log(2*pi)+log(det(Omega))+(Ztilda'/Omega)*Ztilda); 59 | end 60 | % save the likelihood associated with the combination of r and c 61 | Grid(c,r)=LL; 62 | end 63 | end 64 | 65 | % get the position (row and column in the grid) 66 | % of the highest value 67 | 68 | [LLmaxRow IRow]=max(Grid); 69 | [LLmax I]=max(LLmaxRow); 70 | maxindex=[IRow(I) I]; 71 | MLEtheta=[Cgrid(maxindex(1)) Rgrid(maxindex(2))] 72 | C=Cgrid(maxindex(1)); 73 | R=Rgrid(maxindex(2)); 74 | 75 | % conditional to this solution for R and C 76 | % get the whole series of the unobserved component 77 | 78 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 79 | %Compute Kalman filter equations 80 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 81 | P=zeros(dimX,dimX,T+1); 82 | K=zeros(dimX,dimZ,T); 83 | Xtt=zeros(1,T+1); 84 | P(:,:,1)=C*C'/(1-0.9*A^2); 85 | for t=1:T; 86 | P(:,:,t+1)=A*(P(:,:,t)-(P(:,:,t)*D'/(D*P(:,:,t)*D'+R*R'))*D*P(:,:,t))*A'+C*C'; 87 | K(:,:,t)= P(:,:,t+1)*D'/(D*P(:,:,t+1)*D'+R*R'); 88 | Xtt(:,t+1)=A*Xtt(:,t)+K(:,:,t)*(Z(:,t)-D*A*Xtt(:,t)); 89 | end 90 | 91 | figure(1) 92 | plot(Z,'linewidth',2) 93 | hold on; 94 | plot(Xtt(2:end),'linewidth',2,'linestyle','--','color','r') 95 | legend('\pi','\tau_{t|t}') 96 | CC=[C,0;]; 97 | RR=[0,R;]; 98 | [Xsmooth]=smooth(A,CC,D,RR,Z); 99 | 100 | figure(2) % there's a typo here somewhere, Sarah will check it 101 | plot(Xtt(2:end),'linewidth',2,'linestyle','-') 102 | hold on 103 | plot(Xsmooth(1:end),'linewidth',2,'linestyle','--','color','g') 104 | legend('\tau_{t|t}','\tau_{t|T}') 105 | 106 | [M,L,U]=distplot(A,CC,D,RR,Z,0.95,0.05,100,1,0); 107 | 108 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/USdata.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/USdata.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/dist_plot.m: -------------------------------------------------------------------------------- 1 | function [M,L,U]=dist_plot(A,C,D,R,Z,upper,lower,ndraws,plotplease,legendplease) 2 | 3 | %INPUT 4 | % Xt = A*Xt-1 +C*ut 5 | % 6 | % Zt= D1*Xt + R*ut 7 | 8 | % 'upper' and 'lower' are percentiles (e.g. 0.975 and 0.025) 9 | % to be plotted along with median 10 | %ndraws are number of draws used to construct distribution 11 | % set plotplease = 1 if you want plots 12 | % set legendplease = 1 if you want legends 13 | 14 | %OUTPUT 15 | % M,L,U are matrices with rows containing the median, 16 | % lower and upper percentiles of X 17 | 18 | low=ceil(ndraws*lower); 19 | median=ceil(ndraws*.5); 20 | upp=ceil(ndraws*upper); 21 | dimX=length(A); 22 | T=length(Z); 23 | Xdist=zeros(dimX,T+1,ndraws); 24 | 25 | for s=1:ndraws 26 | Xdist(:,:,s)=sim_smooth(A,C,D,R,Z); 27 | end 28 | Xsort=sort(Xdist,3); 29 | 30 | if plotplease==1; 31 | 32 | q=ceil(dimX^.5); 33 | M=reshape(Xsort(:,:,median),dimX,T+1); 34 | L=reshape(Xsort(:,:,low),dimX,T+1); 35 | U=reshape(Xsort(:,:,upp),dimX,T+1); 36 | figure 37 | for jj=1:dimX 38 | subplot(q,q,jj); 39 | plot(L(jj,:),'linewidth',2,'linestyle','--','color','k'); 40 | hold on 41 | plot(M(jj,:),'linewidth',2,'linestyle','-','color','b'); 42 | hold on 43 | plot(U(jj,:),'linewidth',2,'linestyle','--','color','k'); 44 | if legendplease==1 45 | legend('lower','median','upper'); 46 | end 47 | end 48 | end 49 | 50 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/distplot.m: -------------------------------------------------------------------------------- 1 | function [M,L,U]=distplot(A,C,D,R,Z,upper,lower,ndraws,plotplease,legendplease) 2 | 3 | %INPUT 4 | % Xt = A*Xt-1 +C*ut 5 | % 6 | % Zt= D1*Xt + D2*Xt-1 + R*ut 7 | 8 | % 'upper' and 'lower' are percentiles (e.g. 0.975 and 0.025) 9 | % to be plotted along with median 10 | %ndraws are number of draws used to construct distribution 11 | % set plotplease = 1 if you want plots 12 | % set legendplease = 1 if you want legends 13 | 14 | %OUTPUT 15 | % M,L,U are matrices with rows containing the median, 16 | % lower and upper percentiles of X 17 | 18 | low=ceil(ndraws*lower); 19 | median=ceil(ndraws*.5); 20 | upp=ceil(ndraws*upper); 21 | dimX=length(A); 22 | T=length(Z); 23 | Xdist=zeros(dimX,T+1,ndraws); 24 | 25 | for s=1:ndraws 26 | Xdist(:,:,s)=sim_smooth(A,C,D,R,Z); 27 | end 28 | Xsort=sort(Xdist,3); 29 | 30 | if plotplease==1; 31 | 32 | q=ceil(dimX^.5); 33 | M=reshape(Xsort(:,:,median),dimX,T+1); 34 | L=reshape(Xsort(:,:,low),dimX,T+1); 35 | U=reshape(Xsort(:,:,upp),dimX,T+1); 36 | figure 37 | for jj=1:dimX 38 | subplot(q,q,jj); 39 | plot(L(jj,:),'linewidth',2,'linestyle','--','color','k'); 40 | hold on 41 | plot(M(jj,:),'linewidth',2,'linestyle','-','color','b'); 42 | hold on 43 | plot(U(jj,:),'linewidth',2,'linestyle','--','color','k'); 44 | if legendplease==1 45 | legend('lower','median','upper'); 46 | end 47 | end 48 | end 49 | 50 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/kalman_f.m: -------------------------------------------------------------------------------- 1 | function [K,P]=kalman_filter_simulator(A,C,D,R,periods) 2 | %Simulates state space and compute Kalman filter for state space system 3 | % X[t] = AX[t-1] + Cu[t] 4 | % 5 | % Z[t] = DX[t] + Ru[t] 6 | % 7 | % Outputs are the Kalman gain K in 8 | % X[t|t] = X[t|t-1]+ K(Z[t] - DX[t|t-1] ) 9 | % 10 | % and P is the steady state prior error covariance matrix 11 | % 12 | % P = E(X[t]-X[t|t-1])(X[t]-X[t|t-1])' 13 | 14 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 15 | clear all; 16 | clc 17 | close all 18 | 19 | % %Scalar case 20 | A=0.95; 21 | C=1; 22 | D=1; 23 | R=.5; 24 | 25 | 26 | % %Bivariate state, scalar signal 27 | % A=[0.99, 0.1; -.3,0.5;]; 28 | % C=eye(2); 29 | % D=[1 1]; 30 | % R=0; 31 | 32 | % %Bivariate state, two dimensional signal 33 | % A=[0.99, 0.1; -.3,0.5;]; 34 | % C=eye(2); 35 | % D=eye(2); 36 | % R=eye(2); 37 | 38 | 39 | tol=1e-5;maxiter=1000; 40 | diff=1;iter=1; 41 | P0=C*C'; 42 | P1=A*P0*A'+C*C'; 43 | dimX=size(A,1); 44 | dimZ=length(D(:,1)); 45 | 46 | periods=200; 47 | % randn('seed', 12345); 48 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 49 | %Simulate State Space system 50 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 51 | U=randn(dimX,periods); 52 | V=randn(dimZ,periods); 53 | X=zeros(dimX,periods+1); 54 | Z=zeros(dimZ,periods); 55 | for t=1:periods; 56 | X(:,t+1)=A*X(:,t)+C*U(:,t); 57 | Z(:,t+1)=D*X(:,t+1)+R*V(:,t); 58 | end 59 | figure(1) 60 | for j=1:dimX; 61 | subplot(ceil(dimX^0.5),ceil(dimX^0.5),j); 62 | plot(X(j,:)); 63 | end 64 | 65 | figure(2) 66 | for j=1:dimZ; 67 | subplot(ceil(dimZ^0.5),ceil(dimZ^0.5),j); 68 | plot(Z(j,:)); 69 | end 70 | 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 72 | %Compute Kalman filter equations 73 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 74 | P=zeros(dimX,dimX,periods+1); 75 | K=zeros(dimX,dimZ,periods); 76 | Xtt=X*0; 77 | P(:,:,1)=C*C'; 78 | P(:,:,1)=C*C'/(1-A^2); 79 | for t=1:periods; 80 | P(:,:,t+1)=A*(P(:,:,t)-(P(:,:,t)*D'/(D*P(:,:,t)*D'+R*R'))*D*P(:,:,t))*A'+C*C'; 81 | K(:,:,t)= P(:,:,t+1)*D'/(D*P(:,:,t+1)*D'+R*R'); 82 | Xtt(:,t+1)=A*Xtt(:,t)+K(:,:,t)*(Z(:,t)-D*A*Xtt(:,t)); 83 | end 84 | figure(3) 85 | subplot(2,1,1); 86 | hold on 87 | plot(reshape(P(1,1,:),1,periods+1),'linewidth',2) 88 | legend('P_{t|t}') 89 | subplot(2,1,2); 90 | plot(reshape(K(1,1,:),1,periods),'linewidth',2) 91 | legend('K_{t}') 92 | hold on 93 | 94 | figure(4) 95 | subplot(2,1,1) 96 | plot(X','linewidth',2) 97 | hold on; 98 | plot(Z','linewidth',2,'linestyle','-','color','k') 99 | legend('X','Z') 100 | subplot(2,1,2) 101 | plot(X','linewidth',2) 102 | hold on; 103 | plot(Xtt','linewidth',2,'linestyle','--','color','r') 104 | 105 | legend('X','X_{t|t}') 106 | 107 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 108 | %Compute steady state Kalman filter 109 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 110 | P1st=C*C'; 111 | while diff>= tol && iter <= maxiter 112 | P1=A*(P1st-(P1st*D'/(D*P1st*D'+R*R'))*D*P1st)*A'+C*C'; 113 | diff=max(max(abs(P1-P1st))); 114 | iter=iter+1; 115 | P1st=P1; 116 | end 117 | K=P1*D'/(D*P1*D'+R*R'); 118 | P=P1; 119 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/kalman_filter.asv: -------------------------------------------------------------------------------- 1 | function [K,P]=kalman_filter(A,C,D,R) 2 | %Computes and simulates the Kalman filter for state space system 3 | % X[t] = AX[t-1] + Cu[t] 4 | % 5 | % Z[t] = DX[t] + Ru[t] 6 | % 7 | % Outputs are the Kalman gain K in 8 | % X[t|t] = X[t|t-1]+ K(Z[t] - DX[t|t-1] ) 9 | % 10 | % and P is the steady state prior error covariance matrix 11 | % 12 | % P = E(X[t]-X[t|t-1])(X[t]-X[t|t-1])' 13 | 14 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 15 | clear all; 16 | clc 17 | close all 18 | 19 | % %Scalar case 20 | A=0.95; 21 | C=1; 22 | D=1; 23 | R=.5; 24 | 25 | 26 | % %Bivariate state, scalar signal 27 | % A=[0.99, 0.1; -.3,0.5;]; 28 | % C=eye(2); 29 | % D=[1 1]; 30 | % R=0; 31 | 32 | % %Bivariate state, two dimensional signal 33 | % A=[0.99, 0.1; -.3,0.5;]; 34 | % C=eye(2); 35 | % D=eye(2); 36 | % R=eye(2); 37 | 38 | 39 | tol=1e-5;maxiter=1000; 40 | diff=1;iter=1; 41 | P0=C*C'; 42 | P1=A*P0*A'+C*C'; 43 | dimX=size(A,1); 44 | dimZ=length(D(:,1)); 45 | 46 | periods=200; 47 | % randn('seed', 12345); 48 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 49 | %Simulate State Space system 50 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 51 | U=randn(dimX,periods); 52 | V=randn(dimZ,periods); 53 | X=zeros(dimX,periods+1); 54 | Z=zeros(dimZ,periods); 55 | for t=1:periods; 56 | X(:,t+1)=A*X(:,t)+C*U(:,t); 57 | Z(:,t+1)=D*X(:,t+1)+R*V(:,t); 58 | end 59 | figure(1) 60 | for j=1:dimX; 61 | subplot(ceil(dimX^0.5),ceil(dimX^0.5),j); 62 | plot(X(j,:)); 63 | end 64 | 65 | figure(2) 66 | for j=1:dimZ; 67 | subplot(ceil(dimZ^0.5),ceil(dimZ^0.5),j); 68 | plot(Z(j,:)); 69 | end 70 | 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 72 | %Compute Kalman filter equations 73 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 74 | P=zeros(dimX,dimX,periods+1); 75 | K=zeros(dimX,dimZ,periods); 76 | Xtt=X*0; 77 | P(:,:,1)=C*C'; 78 | P(:,:,1)=C*C'/(1-A^2); 79 | for t=1:periods; 80 | P(:,:,t+1)=A*(P(:,:,t)-(P(:,:,t)*D'/(D*P(:,:,t)*D'+R*R'))*D*P(:,:,t))*A'+C*C'; 81 | K(:,:,t)= P(:,:,t+1)*D'/(D*P(:,:,t+1)*D'+R*R'); 82 | Xtt(:,t+1)=A*Xtt(:,t)+K(:,:,t)*(Z(:,t)-D*A*Xtt(:,t)); 83 | end 84 | figure(3) 85 | subplot(2,1,1); 86 | hold on 87 | plot(reshape(P(1,1,:),1,periods+1),'linewidth',2) 88 | legend('P_{t|t}') 89 | subplot(2,1,2); 90 | plot(reshape(K(1,1,:),1,periods),'linewidth',2) 91 | legend('K_{t}') 92 | hold on 93 | 94 | figure(4) 95 | subplot(2,1,1) 96 | plot(X','linewidth',2) 97 | hold on; 98 | plot(Z','linewidth',2,'linestyle','-','color','k') 99 | legend('X','Z') 100 | subplot(2,1,2) 101 | plot(X','linewidth',2) 102 | hold on; 103 | plot(Xtt','linewidth',2,'linestyle','--','color','r') 104 | 105 | legend('X','X_{t|t}') 106 | 107 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 108 | %Compute steady state Kalman filter 109 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 110 | P1st=C*C'; 111 | while diff>= tol && iter <= maxiter 112 | P1=A*(P1st-(P1st*D'/(D*P1st*D'+R*R'))*D*P1st)*A'+C*C'; 113 | diff=max(max(abs(P1-P1st))); 114 | iter=iter+1; 115 | P1st=P1; 116 | end 117 | K=P1*D'/(D*P1*D'+R*R'); 118 | P=P1; 119 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/kalman_filter_sim.m: -------------------------------------------------------------------------------- 1 | function [K,P,Kss,Pss,Z]=kalman_filter_sim(A,C,D,R,periods) 2 | %Simulates state space and computes Kalman filter for state space system 3 | % X[t] = AX[t-1] + Cu[t] 4 | % 5 | % Z[t] = DX[t] + Ru[t] 6 | % 7 | % Outputs are the Kalman gain K in 8 | % X[t|t] = X[t|t-1]+ K(Z[t] - DX[t|t-1] ) 9 | % 10 | % and P is the prior error covariance matrix 11 | % 12 | % P = E(X[t]-X[t|t-1])(X[t]-X[t|t-1])' 13 | % 14 | % Kss and Pss are the corresponding steady state values 15 | 16 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 17 | tol=1e-5;maxiter=1000; 18 | diff=1;iter=1; 19 | P0=C*C'; 20 | P1=A*P0*A'+C*C'; 21 | dimX=size(A,1); 22 | dimZ=length(D(:,1)); 23 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 24 | %Simulate State Space system 25 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 26 | U=randn(dimX,periods); 27 | V=randn(dimZ,periods); 28 | X=zeros(dimX,periods+1); 29 | Z=zeros(dimZ,periods); 30 | for t=1:periods; 31 | X(:,t+1)=A*X(:,t)+C*U(:,t); 32 | Z(:,t+1)=D*X(:,t+1)+R*V(:,t); 33 | end 34 | figure(1) 35 | for j=1:dimX; 36 | subplot(ceil((dimX+dimZ)^0.5),ceil((dimX+dimZ)^0.5),j); 37 | plot(X(j,:)); 38 | legend('X') 39 | end 40 | 41 | for j=1:dimZ; 42 | subplot(ceil((dimX+dimZ)^0.5),ceil((dimX+dimZ)^0.5),dimX+j); 43 | plot(Z(j,:)); 44 | legend('Z') 45 | end 46 | 47 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 48 | %Compute Kalman filter equations 49 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 50 | P=zeros(dimX,dimX,periods+1); 51 | K=zeros(dimX,dimZ,periods); 52 | Xtt=X*0; 53 | P(:,:,1)=C*C'; 54 | P(:,:,1)=C*C'/(1-A^2); 55 | for t=1:periods; 56 | P(:,:,t+1)=A*(P(:,:,t)-(P(:,:,t)*D'/(D*P(:,:,t)*D'+R*R'))*D*P(:,:,t))*A'+C*C'; 57 | K(:,:,t)= P(:,:,t+1)*D'/(D*P(:,:,t+1)*D'+R*R'); 58 | Xtt(:,t+1)=A*Xtt(:,t)+K(:,:,t)*(Z(:,t)-D*A*Xtt(:,t)); 59 | end 60 | 61 | figure(2) 62 | for j=1:dimX; 63 | if dimX>4 64 | subplot(ceil((dimX)^0.5),ceil((dimX)^0.5),j); 65 | plot(X(j,:)); 66 | hold on; 67 | plot(Xtt(j,:),'linestyle','--'); 68 | legend('X','X_{t|t}') 69 | else 70 | subplot(dimX,1,j); 71 | plot(X(j,:)); 72 | hold on; 73 | plot(Xtt(j,:),'linestyle','--'); 74 | legend('X','X_{t|t}') 75 | end 76 | end 77 | % 78 | % for j=1:dimZ; 79 | % subplot(ceil((dimX+dimZ)^0.5),ceil((dimX+dimZ)^0.5),dimX+j); 80 | % plot(Z(j,:)); 81 | % legend('Z') 82 | % end 83 | % figure(3) 84 | % subplot(2,1,1) 85 | % plot(X','linewidth',2) 86 | % hold on; 87 | % plot(Z','linewidth',2,'linestyle','-') 88 | % legend('X','Z') 89 | % 90 | % 91 | % subplot(2,1,2) 92 | % plot(X','linewidth',2) 93 | % hold on; 94 | % plot(Xtt','linewidth',2,'linestyle','--') 95 | % 96 | % legend('X','X_{t|t}') 97 | 98 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 99 | %Compute steady state Kalman filter 100 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 101 | P1st=C*C'; 102 | while diff>= tol && iter <= maxiter 103 | P1=A*(P1st-(P1st*D'/(D*P1st*D'+R*R'))*D*P1st)*A'+C*C'; 104 | diff=max(max(abs(P1-P1st))); 105 | iter=iter+1; 106 | P1st=P1; 107 | end 108 | Kss=P1*D'/(D*P1*D'+R*R'); 109 | Pss=P1; 110 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/lastxopt.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/lastxopt.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/sim.m: -------------------------------------------------------------------------------- 1 | function [X]=sim_smooth(A,C,D,R,Z) 2 | 3 | % Kalman simulation smoother 4 | % Adapted from Durbin and |Koopman (2002) by K Nimark 5 | 6 | % Xt = A*Xt-1 +C*ut 7 | % 8 | % Zt= D*Xt + R*ut 9 | 10 | 11 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 12 | %Define a ancilliary variables, predefine matrices etc 13 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 14 | T=length(Z); 15 | dimX=length(A); %dimension of state 16 | dimS=length(R(1,:)); %dimension of shocks; 17 | dimZ=length(D(:,1)); %dimension of observables; 18 | 19 | CC=C*C'; 20 | 21 | if abs(max(eig(A)))>=1; 22 | P0=CC*1000; 23 | else 24 | P0=reshape(inv(eye(dimX*dimX)-kron(A,A))*CC(:),dimX,dimX); 25 | end 26 | Xhat=zeros(dimX,T+1); 27 | PP0=zeros(dimX,dimX,T); 28 | PP1=zeros(dimX,dimX,T); 29 | 30 | 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 32 | %draw from (unconditional) state distibution 33 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 34 | Xplus=zeros(dimX,T+1); 35 | Zplus=zeros(dimZ,T); 36 | Xplus(:,1)=chol(P0+1e-6*eye(dimX))'*randn(dimX,1); 37 | shocks=randn(dimS,T); 38 | for tt=2:T+1; 39 | Xplus(:,tt)=A*Xplus(:,tt-1) + C*shocks(:,tt-1); 40 | Zplus(:,tt) = D*Xplus(:,tt) + R*shocks(:,tt-1); 41 | end 42 | 43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 44 | %Construct new observables 45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 46 | Zstar=Z;%-Zplus(:,2:end); 47 | 48 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 49 | % forward recursion 50 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 51 | for tt=1:T 52 | Ztilde=Zstar(:,tt)-D*A*Xhat(:,tt); 53 | Omega=(D*A)*P0*(D*A)'+(D*C+R)*(D*C+R)'; 54 | Omegainv=eye(dimZ)/Omega; 55 | K=(A*P0*(D*A)'+C*C'*D'+C*R')*Omegainv; 56 | Xhat(:,tt+1)=A*Xhat(:,tt)+K*Ztilde; 57 | P1=A*P0*A'+C*C'; 58 | P0=P1-K*Omega*K'; 59 | P1=A*P0*A'+C*C'; 60 | PP0(:,:,tt)=P0; 61 | PP1(:,:,tt)=P1; 62 | end 63 | 64 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 65 | %construct what is needed for last step 66 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 67 | 68 | Xtilde=A*Xhat; 69 | Xhat=Xhat; 70 | 71 | Xsm=Xhat*0; 72 | Xsm(:,T)=Xhat(:,end); 73 | 74 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 75 | %backward recursion 76 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 77 | 78 | for tt=T:-1:1 79 | J=PP0(:,:,tt)*A'*inv(PP1(:,:,tt)+eye(dimX)*1e-10); 80 | Xsm(:,tt)=Xhat(:,tt)+J*(Xsm(:,tt+1)-Xtilde(:,tt+1)); 81 | end 82 | 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 84 | %add up and spit out 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 86 | X=Xsm+Xplus; 87 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/sim_smooth.m: -------------------------------------------------------------------------------- 1 | function [X]=sim_smooth(A,C,D,R,Z) 2 | 3 | % Kalman simulation smoother 4 | % Adapted from Durbin and |Koopman (2002) by K Nimark 5 | 6 | % Xt = A*Xt-1 +C*ut 7 | % 8 | % Zt= D*Xt + R*ut 9 | 10 | 11 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 12 | %Define a ancilliary variables, predefine matrices etc 13 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 14 | T=length(Z); 15 | dimX=length(A); %dimension of state 16 | dimS=length(R(1,:)); %dimension of shocks; 17 | dimZ=length(D(:,1)); %dimension of observables; 18 | 19 | CC=C*C'; 20 | 21 | if abs(max(eig(A)))>=1; 22 | P0=CC*1000; 23 | else 24 | P0=reshape(inv(eye(dimX*dimX)-kron(A,A))*CC(:),dimX,dimX); 25 | end 26 | Xhat=zeros(dimX,T+1); 27 | PP0=zeros(dimX,dimX,T); 28 | PP1=zeros(dimX,dimX,T); 29 | 30 | 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 32 | %draw from (unconditional) state distibution 33 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 34 | Xplus=zeros(dimX,T+1); 35 | Zplus=zeros(dimZ,T); 36 | Xplus(:,1)=chol(P0+1e-6*eye(dimX))'*randn(dimX,1); 37 | shocks=randn(dimS,T); 38 | for tt=2:T+1; 39 | Xplus(:,tt)=A*Xplus(:,tt-1) + C*shocks(:,tt-1); 40 | Zplus(:,tt) = D*Xplus(:,tt) + R*shocks(:,tt-1); 41 | end 42 | 43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 44 | %Construct new observables 45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 46 | Zstar=Z;%-Zplus(:,2:end); 47 | 48 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 49 | % forward recursion 50 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 51 | for tt=1:T 52 | Ztilde=Zstar(:,tt)-D*A*Xhat(:,tt); 53 | Omega=(D*A)*P0*(D*A)'+(D*C+R)*(D*C+R)'; 54 | Omegainv=eye(dimZ)/Omega; 55 | K=(A*P0*(D*A)'+C*C'*D'+C*R')*Omegainv; 56 | Xhat(:,tt+1)=A*Xhat(:,tt)+K*Ztilde; 57 | P1=A*P0*A'+C*C'; 58 | P0=P1-K*Omega*K'; 59 | P1=A*P0*A'+C*C'; 60 | PP0(:,:,tt)=P0; 61 | PP1(:,:,tt)=P1; 62 | end 63 | 64 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 65 | %construct what is needed for last step 66 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 67 | Xtilde=A*Xhat; 68 | Xsm=Xhat*0; 69 | Xsm(:,T)=Xhat(:,end); 70 | 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 72 | %backward recursion 73 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 74 | 75 | for tt=T:-1:1 76 | J=PP0(:,:,tt)*A'*inv(PP1(:,:,tt)+eye(dimX)*1e-10); 77 | Xsm(:,tt)=Xhat(:,tt)+J*(Xsm(:,tt+1)-Xtilde(:,tt+1)); 78 | end 79 | 80 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 81 | %add up and spit out 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 83 | X=Xsm+Xplus; 84 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_2_Kalman_App/smooth.m: -------------------------------------------------------------------------------- 1 | function [X]=smooth(A,C,D,R,Z) 2 | 3 | % Kalman simulation smoother 4 | % Adapted from Durbin and |Koopman (2002) by K Nimark 5 | 6 | % Xt = A*Xt-1 +C*ut 7 | % 8 | % Zt= D*Xt + R*ut 9 | 10 | 11 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 12 | %Define a ancilliary variables, predefine matrices etc 13 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 14 | T=length(Z); 15 | dimX=length(A); %dimension of state 16 | dimS=length(R(1,:)); %dimension of shocks; 17 | dimZ=length(D(:,1)); %dimension of observables; 18 | 19 | CC=C*C'; 20 | if abs(max(eig(A)))>=1; 21 | P0=CC*1000; 22 | else 23 | P0=reshape(inv(eye(dimX*dimX)-kron(A,A))*CC(:),dimX,dimX); 24 | end 25 | Xhat=zeros(dimX,T+1); 26 | PP0=zeros(dimX,dimX,T); 27 | PP1=zeros(dimX,dimX,T); 28 | 29 | 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 31 | % forward recursion 32 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 33 | for tt=1:T 34 | Ztilde=Z(:,tt)-D*A*Xhat(:,tt); 35 | Omega=(D*A)*P0*(D*A)'+(D*C+R)*(D*C+R)'; 36 | Omegainv=eye(dimZ)/Omega; 37 | K=(A*P0*(D*A)'+C*C'*D'+C*R')*Omegainv; 38 | Xhat(:,tt+1)=A*Xhat(:,tt)+K*Ztilde; 39 | P1=A*P0*A'+C*C'; 40 | P0=P1-K*Omega*K'; 41 | P1=A*P0*A'+C*C'; 42 | PP0(:,:,tt)=P0; 43 | PP1(:,:,tt)=P1; 44 | end 45 | 46 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 47 | %construct what is needed for last step 48 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 49 | 50 | Xtilde=A*Xhat(:,1:end); 51 | Xhat=Xhat(:,1:end); 52 | 53 | Xsm=Xhat(:,1:end)*0; 54 | Xsm(:,T+1)=Xhat(:,end); 55 | 56 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 57 | %backward recursion 58 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 59 | 60 | for tt=T:-1:1 61 | % J=PP0(:,:,tt)*A'*inv(PP1(:,:,tt)+eye(dimX)*1e-8); 62 | J=PP0(:,:,tt)*A'*inv(PP1(:,:,tt)); 63 | Xsm(:,tt)=Xhat(:,tt)+J*(Xsm(:,tt+1)-Xtilde(:,tt+1)); 64 | end 65 | 66 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 67 | %spit out 68 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 69 | X=Xsm; 70 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/DSGE_SS.m: -------------------------------------------------------------------------------- 1 | function [A,C,D,R]=DSGE_SS(theta) 2 | 3 | 4 | 5 | r=theta(1); %productivity persistence 6 | g=theta(2); %relative risk aversion 7 | d=theta(3); %Calvo parameter 8 | b=theta(4); %discount factor 9 | k=((1-d)*(1-d*b))/d; %slope of Phillips curve 10 | f=theta(5);% coefficient on inflation in Taylor rule 11 | sigx=theta(6);% s.d. prod shock 12 | sigy=theta(7);% s.d. demand shock 13 | sigp=theta(8);% s.d. cost push shock 14 | sigr=theta(9);% s.d. cost push shock 15 | 16 | c=g-k*r-2*g*r+k*f+g*(r^2); 17 | 18 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 19 | % Put model in state space form 20 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 21 | A=r; 22 | C=[sigx,0,0,0;]; 23 | D=[f*((k*(1-r))/-c);(k*(1-r))/-c;(-k*g*(f-r))/-c;]; 24 | A1=[1,-f,0; 25 | 0,1,-k; 26 | (1/g),0,1;]; 27 | R1=[sigr,0,0;0,sigp,0;0,0,sigy;]; 28 | R=A1\R1; 29 | 30 | R=[zeros(3,1),R;]; 31 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/LLDSGE.m: -------------------------------------------------------------------------------- 1 | function [LL]=LLDSGE(theta) 2 | global Z 3 | 4 | [A,C,D,R]=DSGE_SS(theta); 5 | LL=LL_state_space(A,C,D,R,Z); 6 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/LL_state_space.m: -------------------------------------------------------------------------------- 1 | function [LL]=LL_state_space(A,C,D,R,Z) 2 | 3 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 4 | % Computes log likelihood for state space system 5 | % X[t] = AX[t-1] + Cu[t] 6 | % 7 | % Z[t] = DX[t] + Ru[t] 8 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 9 | 10 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 11 | % Set initial values for Kalman filter etc 12 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 13 | T=length(Z); 14 | LL=0; 15 | if max(abs(eig(A)))<1 16 | P0=dlyap(A,C*C'); 17 | else 18 | P0=0; 19 | for j=1:100; 20 | P0=A*P0*A'+C*C'; 21 | end 22 | end 23 | dimZ=length(D(:,1)); 24 | Xfilt=A(:,1)*0; %initial value for the filtered state. 25 | %Compute recursive likleihood using the Kalman filter 26 | for tt=1:T 27 | Ztilde=Z(:,tt)-D*A*Xfilt; 28 | Omega=(D*A)*P0*(D*A)'+(D*C+R)*(D*C+R)'; 29 | Omegainv=eye(dimZ)/Omega; 30 | K=(A*P0*(D*A)'+C*C'*D'+C*R')*Omegainv; 31 | Xfilt=A*Xfilt+K*Ztilde; 32 | P1=A*P0*A'+C*C'; 33 | P0=P1-K*Omega*K'; 34 | 35 | LL = LL - 0.5*dimZ*log(2*pi) - 0.5*(log(det(Omega)) + Ztilde'*Omegainv*Ztilde); 36 | end 37 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/LOGPRIOR.m: -------------------------------------------------------------------------------- 1 | function LP=LOGPRIOR(theta) 2 | 3 | 4 | LP=0; 5 | LP=LP+lpdfNormal(theta(2),2,0.1); 6 | 7 | LP=LP+lpdfBeta(theta(3),0.75,0.05); 8 | 9 | LP=LP+lpdfBeta(theta(4),0.99,0.1); -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/Plot_prob_intervals.m: -------------------------------------------------------------------------------- 1 | %Program to plot probability intervals for IRFs etc using MCMC from M-H 2 | %chain. 3 | clc 4 | clear all 5 | close all 6 | global Z 7 | load('Z'); 8 | load('bb_'); 9 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 10 | %set control parameters 11 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 12 | S=300;% NUmber of independent draws from the markov chain 13 | periods=25; %Number of periods in the IRFs 14 | 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | %Find and plot probability intervals for IRFS, Variance decompisition and 17 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 18 | 19 | ra = max(size(bb_)); 20 | ff = ceil(ra.*rand(S,1)); 21 | 22 | IMPR=zeros(periods,S);%This is where we store the IRFs for the different draws 23 | VARDECOMP=zeros(2,S);%This is where we store the variance decompositions for the different draws 24 | for s=1:S; 25 | theta=bb_(:,ff(s,1));%Take a random draw from the MCMC 26 | [A,C,D,R]=DSGE_SS(theta);% Compute the state space matrices for theta 27 | %%%%%%%%%%%%%%%%%%%%%%%%%%% 28 | %Impulse response functiom 29 | %%%%%%%%%%%%%%%%%%%%%%%%%%% 30 | IR=zeros(periods,1); 31 | for ss=0:periods-1 32 | IR(ss+1)= D(1,1)*(A^ss)*C(1,1); 33 | end 34 | IMPR(:,s)=IR; 35 | %%%%%%%%%%%%%%%%%%%%%%%% 36 | %Variance decomp 37 | %%%%%%%%%%%%%%%%%%%%%%%% 38 | % pivar=D(2)^2*inv(1-A^2)*sigx^2 + sigp^2; 39 | % VARDECOMP(:,s)=[(sigp^2)/pivar (D(2)^2)*(inv(1-r^2)*sigx^2)/pivar ]'; 40 | % 41 | 42 | end; 43 | 44 | VARDECOMPsort=sort(VARDECOMP,2); 45 | ImpSort=sort(IMPR,2); 46 | figure 47 | hold on; 48 | plot(ImpSort(:,S*.95),':','color','black','LineWidth',3); 49 | hold on; 50 | plot(ImpSort(:,S*.5),'color','black','LineWidth',3); 51 | hold on; 52 | plot(ImpSort(:,S*.05),':','color','black','LineWidth',3); 53 | 54 | figure 55 | plot(IMPR) 56 | 57 | figure 58 | subplot(1,2,1); 59 | hist(VARDECOMP(1,:),50); 60 | subplot(1,2,2); 61 | hist(VARDECOMP(2,:),50); 62 | % close all 63 | plotpost(VARDECOMP,0) -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/Z.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/Z.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/convcheck.m: -------------------------------------------------------------------------------- 1 | function x=convcheck(MCMC) 2 | n=size(MCMC,1); 3 | m=size(MCMC,2); 4 | x=[]; 5 | for j=1:10000:m 6 | X=diag(cov(MCMC(:,100:j)')); 7 | 8 | x=[x X]; 9 | end 10 | % 11 | sqrn=n^.5; 12 | figure 13 | for j=1:n; 14 | subplot(ceil(sqrn),ceil(sqrn),j); 15 | plot(x(j,:)); 16 | end 17 | 18 | figure 19 | for j=1:n; 20 | subplot(ceil(sqrn),ceil(sqrn),j); 21 | plot(MCMC(j,10:end)); 22 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/hpfilter.m: -------------------------------------------------------------------------------- 1 | function [s]=hpfilter(y,w) 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | % Author: Ivailo Izvorski, 4 | % Department of Economics 5 | % Yale University. 6 | % izvorski@econ.yale.edu 7 | % This code has been used and seems to be free of error. 8 | % However, it carries no explicit or implicit guarantee. 9 | % 10 | % function [s]=hpfilter(y,w) 11 | % Hondrick Prescott filter where: 12 | % w - smoothing parameter; w=1600 for quarterly data 13 | % y - the original series that has to be smoothed 14 | % s - the filtered series 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | if size(y,1)=0 & x<=1 14 | LogDens = -betaln(a,b) + (a-1).*log(x) + (b-1).*log(1-x); 15 | else 16 | LogDens=-inf; 17 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/lpdfGamma.asv: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfGamma(x,a,b); 2 | 3 | %-------------------------------------------------------------------------- 4 | % The log height of the Gamma(a,b) density 5 | % 6 | % x: The density is evaluated at 7 | % a: First (converted) parameter 8 | % b: Second (converted) parameter 9 | % a and b specified by mean and degrees of freedom 10 | %-------------------------------------------------------------------------- 11 | 12 | 13 | LogDens = -gammaln(a) -a.*log(b)+ (a-1).*log(x) -x./b ; 14 | 15 | 16 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/lpdfGamma.m: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfGamma(x,a,b); 2 | 3 | %-------------------------------------------------------------------------- 4 | % The log height of the Gamma(a,b) density 5 | % 6 | % x: The density is evaluated at 7 | % a: First (converted) parameter 8 | % b: Second (converted) parameter 9 | % a and b specified by mean and std 10 | %-------------------------------------------------------------------------- 11 | 12 | 13 | LogDens = -gammaln(a) -a.*log(b)+ (a-1).*log(x) -x./b ; 14 | 15 | 16 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/lpdfInvGamma.m: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfInvGamma(x,a,b) 2 | 3 | %-------------------------------------------------------------------------- 4 | % The log height of the InvertedGamma(a,b) density 5 | % 6 | % x: The density is evaluated at 7 | % a: First parameter 8 | % b: Second parameter 9 | % 10 | %-------------------------------------------------------------------------- 11 | 12 | LogDens = log(2)-gammaln(b/2)+(b/2).*log(b*a^2/2) -((b+1)/2).*log(x.^2)-b*a^2./(2*x.^2); 13 | 14 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/lpdfNormal.m: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfNormal(x,a,b) 2 | 3 | %-------------------------------------------------------------------------- 4 | % Compute the log of the N(mu,sigma^2) density 5 | % 6 | % x: Evaluated at 7 | % a: Mean 8 | % b: Standard deviation 9 | % 10 | %-------------------------------------------------------------------------- 11 | 12 | LogDens = -log(b) -0.5*log(2*pi) -0.5*((x-a)./b).^2; 13 | 14 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_3_ DSGE_M-H/lpdfTruncNormal.asv: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfTruncNormal(x,a,b,bound) 2 | 3 | %-------------------------------------------------------------------------- 4 | % Compute the log height of the left Truncated N(mu,sigma^2) density 5 | % 6 | % x Point where the density is evaluated 7 | % a Mean 8 | % b Standard deviation 9 | % bound Left truncation point 10 | %-------------------------------------------------------------------------- 11 | 12 | if x LB)==1 70 | if min(bcan < UB)==1 71 | % lpostcan = LLDSGE(bcan);%Uncomment for improper uniform priors 72 | % lpostcan = log_prior_DSGE(bcan)+LLDSGE(bcan);%switch on for use of priors 73 | lpostcan = log_prior_DSGE(bcan);%Uncomment for prior predictive analysis 74 | laccprob = lpostcan-lpostdraw; 75 | else 76 | laccprob=-9e+200; 77 | q=q+1; 78 | end 79 | else 80 | laccprob=-9e+200; 81 | q=q+1; 82 | end 83 | 84 | %Accept candidate draw with log prob = laccprob, else keep old draw 85 | if log(rand)= 50 && mod(iter,1000)==0 97 | vscale=5e-1*cov(bb_(:,1:iter)'); 98 | iter 99 | SwitchesProp(iter) 100 | end 101 | 102 | end 103 | toc 104 | 105 | disp(['iter: ',num2str(iter)]); 106 | disp(['acceptance rate: ',num2str(SwitchesProp(iter))]); 107 | 108 | figure 109 | bb_=bb_(:,50:end); 110 | for j=1:8; 111 | subplot(3,3,j); 112 | hist(bb_(j,:),50); 113 | end 114 | 115 | convcheck(bb_(:,10:end)); 116 | 117 | % figure 118 | plotpost(bb_(:,burnin:end),0) 119 | load bb_mle 120 | plotpost(bb_mle(:,burnin:end),1) 121 | load bb_prior 122 | plotpost(bb_prior(:,burnin:end),0) 123 | %% 124 | 125 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/DSGE_SS.m: -------------------------------------------------------------------------------- 1 | function [A,C,D,R]=DSGE_SS(theta) 2 | 3 | 4 | 5 | r=theta(1); %productivity persistence 6 | g=theta(2); %relative risk aversion 7 | d=theta(3); %Calvo parameter 8 | b=theta(4); %discount factor 9 | k=((1-d)*(1-d*b))/d; %slope of Phillips curve 10 | f=theta(5);% coefficient on inflation in Taylor rule 11 | sigx=theta(6);% s.d. prod shock 12 | sigy=theta(7);% s.d. demand shock 13 | sigp=theta(8);% s.d. cost push shock 14 | sigr=theta(9);% s.d. cost push shock 15 | 16 | c=g-k*r-2*g*r+k*f+g*(r^2); 17 | 18 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 19 | % Put model in state space form 20 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 21 | A=r; 22 | C=[sigx,0,0,0;]; 23 | D=[f*((k*(1-r))/-c);(k*(1-r))/-c;(-k*g*(f-r))/-c;]; 24 | A1=[1,-f,0; 25 | 0,1,-k; 26 | (1/g),0,1;]; 27 | R1=[sigr,0,0;0,sigp,0;0,0,sigy;]; 28 | R=A1\R1; 29 | 30 | R=[zeros(3,1),R;]; 31 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/LLDSGE.m: -------------------------------------------------------------------------------- 1 | function [LL]=LLDSGE(theta) 2 | global Z 3 | 4 | [A,C,D,R]=DSGE_SS(theta); 5 | LL=LL_state_space(A,C,D,R,Z); 6 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/LL_state_space.m: -------------------------------------------------------------------------------- 1 | function [LL]=LL_state_space(A,C,D,R,Z) 2 | 3 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 4 | % Computes log likelihood for state space system 5 | % X[t] = AX[t-1] + Cu[t] 6 | % 7 | % Z[t] = DX[t] + Ru[t] 8 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 9 | 10 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 11 | % Set initial values for Kalman filter etc 12 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 13 | T=length(Z); 14 | LL=0; 15 | if max(abs(eig(A)))<1 16 | P0=dlyap(A,C*C'); 17 | else 18 | P0=0; 19 | for j=1:100; 20 | P0=A*P0*A'+C*C'; 21 | end 22 | end 23 | dimZ=length(D(:,1)); 24 | Xfilt=A(:,1)*0; %initial value for the filtered state. 25 | %Compute recursive likleihood using the Kalman filter 26 | for tt=1:T 27 | Ztilde=Z(:,tt)-D*A*Xfilt; 28 | Omega=(D*A)*P0*(D*A)'+(D*C+R)*(D*C+R)'; 29 | Omegainv=eye(dimZ)/Omega; 30 | K=(A*P0*(D*A)'+C*C'*D'+C*R')*Omegainv; 31 | Xfilt=A*Xfilt+K*Ztilde; 32 | P1=A*P0*A'+C*C'; 33 | P0=P1-K*Omega*K'; 34 | 35 | LL = LL - 0.5*dimZ*log(2*pi) - 0.5*(log(det(Omega)) + Ztilde'*Omegainv*Ztilde); 36 | end 37 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/LOGPRIOR.m: -------------------------------------------------------------------------------- 1 | function LP=LOGPRIOR(theta) 2 | 3 | 4 | LP=0; 5 | LP=LP+lpdfNormal(theta(2),2,0.1); 6 | 7 | LP=LP+lpdfBeta(theta(3),0.75,0.05); 8 | 9 | LP=LP+lpdfBeta(theta(4),0.99,0.1); -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/Z.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/Z.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/bb_.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/bb_.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/bb_ML.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/bb_ML.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/bb_mle.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/bb_mle.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/bb_prior.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/bb_prior.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/convcheck.m: -------------------------------------------------------------------------------- 1 | function x=convcheck(MCMC) 2 | n=size(MCMC,1); 3 | m=size(MCMC,2); 4 | x=[]; 5 | mu=[]; 6 | for j=1:10000:m 7 | X=diag(cov(MCMC(:,10:j)')); 8 | x=[x X]; 9 | MU=mean(MCMC(:,1:j),2); 10 | mu=[mu MU]; 11 | end 12 | % 13 | sqrn=n^.5; 14 | figure(1) 15 | for j=1:n; 16 | subplot(ceil(sqrn),ceil(sqrn),j); 17 | plot(x(j,:)); 18 | end 19 | 20 | figure(2) 21 | for j=1:n; 22 | subplot(ceil(sqrn),ceil(sqrn),j); 23 | plot(MCMC(j,10:end)); 24 | end 25 | 26 | figure(3) 27 | for j=1:n; 28 | subplot(ceil(sqrn),ceil(sqrn),j); 29 | plot(mu(j,:)); 30 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/hpfilter.m: -------------------------------------------------------------------------------- 1 | function [s]=hpfilter(y,w) 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | % Author: Ivailo Izvorski, 4 | % Department of Economics 5 | % Yale University. 6 | % izvorski@econ.yale.edu 7 | % This code has been used and seems to be free of error. 8 | % However, it carries no explicit or implicit guarantee. 9 | % 10 | % function [s]=hpfilter(y,w) 11 | % Hondrick Prescott filter where: 12 | % w - smoothing parameter; w=1600 for quarterly data 13 | % y - the original series that has to be smoothed 14 | % s - the filtered series 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | if size(y,1)= tol && iter <= maxiter 48 | P1=A*(P1st-(P1st*D'/(D*P1st*D'+R*R'))*D*P1st)*A'+C*C'; 49 | diff=max(max(abs(P1-P1st))); 50 | iter=iter+1; 51 | P1st=P1; 52 | end 53 | K=P1*D'/(D*P1*D'+R*R'); 54 | P=P1; 55 | p=(P1st-(P1st*D'/(D*P1st*D'+R*R'))*D*P1st); 56 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/lastxopt.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/lastxopt.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/log_prior_DSGE.asv: -------------------------------------------------------------------------------- 1 | function LP=log_prior_DSGE(theta) 2 | 3 | 4 | LP=0; 5 | LP=LP+lpdfNormal(theta(2),2,0.1); 6 | 7 | LP=LP+lpdfBeta(theta(3),0.75,0.05); 8 | 9 | LP=LP+lpdfBeta(theta(4),0.99,0.1); 10 | 11 | %%Set priors 12 | r=0.95; %productivity persistence 13 | g=5; % Consumption utility curvature/coeff. of relative risk aversion 14 | d=0.75; %Calvo parameter 15 | b=0.99; %discount factor 16 | k=((1-d)*(1-d*b))/d; %slope of Phillips curve 17 | f=1.5;% coefficient on inflation in Taylor rule 18 | sigx=0.1;% s.d. prod shock 19 | sigy=0.1;% s.d. demand shock 20 | sigp=0.1;% s.d. cost push shock 21 | sigr=0.1;% s.d. cost push shock 22 | 23 | % %(1) r - productivity persistence 24 | % pmean=0.9; pstdd=0.05; 25 | % a = (1-pmean)*pmean^2/pstdd^2 - pmean; 26 | % b = a*(1/pmean - 1); 27 | 28 | LP = LP + lpdfbeta(theta(1),a,b); 29 | 30 | %(2) g - Consumption utility curvature/coeff. of relative risk aversion 31 | pmean=2; pstdd=0.1; 32 | LP = LP + lpdfNormal(theta(2),pmean,pstdd) ; 33 | 34 | %(3) d - Calvo price setting parameter 35 | pmean=0.75; pstdd=0.05; 36 | a = (1-pmean)*pmean^2/pstdd^2 - pmean; 37 | b = a*(1/pmean - 1); 38 | 39 | LP = LP + lpdfbeta(theta(3),a,b); 40 | 41 | %(4) b - discount factor 42 | pmean=0.99; pstdd=0.05; 43 | a = (1-pmean)*pmean^2/pstdd^2 - pmean; 44 | b = a*(1/pmean - 1); 45 | 46 | LP = LP + lpdfbeta(theta(4),a,b); 47 | 48 | 49 | %(5) f - coefficient on inflation in Taylor rule 50 | pmean=2; pstdd=0.1; 51 | LP = LP + lpdfNormal(theta(2),pmean,pstdd) ; 52 | 53 | %(6) xi_e - Calvo employment 54 | pmean=0.675; pstdd=0.05; 55 | a = (1-pmean)*pmean^2/pstdd^2 - pmean; 56 | b = a*(1/pmean - 1); 57 | 58 | LogPriorDens = LogPriorDens + lpdfbeta(PARAVEC(6),a,b); 59 | 60 | % %(7) kappa_w - Wage indexation 61 | % pmean=0.500; pstdd=0.150; 62 | % a = (1-pmean)*pmean^2/pstdd^2 - pmean; 63 | % b = a*(1/pmean - 1); 64 | % 65 | % LogPriorDens = LogPriorDens + lpdfbeta(PARAVEC(7),a,b); 66 | % 67 | % %(8) kappa_d - Indexation domestic prices 68 | % pmean=0.500; pstdd=0.150; 69 | % a = (1-pmean)*pmean^2/pstdd^2 - pmean; 70 | % b = a*(1/pmean - 1); 71 | % 72 | % LogPriorDens = LogPriorDens + lpdfbeta(PARAVEC(8),a,b); 73 | % 74 | % %(9) kappa_mc - Indexation imported cons prices 75 | % pmean=0.500; pstdd=0.150; 76 | % a = (1-pmean)*pmean^2/pstdd^2 - pmean; 77 | % b = a*(1/pmean - 1); 78 | % 79 | % LogPriorDens = LogPriorDens + lpdfbeta(PARAVEC(9),a,b); 80 | % 81 | % %(10) kappa_mi - Indexation imported inv prices 82 | % pmean=0.500; pstdd=0.150; 83 | % a = (1-pmean)*pmean^2/pstdd^2 - pmean; 84 | % b = a*(1/pmean - 1); 85 | % 86 | % LogPriorDens = LogPriorDens + lpdfbeta(PARAVEC(10),a,b); 87 | % 88 | % %(11) kappa_x - Indexation export prices 89 | % pmean=0.500; pstdd=0.150; 90 | % a = (1-pmean)*pmean^2/pstdd^2 - pmean; 91 | % b = a*(1/pmean - 1); 92 | % 93 | % LogPriorDens = LogPriorDens + lpdfbeta(PARAVEC(11),a,b); 94 | % % 95 | %(12) lambda_d - Mark-up domestic (estimated) 96 | pmean=1.200; pstdd=2; 97 | a = pmean; 98 | b = pstdd; 99 | 100 | L -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/log_prior_DSGE.m: -------------------------------------------------------------------------------- 1 | function LP=log_prior_DSGE(theta) 2 | 3 | 4 | LP=0; 5 | 6 | % %(1) r - productivity persistence 7 | % pmean=0.9; pstdd=0.05; 8 | % a = (1-pmean)*pmean^2/pstdd^2 - pmean; 9 | % b = a*(1/pmean - 1); 10 | % LP = LP + lpdfBeta(theta(1),a,b); 11 | 12 | %(2) g - Consumption utility curvature/coeff. of relative risk aversion 13 | pmean=3; pstdd=0.05; 14 | LP = LP + lpdfNormal(theta(2),pmean,pstdd) ; 15 | 16 | %(3) d - Calvo price setting parameter 17 | pmean=0.75; pstdd=0.05; 18 | a = (1-pmean)*pmean^2/pstdd^2 - pmean; 19 | b = a*(1/pmean - 1); 20 | 21 | LP = LP + lpdfBeta(theta(3),a,b); 22 | 23 | %(4) b - discount factor 24 | pmean=0.99; pstdd=0.01; 25 | a = (1-pmean)*pmean^2/pstdd^2 - pmean; 26 | b = a*(1/pmean - 1); 27 | 28 | LP = LP + lpdfBeta(theta(4),a,b); 29 | 30 | % 31 | %(5) f - coefficient on inflation in Taylor rule 32 | pmean=1.5; pstdd=0.1; 33 | LP = LP + lpdfNormal(theta(5),pmean,pstdd) ; -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/lpdfBeta.m: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfBeta(x,a,b); 2 | 3 | %-------------------------------------------------------------------------- 4 | % Compute the logBeta(a,b) density fcn 5 | % 6 | % x: The density is evaluated at x 7 | % a: First (converted) parameter 8 | % b: Second (converted) parameter 9 | % 10 | %-------------------------------------------------------------------------- 11 | 12 | 13 | if x>=0 & x<=1 14 | LogDens = -betaln(a,b) + (a-1).*log(x) + (b-1).*log(1-x); 15 | else 16 | LogDens=-inf; 17 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/lpdfGamma.asv: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfGamma(x,a,b); 2 | 3 | %-------------------------------------------------------------------------- 4 | % The log height of the Gamma(a,b) density 5 | % 6 | % x: The density is evaluated at 7 | % a: First (converted) parameter 8 | % b: Second (converted) parameter 9 | % a and b specified by mean and degrees of freedom 10 | %-------------------------------------------------------------------------- 11 | 12 | 13 | LogDens = -gammaln(a) -a.*log(b)+ (a-1).*log(x) -x./b ; 14 | 15 | 16 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/lpdfGamma.m: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfGamma(x,a,b); 2 | 3 | %-------------------------------------------------------------------------- 4 | % The log height of the Gamma(a,b) density 5 | % 6 | % x: The density is evaluated at 7 | % a: First (converted) parameter 8 | % b: Second (converted) parameter 9 | % a and b specified by mean and std 10 | %-------------------------------------------------------------------------- 11 | 12 | 13 | LogDens = -gammaln(a) -a.*log(b)+ (a-1).*log(x) -x./b ; 14 | 15 | 16 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/lpdfInvGamma.m: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfInvGamma(x,a,b) 2 | 3 | %-------------------------------------------------------------------------- 4 | % The log height of the InvertedGamma(a,b) density 5 | % 6 | % x: The density is evaluated at 7 | % a: First parameter 8 | % b: Second parameter 9 | % 10 | %-------------------------------------------------------------------------- 11 | 12 | LogDens = log(2)-gammaln(b/2)+(b/2).*log(b*a^2/2) -((b+1)/2).*log(x.^2)-b*a^2./(2*x.^2); 13 | 14 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/lpdfNormal.m: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfNormal(x,a,b) 2 | 3 | %-------------------------------------------------------------------------- 4 | % Compute the log of the N(mu,sigma^2) density 5 | % 6 | % x: Evaluated at 7 | % a: Mean 8 | % b: Standard deviation 9 | % 10 | %-------------------------------------------------------------------------- 11 | 12 | LogDens = -log(b) -0.5*log(2*pi) -0.5*((x-a)./b).^2; 13 | 14 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/DSGE M-H/lpdfTruncNormal.asv: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfTruncNormal(x,a,b,bound) 2 | 3 | %-------------------------------------------------------------------------- 4 | % Compute the log height of the left Truncated N(mu,sigma^2) density 5 | % 6 | % x Point where the density is evaluated 7 | % a Mean 8 | % b Standard deviation 9 | % bound Left truncation point 10 | %-------------------------------------------------------------------------- 11 | 12 | if x= 1; 17 | ll=-1d20; 18 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/M-H_SVAR/convcheck.m: -------------------------------------------------------------------------------- 1 | function x=convcheck(MCMC) 2 | n=size(MCMC,1); 3 | m=size(MCMC,2); 4 | x=[]; 5 | mu=[]; 6 | for j=10:10:m 7 | X=diag(cov(MCMC(:,1:j)')); 8 | x=[x X]; 9 | MU=mean(MCMC(:,1:j),2); 10 | mu=[mu MU;]; 11 | end 12 | 13 | 14 | sqrn=n^.5; 15 | 16 | 17 | figure 18 | for j=1:n; 19 | subplot(ceil(sqrn),ceil(sqrn),j); 20 | plot(MCMC(j,:),'linewidth',2); 21 | if j==1; 22 | xlabel({'c_{11} '}); 23 | end; 24 | if j==2; 25 | xlabel('c_{21} '); 26 | end; 27 | if j==3; 28 | xlabel('c_{22} '); 29 | end; 30 | if j==4; 31 | xlabel('a_{11} '); 32 | end; 33 | if j==5; 34 | xlabel('a_{12} '); 35 | end; 36 | if j==6; 37 | xlabel('a_{21} '); 38 | end; 39 | if j==7; 40 | xlabel('a_{22}'); 41 | end; 42 | 43 | 44 | end 45 | figure 46 | for j=1:n; 47 | subplot(ceil(sqrn),ceil(sqrn),j); 48 | plot(mu(j,:),'linewidth',2); 49 | if j==1; 50 | xlabel({'c_{11} '}); 51 | end; 52 | if j==2; 53 | xlabel('c_{21} '); 54 | end; 55 | if j==3; 56 | xlabel('c_{22} '); 57 | end; 58 | if j==4; 59 | xlabel('a_{11} '); 60 | end; 61 | if j==5; 62 | xlabel('a_{12} '); 63 | end; 64 | if j==6; 65 | xlabel('a_{21} '); 66 | end; 67 | if j==7; 68 | xlabel('a_{22}'); 69 | end; 70 | title('Recursive mean of MCMC') 71 | end 72 | figure 73 | for j=1:n; 74 | subplot(ceil(sqrn),ceil(sqrn),j); 75 | plot(x(j,:),'linewidth',2); 76 | if j==1; 77 | xlabel({'c_{11} '}); 78 | end; 79 | if j==2; 80 | xlabel('c_{21} '); 81 | end; 82 | if j==3; 83 | xlabel('c_{22} '); 84 | end; 85 | if j==4; 86 | xlabel('a_{11} '); 87 | end; 88 | if j==5; 89 | xlabel('a_{12} '); 90 | end; 91 | if j==6; 92 | xlabel('a_{21} '); 93 | end; 94 | if j==7; 95 | xlabel('a_{22}'); 96 | end; 97 | title('Recursive variance of MCMC') 98 | 99 | end 100 | 101 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_4_Bayesian_Analysis/M-H_SVAR/hpfilter.m: -------------------------------------------------------------------------------- 1 | function [s]=hpfilter(y,w) 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | % Author: Ivailo Izvorski, 4 | % Department of Economics 5 | % Yale University. 6 | % izvorski@econ.yale.edu 7 | % This code has been used and seems to be free of error. 8 | % However, it carries no explicit or implicit guarantee. 9 | % 10 | % function [s]=hpfilter(y,w) 11 | % Hondrick Prescott filter where: 12 | % w - smoothing parameter; w=1600 for quarterly data 13 | % y - the original series that has to be smoothed 14 | % s - the filtered series 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | if size(y,1)=1 55 | Err=1; 56 | end 57 | catch 58 | Err=1; 59 | end 60 | 61 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 62 | 63 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_5_Heterogeneous_info/Kalman.m: -------------------------------------------------------------------------------- 1 | %Computes the Kalman filter 2 | function [K,P1]=Kalman(A,C,D1,R1,R2) 3 | R=[R1 R2]; 4 | tol=1e-6; 5 | maxiter=1000; 6 | diff=1;iter=1; 7 | P0=C*C'; 8 | P1=A*P0*A'+C*C'; 9 | while diff>= tol && iter <= maxiter 10 | L=(D1*A)*P0*(D1*A)'+(D1*C+R)*(D1*C+R)'; 11 | K=(A*P0*(D1*A)'+C*C'*D1'+C*R')/(L); 12 | P0=P1-K*L*K'; 13 | P1st=A*P0*A'+C*C'; 14 | diff=max(max(abs(P1-P1st))); 15 | iter=iter+1; 16 | P1=P1st; 17 | end 18 | 19 | P=P1; 20 | 21 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_5_Heterogeneous_info/Z_f.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_5_Heterogeneous_info/Z_f.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_5_Heterogeneous_info/Z_p.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lecture_5_Heterogeneous_info/Z_p.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_5_Heterogeneous_info/iterdlyap.m: -------------------------------------------------------------------------------- 1 | function [S]=iterdlyap(A,CC); 2 | S=zeros(size(A)); 3 | diff=1;tol=0.00000001; 4 | while diff > tol; 5 | Sst=CC+A*S*A'; 6 | diff=max(max(abs(Sst-S))); 7 | S=.5*Sst+.5*S; 8 | end; 9 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_5_Heterogeneous_info/lpdfBeta.m: -------------------------------------------------------------------------------- 1 | function LogDens = lpdfBeta(x,a,b); 2 | 3 | %-------------------------------------------------------------------------- 4 | % Compute the logBeta(a,b) density fcn 5 | % 6 | % x: The density is evaluated at x 7 | % a: First (converted) parameter 8 | % b: Second (converted) parameter 9 | % 10 | %-------------------------------------------------------------------------- 11 | 12 | 13 | if x>=0 & x<=1 14 | LogDens = -betaln(a,b) + (a-1).*log(x) + (b-1).*log(1-x); 15 | else 16 | LogDens=-inf; 17 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lecture_5_Heterogeneous_info/norm_rnd.m: -------------------------------------------------------------------------------- 1 | function y = norm_rnd(sig); 2 | % PURPOSE: random multivariate random vector based on 3 | % var-cov matrix sig 4 | %--------------------------------------------------- 5 | % USAGE: y = norm_rnd(sig) 6 | % where: sig = a square-symmetric covariance matrix 7 | % NOTE: for mean b, var-cov sig use: b + norm_rnd(sig) 8 | %--------------------------------------------------- 9 | % RETURNS: y = random vector normal draw mean 0, var-cov(sig) 10 | %--------------------------------------------------- 11 | 12 | % written by: 13 | % James P. LeSage, Dept of Economics 14 | % University of Toledo 15 | % 2801 W. Bancroft St, 16 | % Toledo, OH 43606 17 | % jlesage@spatial-econometrics.com 18 | if nargin ~= 1 19 | error('Wrong # of arguments to norm_rnd'); 20 | end; 21 | 22 | h = chol(sig); 23 | [nrow, ncol] = size(sig); 24 | x = randn(nrow,1); 25 | y = h'*x; 26 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/$matlab_temp_file$4d9a0bf9: -------------------------------------------------------------------------------- 1 | $matlab_temp_file$ -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/$matlab_temp_file$509d11e3: -------------------------------------------------------------------------------- 1 | $matlab_temp_file$ -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/$matlab_temp_file$509d7276: -------------------------------------------------------------------------------- 1 | $matlab_temp_file$ -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/$matlab_temp_file$5102f2ac: -------------------------------------------------------------------------------- 1 | $matlab_temp_file$ -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/CPId.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/CPId.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/CPIdisp.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/CPIdisp.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/CPIsurv.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/CPIsurv.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/CleanData.m: -------------------------------------------------------------------------------- 1 | function [Z,CPImean,NGDPmean,NonZeroCPI,NonZeroNGDP]=CleanData(maxsurv) 2 | 3 | %clean up the data 4 | % clear all 5 | % clc 6 | % maxsurv=25; 7 | 8 | 9 | load('NGDPsurv'); 10 | load('NGDPactual'); 11 | load('CPIsurv'); 12 | load('dCPI'); 13 | load('logGDP'); 14 | load('dTFP'); 15 | load('FFunds'); 16 | dTFP=0.0025*dTFP; 17 | 18 | % hpCPI=hpfilter(dCPI,1000); 19 | trendCPI=dCPI-detrend(dCPI); 20 | % ZFFunds=detrend(FFunds)*0.0025; 21 | ZFFunds=FFunds-trendCPI(1:117); 22 | ZFFunds=0.01*(ZFFunds-mean(ZFFunds)); 23 | hpGDP=hpfilter(logGDP,1400); 24 | hpGDP=[hpGDP;hpGDP(end)+hpGDP(end)-hpGDP(end-1);]; 25 | 26 | 27 | 28 | for t=1:length(dTFP)-1; 29 | TFP(t)=sum(dTFP(1:t+1)); 30 | end 31 | TFPdev=detrend(TFP); 32 | 33 | TT=length(NGDPsurv); 34 | SampleDates=[1981.5:.25:2010.75]; 35 | 36 | SampleNGDP=[SampleDates;zeros(50,length(SampleDates));]; 37 | 38 | 39 | 40 | SampleCPI=[SampleDates;zeros(50,length(SampleDates));]; 41 | 42 | dateold=1; 43 | for j=1:TT; 44 | 45 | date=CPIsurv(j,1)+CPIsurv(j,2)*0.25 - 0.25; 46 | t=(date-1981.5)*4+1; 47 | 48 | if date == dateold; 49 | c=c+1 ; 50 | else 51 | c=0; 52 | end 53 | 54 | 55 | if CPIsurv(j,7) ==-999; 56 | c=c-1; 57 | else 58 | SampleCPI(c+2,t)=0.01*CPIsurv(j,7)-0.01*trendCPI(t); 59 | end 60 | dateold=date; 61 | end 62 | 63 | CPImean=mean(mean(SampleCPI(2:14,:),2)); 64 | 65 | dateold=1; 66 | for j=1:TT; 67 | 68 | date=NGDPsurv(j,1)+NGDPsurv(j,2)*0.25 - 0.25; 69 | t=(date-1981.5)*4+1; 70 | 71 | if date == dateold; 72 | c=c+1 ; 73 | else 74 | c=0; 75 | end 76 | 77 | 78 | if NGDPsurv(j,7) ==-999 || NGDPsurv(j,6) ==-999 ; 79 | c=c-1; 80 | else 81 | SampleNGDP(c+2,t)=log(NGDPsurv(j,7)) - log(NGDPsurv(j,6 ))-0.0025*trendCPI(t)-0.0025*CPImean-(hpGDP(t+1)-hpGDP(t)); 82 | end 83 | dateold=date; 84 | end 85 | 86 | NGDPmean=0.0234;%mean(mean(SampleNGDP(2:7,:),2)); 87 | 88 | 89 | NonZeroNGDP=zeros(length(SampleNGDP),1); 90 | for t=1:length(SampleNGDP); 91 | NonZeroNGDP(t)=sum(SampleNGDP(2:end,t)~=0); 92 | NonZeroNGDP(t)=min(maxsurv,NonZeroNGDP(t)); 93 | end 94 | 95 | NonZeroCPI=zeros(length(SampleCPI),1); 96 | for t=1:length(SampleNGDP); 97 | NonZeroCPI(t)=sum(SampleCPI(2:end,t)~=0); 98 | NonZeroCPI(t)=min(maxsurv,NonZeroCPI(t)); 99 | end 100 | % for t=1:length(SampleNGDP); 101 | % hist(SampleNGDP(2:1+NonZeroNGDP(t),t),25); 102 | % t 103 | % pause 104 | % end 105 | % 106 | % for t=1:length(SampleCPI); 107 | % hist(SampleCPI(2:1+NonZeroCPI(t),t),25); 108 | % t 109 | % pause 110 | % end 111 | % 112 | 113 | 114 | ZTFP=TFP'-hpfilter(TFP,1400); 115 | ZCPI=detrend(0.01*dCPI(1:117)); 116 | ZGDP=logGDP(1:117)-hpGDP(1:117); 117 | ZCPIsurv=SampleCPI(2:end,1:117); 118 | ZGDPsurv=SampleNGDP(2:end,1:117); 119 | Z=[ZTFP';0.25*ZFFunds';0.25*ZCPI';ZGDP';0.25*(ZCPIsurv(1:maxsurv,:));(ZGDPsurv(1:maxsurv,:));]; 120 | % 121 | % figure(1) 122 | % plot(Z(1:4,:)') 123 | % 124 | % figure(2) 125 | % plot(Z(3,:)','linewidth',3) 126 | % hold on 127 | % plot(0.25*(ZCPIsurv(1:maxsurv,:))') 128 | % 129 | % figure(3) 130 | % plot(Z(3,:)','linewidth',3) 131 | % hold on 132 | % plot(Z(4,:)','linewidth',3,'linestyle',':') 133 | % hold on 134 | % plot((ZGDPsurv(1:maxsurv,:))') 135 | % 136 | % S=cov(Z(1:4,:)') 137 | % r=corrvc(S) 138 | 139 | 140 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DMbad.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DMbad.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DMbadraw.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DMbadraw.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DMgood.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DMgood.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DMgoodraw.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DMgoodraw.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DMtransform.asv: -------------------------------------------------------------------------------- 1 | %transform Doms and Morin data 2 | clear all 3 | 4 | load('DMgoodraw'); 5 | load('DMbadraw'); 6 | 7 | sampleDates=[1981.5:.25:2010.5]; 8 | 9 | 10 | DMgood=[]; 11 | for j=1981.5:.25:2010.5; 12 | s=[]; 13 | 14 | for t=1:length(DMgoodraw); 15 | if DMgoodraw(t,1) >= j && DMgoodraw(t,1) < j+0.25; 16 | s=[s DMgoodraw(t,2)]; 17 | 18 | end 19 | end 20 | DMgood=[DMgood mean(s)]; 21 | end 22 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DMtransform.m: -------------------------------------------------------------------------------- 1 | %transform Doms and Morin data 2 | clear all 3 | 4 | load('DMgoodraw'); 5 | load('DMbadraw'); 6 | 7 | sampleDates=[1981.5:.25:2003.6]; 8 | 9 | 10 | DMgood=[]; 11 | for j=1981.5:.25:2010.5; 12 | s=[]; 13 | 14 | for t=1:length(DMgoodraw); 15 | if DMgoodraw(t,1) >= j && DMgoodraw(t,1) < j+0.25; 16 | s=[s DMgoodraw(t,2)]; 17 | 18 | end 19 | end 20 | DMgood=[DMgood mean(s)]; 21 | end 22 | 23 | 24 | DMbad=[]; 25 | for j=1981.5:.25:2010.5; 26 | s=[]; 27 | 28 | for t=1:length(DMbadraw); 29 | if DMbadraw(t,1) >= j && DMbadraw(t,1) < j+0.25; 30 | s=[s DMbadraw(t,2)]; 31 | 32 | end 33 | end 34 | DMbad=[DMbad mean(s)]; 35 | end 36 | 37 | 38 | DMgood=DMgood-min(DMgood); 39 | DMgood=DMgood/max(DMgood); 40 | 41 | DMbad=DMbad-min(DMbad); 42 | DMbad=DMbad/max(DMbad); 43 | 44 | plot(DMgood); 45 | hold on 46 | plot(DMbad,'k'); 47 | 48 | save('DMgood','DMgood'); 49 | save('DMbad','DMbad'); 50 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DispCovPlot.asv: -------------------------------------------------------------------------------- 1 | %Program to compute correlations between changes and dispersion 2 | 3 | load CPId 4 | load CPIdisp 5 | load NGDPd 6 | load NGDPdisp 7 | 8 | samplecut=50; 9 | 10 | CPId=CPId(samplecut:end); 11 | CPIdisp=CPIdisp(samplecut:end); 12 | NGDPd=NGDPd(samplecut:end); 13 | NGDPdisp=NGDPdisp(samplecut:end); 14 | 15 | %normalize 16 | CPId=CPId-mean(CPId); 17 | CPIdisp=CPIdisp-mean(CPIdisp); 18 | NGDPd=NGDPd-mean(NGDPd); 19 | NGDPdisp=NGDPdisp-mean(NGDPdisp); 20 | 21 | CPIdabs=abs(CPId)-mean(abs(CPId)); 22 | NGDPdabs=abs(NGDPd)-mean(abs(NGDPd)); 23 | 24 | CPId=CPId./(cov(CPId)^0.5); 25 | CPIdisp=CPIdisp./(cov(CPIdisp)^0.5); 26 | 27 | 28 | NGDPd=NGDPd./(cov(NGDPd)^0.5); 29 | NGDPdisp=NGDPdisp./(cov(NGDPdisp)^0.5); 30 | 31 | CPIdabs=CPIdabs./(cov(CPIdabs)^0.5); 32 | 33 | NGDPdabs=NGDPdabs./(cov(NGDPdabs)^0.5); 34 | 35 | subplot(2,1,1) 36 | plot(CPId);hold on; plot(CPIdisp,'k') 37 | 38 | subplot(2,1,2) 39 | plot(NGDPd);hold on; plot(NGDPdisp,'k') 40 | 41 | cov(CPId,CPIdisp) 42 | cov(CPIdabs,CPIdisp) 43 | 44 | cov(NGDPd,NGDPdisp) 45 | cov(NGDPdabs,NGDPdisp) 46 | 47 | infl= 48 | 49 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/DispCovPlot.m: -------------------------------------------------------------------------------- 1 | %Program to compute correlations between changes and dispersion 2 | clear all 3 | clc 4 | 5 | load CPId 6 | load CPIdisp 7 | load NGDPd 8 | load NGDPdisp 9 | 10 | samplecut=50; 11 | 12 | CPId=CPId(samplecut:end); 13 | CPIdisp=CPIdisp(samplecut:end); 14 | NGDPd=NGDPd(samplecut:end); 15 | NGDPdisp=NGDPdisp(samplecut:end); 16 | 17 | %normalize 18 | CPId=CPId-mean(CPId); 19 | CPIdisp=CPIdisp-mean(CPIdisp); 20 | NGDPd=NGDPd-mean(NGDPd); 21 | NGDPdisp=NGDPdisp-mean(NGDPdisp); 22 | 23 | CPIdabs=abs(CPId)-mean(abs(CPId)); 24 | NGDPdabs=abs(NGDPd)-mean(abs(NGDPd)); 25 | 26 | CPId=CPId./(cov(CPId)^0.5); 27 | CPIdisp=CPIdisp./(cov(CPIdisp)^0.5); 28 | 29 | 30 | NGDPd=NGDPd./(cov(NGDPd)^0.5); 31 | NGDPdisp=NGDPdisp./(cov(NGDPdisp)^0.5); 32 | 33 | CPIdabs=CPIdabs./(cov(CPIdabs)^0.5); 34 | 35 | NGDPdabs=NGDPdabs./(cov(NGDPdabs)^0.5); 36 | 37 | infld=CPId(2:end)-CPId(1:end-1); 38 | infld=infld-mean(infld); 39 | 40 | inflabs=abs(infld)-mean(abs(infld)); 41 | inflabs=inflabs./(cov(inflabs)^0.5); 42 | infld=infld./(cov(infld)^0.5); 43 | 44 | subplot(2,1,1) 45 | plot(CPId);hold on; plot(CPIdisp,'k') 46 | 47 | subplot(2,1,2) 48 | plot(NGDPd);hold on; plot(NGDPdisp,'k') 49 | 50 | cov(CPId,CPIdisp) 51 | cov(CPIdabs,CPIdisp) 52 | 53 | cov(NGDPd,NGDPdisp) 54 | cov(NGDPdabs,NGDPdisp) 55 | 56 | cov(infld,CPIdisp(2:end)) 57 | cov(inflabs,CPIdisp(2:end)) 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/FFunds.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/FFunds.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/GDPsurv.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/GDPsurv.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/IntroFigures.m: -------------------------------------------------------------------------------- 1 | %produce graphs for distributions in introduction 2 | clear all 3 | close all 4 | 5 | a=0.5; %prob of z=1 6 | sig=.15; %s.d. innovation when z=0; 7 | gamma=2; %s.d. multiple of standard deviation when z=1 8 | 9 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 10 | % mixture distribution 11 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 12 | 13 | %normal plotter 14 | fy=[]; 15 | fx=[]; 16 | fr=[]; 17 | 18 | for j=1:200; 19 | 20 | x=-4*(gamma*sig)+j*0.04*(gamma*sig); 21 | 22 | fy(j)=a*(1/((sig*(2*pi)^.5)))*exp(-(x^2)/(2*sig^2))+... 23 | (1-a)*(1/(((gamma*sig)*(2*pi)^.5)))*exp(-(x^2)/(2*(gamma*sig)^2)); 24 | fx(j)=x; 25 | 26 | fya(j)=(1/((sig*(2*pi)^.5)))*exp(-(x^2)/(2*sig^2)); 27 | fyb(j)=(1/(((gamma*sig)*(2*pi)^.5)))*exp(-(x^2)/(2*(gamma*sig)^2)); 28 | 29 | fr(j)=((1-a)*(1/(((gamma*sig)*(2*pi)^.5)))*exp(-(x^2)/(2*(gamma*sig)^2)))/(a*(1/((sig*(2*pi)^.5)))*exp(-(x^2)/(2*sig^2))+(1-a)*(1/(((gamma*sig)*(2*pi)^.5)))*exp(-(x^2)/(2*(gamma*sig)^2))); 30 | % fr(j)=((1/(((gamma*sig)*(2*pi)^.5)))*exp(-(x^2)/(2*(gamma*sig)^2)))/((1/((sig*(2*pi)^.5)))*exp(-(x^2)/(2*sig^2))+(1/(((gamma*sig)*(2*pi)^.5)))*exp(-(x^2)/(2*(gamma*sig)^2))); 31 | end 32 | 33 | figure 34 | plot(fx,fy,'linewidth',3,'linestyle','-'); 35 | legend('p(x)'); 36 | 37 | figure 38 | plot(fx,fy,'linewidth',3,'linestyle','-'); 39 | hold on; 40 | plot(fx,fr,'linewidth',3,'linestyle','--'); 41 | hold on; 42 | plot(fx,fyb,'linewidth',3,'linestyle',':'); 43 | legend('p(x)', 'p(S=1|x)','p(x|S=1)' ); 44 | 45 | figure 46 | plot(fx,fy,'linewidth',2,'linestyle','-'); 47 | hold on; 48 | plot(fx,fya,'linewidth',2,'linestyle','--'); 49 | hold on; 50 | plot(fx,fyb,'linewidth',2,'linestyle',':'); 51 | legend('p(x)', 'p(x|S=1)', 'p(x|S=0)'); 52 | %% 53 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 54 | %Standard Bayesian updating illustrated 55 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 56 | 57 | %normal plotter 58 | fy=[]; 59 | fx=[]; 60 | fr=[]; 61 | sigeps=.1; 62 | mux=-0.2*sig/(sigeps+sig); 63 | 64 | postsig=inv(1/sig + 1/sigeps); 65 | 66 | for j=1:200; 67 | 68 | x=-4*(sig)+j*0.04*(sig); 69 | fy(j)=(1/((sig*(2*pi)^.5)))*exp(-(x^2)/(2*sig^2)); 70 | fb(j)=(1/((postsig*(2*pi)^.5)))*exp(-((x-mux)^2)/(2*postsig^2)); 71 | 72 | fx(j)=x; 73 | 74 | 75 | end 76 | 77 | figure 78 | plot(fx,fy,'linewidth',3,'linestyle','-'); 79 | hold on; 80 | plot(fx,fb,'linewidth',3,'linestyle','--'); 81 | 82 | legend('p(x)','p(x|x_{j})'); 83 | 84 | %% 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 86 | % M-B-D Bayesian updating illustrated 87 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 88 | 89 | %normal plotter 90 | fy=[]; 91 | fx=[]; 92 | fr=[]; 93 | sigeps=1.5; 94 | g=((gamma*sig)^2)/((gamma*sig)^2+sigeps^2); 95 | 96 | mux=-10*(gamma*sig)*g; 97 | 98 | postsig=inv(1/(gamma*sig)+1/(sigeps^2)); 99 | 100 | for j=1:200; 101 | 102 | x=-4*(gamma*sig)+j*0.04*(gamma*sig); 103 | fy(j)=a*(1/((sig*(2*pi)^.5)))*exp(-(x^2)/(2*sig^2))+... 104 | (1-a)*(1/(((gamma*sig)*(2*pi)^.5)))*exp(-(x^2)/(2*(gamma*sig)^2)); 105 | fx(j)=x; 106 | 107 | % fya(j)=(1/((sig*(2*pi)^.5)))*exp(-(x^2)/(2*sig^2)); 108 | fyb(j)=(1/(((gamma*sig)*(2*pi)^.5)))*exp(-(x^2)/(2*(gamma*sig)^2)); 109 | fb(j)=(1/((postsig*(2*pi)^.5)))*exp(-((x-mux)^2)/(2*postsig^2)); 110 | 111 | 112 | end 113 | 114 | figure 115 | plot(fx,fy,'linewidth',3); 116 | legend('p(x)'); 117 | % hold on; 118 | % plot(fx,fyb,'linewidth',3,'linestyle','--'); 119 | % legend('p(x)','p(x|S)'); 120 | hold on 121 | plot(fx,fb,'linewidth',3,'linestyle',':'); 122 | % legend('p(x)','p(x|S)','p(x|y,S)'); 123 | legend('p(x)','p(x|y,S)'); 124 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/Kalman.m: -------------------------------------------------------------------------------- 1 | %Computes the Kalman filter for systems with lagged observables 2 | function [K,P1,P0]=Kalman(A,C,D1,R) 3 | % R=[R1 R2]; 4 | tol=1e-8; 5 | maxiter=1000; 6 | diff=1;iter=1; 7 | P0=C*C'; 8 | P1=A*P0*A'+C*C'; 9 | while diff>= tol && iter <= maxiter 10 | L=(D1*A)*P0*(D1*A)'+(D1*C+R)*(D1*C+R)'; 11 | K=(A*P0*(D1*A)'+C*C'*D1'+C*R')/(L); 12 | P0=P1-K*L*K'; 13 | P1st=A*P0*A'+C*C'; 14 | diff=max(max(abs(P1-P1st))); 15 | iter=iter+1; 16 | P1=P1st; 17 | end 18 | 19 | P=P1; 20 | 21 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MBD1sidedFit.asv: -------------------------------------------------------------------------------- 1 | function MBD1sidedFit(M,N,a,b,e1,bindim,dimX,jlead1,jlead0,SigJ,theta,Z,ST,T,CPImean,NGDPmean,NonZeroCPI,NonZeroNGDP,maxsurv,H) 2 | 3 | % try 4 | pp=M(:,:,2)*0; 5 | for j=1:10; 6 | pp=M(:,:,2)*pp*M(:,:,2)'+N(:,:,2)*N(:,:,2)'; 7 | end 8 | X=zeros(dimX,T); 9 | stick=theta(15); %Calvo parameter 10 | beta=theta(16); %discount rate 11 | delta=theta(10); %labour supply curvature 12 | 13 | lambda=(1-stick)*(1-stick*beta)/stick; 14 | 15 | LL=0; 16 | Rlag=theta(12)*Z(2,1); 17 | Gr=[-1;-lambda+lambda*delta;]*theta(12)/(1-theta(12)); 18 | Gu=[-1;-lambda+lambda*delta;]/(1-theta(12)); 19 | ZZtilde=[]; 20 | for t=1:T-1; 21 | 22 | %% 23 | 24 | s=ST(t:t+bindim-1)'; 25 | j=binvec2dec(s)+1; 26 | 27 | Fpi=ones(NonZeroCPI(t),1)*(theta(17)*a(:,:,jlead1(j)+1)*M(:,:,jlead1(j)+1)+... 28 | (1-theta(17))*a(:,:,jlead0(j)+1)*M(:,:,jlead0(j)+1))*H; 29 | Fny=ones(NonZeroNGDP(t),1)*(theta(17)*(a(:,:,jlead1(j)+1)+b(:,:,jlead1(j)+1))*M(:,:,jlead1(j)+1)*H+... 30 | ((1-theta(17))*(a(:,:,jlead0(j)+1)+b(:,:,jlead0(j)+1))*M(:,:,jlead0(j)+1)*H)-b(:,:,j)*H); 31 | 32 | DD=[e1; 33 | (1-theta(12))*theta(13)*a(:,:,j)+(1-theta(12))*theta(14)*b(:,:,j); 34 | a(:,:,j); 35 | b(:,:,j); 36 | Fpi; 37 | Fny;]; 38 | 39 | ZZ=[Z(1:4,t); 40 | Z(4:3+NonZeroCPI(t),t)-0.25*CPImean; 41 | Z(maxsurv+4:maxsurv+3+NonZeroNGDP(t),t)-0.25*NGDPmean;]; 42 | 43 | % RRR=zeros(size(ZZ,1),size(ZZ,1)); 44 | RRR=[0.001,zeros(1,NonZeroCPI(t)+3+NonZeroNGDP(t)); 45 | 0,theta(5),zeros(1,NonZeroCPI(t)+2+NonZeroNGDP(t)); 46 | 47 | zeros(2,1),[Gr(2);Gr(1);]*theta(5), eye(2)*0.001,zeros(2,NonZeroCPI(t)+NonZeroNGDP(t)); 48 | zeros(NonZeroCPI(t),4),eye(NonZeroCPI(t))*((Fpi(1,:)*SigJ(:,:,j)*Fpi(1,:)')^0.5),zeros(NonZeroCPI(t),NonZeroNGDP(t)); 49 | zeros(NonZeroNGDP(t),NonZeroCPI(t)+4),eye(NonZeroNGDP(t))*((Fny(1,:)*SigJ(:,:,j)*Fny(1,:)')^0.5);]; 50 | 51 | 52 | Ztilde = ZZ-DD*M(:,:,j)*X(:,t); 53 | Ztilde(2)=Ztilde(2)-theta(12)*Rlag;Rlag=Z(2,t); 54 | Ztilde(3)=Ztilde(3)-Gr(2,1)*Rlag; 55 | Ztilde(4)=Ztilde(4)-Gr(1,1)*Rlag; 56 | PP=M(:,:,j)*pp*M(:,:,j)'+N(:,:,j)*N(:,:,j)'; 57 | OMEGA=DD*PP*DD'+RRR*RRR'; 58 | ZZtilde=[ZZtilde Ztilde(1:4,:)]; 59 | K=PP*DD'/OMEGA; 60 | X(:,t+1)= M(:,:,j)*X(:,t)+K*Ztilde; 61 | pp=PP-K*OMEGA*K'; 62 | 63 | 64 | 65 | end 66 | for j=1:4; 67 | subplot(4,1,j); 68 | plot(ZZ(j,:)') 69 | hold on 70 | plot(ZZZZtilde(j,:)','k') 71 | end 72 | 73 | 74 | % catch 75 | % 76 | % display('Not possible to compute Kalman innovations') 77 | % end 78 | % 79 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MBD1sidedFit.m: -------------------------------------------------------------------------------- 1 | function MBD1sidedFit(M,N,a,b,e1,bindim,dimX,jlead1,jlead0,SigJ,theta,Z,ST,T,CPImean,NGDPmean,NonZeroCPI,NonZeroNGDP,maxsurv,H) 2 | 3 | % try 4 | pp=M(:,:,2)*0; 5 | for j=1:10; 6 | pp=M(:,:,2)*pp*M(:,:,2)'+N(:,:,2)*N(:,:,2)'; 7 | end 8 | X=zeros(dimX,T); 9 | stick=theta(15); %Calvo parameter 10 | beta=theta(16); %discount rate 11 | delta=theta(10); %labour supply curvature 12 | 13 | lambda=(1-stick)*(1-stick*beta)/stick; 14 | 15 | LL=0; 16 | Rlag=theta(12)*Z(2,1); 17 | Gr=[-1;-lambda+lambda*delta;]*theta(12)/(1-theta(12)); 18 | Gu=[-1;-lambda+lambda*delta;]/(1-theta(12)); 19 | ZZtilde=[]; 20 | ZZZ=[]; 21 | for t=1:T-1; 22 | 23 | %% 24 | 25 | s=ST(t:t+bindim-1)'; 26 | j=binvec2dec(s)+1; 27 | 28 | Fpi=ones(NonZeroCPI(t),1)*(theta(17)*a(:,:,jlead1(j)+1)*M(:,:,jlead1(j)+1)+... 29 | (1-theta(17))*a(:,:,jlead0(j)+1)*M(:,:,jlead0(j)+1))*H; 30 | Fny=ones(NonZeroNGDP(t),1)*(theta(17)*(a(:,:,jlead1(j)+1)+b(:,:,jlead1(j)+1))*M(:,:,jlead1(j)+1)*H+... 31 | ((1-theta(17))*(a(:,:,jlead0(j)+1)+b(:,:,jlead0(j)+1))*M(:,:,jlead0(j)+1)*H)-b(:,:,j)*H); 32 | 33 | DD=[e1; 34 | (1-theta(12))*theta(13)*a(:,:,j)+(1-theta(12))*theta(14)*b(:,:,j); 35 | a(:,:,j); 36 | b(:,:,j); 37 | Fpi; 38 | Fny;]; 39 | 40 | ZZ=[Z(1:4,t); 41 | Z(4:3+NonZeroCPI(t),t)-0.25*CPImean; 42 | Z(maxsurv+4:maxsurv+3+NonZeroNGDP(t),t)-0.25*NGDPmean;]; 43 | 44 | % RRR=zeros(size(ZZ,1),size(ZZ,1)); 45 | RRR=[0.001,zeros(1,NonZeroCPI(t)+3+NonZeroNGDP(t)); 46 | 0,theta(5),zeros(1,NonZeroCPI(t)+2+NonZeroNGDP(t)); 47 | 48 | zeros(2,1),[Gr(2);Gr(1);]*theta(5), eye(2)*0.001,zeros(2,NonZeroCPI(t)+NonZeroNGDP(t)); 49 | zeros(NonZeroCPI(t),4),eye(NonZeroCPI(t))*((Fpi(1,:)*SigJ(:,:,j)*Fpi(1,:)')^0.5),zeros(NonZeroCPI(t),NonZeroNGDP(t)); 50 | zeros(NonZeroNGDP(t),NonZeroCPI(t)+4),eye(NonZeroNGDP(t))*((Fny(1,:)*SigJ(:,:,j)*Fny(1,:)')^0.5);]; 51 | 52 | 53 | Ztilde = ZZ-DD*M(:,:,j)*X(:,t); 54 | Ztilde(2)=Ztilde(2)-theta(12)*Rlag;Rlag=Z(2,t); 55 | Ztilde(3)=Ztilde(3)-Gr(2,1)*Rlag; 56 | Ztilde(4)=Ztilde(4)-Gr(1,1)*Rlag; 57 | PP=M(:,:,j)*pp*M(:,:,j)'+N(:,:,j)*N(:,:,j)'; 58 | OMEGA=DD*PP*DD'+RRR*RRR'; 59 | 60 | ZZZ=[ZZZ ZZ(1:4,:)]; 61 | ZZtilde=[ZZtilde Ztilde(1:4,:)]; 62 | K=PP*DD'/OMEGA; 63 | X(:,t+1)= M(:,:,j)*X(:,t)+K*Ztilde; 64 | pp=PP-K*OMEGA*K'; 65 | 66 | 67 | 68 | end 69 | 70 | 71 | for j=1:4; 72 | subplot(4,1,j); 73 | plot(ZZZ(j,:)','r') 74 | hold on 75 | plot(ZZZ(j,:)'-ZZtilde(j,:)','k') 76 | end 77 | 78 | 79 | % catch 80 | % 81 | % display('Not possible to compute Kalman innovations') 82 | % end 83 | % 84 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MBDLL.asv: -------------------------------------------------------------------------------- 1 | function [LL]=MBDLL(M,N,a,b,e1,bindim,dimX,jlead1,jlead0,SigJ,theta,Z,ST,T,CPImean,NGDPmean,NonZeroCPI,NonZeroNGDP,maxsurv,H) 2 | 3 | try 4 | pp=M(:,:,2)*0; 5 | for j=1:10; 6 | pp=M(:,:,2)*pp*M(:,:,2)'+N(:,:,2)*N(:,:,2)'; 7 | end 8 | X=zeros(dimX,T); 9 | stick=theta(15); %Calvo parameter 10 | beta=theta(16); %discount rate 11 | delta=theta(10); %labour supply curvature 12 | 13 | lambda=(1-stick)*(1-stick*beta)/stick; 14 | 15 | LL=0; 16 | Rlag=theta(12)*Z(2,1); 17 | Gr=[1;lambda+lambda*delta;]*theta(12)/(1-theta(12)); 18 | Gu=[1;lambda+lambda*delta;]/(1-theta(12)); 19 | for t=1:T-1; 20 | 21 | %% 22 | 23 | s=ST(t:t+bindim-1)'; 24 | j=binvec2dec(s)+1; 25 | 26 | Fpi=ones(NonZeroCPI(t),1)*(theta(17)*a(:,:,jlead1(j)+1)*M(:,:,jlead1(j)+1)+... 27 | (1-theta(17))*a(:,:,jlead0(j)+1)*M(:,:,jlead0(j)+1))*H; 28 | Fny=ones(NonZeroNGDP(t),1)*(theta(17)*(a(:,:,jlead1(j)+1)+b(:,:,jlead1(j)+1))*M(:,:,jlead1(j)+1)*H+... 29 | ((1-theta(17))*(a(:,:,jlead0(j)+1)+b(:,:,jlead0(j)+1))*M(:,:,jlead0(j)+1)*H)-b(:,:,j)*H); 30 | 31 | DD=[e1; 32 | (1-theta(12))*theta(13)*a(:,:,j)+(1-theta(12))*theta(14)*b(:,:,j); 33 | a(:,:,j); 34 | b(:,:,j); 35 | Fpi; 36 | Fny;]; 37 | 38 | ZZ=[Z(1:4,t); 39 | Z(4:3+NonZeroCPI(t),t)-0.25*CPImean; 40 | Z(maxsurv+4:maxsurv+3+NonZeroNGDP(t),t)-0.25*NGDPmean;]; 41 | 42 | % RRR=zeros(size(ZZ,1),size(ZZ,1)); 43 | RRR=[0.0001,zeros(1,NonZeroCPI(t)+3+NonZeroNGDP(t)); 44 | 0,1, 45 | 46 | zeros(1,NonZeroCPI(t)+4+NonZeroNGDP(t)); 47 | zeros(2,1),eye(2)*0.0001,zeros(2,NonZeroCPI(t)+NonZeroNGDP(t)); 48 | zeros(NonZeroCPI(t),3),eye(NonZeroCPI(t))*((Fpi(1,:)*SigJ(:,:,j)*Fpi(1,:)')^0.5),zeros(NonZeroCPI(t),NonZeroNGDP(t)); 49 | zeros(NonZeroNGDP(t),NonZeroCPI(t)+3),eye(NonZeroNGDP(t))*((Fny(1,:)*SigJ(:,:,j)*Fny(1,:)')^0.5);]; 50 | 51 | 52 | Ztilde = ZZ-DD*M(:,:,j)*X(:,t); 53 | Ztilde(2)=Ztilde(2)-theta(12)*Rlag;Rlag=Z(2,t); 54 | Ztilde(3)=Ztilde(3)-Gr(2,1)*Rlag; 55 | Ztilde(4)=Ztilde(4)-Gr(1,1)*Rlag; 56 | PP=M(:,:,j)*pp*M(:,:,j)'+N(:,:,j)*N(:,:,j)'; 57 | OMEGA=DD*PP*DD'+RRR*RRR'; 58 | 59 | LL=LL-0.5*(logdet(OMEGA)+Ztilde'/OMEGA*Ztilde); 60 | % if LL==Inf; 61 | % LL=LL-0.5*(log(det(OMEGA+1e-6*eye(length(OMEGA))))+Ztilde'/(OMEGA+1e-6*eye(length(OMEGA)))*Ztilde); 62 | % end 63 | 64 | K=PP*DD'/OMEGA; 65 | X(:,t+1)= M(:,:,j)*X(:,t)+K*Ztilde; 66 | pp=PP-K*OMEGA*K'; 67 | 68 | 69 | 70 | end 71 | 72 | if LL==Inf; 73 | LL=-1e200; 74 | end 75 | 76 | catch 77 | LL=-1e200; 78 | display('Not possible to evaluate LL') 79 | end 80 | 81 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MBDLL.m: -------------------------------------------------------------------------------- 1 | function [LL]=MBDLL(M,N,a,b,e1,bindim,dimX,jlead1,jlead0,SigJ,theta,Z,ST,T,CPImean,NGDPmean,NonZeroCPI,NonZeroNGDP,maxsurv,H) 2 | 3 | try 4 | pp=M(:,:,2)*0; 5 | for j=1:10; 6 | pp=M(:,:,2)*pp*M(:,:,2)'+N(:,:,2)*N(:,:,2)'; 7 | end 8 | X=zeros(dimX,T); 9 | stick=theta(16); %Calvo parameter 10 | beta=theta(17); %discount rate 11 | varphi=theta(11); %labour supply curvature 12 | 13 | lambda=(1-stick)*(1-stick*beta)/stick; 14 | 15 | LL=0; 16 | Rlag=theta(13)*Z(2,1); 17 | Gr=[-1;-lambda+lambda*varphi;]*theta(13)/(1-theta(13)); 18 | Gu=[-1;-lambda+lambda*varphi;]/(1-theta(13)); 19 | for t=1:T-1; 20 | 21 | %% 22 | 23 | s=ST(t:t+bindim-1)'; 24 | j=binvec2dec(s)+1; 25 | fpi=(theta(18)*a(:,:,jlead1(j)+1)*M(:,:,jlead1(j)+1)+... 26 | (1-theta(18))*a(:,:,jlead0(j)+1)*M(:,:,jlead0(j)+1)); 27 | Fpi=ones(NonZeroCPI(t),1)*fpi*H; 28 | fny=theta(18)*(a(:,:,jlead1(j)+1)+b(:,:,jlead1(j)+1))*M(:,:,jlead1(j)+1)+... 29 | (1-theta(18))*(a(:,:,jlead0(j)+1)+b(:,:,jlead0(j)+1))*M(:,:,jlead0(j)+1); 30 | Fny=ones(NonZeroNGDP(t),1)*(fny-b(:,:,j))*H; 31 | 32 | DD=[e1; 33 | (1-theta(13))*theta(14)*a(:,:,j)+(1-theta(14))*theta(15)*b(:,:,j); 34 | a(:,:,j); 35 | b(:,:,j); 36 | Fpi; 37 | Fny;]; 38 | 39 | ZZ=[Z(1:4,t); 40 | Z(4:3+NonZeroCPI(t),t)-0.25*CPImean; 41 | Z(maxsurv+4:maxsurv+3+NonZeroNGDP(t),t)-0.25*NGDPmean;]; 42 | 43 | % RRR=zeros(size(ZZ,1),size(ZZ,1)); 44 | RRR=[0.001,zeros(1,NonZeroCPI(t)+3+NonZeroNGDP(t)); 45 | 0,theta(5),zeros(1,NonZeroCPI(t)+2+NonZeroNGDP(t)); 46 | 47 | zeros(2,1),[Gr(2);Gr(1);]*theta(5), eye(2)*0.001,zeros(2,NonZeroCPI(t)+NonZeroNGDP(t)); 48 | zeros(NonZeroCPI(t),4),eye(NonZeroCPI(t))*((fpi*SigJ(:,:,j)*fpi')^0.5),zeros(NonZeroCPI(t),NonZeroNGDP(t)); 49 | zeros(NonZeroNGDP(t),NonZeroCPI(t)+4),eye(NonZeroNGDP(t))*((fny*SigJ(:,:,j)*fny')^0.5);]; 50 | 51 | 52 | Ztilde = ZZ-DD*M(:,:,j)*X(:,t); 53 | Ztilde(2)=Ztilde(2)-theta(13)*Rlag;Rlag=Z(2,t); 54 | Ztilde(3)=Ztilde(3)-Gr(2,1)*Rlag; 55 | Ztilde(4)=Ztilde(4)-Gr(1,1)*Rlag; 56 | PP=M(:,:,j)*pp*M(:,:,j)'+N(:,:,j)*N(:,:,j)'; 57 | OMEGA=DD*PP*DD'+RRR*RRR'; 58 | OMEGAinv=eye(length(Ztilde))/OMEGA; 59 | LL=LL-0.5*(logdet(OMEGA)+Ztilde'*OMEGAinv*Ztilde); 60 | 61 | % if LL==Inf; 62 | % LL=LL-0.5*(log(det(OMEGA+1e-6*eye(length(OMEGA))))+Ztilde'/(OMEGA+1e-6*eye(length(OMEGA)))*Ztilde); 63 | % end 64 | 65 | K=PP*DD'*OMEGAinv; 66 | X(:,t+1)= M(:,:,j)*X(:,t)+K*Ztilde; 67 | pp=PP-K*OMEGA*K'; 68 | 69 | 70 | 71 | end 72 | 73 | if LL==Inf; 74 | LL=-1e200; 75 | end 76 | if isreal(LL)==0; 77 | LL=-1e200; 78 | end 79 | 80 | catch 81 | LL=-1e200; 82 | display('Not possible to evaluate LL') 83 | 84 | end 85 | 86 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MBDLL_Lorenz.m: -------------------------------------------------------------------------------- 1 | function [LL]=MBDLL_Lorenz(M,N,a,b,e1,dimX,SigJ,theta,Z,T,CPImean,NGDPmean,NonZeroCPI,NonZeroNGDP,maxsurv,H) 2 | 3 | % try 4 | pp=M*0; 5 | for j=1:10; 6 | pp=M*pp*M'+N*N'; 7 | end 8 | X=zeros(dimX,T); 9 | stick=theta(16); %Calvo parameter 10 | beta=theta(17); %discount rate 11 | varphi=theta(11); %labour supply curvature 12 | omega=1; 13 | lambda=(1-stick)*(1-stick*beta)/stick; 14 | 15 | LL=0; 16 | Rlag=theta(13)*Z(2,1); 17 | Gr=[-1;-lambda+lambda*varphi;]*theta(13)/(1-theta(13)); 18 | % Gu=[-1;-lambda+lambda*varphi;]/(1-theta(13)); 19 | for t=1:T-1; 20 | 21 | %% 22 | 23 | fpi=a*M; 24 | fny=(a+b)*M - b; 25 | Fpi=ones(NonZeroCPI(t),1)*fpi*H; 26 | Fny=ones(NonZeroNGDP(t),1)*fny*H; 27 | 28 | DD=[e1; 29 | (1-theta(13))*theta(14)*a+(1-theta(14))*theta(15)*b; 30 | a; 31 | b; 32 | Fpi; 33 | Fny;]; 34 | 35 | ZZ=[Z(1:4,t); 36 | Z(4:3+NonZeroCPI(t),t)-0.25*CPImean; 37 | Z(maxsurv+4:maxsurv+3+NonZeroNGDP(t),t)-0.25*NGDPmean;]; 38 | 39 | 40 | RRR=[0.001,zeros(1,NonZeroCPI(t)+3+NonZeroNGDP(t)); 41 | 0,theta(5),zeros(1,NonZeroCPI(t)+2+NonZeroNGDP(t)); 42 | 43 | zeros(2,1),[Gr(2);Gr(1);]*theta(5), eye(2)*0.001,zeros(2,NonZeroCPI(t)+NonZeroNGDP(t)); 44 | zeros(NonZeroCPI(t),4),eye(NonZeroCPI(t))*((fpi(1,:)*SigJ*fpi(1,:)')^0.5),zeros(NonZeroCPI(t),NonZeroNGDP(t)); 45 | zeros(NonZeroNGDP(t),NonZeroCPI(t)+4),eye(NonZeroNGDP(t))*((fny(1,:)*SigJ*fny(1,:)')^0.5);]; 46 | 47 | 48 | Ztilde = ZZ-DD*M*X(:,t); 49 | Ztilde(2)=Ztilde(2)-theta(13)*Rlag;Rlag=Z(2,t); 50 | Ztilde(3)=Ztilde(3)-Gr(2,1)*Rlag; 51 | Ztilde(4)=Ztilde(4)-Gr(1,1)*Rlag; 52 | PP=M*pp*M'+N*N'; 53 | OMEGA=DD*PP*DD'+RRR*RRR'; 54 | 55 | LL=LL-0.5*(logdet(OMEGA)+Ztilde'/OMEGA*Ztilde); 56 | 57 | K=PP*DD'/OMEGA; 58 | X(:,t+1)= M*X(:,t)+K*Ztilde; 59 | pp=PP-K*OMEGA*K'; 60 | 61 | 62 | 63 | end 64 | 65 | 66 | if isnan(LL)==1; 67 | LL=-1e200; 68 | display('LL is NaN') 69 | end 70 | 71 | if LL==Inf; 72 | LL=-1e200; 73 | display('LL is infinite') 74 | end 75 | if isreal(LL)==0; 76 | LL=-1e200; 77 | display('LL is imaginary') 78 | end 79 | 80 | % catch 81 | % LL=-1e200; 82 | % display('Not possible to evaluate LL') 83 | % 84 | % end 85 | 86 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MBDplotter.m: -------------------------------------------------------------------------------- 1 | function MBDplotter(theta) 2 | 3 | 4 | %Man Bites Dog simple example: Only exogenous signals (c) K Nimark 2010 5 | 6 | 7 | [M,N,A,DD0,DD1,KK0,KK1,R0,R1,P,p]=MBDsolve(theta) 8 | 9 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 10 | %Define hyper parameters 11 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 12 | kbar=10;% Number of orders of expectations 13 | tol=1e-4; %convergence criteria 14 | bindim=6;% Number of periods that matter (Markov order, if you like) and dimension of binary identifier of time varying matrices 15 | binmax=2^(bindim); % Number of "regimes" 16 | for j=1:bindim; 17 | binbase(1,bindim-j+1)=2^(j-1); 18 | end 19 | 20 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 21 | %Define parameters 22 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 23 | rho=theta(1); %persistence of state 24 | sigu=theta(2);%S.d. of state innov 25 | sigeta=theta(3);%s.d. of private info noise 26 | sigeps=theta(4);%s.d. of public signal noise 27 | alfa=theta(5);%unconditional prob of Z=1, i.e. of observing pub signal 28 | gamma=theta(6);%s.d. multiplier of u when Z=1 29 | 30 | 31 | 32 | H=[zeros(kbar,1), eye(kbar);];H=H(:,1:kbar); 33 | e1=[1,zeros(1,kbar-1)]; 34 | 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 36 | % Figures 37 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 38 | % Price and hierarchy IRF 39 | periods=20; 40 | ZVEC=[zeros(1, bindim-1) 1 zeros(1,periods-1) ]; 41 | j=binbase*ZVEC(1,(1:bindim))'; 42 | if ZVEC(1,bindim)==0; 43 | state(:,1)=N(:,1,j+1); 44 | else 45 | state(:,1)=N(:,1,j+1); 46 | end 47 | for t=1:periods; 48 | j=binbase*ZVEC(1,t:t+bindim-1)'; 49 | state(:,t+1)=M(:,:,j+1)*state(:,t); 50 | price(1,t)=A(1,:,j+1)*state(:,t); 51 | end 52 | figure(1) 53 | subplot(3,1,1);plot(price);hold on; 54 | subplot(3,1,2);plot(state');hold on; 55 | 56 | ZVEC=[zeros(1, bindim-1) 0 zeros(1,periods-1) ]; 57 | j=binbase*ZVEC(1,(1:bindim))'; 58 | if ZVEC(1,bindim)==0; 59 | state(:,1)=N(:,1,j+1)*gamma; 60 | else 61 | state(:,1)=N(:,1,j+1)*gamma; 62 | end 63 | for t=1:periods; 64 | j=binbase*ZVEC(1,t:t+bindim-1)'; 65 | state(:,t+1)=M(:,:,j+1)*state(:,t); 66 | price(1,t)=A(1,:,j+1)*state(:,t); 67 | end 68 | figure(1) 69 | subplot(3,1,1);plot(price,'--');hold on; 70 | subplot(3,1,3);plot(state');hold on; 71 | 72 | % %IRF of cross-sectional dispersion 73 | 74 | periods=20; 75 | ZVEC=[zeros(1, bindim) 1 zeros(1,periods-1) ]; %define sequence of Z 76 | j=binbase*ZVEC(1,(1:bindim))'; 77 | 78 | LM=(eye(kbar)-KK0(:,:,j+1)*DD0(:,:,j+1))*M(:,:,j+1); 79 | ISV=dlyap(LM,KK0(:,:,j+1)*R0(1,:)*R0(1,:)'*KK0(:,:,j+1)'); 80 | DISP1=[]; 81 | DISP1(1,1)= e1*ISV*(e1)'; 82 | SIG=ISV; 83 | for t=1:periods; 84 | 85 | j=binbase*ZVEC(1,t+1:t+bindim)'; 86 | 87 | if ZVEC(1,t+bindim)==0; 88 | LM=(eye(kbar)-KK0(:,:,j+1)*DD0(:,:,j+1))*M(:,:,j+1); 89 | KRRK0=KK0(:,:,j+1)*R0*R0'*KK0(:,:,j+1)'; 90 | SIG=LM*SIG*LM'+ KRRK0; 91 | 92 | 93 | else 94 | LM=(eye(kbar)-KK1(:,:,j+1)*DD1(:,:,j+1))*M(:,:,j+1); 95 | KRRK1=KK1(:,:,j+1)*R1*R1'*KK1(:,:,j+1)'; 96 | SIG=LM*SIG*LM'+ KRRK1; 97 | end 98 | DISP1(1,t+1)= e1*SIG*(e1)'; 99 | 100 | 101 | end 102 | 103 | 104 | figure 105 | plot(DISP1) 106 | 107 | %normal plotter 108 | fDISP1=zeros(200,10); 109 | axmin=max(DISP1.^.5)*4; 110 | for jj=1:10; 111 | for j=1:200; 112 | sig=DISP1(1,jj); 113 | x=-axmin+j*axmin*0.01; 114 | 115 | fy(j,1)=(1/((sig*(2*pi)^.5)))*exp(-(x^2)/(2*sig^2)); 116 | fDISP1(j,jj)=fy(j,1); 117 | 118 | end 119 | 120 | 121 | end 122 | figure 123 | surf(fDISP1); 124 | 125 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 126 | % ARCH 127 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 128 | periods=20; 129 | ZVEC=[zeros(1, bindim-1) 1 zeros(1,periods-1) ]; 130 | 131 | for t=1:periods; 132 | j=binbase*ZVEC(1,t:t+bindim-1)'; 133 | z=ZVEC(1,t:t+bindim-1); 134 | if z(end) ==1; 135 | SENS(t)=A(1,:,j+1)*N(:,1,j+1); 136 | else 137 | SENS(t)=theta(6)*A(1,:,j+1)*N(:,1,j+1); 138 | end 139 | end 140 | figure 141 | plot(SENS) 142 | 143 | 144 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MBDsimsmooth.asv: -------------------------------------------------------------------------------- 1 | function [X]=MBDsimsmooth(M,N,a,b,e1,bindim,dimX,jlead1,jlead0,SigJ,theta,Z,ST,T,CPImean,NGDPmean,NonZeroCPI,NonZeroNGDP,maxsurv,H) 2 | % Kalman simulation smoother 3 | % Adapted from Durbin and |Koopman (2002) by K Nimark 4 | 5 | % Xt = A*Xt-1 +C*ut 6 | % 7 | % Zt= D1*Xt + D2*Xt-1 + R*ut 8 | 9 | 10 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 11 | %Define a ancilliary variables, predefine matrices etc 12 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 13 | Xhat=zeros(dimX,T+1); 14 | PP0=zeros(dimX,dimX,T); 15 | PP1=zeros(dimX,dimX,T); 16 | 17 | 18 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 19 | %draw from (unconditional) state distibution 20 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 21 | pp=M(:,:,2)*0; 22 | for j=1:10; 23 | pp=M(:,:,2)*pp*M(:,:,2)'+N(:,:,2)*N(:,:,2)'; 24 | end 25 | Xplus=zeros(dimX,T+1); 26 | Xplus(:,1)=chol(pp+1e-6*eye(dimX))'*randn(dimX,1); 27 | dimS=length(N(1,:,1)); 28 | shocks=randn(dimS,T); 29 | 30 | for tt=2:T; 31 | tt 32 | s=ST(tt-1:tt-1+bindim-2)'; 33 | j=binvec2dec(s)+1; 34 | Xplus(:,tt)=M(:,:,j)*Xplus(:,tt-1) + N(:,:,j)*shocks(:,tt-1); 35 | % Zplus(:,tt) = D1*Xplus(:,tt)+ D2*Xplus(:,tt-1) + R*shocks(:,tt-1); 36 | end 37 | 38 | Rlag=theta(12)*Z(2,1); 39 | 40 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 41 | % forward recursion 42 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 43 | for t=1:T 44 | s=ST(t:t+bindim-1)'; 45 | j=binvec2dec(s)+1; 46 | 47 | Fpi=ones(NonZeroCPI(t),1)*(theta(17)*a(:,:,jlead1(j)+1)*M(:,:,jlead1(j)+1)+... 48 | (1-theta(17))*a(:,:,jlead0(j)+1)*M(:,:,jlead0(j)+1))*H; 49 | Fny=ones(NonZeroNGDP(t),1)*(theta(17)*(a(:,:,jlead1(j)+1)+b(:,:,jlead1(j)+1))*M(:,:,jlead1(j)+1)*H+... 50 | ((1-theta(17))*(a(:,:,jlead0(j)+1)+b(:,:,jlead0(j)+1))*M(:,:,jlead0(j)+1)*H)-b(:,:,j)*H); 51 | 52 | DD=[e1; 53 | (1-theta(12))*theta(13)*a(:,:,j)+(1-theta(12))*theta(14)*b(:,:,j); 54 | a(:,:,j); 55 | b(:,:,j); 56 | Fpi; 57 | Fny;]; 58 | 59 | ZZ=[Z(1:4,t); 60 | Z(4:3+NonZeroCPI(t),t)-0.25*CPImean; 61 | Z(maxsurv+4:maxsurv+3+NonZeroNGDP(t),t)-0.25*NGDPmean;]; 62 | 63 | % RRR=zeros(size(ZZ,1),size(ZZ,1)); 64 | RRR=[0.001,zeros(1,NonZeroCPI(t)+2+NonZeroNGDP(t)); 65 | zeros(1,NonZeroCPI(t)+3+NonZeroNGDP(t)); 66 | zeros(2,1),eye(2)*0.01,zeros(2,NonZeroCPI(t)+NonZeroNGDP(t)); 67 | zeros(NonZeroCPI(t),3),eye(NonZeroCPI(t))*((Fpi(1,:)*SigJ(:,:,j)*Fpi(1,:)')^0.5),zeros(NonZeroCPI(t),NonZeroNGDP(t)); 68 | zeros(NonZeroNGDP(t),NonZeroCPI(t)+3),eye(NonZeroNGDP(t))*((Fny(1,:)*SigJ(:,:,j)*Fny(1,:)')^0.5);]; 69 | 70 | 71 | Ztilde = ZZ-DD*M(:,:,j)*Xhat(:,t); 72 | Ztilde(2)=Ztilde(2)-theta(12)*Rlag; Rlag=Z(2,t); 73 | PP=M(:,:,j)*pp*M(:,:,j)'+N(:,:,j)*N(:,:,j)'; 74 | OMEGA=DD*PP*DD'+RRR*RRR'; 75 | K=PP*DD'/OMEGA; 76 | Xhat(:,t+1)= M(:,:,j)*Xhat(:,t)+K*Ztilde; 77 | pp=PP-K*OMEGA*K'; 78 | 79 | PP0(:,:,tt)=pp; 80 | PP1(:,:,tt)=PP; 81 | end 82 | 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 84 | %construct what is needed for last step 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 86 | 87 | 88 | for tt=1:T; 89 | tt 90 | s=ST(tt-1:tt-1+bindim-2)'; 91 | j=binvec2dec(s)+1; 92 | Xtilde(:,tt)=M(:,:,j)*Xhat(:,tt) + N(:,:,j)*shocks(:,tt-1); 93 | % Zplus(:,tt) = D1*Xplus(:,tt)+ D2*Xplus(:,tt-1) + R*shocks(:,tt-1); 94 | end 95 | 96 | Xtilde=A*Xhat; 97 | Xsm=Xhat*0; 98 | Xsm(:,T)=Xhat(:,end); 99 | 100 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 101 | %backward recursion 102 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 103 | 104 | for tt=T:-1:1 105 | J=PP0(:,:,tt)*A'*inv(PP1(:,:,tt)+eye(dimX)*1e-8); 106 | Xsm(:,tt)=Xhat(:,tt)+J*(Xsm(:,tt+1)-Xtilde(:,tt+1)); 107 | end 108 | 109 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 110 | %add up and spit out 111 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 112 | X=Xsm+Xplus; -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MBDsimulator.m: -------------------------------------------------------------------------------- 1 | % Man-Bites-Dog simulator 2 | 3 | clc 4 | clear all 5 | close all 6 | 7 | rho=0.95; %persistence of state 8 | sigu=1;%S.d. of state innov 9 | sigeta=1;%s.d. of private info noise 10 | sigeps=1;%s.d. of public signal noise 11 | alfa=0.1;%unconditional prob of Z=1, i.e. of observing pub signal 12 | gamma=3;%s.d. multiplier of u when Z=1 13 | 14 | tol=1e-4; %convergence criteria 15 | bindim=6;% Number of periods that matter (Markov order, if you like) and dimension of binary identifier of time varying matrices 16 | binmax=2^(bindim); % Number of "regimes" 17 | for j=1:bindim; 18 | binbase(1,bindim-j+1)=2^(j-1); 19 | end 20 | 21 | theta=[rho,sigu,sigeta,sigeps,alfa,gamma]'; 22 | % load('xmax'); 23 | % theta=xmax; 24 | %MBDplotter(theta); 25 | %% 26 | % [M,N,A,DD0,DD1,KK0,KK1,R0,R1,P,p]=MBDsolve(theta); 27 | [M,N,A,DD0,DD1,KK0,KK1,R0,R1,P,p]=MBDsolve(theta); 28 | 29 | T=100+bindim;Z=zeros(1,T); 30 | for t=1:T; 31 | if (rand) <= alfa ; 32 | Z(1,t)=1; 33 | end 34 | end 35 | 36 | U=randn(3,T); 37 | X=zeros(size(M,1),T); 38 | S=zeros(1,T); 39 | 40 | for t=bindim:T 41 | z=Z(1,t-bindim+1:t); 42 | j=binbase*z'+1; 43 | 44 | X(:,t)= M(:,:,j)*X(:,t-1)+N(:,:,j)*U(:,t); 45 | S(t)=A(:,:,j)*X(:,t); 46 | 47 | end 48 | 49 | 50 | Xc=zeros(size(M,1),T); 51 | Sc=zeros(1,T); 52 | Uc=U; 53 | for t=1:T; 54 | if Z(t)==1; 55 | Uc(1,t)=theta(6)*U(1,t); 56 | end 57 | end 58 | 59 | Z0=Z*0; 60 | for t=bindim:T 61 | z=Z0(1,t-bindim+1:t); 62 | j=binbase*z'+1; 63 | Xc(:,t)= M(:,:,j)*X(:,t-1)+N(:,:,j)*Uc(:,t); 64 | Sc(t)=A(:,:,j)*Xc(:,t); 65 | 66 | end 67 | 68 | figure 69 | subplot(2,1,1); 70 | plot(S(5:end),'r'); hold on; plot(Sc(5:end)); 71 | subplot(2,1,2); 72 | plot(Z(5:end)); 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MCMC_25_prior.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MCMC_25_prior.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MCMC_Lorenz_25_prior.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/MCMC_Lorenz_25_prior.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/NGDPactual.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/NGDPactual.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/NGDPd.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/NGDPd.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/NGDPdisp.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/NGDPdisp.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/NGDPsurv.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/NGDPsurv.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/Qdoms.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/Qdoms.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/STMCMC.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/STMCMC.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/ST_25_prior.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/ST_25_prior.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/StartST.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/StartST.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/StartST2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/StartST2.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/Untitled.m: -------------------------------------------------------------------------------- 1 | 2 | ST=zeros(116,1); 3 | for j=1:length(resid) 4 | if abs(resid(j)) >= 1.5*(cov(resid))^.5 5 | ST(j)=1; 6 | end 7 | end 8 | 9 | subplot(2,1,1);plot(ST) 10 | subplot(2,1,2);plot(resid) -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/benchMCMC.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/benchMCMC.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/beta_pdf.m: -------------------------------------------------------------------------------- 1 | function pdf = beta_pdf(x, a, b) 2 | % PURPOSE: pdf of the beta(a,b) distribution 3 | %-------------------------------------------------------------- 4 | % USAGE: pdf = beta_pdf(x,a,b) 5 | % where: x = vector of components 6 | % a = beta distribution parameter, a = scalar 7 | % b = beta distribution parameter b = scalar 8 | % NOTE: mean[(beta(a,b)] = a/(a+b), variance = ab/((a+b)*(a+b)*(a+b+1)) 9 | %-------------------------------------------------------------- 10 | % RETURNS: pdf at each element of x of the beta(a,b) distribution 11 | %-------------------------------------------------------------- 12 | % SEE ALSO: beta_d, beta_pdf, beta_inv, beta_rnd 13 | %-------------------------------------------------------------- 14 | 15 | % Anders Holtsberg, 18-11-93 16 | % Copyright (c) Anders Holtsberg 17 | % documentation modified by LeSage to 18 | % match the format of the econometrics toolbox 19 | 20 | 21 | if (nargin ~=3) 22 | error('Wrong # of arguments to beta_pdf'); 23 | end 24 | 25 | if any(any((a<=0)|(b<=0))) 26 | error('Parameter a or b is nonpositive'); 27 | end 28 | 29 | I = find((x<0)|(x>1)); 30 | 31 | pdf = x.^(a-1) .* (1-x).^(b-1) ./ beta(a,b); 32 | pdf(I) = 0*I; -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/bindec.m: -------------------------------------------------------------------------------- 1 | %binary vector - decimal conversions 2 | 3 | 4 | %decimal to binary vector 5 | binmaxdim=8; 6 | n=7 7 | v=dec2bin(n)=='1' 8 | binvec=[zeros(1,binmaxdim-length(v)) v ;] 9 | 10 | %binary vector to decimal 11 | 12 | for j=1:binmaxdim 13 | binbase(1,binmaxdim-j+1)=2^(j-1); 14 | end 15 | binbase 16 | binbase*binvec' 17 | 18 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/binom_dist.m: -------------------------------------------------------------------------------- 1 | function pmf = binom_dist(N,p,k) 2 | nValues = numel(k); 3 | pmf = zeros(1,nValues); 4 | for i = 1:nValues 5 | pmf(i) = nchoosek(N,k(i))*p^k(i)*(1-p)^(N-k(i)); 6 | end 7 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/binvec2dec.m: -------------------------------------------------------------------------------- 1 | function d=binvec2dec(v) 2 | 3 | for j=1:length(v) 4 | binbase(1,length(v)-j+1)=2^(j-1); 5 | end 6 | d=binbase*v'; 7 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/dCPI.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/dCPI.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/dTFP.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/dTFP.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/dec2binvec.m: -------------------------------------------------------------------------------- 1 | function v=dec2binvec(d,dim) 2 | 3 | vv=dec2bin(d)=='1'; 4 | 5 | if d <= 2^(dim-1) 6 | v=[zeros(1,dim-length(vv)) vv ;]; 7 | else 8 | v= vv ; 9 | end -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/doubleo.m: -------------------------------------------------------------------------------- 1 | 2 | function[K,S]=doubleo(A,C,Q,R) 3 | %DOUBLEo.M 4 | %function[K,S]=doubleo(A,C,Q,R) 5 | % This program uses the "doubling algorithm" to solve the 6 | % Riccati matrix difference equations associated with the 7 | % Kalman filter. A is nxn, C is kxn, Q is nxn, R is kxk. 8 | % The program returns the gain K and the stationary covariance 9 | % matrix of the one-step ahead errors in forecasting the state. 10 | % 11 | % The program creates the Kalman filter for the following system: 12 | % 13 | % x(t+1) = A * x(t) + e(t+1) 14 | % 15 | % y(t) = C * x(t) + v(t) 16 | % 17 | % where E e(t+1)*e(t+1)' = Q, and E v(t)*v(t)' = R, and v(s) is orthogonal 18 | % to e(t) for all t and s. The program creates the observer system 19 | % 20 | % xx(t+1) = A * xx(t) + K * a(t) 21 | % y(t) = C * xx(t) + a(t), 22 | % 23 | % where K is the Kalman gain ,S = E (x(t) - xx(t))*(x(t) - xx(t))', and 24 | % a(t) = y(t) - E[y(t)| y(t-1), y(t-2), ... ], and xx(t)=E[x(t)|y(t-1),...]. 25 | % NOTE: By using DUALITY, control problems can also be solved. 26 | a0=A'; 27 | b0=C'*(R\C); 28 | g0=Q; 29 | tol=1e-15; 30 | dd=1; 31 | ss=max(size(A)); 32 | v=eye(ss); 33 | while dd>tol 34 | a1=a0 *((v+b0*g0)\a0); 35 | b1=b0+a0*((v+b0*g0)\(b0*a0')); 36 | g1=g0+a0'*g0*((v+b0*g0)\a0); 37 | k1=A*g1*C'/(C*g1*C'+R); 38 | k0=A*g0*C'/(C*g0*C'+R); 39 | dd=max(max(abs(k1-k0))); 40 | a0=a1; 41 | b0=b1; 42 | g0=g1; 43 | end 44 | K=k1;S=g1; 45 |  -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/hpfilter.m: -------------------------------------------------------------------------------- 1 | function [s]=hpfilter(y,w) 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | % Author: Ivailo Izvorski, 4 | % Department of Economics 5 | % Yale University. 6 | % izvorski@econ.yale.edu 7 | % This code has been used and seems to be free of error. 8 | % However, it carries no explicit or implicit guarantee. 9 | % 10 | % function [s]=hpfilter(y,w) 11 | % Hondrick Prescott filter where: 12 | % w - smoothing parameter; w=1600 for quarterly data 13 | % y - the original series that has to be smoothed 14 | % s - the filtered series 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | if size(y,1)1e-6 & it<=maxit); 62 | 63 | k0= (A*s0*C'+V12)/(V2+C*s0*C'); 64 | 65 | s1= A*s0*A' + V1 -(A*s0*C'+V12)*k0'; 66 | 67 | k1= (A*s1*C'+V12)/(V2+C*s1*C'); 68 | 69 | dd=max(max(abs(k1-k0))); 70 | 71 | it=it+1; 72 | 73 | s0=s1; 74 | 75 | end; 76 | 77 | k=k1;s=s0; 78 | 79 | if it>=maxit; 80 | 81 | % disp('WARNING: Iteration limit of 1000 reached in KFILTER.M'); 82 | k=[];s=[]; 83 | 84 | end; 85 | 86 | end; 87 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/lastMCMC.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/lastMCMC.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/lldraw.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/lldraw.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/llmax.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/llmax.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/logGDP.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anorring/Bayesian-methods-for-DSGE-models/d63e4501a09df9d7eea501edbec97f4bd91dec37/CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/logGDP.mat -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/logdet.m: -------------------------------------------------------------------------------- 1 | function v = logdet(A, op) 2 | %LOGDET Computation of logarithm of determinant of a matrix 3 | % 4 | % v = logdet(A); 5 | % computes the logarithm of determinant of A. 6 | % 7 | % Here, A should be a square matrix of double or single class. 8 | % If A is singular, it will returns -inf. 9 | % 10 | % Theoretically, this function should be functionally 11 | % equivalent to log(det(A)). However, it avoids the 12 | % overflow/underflow problems that are likely to 13 | % happen when applying det to large matrices. 14 | % 15 | % The key idea is based on the mathematical fact that 16 | % the determinant of a triangular matrix equals the 17 | % product of its diagonal elements. Hence, the matrix's 18 | % log-determinant is equal to the sum of their logarithm 19 | % values. By keeping all computations in log-scale, the 20 | % problem of underflow/overflow caused by product of 21 | % many numbers can be effectively circumvented. 22 | % 23 | % The implementation is based on LU factorization. 24 | % 25 | % v = logdet(A, 'chol'); 26 | % If A is positive definite, you can tell the function 27 | % to use Cholesky factorization to accomplish the task 28 | % using this syntax, which is substantially more efficient 29 | % for positive definite matrix. 30 | % 31 | % Remarks 32 | % ------- 33 | % logarithm of determinant of a matrix widely occurs in the 34 | % context of multivariate statistics. The log-pdf, entropy, 35 | % and divergence of Gaussian distribution typically comprises 36 | % a term in form of log-determinant. This function might be 37 | % useful there, especially in a high-dimensional space. 38 | % 39 | % Theoretially, LU, QR can both do the job. However, LU 40 | % factorization is substantially faster. So, for generic 41 | % matrix, LU factorization is adopted. 42 | % 43 | % For positive definite matrices, such as covariance matrices, 44 | % Cholesky factorization is typically more efficient. And it 45 | % is STRONGLY RECOMMENDED that you use the chol (2nd syntax above) 46 | % when you are sure that you are dealing with a positive definite 47 | % matrix. 48 | % 49 | % Examples 50 | % -------- 51 | % % compute the log-determinant of a generic matrix 52 | % A = rand(1000); 53 | % v = logdet(A); 54 | % 55 | % % compute the log-determinant of a positive-definite matrix 56 | % A = rand(1000); 57 | % C = A * A'; % this makes C positive definite 58 | % v = logdet(C, 'chol'); 59 | % 60 | 61 | % Copyright 2008, Dahua Lin, MIT 62 | % Email: dhlin@mit.edu 63 | % 64 | % This file can be freely modified or distributed for any kind of 65 | % purposes. 66 | % 67 | 68 | %% argument checking 69 | 70 | assert(isfloat(A) && ndims(A) == 2 && size(A,1) == size(A,2), ... 71 | 'logdet:invalidarg', ... 72 | 'A should be a square matrix of double or single class.'); 73 | 74 | if nargin < 2 75 | use_chol = 0; 76 | else 77 | assert(strcmpi(op, 'chol'), ... 78 | 'logdet:invalidarg', ... 79 | 'The second argument can only be a string ''chol'' if it is specified.'); 80 | use_chol = 1; 81 | end 82 | 83 | %% computation 84 | 85 | if use_chol 86 | v = 2 * sum(log(diag(chol(A)))); 87 | else 88 | [L, U, P] = lu(A); 89 | du = diag(U); 90 | c = det(P) * prod(sign(du)); 91 | v = log(c) + sum(log(abs(du))); 92 | end 93 | 94 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/logpriorMBD.m: -------------------------------------------------------------------------------- 1 | function lprior=logpriorMBD(bcan) 2 | lprior=0; 3 | %prod persistence 4 | pmean=0.9; 5 | stdd=0.02; 6 | 7 | a = (1-pmean)*pmean^2/stdd^2 - pmean; 8 | b = a*(1/pmean - 1); 9 | 10 | lprior= lprior + log(beta_pdf(bcan(1), a, b)); 11 | 12 | %demand persistence 13 | pmean=0.7; 14 | stdd=0.1; 15 | 16 | a = (1-pmean)*pmean^2/stdd^2 - pmean; 17 | b = a*(1/pmean - 1); 18 | 19 | lprior= lprior + log(beta_pdf(bcan(2), a, b)); 20 | 21 | 22 | 23 | %Disutility of labor curvature 24 | pmean=1; 25 | stdd=0.1; 26 | lprior= lprior + log(norm_pdf(bcan(11),pmean ,stdd^2)); 27 | 28 | 29 | %Demand elasticity 30 | pmean=1; 31 | stdd=0.2; 32 | lprior= lprior + log(norm_pdf(bcan(12),pmean ,stdd^2)); 33 | 34 | 35 | 36 | %Taylor rule coeff lagged interest rate 37 | pmean=0.5; 38 | stdd=0.1; 39 | lprior= lprior + log(beta_pdf(bcan(13), pmean, stdd^2)); 40 | 41 | %Taylor rule coeff on infl 42 | pmean=1.5; 43 | stdd=0.05; 44 | lprior= lprior + log(norm_pdf(bcan(14), pmean, stdd^2)); 45 | 46 | %Taylor rule coeff on output 47 | pmean=0.5; 48 | stdd=0.1; 49 | lprior= lprior + log(norm_pdf(bcan(15),pmean ,stdd^2)); 50 | 51 | 52 | %Calvo sticky param 53 | pmean=0.7; 54 | stdd=0.05; 55 | a = (1-pmean)*pmean^2/stdd^2 - pmean; 56 | b = a*(1/pmean - 1); 57 | lprior= lprior + log(beta_pdf(bcan(16), a, b)); 58 | 59 | %Discount rate 60 | pmean=0.99; 61 | stdd=0.01; 62 | a = (1-pmean)*pmean^2/stdd^2 - pmean; 63 | b = a*(1/pmean - 1); 64 | lprior= lprior + log(beta_pdf(bcan(17), a, b)); 65 | % 66 | % %omega (unconditional frequency) 67 | % pmean=0.2; 68 | % stdd=0.05; 69 | % a = (1-pmean)*pmean^2/stdd^2 - pmean; 70 | % b = a*(1/pmean - 1); 71 | % lprior= lprior + log(beta_pdf(bcan(18), a, b)); 72 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/norm_cdf.m: -------------------------------------------------------------------------------- 1 | function cdf = norm_cdf (x, m, v) 2 | % PURPOSE: computes the cumulative normal distribution 3 | % for each component of x with mean m, variance v 4 | %--------------------------------------------------- 5 | % USAGE: cdf = norm_cdf(x,m,v) 6 | % where: x = variable vector (nx1) 7 | % m = mean vector (default=0) 8 | % v = variance vector (default=1) 9 | %--------------------------------------------------- 10 | % RETURNS: cdf (nx1) vector 11 | %--------------------------------------------------- 12 | 13 | % Written by TT (Teresa.Twaroch@ci.tuwien.ac.at) on Jun 3, 1993 14 | % Updated by KH (Kurt.Hornik@ci.tuwien.ac.at) on Oct 26, 1994 15 | % Copyright Dept of Probability Theory and Statistics TU Wien 16 | % Updated by James P. Lesage, jpl@jpl.econ.utoledo.edu 1/7/97 17 | 18 | [r, c] = size(x); 19 | 20 | if (r*c == 0) 21 | error('norm_cdf: x must not be empty'); 22 | end; 23 | 24 | if (nargin == 1) 25 | m = zeros(r,1); 26 | v = ones(r,1); 27 | end; 28 | 29 | cdf = zeros(r, 1); 30 | cdf(1:r,1) = stdn_cdf((x(1:r,1) - m(1:r,1)) ./ sqrt (v(1:r,1))); 31 | 32 | 33 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/norm_pdf.m: -------------------------------------------------------------------------------- 1 | function pdf = norm_pdf (x, m, v) 2 | % PURPOSE: computes the normal probability density function 3 | % for each component of x with mean m, variance v 4 | %--------------------------------------------------- 5 | % USAGE: pdf = norm_pdf(x,m,v) 6 | % where: x = variable vector (nx1) 7 | % m = mean vector (default=0) 8 | % v = variance vector (default=1) 9 | %--------------------------------------------------- 10 | % RETURNS: pdf (nx1) vector 11 | %--------------------------------------------------- 12 | % SEE ALSO: norm_d, norm_rnd, norm_inv, norm_cdf 13 | %--------------------------------------------------- 14 | 15 | % Written by TT (Teresa.Twaroch@ci.tuwien.ac.at) on Jun 3, 1993 16 | % Updated by KH (Kurt.Hornik@ci.tuwien.ac.at) on Oct 26, 1994 17 | % Copyright Dept of Probability Theory and Statistics TU Wien 18 | % Updated by James P. Lesage, 19 | % jlesage@spatial-econometrics.com 1/7/97 20 | 21 | 22 | if ~((nargin == 1) | (nargin == 3)) 23 | error('Wrong # of arguments to norm_pdf'); 24 | end 25 | 26 | [r, c] = size (x); 27 | 28 | if (nargin == 1) 29 | m = zeros(r,1); 30 | v = ones(r,1); 31 | end 32 | 33 | pdf = zeros (r,1); 34 | 35 | pdf(1:r,1) = stdn_pdf((x(1:r,1) - m(1:r,1)) ./ sqrt (v(1:r,1))) ... 36 | ./ sqrt (v(1:r,1)); 37 | 38 | 39 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/norm_rnd.m: -------------------------------------------------------------------------------- 1 | function y = norm_rnd(sig); 2 | % PURPOSE: random multivariate random vector based on 3 | % var-cov matrix sig 4 | %--------------------------------------------------- 5 | % USAGE: y = norm_rnd(sig) 6 | % where: sig = a square-symmetric covariance matrix 7 | % NOTE: for mean b, var-cov sig use: b + norm_rnd(sig) 8 | %--------------------------------------------------- 9 | % RETURNS: y = random vector normal draw mean 0, var-cov(sig) 10 | %--------------------------------------------------- 11 | 12 | % written by: 13 | % James P. LeSage, Dept of Economics 14 | % University of Toledo 15 | % 2801 W. Bancroft St, 16 | % Toledo, OH 43606 17 | % jlesage@spatial-econometrics.com 18 | 19 | if nargin ~= 1 20 | error('Wrong # of arguments to norm_rnd'); 21 | end; 22 | 23 | h = chol(sig); 24 | [nrow, ncol] = size(sig); 25 | x = randn(nrow,1); 26 | y = h'*x; 27 | -------------------------------------------------------------------------------- /CM_Bayesian Macro - Matlab - Nimark/Lorenz No regimes 25 prior/normlt_rnd.m: -------------------------------------------------------------------------------- 1 | function result = normlt_rnd(mu,sigma2,left) 2 | % PURPOSE: compute random draws from a left-truncated normal 3 | % distribution, with mean = mu, variance = sigma2 4 | % ------------------------------------------------------ 5 | % USAGE: y = normlt_rnd(mu,sigma2,left) 6 | % where: mu = mean (scalar or vector) 7 | % sigma2 = variance (scalar or vector) 8 | % left = left truncation point (scalar or vector) 9 | % ------------------------------------------------------ 10 | % RETURNS: y = (scalar or vector) the size of mu, sigma2 11 | % ------------------------------------------------------ 12 | % NOTES: This is merely a convenience function that 13 | % calls normt_rnd with the appropriate arguments 14 | % ------------------------------------------------------ 15 | 16 | % written by: 17 | % James P. LeSage, Dept of Economics 18 | % University of Toledo 19 | % 2801 W. Bancroft St, 20 | % Toledo, OH 43606 21 | % jpl@jpl.econ.utoledo.edu 22 | 23 | if nargin ~= 3 24 | error('normlt_rnd: Wrong # of input arguments'); 25 | end; 26 | 27 | right = mu + 5*sqrt(sigma2); 28 | 29 | result = normt_rnd(mu,sigma2,left,right); 30 | if result