MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# SVAR (sign restrictions)-- Index exceeds matrix dimensions error

Asked by Gareth on 19 Jul 2013

I want to do a SVAR sign restrictions on expenditure shock and revenue shock. my variables are expenditure, gdp, inflation, revenue, interest rate and stock price index.

I get the error below when I run the script

`     Index exceeds matrix dimensions.`

------------------------------------------------------------------------------

```   load SignRes
% order of the data
% 1) Expend
% 2) GDP
% 3) Inflation
% 4) Revenue
% 5) Interest Rate
% 6) Stock Price Index```
`   ddata=    [data(2:49,2),data(2:49,3),data(2:49,4),data(2:49,5),data(2:49,6),data(2:49,7)]`
```   n = size(ddata,2); % number of variables in the VAR
c = 1;          % number of deterministic variables
nlags = 4;      % number of lags of the VAR.
nimp = 20;      % number of impulses to be computed.
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(ddata,c,nlags);
Bet=inv(X'*X)*X'*Y;
res=Y-X*Bet;
Sigma=cov(res);```

% choleski decomposition

```   wimpu=[];
cimpu=[];
BB=companion(Bet,nlags,n,c);
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;```
```     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 decomposition implied by q

```        for j=1:n
for i = 1:ho
S(:,j,ho) = cimpu(:,:,i)*q(:,j);
end
end
invchol=inv(cimpu(:,:,1));
imp=q(:,1)'*invchol(:,1);```

%Check if the restrictions are satisfied for shock 1 and 2

```      z1=[squeeze(S(1,1,1:ho))'>=0 squeeze(S(2,1,1:ho))'<=0 ...
squeeze(S(1,2,1:ho))'>=0 squeeze(S(2,2,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(2,1,1:ho))'>=0 ...
squeeze(S(1,2,1:ho))'>=0 squeeze(S(2,2,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
mz3=[squeeze(S(1,1,1:ho))'>=0 squeeze(S(2,1,1:ho))'<=0 ...
squeeze(S(1,2,1:ho))'<=0 squeeze(S(2,2,1:ho))'<=0];
mzz3=sum(mz3);
if mzz3==size(mz3,2)
q(:,2)=-q(:,2);
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 or draws and accepted draws')
[dr gg]```
```   sir=sort(irf,4);
figure(1)
me11=squeeze(sir(1,1,:,fix(gg*0.50)));
up11=squeeze(sir(1,1,:,fix(gg*0.84)));
lo11=squeeze(sir(1,1,:,fix(gg*0.16)));
me21=squeeze(sir(2,1,:,fix(gg*0.50)));
up21=squeeze(sir(2,1,:,fix(gg*0.84)));
lo21=squeeze(sir(2,1,:,fix(gg*0.16)));
me12=squeeze(sir(1,2,:,fix(gg*0.50)));
up12=squeeze(sir(1,2,:,fix(gg*0.84)));
lo12=squeeze(sir(1,2,:,fix(gg*0.16)));
me22=squeeze(sir(2,2,:,fix(gg*0.50)));
up22=squeeze(sir(2,2,:,fix(gg*0.84)));
lo22=squeeze(sir(2,2,:,fix(gg*0.16)));```
```    subplot(2,2,1),plot([me11 up11 lo11]),axis tight;
title('RGDP')
ylabel('Supply shock')
subplot(2,2,2),plot([me21 up21 lo21]),axis tight;
title('Inflation')
subplot(2,2,3),plot([me12 up12 lo12]),axis tight;
ylabel('Demand shock')
subplot(2,2,4),plot([me22 up22 lo22]),axis tight;```

return