# I need to do a Sign Restriction SVAR in Matlab

34 views (last 30 days)
Gareth on 15 Jul 2013
Hi Sir/Madam,
I need to do an SVAR sign restrictions on shocks in Matlab.
my variables of interest are expenditure, gdp, inflation, revenue, interest rate stock price index and i have the code below
I want to impose the restriction on expenditure shock and revenue shock
------------------------------------------------------------------------------ clear all; close all;
ddata = xlsread ('Data for Sign Restrictions.xls');
data= [ddata(3:48,2) ddata(3:48,3) ddata(3:48,4) ddata(3:48,5) ddata(3:48,6) ddata(3:48,7)];
% pick log of Expend, log of Gdp,log of Inf, log of Rev, log of Int and log % of Stockpix
% Preliminary transformation of the data
const=ones(46,1);
trend=1:46;
qtrend=(1:46).^2;
XXX=[const,trend',qtrend'];
res1=data-XXX*inv(XXX'*XXX)*XXX'*data;
nlags = 4; % number of lags
nimp = 20; % number of impulses
c =0 ; % use no constant in the VAR
n=size(data,2); % number of variables
ho = 1; % number of horizons over which sign restrictions are
imposed.
maxdraws = 1000; % maximum number of rotations tried
% estimation of the var system
[Y,X] = VAR_str(res1,c,nlags); % creates the variables X and the Y
Bet=inv(X'*X)*X'*Y; % beta_ols
res=Y-X*Bet;
Sigma=cov(res); % sigma_ols
% choleski decomposition
wimpu=[];
cimpu=[];
BB=companion2(Bet,nlags,n,c); % construct companion form of the VAR
for j=1:nimp
%%(non-orthogonal) Wold impulses
if j==1
wimpu(:,:,j)=eye(size(BB,1),size(BB,2));
elseif j>1
wimpu(:,:,j)=BB^(j-1);
end
%% (orthogonal) Choleski impulses
cimpu(:,:,j)=wimpu(1:n,1:n,j)*chol(Sigma)';
end
[n n nlagsimp]=size(cimpu);
dr=1;
gg=0;
irf=[];
idx=0;
% computation of sign-based impulse responses
% 1) draw a normal (0,1) random matrix of dimension nxn
% 2) use the qr decompostion of the draw and store q
% 3) multiply choleski response by q
% 4) check if the impulse constructed in 3) satisfy the sign restrictions while dr<=maxdraws
a=normrnd(0,1,n,n);
% Compute the qr decomposition of the random matrix a
[q r]=qr(a);
for ii=1:n;
if r(ii,ii)<0
q(:,ii)=-q(:,ii); % reflection matrix
end
end
% Compute the candidate responses implied by q
for j=1:n
for i = 1:nlagsimp
S(:,j,i) = cimpu(:,:,i)*q(:,j);
end
end
invchol=inv(cimpu(:,:,1));
imp=q(:,1)'*invchol(:,1);
% Check if the restrictions are satisfied for shock 1
% Want Expend up (variable 1), Rev up (variable 3)
% down at horizons 1 to ho
z1=[squeeze(S(1,1,1:ho))'>=0 squeeze(S(3,1,1:ho))'<=0 squeeze(S(2,1,1:ho))'- squeeze(S(1,1,1:ho))'<=0];
zz1=sum(z1);
if zz1==size(z1,2)
gg=gg+1;
for j=1:n
for i = 1:nlagsimp
irf(:,j,i,gg) = cimpu(:,:,i)*q(:,j);
end
end
end
if zz1<size(z1,2)
mz1=[squeeze(S(1,1,1:ho))'<=0 squeeze(S(3,1,1:ho))'>=0
squeeze(S(2,1,1:ho))'-squeeze(S(1,1,1:ho))'>=0];
mzz1=sum(mz1);
if mzz1==size(mz1,2)
q(:,1)=-q(:,1);
gg= gg+1;
for j=1:n
for i = 1:nlagsimp
irf(:,j,i,gg) = cimpu(:,:,i)*q(:,j);
end
end
end
end
dr=dr+1;
end
disp('number of draws made and accepted draws')
[dr-1 gg]
% estract 68 intervals for the responses using identification uncertainty
sir=sort(irf,4);
figure(1)
me11=squeeze(sir(3,1,:,fix(gg*0.50)));
up11=squeeze(sir(3,1,:,fix(gg*0.84)));
lo11=squeeze(sir(3,1,:,fix(gg*0.16)));
me21=squeeze(sir(5,1,:,fix(gg*0.50)));
up21=squeeze(sir(5,1,:,fix(gg*0.84)));
lo21=squeeze(sir(5,1,:,fix(gg*0.16)));
subplot(1,2,1),plot([me11 up11 lo11]),axis tight;
legend('RGDP responses')
subplot(1,2,2),plot([me21 up21 lo21]),axis tight;
legend('Inflation responses')
return ---------------------------------------------------------------------------- i am getting the error below when i run the code above
Undefined function 'VAR_str' for input arguments of type 'double'.
and please see below for the code i have on 'VAR_str'
---------------------------------------------------------------------------- %Creates matrices for VAR(k)
function [yy,xx,x] = VAR_str(y,c,k)
s=size(y);
T=s(1); N=s(2);
for i=1:N,
yy(:,i)=y(k+1:T,i);
for j=1:k,
xx(:,k*(i-1)+j)=y(k+1-j:T-j,i);
end; end;
if c==0,xx=xx;
elseif c==1,xx=[ones(T-k,1) xx];
elseif c==2,xx=[ones(T-k,1) (1:T-k)' xx];
elseif c==3,xx=[ones(T-k,1) (1:T-k)' ((1:t-k).^2)' xx] % added july 6, 2011
end
z(:,1)=xx(:,1);
for ij = 1:k
zz(:,(ij-1)*N+1:N*ij) = xx(:,ij+c:k:size(xx,2));
end
if c==1,x = [z zz];
elseif c==0 x=zz;
----------------------------------------------------------------------------
I get the error below when I run the 'VAR_str' code
function [yy,xx,x] = VAR_str(y,c,k) | Error: Function definitions are not permitted in this context.
----------------------------------------------------------------------------
when I create a file VAR_str.m and move the code for function VAR_str(my understanding is that I mark up function [yy,xx,x] = VAR_str(y,c,k) to achieve this), i get rid of the error, "Error: Function definitions are not permitted in this context."
but if I comment out the function below it then becomes a script
function [yy,xx,x] = VAR_str(y,c,k)
and try running the whole script (the SVAR sign restrictions code) i get the error below
Undefined function 'VAR_str' for input arguments of type
'double'.
##### 2 CommentsShowHide 1 older comment
Gareth on 17 Jul 2013
I have made it readable. Someone please help me out

Sign in to comment.

### Accepted Answer

Muthu Annamalai on 18 Jul 2013
@Gareth MATLAB has concepts of script and a function. You cannot mix both.
If you start a M-file with a MATLAB command that is not a function or class definition, it is considered a script. In MATLAB scripts you may not define functions.
Try creating a file VAR_str.m and move the code for function VAR_str and you should get rid of the error, "Error: Function definitions are not permitted in this context."
##### 4 CommentsShowHide 3 older comments
Gareth on 21 Jul 2013
The entire script does not work so I have put up another one for SVAR which works for the original data but I need it to work for mine and so I guess I have to make a few tweaks. Can you please have a look for me and help me out as my deadline is fast approaching. Please help me

Sign in to comment.

### Categories

Find more on Cell Arrays in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!