0

I have a problem in the simulation loop. The problem is that new matrices don't have the same length as old variables. I'm not sure if the problem comes from the initial values of the variables or from the simulation loop.

I'm using julia v1.0.3

using Distributions

using DataFrames

n=5000
t=15000
gamma   = 2;        
delta   = 0.057;   
betta   = 0.99;     
alfa    = 0.47;     
miz     = 1;         
roz     = 0.75;     
sigmaz  = 0.013;    
phi     = 0.05;     
rok     = 0.7;     
mie     = 1;        
roe     = 0.7;      
sigmae1 = 0.05;     
sigmae2 = 0.1;     
roez    = 0.3;      
lambda  = 0.8;      
tau     = 0;        
ass     = 31.7838916986589918973; 
kss     = lambda*ass;              

vtheta1 =0.1; 
vtheta2 =0.2;
vtheta3 =0.3; 
vtheta4 =0.4; 
vtheta5 =0.5; 

n_lambda = trunc(Int, lambda * n)

eshocks1= rand(Normal(0.0,sigmae1),n_lambda ,t); 
eshocks2= rand(Normal(0.0,sigmae2),n-n_lambda,t); 
zshocks1= rand(Normal(0.0, sigmaz),1 , t); 

# Variables:
a=ones(t,n);
c=ones(t,n); 
y= ones(t,n);
k=ones(t); 
w=ones(t); 
r=ones(t); 
z=ones(t);
l=ones(t);
e=ones(t,n);

Initial values of the variables:

a[1:2,1:n_lambda].=ass;
a[1:t,(n_lambda+1):n].=0;
k[1:2].=lambda*ass;
c[1:n_lambda,1].=(ass^alfa)*(1-tau*alfa)-delta*ass;

Loop of simulation

for i = 2:(t-1)
    k[i]=lambda*mean(a[i,1:n_lambda]);         
    z[i]=(1-roz)+roz*z[i-1]+zshocks1[i];
    l[i]=1+(1-lambda)*roez*(z[i]-1)/(1-roe);
    w[i]=(1-alfa)*z[i]*((k[i])^(alfa))*l[i]^(-alfa);
    r[i]=alfa*z[i]*((k[i])^(alfa-1))*l[i]^(1-alfa);
    for j=1:n_lambda
        e[i,j]=(1-roe)+roe*e[i-1,j]+eshocks1[j,i];
        a[i+1,j]=(vtheta1)+(vtheta2)*a[i,j]+(vtheta3)*e[i,j]+(vtheta4)*z[i]+(vtheta5)*k[i];
        y[i,j]=(1-tau)*r[i]*a[i,j]+w[i]*e[i,j]; 
    end 

    for m=1:(n-n_lambda)
        e[i,m+n_lambda]=(1-roe)+roe*e[i-1,m+n_lambda]+roez*(z[i]-1)+eshocks2[m,i];
        c[i,m+n_lambda]=w[i]*e[i,m+n_lambda]+tau*r[i]*k[i]/(1-lambda);
        y[i,m+n_lambda]=c[i,m+n_lambda];
    end
end

Preparing the variables for the calculation of the Gini index

ysort = ones(t,n);
for i = 1:(t-1) 
    ysort[i,:]=sort(y[i,:]); 
end

Calculation of the Gini index of income

giniY=ones(t);
for i=1:(t-1)
    sum1=0;
    sum2=0;
    for j=1:n
        sum1=(n+1-j)*ysort[i,j]+sum1;
        sum2=ysort[i,j]+sum2;
    end
    giniY[i]=(n+1-2*(sum1/sum2))/n;
end

tab = DataFrame()
tab[ :periodos] = 15000
tab[ :familias] = 5000
tab[ :giniY]= giniY

return tab

ArgumentError: New columns must have the same length as old columns

Stacktrace: [1] insert_single_column!(::DataFrame, ::Array{Float64,1}, ::Symbol) at /home/jrun/.julia/packages/DataFrames/IKMvt/src/dataframe/dataframe.jl:366 [2] setindex!(::DataFrame, ::Array{Float64,1}, ::Symbol) at /home/jrun/.julia/packages/DataFrames/IKMvt/src/dataframe/dataframe.jl:420 [3] top-level scope at In[1]:104

1 Answers1

2

This is the way to create tab DataFrame you want (assuming you want periodos and familias columns to contain constant values):

tab = DataFrame(periodos=15000, familias=5000, giniY=giniY)
Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107