0

我在模拟循环中有问题。问题是新矩阵的长度与旧变量的长度不同。我不确定问题是来自变量的初始值还是来自模拟循环。

我正在使用 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);

变量的初始值:

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;

模拟循环

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

准备用于计算基尼指数的变量

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

收入基尼指数的计算

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:新列的长度必须与旧列相同

Stacktrace: [1] insert_single_column!(::DataFrame, ::Array{Float64,1}, ::Symbol) 在 /home/jrun/.julia/packages/DataFrames/IKMvt/src/dataframe/dataframe.jl:366 [ 2] setindex!(::DataFrame, ::Array{Float64,1}, ::Symbol) 在 /home/jrun/.julia/packages/DataFrames/IKMvt/src/dataframe/dataframe.jl:420 [3] 顶部In[1]:104 的级别范围

4

1 回答 1

2

这是创建tab DataFrame您想要的方式(假设您希望periodosfamilias包含常量值):

tab = DataFrame(periodos=15000, familias=5000, giniY=giniY)
于 2019-08-21T01:01:03.347 回答