Home > . > UnivariateSpectralDensity.m

UnivariateSpectralDensity

PURPOSE ^

stephane.adjemian@ens.fr

SYNOPSIS ^

function [omega,f] = UnivariateSpectralDensity(dr,var_list)

DESCRIPTION ^

 stephane.adjemian@ens.fr
 This function computes the theoretical spectral density of each
 endogenous variable declared in var_list. Results are stored in 
 oo_ and may be plotted.
 
 Adapted from th_autocovariances.m.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % stephane.adjemian@ens.fr
0002 function [omega,f] = UnivariateSpectralDensity(dr,var_list)
0003 % This function computes the theoretical spectral density of each
0004 % endogenous variable declared in var_list. Results are stored in
0005 % oo_ and may be plotted.
0006 %
0007 % Adapted from th_autocovariances.m.
0008 global options_ oo_ M_
0009 
0010 omega = []; f = [];
0011 
0012 if options_.order > 1
0013   disp('UnivariateSpectralDensity :: I Cannot compute the theoretical spectral density') 
0014   disp('with a second order approximation of the DSGE model!')
0015   disp('Set order = 1.')
0016   return
0017 end
0018 
0019 pltinfo  = 1;%options_.SpectralDensity.Th.plot;
0020 cutoff   = 100;%options_.SpectralDensity.Th.cutoff;
0021 sdl      = 0.1;%options_.SepctralDensity.Th.sdl;
0022 omega    = (0:sdl:pi)';
0023 GridSize = length(omega);
0024 exo_names_orig_ord  = M_.exo_names_orig_ord;
0025 if sscanf(version('-release'),'%d') < 13
0026   warning off
0027 else
0028   eval('warning off MATLAB:dividebyzero')
0029 end
0030 if nargin<2
0031   var_list = [];
0032 end
0033 nvar = size(var_list,1);
0034 if nvar == 0
0035   nvar = length(dr.order_var);
0036   ivar = [1:nvar]';
0037 else
0038     ivar=zeros(nvar,1);
0039     for i=1:nvar
0040       i_tmp = strmatch(var_list(i,:),M_.endo_names,'exact');
0041       if isempty(i_tmp)
0042           error (['One of the variable specified does not exist']) ;
0043       else
0044     ivar(i) = i_tmp;
0045       end
0046     end
0047 end
0048 f = zeros(nvar,GridSize);
0049 ghx = dr.ghx;
0050 ghu = dr.ghu;
0051 npred = dr.npred;
0052 nstatic = dr.nstatic;
0053 kstate = dr.kstate;
0054 order = dr.order_var;
0055 iv(order) = [1:length(order)];
0056 nx = size(ghx,2);
0057 ikx = [nstatic+1:nstatic+npred];
0058 A = zeros(nx,nx);
0059 k0 = kstate(find(kstate(:,2) <= M_.maximum_lag+1),:);
0060 i0 = find(k0(:,2) == M_.maximum_lag+1);
0061 i00 = i0;
0062 n0 = length(i0);
0063 A(i0,:) = ghx(ikx,:);
0064 AS = ghx(:,i0);
0065 ghu1 = zeros(nx,M_.exo_nbr);
0066 ghu1(i0,:) = ghu(ikx,:);
0067 for i=M_.maximum_lag:-1:2
0068   i1 = find(k0(:,2) == i);
0069   n1 = size(i1,1);
0070   j1 = zeros(n1,1);
0071   j2 = j1;
0072   for k1 = 1:n1
0073     j1(k1) = find(k0(i00,1)==k0(i1(k1),1));
0074     j2(k1) = find(k0(i0,1)==k0(i1(k1),1));
0075   end
0076   AS(:,j1) = AS(:,j1)+ghx(:,i1);
0077   i0 = i1;
0078 end
0079 b = ghu1*M_.Sigma_e*ghu1';
0080 [A,B] = kalman_transition_matrix(dr);
0081 % index of predetermined variables in A
0082 i_pred = [nstatic+(1:npred) M_.endo_nbr+1:length(A)];
0083 A = A(i_pred,i_pred);
0084 [vx, ns_var] =  lyapunov_symm(A,b);
0085 i_ivar = find(~ismember(ivar,dr.order_var(ns_var+nstatic)));
0086 ivar = ivar(i_ivar);
0087 iky = iv(ivar);  
0088 aa = ghx(iky,:);
0089 bb = ghu(iky,:);
0090 Gamma = zeros(nvar,cutoff+1);
0091 tmp = aa*vx*aa'+ bb*M_.Sigma_e*bb';
0092 k = find(abs(tmp) < 1e-12);
0093 tmp(k) = 0;
0094 Gamma(:,1) = diag(tmp);
0095 vxy = (A*vx*aa'+ghu1*M_.Sigma_e*bb');
0096 tmp = aa*vxy;
0097 k = find(abs(tmp) < 1e-12);
0098 tmp(k) = 0;
0099 Gamma(:,2) = diag(tmp);
0100 for i=2:cutoff
0101   vxy = A*vxy;
0102   tmp = aa*vxy;
0103   k = find(abs(tmp) < 1e-12);
0104   tmp(k) = 0;
0105   Gamma(:,i+1) = diag(tmp);
0106 end
0107 H = 1:cutoff;
0108 for i=1:nvar
0109   f(i,:) = Gamma(i,1)/(2*pi) + Gamma(i,H+1)*cos(H'*omega')/pi;
0110 end  
0111 
0112 if sscanf(version('-release'),'%d') < 13
0113   warning on
0114 else
0115   eval('warning on MATLAB:dividebyzero')
0116 end
0117 
0118 if pltinfo
0119   for i= 1:nvar
0120     figure('Name',['Spectral Density of ' deblank(M_.endo_names(ivar(i),:)) '.'])
0121     plot(omega,f(i,:),'-k','linewidth',2)
0122     xlabel('0 \leq \omega \leq \pi')
0123     ylabel('f(\omega)')
0124     box on
0125     axis tight
0126   end
0127 end

Generated on Fri 16-Jun-2006 09:09:06 by m2html © 2003