Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MethodError: Cannot convert an object of type Nothing to an object of type Int64 #90

Open
gpetrini opened this issue Aug 21, 2024 · 7 comments

Comments

@gpetrini
Copy link

Hi again,

I am trying to replicate a model in which I have a dynare script to depart from (I can paste it here if it helps). I notice that the lags/leads of some variables causes the error MethodError: Cannot convert an object of type Nothing to an object of type Int64. I tried in different julia version and the error still persists. I manage to replicate the error with only two equations, so I imagine that it became easier to debug:

Code that is not working:

@model Baseline begin
           ## 1 FOC labor demand
           lvar[0] - mcvar[0] + w[0] - y[0] = 0
           ##2 FOC capital utilization
           (1 - alph) * k[-1] = (1 - alph)*lvar[0] + mcvar[0] - pi[0] + tfp[0] + (-1 + alph - edp)*uvar[0] + (-1 + thetta) * a[-1]
       end

However, when I change k[-1] to k[0] and a[-1] to a[0], the error no longer raises. This error message appears for some (not for every) equation that has mixed time leads/lags.

Also, since I am trying to replicate a dynare script, do you have any suggestion on how to define those local-scope variables from dynare? (If I am not wrong, are those expressions starting with #). So far, I simply treated them as parameters defined by other parameters.

Thank you again for your help and sorry for opening another issue.

Best

@thorek1
Copy link
Owner

thorek1 commented Aug 22, 2024

I would need the whole model in order to have a look at what might be going on here.

@gpetrini
Copy link
Author

Ok, here is the code for the whole model.

Julia code

using MacroModelling

@model Baseline begin
    ## 1 FOC labor demand
    lvar[0] - mcvar[0] + w[0] - y[0] = 0
    ##2 FOC capital utilization
    (1 - alph) * k[-1] = (1 - alph)*lvar[0] + mcvar[0] - pi[0] + tfp[0] + (-1 + alph - edp)*uvar[0] + (-1 + thetta) * a[0]
    ## 3 FOC capital demand (definition of dividend)
    alph*betta*mcvar[0] + d[0]*(-1 + betta - gy)*KY*muin*muvar + (-(alph*betta) + (1 - betta + gy)*KY*muin*muvar)*pi[0] - alph*betta*uvar[0] + alph*betta*y[0] - alph*betta*k[-1] = 0
    ## 4 Production function
    (1 - alph)*lvar[0] + tfp[0] + alph*uvar[0] - y[0] + (-1 + thetta)*a[-1] + alph*k[-1] = 0
    ## 5 Marginal utility of consumption
    ctil[0]*(-1 - gy + h)*(-1 - gy + betta*h) + c[0]*((1 + gy)^2 + betta*h^2) - (1 + gy)*h*c[-1] - betta*(1 + gy)*h*c[1] = 0
    ## 6 Euler equation
    ctil[1] = ctil[0] - mub[0] - rfvar[0]
    ## 7 Capital arbitrage condition
    ctil[0]*(1 + gy) + (1 + gy)*pi[0] + (-1 - gy)*ctil[1] + (-1 + betta - gy)*d[1] - betta*pi[1] = 0
    ## 8 Optimal investment (price of capital)
    (-1 - betta/(1+gy))*ddac*invs[0] + pi[0] - pinv[0] + ddac*invs[-1] + betta/(1+gy)*ddac*invs[1] = 0
    ## 9 Capital law of motion
    betta*invs[0]*IY - betta*(1 + gy)*k[0]*KY - (1 + betta*(-1 + delta) + gy)*KY*uvar[0] - betta*(-1 + delta)*KY*k[-1] = 0
    ## 10 Taylor Rule
    -mpvar[0] + lvar[0]*phi_y*(-1 + rho_smooth) + phi_m*pivar[0]*(-1 + rho_smooth) + Rnomvar[0] - rho_smooth*Rnomvar[-1] = 0
    ## 11 Fisher equation
    rfvar[0] - Rnomvar[0] + pivar[1] = 0
    ## 12 Tech LOM
    chird[0]*sslabsr^rhoz*varphibar + labsr[0]*rhoz*sslabsr^rhoz*varphibar + (-1 - ga)*z[0] + (1 + ga)*z[-1] = 0
    ## 13 FOC RD
    chird[0] - ctil[0] + labsr[0]*(-1 + rhoz) - ws[0] - z[0] + ctil[1] + jz[1] + z[-1] = 0
    ## 14 Value of Unadopted Intermediate
    -((1 + ga)*jz[0]*JZY*ssa) - a[0]*betta*lam*(1 - obso)*ssz*VAY - (1 + ga)*labsaz[0]*ssa*sslabsaz*WSY - (1 + ga)*ssa*sslabsaz*ws[0]*WSY - ctil[0]*(1 + ga)*ssa*(JZY + sslabsaz*WSY) + lamvar[0]*ssa*(JZY*(1 + ga - betta*(1 - obso)) + (1 + ga)*sslabsaz*WSY) + (-((1 + ga)*JZY*ssa) + betta*lam*(1 - obso)*ssz*VAY - (1 + ga)*ssa*sslabsaz*WSY)*z[0] + (1 + ga)*ssa*(JZY + sslabsaz*WSY)*ctil[1] + ((1 + ga)*JZY*ssa - betta*lam*(1 - obso)*ssz*VAY + (1 + ga)*ssa*sslabsaz*WSY)*jz[1] + betta*lam*(1 - obso)*ssz*VAY*va[1] + (1 + ga)*ssa*(JZY + sslabsaz*WSY)*z[-1] = 0
    ## 15 Adoption Probability
    -lamvar[0] + labsaz[0]*rholambda = 0
    ## 16 Adopted LOM
    -(a[0]*(1 + ga)*ssa) + labsaz[0]*(ga + obso)*rholambda*ssa - (-1 + lam)*(1 - obso)*ssa*a[-1] + lam*(1 - obso)*ssz*z[-1] = 0
    ## 17 Value of Adopted Intermediate
    (-1 - ga)*mcvar[0]*(-1 + muin) + a[0]*betta*muin*muvar*(1 - obso)*VAY + betta*ctil[0]*muin*muvar*(1 - obso)*VAY + (1 + ga)*muin*muvar*va*VAY + (-1 - ga)*(-1 + muin)*y[0] - betta*muin*muvar*(1 - obso)*VAY*a[-1] - betta*muin*muvar*(1 - obso)*VAY*ctil[1] - betta*muin*muvar*(1 - obso)*VAY*va[1] = 0
    ## 18 FOC adoption
    -(a[0]*betta*lam*(1 - obso)*rholambda*ssz*VAY) - ctil[0]*(1 + ga)*ssa*sslabsaz*WSY + (1 + ga)*labsaz[0]*(-1 + rholambda)*ssa*sslabsaz*WSY - (1 + ga)*ssa*sslabsaz*ws[0]*WSY + (betta*lam*(1 - obso)*rholambda*ssz*VAY - (1 + ga)*ssa*sslabsaz*WSY)*z[0] + (1 + ga)*ssa*sslabsaz*WSY*ctil[1] + (-(betta*lam*(1 - obso)*rholambda*ssz*VAY) + (1 + ga)*ssa*sslabsaz*WSY)*jz[1] + betta*lam*(1 - obso)*rholambda*ssz*VAY*va[1] + (1 + ga)*ssa*sslabsaz*WSY*z[-1] = 0
    ## 19 Market clearing consumption goods
    c[0]*CY + g[0]*GY + invs[0]*IY + IY*pinv[0] - y[0] = 0
    ## 20 Market clearing research labor
    labs[0]*sslabs*ssz - labsr[0]*sslabsr*ssz + labsaz[0]*(-sslabs + sslabsr)*ssz + ssa*sslabsaz*a[-1] - ssa*sslabsaz*z[-1] = 0
    ## % wage setting
    (1+kw[0])*w[0]-1/(1+betta)*w[-1]-betta/(1+betta)*w[1]+kw[0]*(ctil[0]-phi*lvar[0])-indw/(1+betta)*pivar[-1]+(1+betta*indw)/(1+betta)*pivar[0]-(betta)/(1+betta)*pivar[1]-muwvar[0] = 0
    (1+kw[0])*ws[0]-1/(1+betta)*ws[-1]-betta/(1+betta)*ws[1]+kw[0]*(ctil[0]-phi*labs[0])-indw/(1+betta)*pivar[-1]+(1+betta*indw)/(1+betta)*pivar[0]-(betta)/(1+betta)*pivar[1] = 0
    ## philips curve
    pivar[0]=(indp/(1+betta*indp))*(pivar[-1])+(betta/(1+betta*indp))*(pivar[1])+((1-betta*epsp)*(1-epsp)/(epsp*(1+betta*indp)))*mcvar[0]+mu[0] = 0
    ## adoption and r&d expenditure
    rd_exp[0] = ws[0] + labsr[0]
    adopt_exp[0] = ws[0] + labsaz[0] + ssa/(ssz-ssa)*(z[-1]-a[-1]) = 0

    ## NOTE shocks dynamics
    tfp[0] - rhotfp*tfp[-1] - etfp[x]
    pinv[0] - rhopk*pinv[-1] - epinv[x]
    g[0] - rhog*g[-1] - eg[x]
    mub[0] - rhob*mub[-1] - eb[x]
    mu[0] - rhomu*mu[-1] - emu[x]
    muwvar[0] - rhow*muwvar[-1] - emuw[x]
    mpvar[0] - rhomp*mpvar[-1] - empvar[x]
    chird[0] - rhochi*chird[-1] - echi[x]

    ## NOTE measurement equations
    Dy[0]=gy*100+(y[0]-y[-1])
    Dc[0]=gy*100+(c[0]-c[-1])
    Di[0]=gy*100+(invs[0]-invs[-1])+(pinv[0]-pinv[-1])
    lvarobs[0]=lvar[0]
    Dw[0]=gy*100+w[0]-w[-1]
    Rnomobs[0] = Rnomvar[0]+((1-mubss)*(1+gy)/betta-1)*100
    pinfobs[0] = pivar[0]+piconst
    DrdNSF[0] = gy*4*100+rd_exp[0]-rd_exp[-4]
end

@parameters Baseline begin
    # liquidity premium in steady state 50 bps annually
    mubss = 0.005/4
    delta = 0.02
    GY = 0.16
    lam = 0.05
    muin = 1.18
    muvar = 1.1
    obso = 0.02
    rholambda = 0.925
    thetta = 1.35
    varphibar = 0.05
    rconst = 0.5
    piconst = 0.5
    constelab = 0.013
    ## NOTE estimated parameters (latter)
    std_eg = 2.5595
    std_epinv = 1.2712
    std_eb = 0.22225
    std_emu = 0.093091
    std_etfp = 0.48905
    std_empvar = 0.096492
    std_emuw = 0.28711
    std_echi = 2.2119
    rhotfp = 0.95092
    rhopk = 0.8999
    rhob = 0.92619
    rhomp = 0.47126
    rhomu = 0.40463
    rhog = 0.9681
    rhow = 0.27224
    rhochi = 0.80216
    rho_smooth = 0.82854
    phi_m = 1.5745
    phi_y = 0.38614
    phi = 2.7026
    ddac = 5.6386
    epsp = 0.9346
    epsw = 0.90568
    indp = 0.24118
    indw = 0.38198
    h = 0.48303
    edp = 4.0442
    rhoz = 0.37076
    alph = 0.20068
    gyest = 0.45317
    bettaest = 0.42122
    muwest = 0.15
    Rf_zlb = 100*(-1 + (1 + gyest/100)*(bettaest/100+1))

    ## NOTE: Local-scope variables in dynare
    gy = gyest/100
    betta = (1 + bettaest/100)^(-1)
    ssa = 1
    ssR = (1-mubss)*(1+gy)/betta
    KY = alph/(muin*muvar*(ssR-1+delta))
    IY = KY*(delta+gy)
    CY = 1 - GY - IY
    ga = -1 + (1 + gy)^((1 - alph)/(-1 + thetta))
    ssz = ((1 + ga + (-1 + lam)*(1 - obso))*ssa)/(lam*(1 - obso))
    sslabsr = ((ga + obso)/varphibar)^(1/rhoz)
    VAY = ((1 + ga)*(-1 + muin))/(muin*muvar*(1 + ga - betta*(1 - obso)))
    sslabsaz = ((-1 - ga + betta*(1 - obso))*rholambda*sslabsr^(1 - rhoz))/(betta*(-1 + rholambda)*varphibar)
    WSY = -((betta^2*lam*(1 - obso)*(-1 + rholambda)*sslabsr^(-1 + rhoz)*ssz*varphibar*VAY)/((1 + ga)*(1 + ga + betta*(1 - obso)*(-1 + lam - lam*rholambda))*ssa))
    JZY = -((betta*lam*(1 - obso)*(-1 + rholambda)*ssz*VAY)/((1 + ga + betta*(1 - obso)*(-1 + lam - lam*rholambda))*ssa))
    sslabs = sslabsaz + sslabsr - (ssa*sslabsaz)/ssz
    muw = muwest+1
    kw = (1-epsw)*(1-beta*epsw)/(epsw*(1+beta))/(1+phi/(1+(1/muw)))
end

Dynare code (for reference)

var c invs Rnomvar w lvar mcvar y k pi uvar a d ctil rfvar pivar z labsr ws jz labsaz lamvar va labs tfp mub pinv mpvar chird g mu muwvar Dy Di pinfobs Dc lvarobs DrdNSF Dw Rnomobs rd_exp adopt_exp;

parameters alpha ddac delta edp GY h lam muin muvar obso phi_y phi_m rholambda rho_smooth rhoz thetta varphibar betaest gyest  phi muwest indw indp epsp epsw rhotfp rhopk rhow rhob rhog rhomu rhomp rhochi std_eb std_eg std_emu std_empvar std_epinv std_etfp std_echi std_emuw rconst piconst constelab Rf_zlb mubss;

varexo eb eg etfp epinv emu emuw echi empvar;

set_param_value('alpha',alpha);
set_param_value('ddac',ddac);
set_param_value('delta',delta);
set_param_value('edp',edp);
set_param_value('GY',GY);
set_param_value('h',h);
set_param_value('lam',lam);
set_param_value('muin',muin);
set_param_value('muvar',muvar);
set_param_value('obso',obso);
set_param_value('phi_y',phi_y);
set_param_value('phi_m',phi_m);
set_param_value('rholambda',rholambda);
set_param_value('rho_smooth',rho_smooth);
set_param_value('rhoz',rhoz);
set_param_value('thetta',thetta);
set_param_value('varphibar',varphibar);
set_param_value('betaest',betaest);
set_param_value('gyest',gyest);
set_param_value('phi',phi);
set_param_value('indw',indw);
set_param_value('indp',indp);
set_param_value('epsp',epsp);
set_param_value('epsw',epsw);
set_param_value('rhotfp',rhotfp);
set_param_value('rhopk',rhopk);
set_param_value('rhow',rhow);
set_param_value('rhob',rhob);
set_param_value('rhog',rhog);
set_param_value('rhomu',rhomu);
set_param_value('rhomp',rhomp);
set_param_value('rhochi',rhochi);
set_param_value('std_eb',std_eb);
set_param_value('std_eg',std_eg);
set_param_value('std_emu',std_emu);
set_param_value('std_empvar',std_empvar);
set_param_value('std_epinv',std_epinv);
set_param_value('std_etfp',std_etfp);
set_param_value('std_echi',std_echi);
set_param_value('std_emuw',std_emuw);
set_param_value('rconst',rconst);
set_param_value('piconst',piconst);
set_param_value('constelab',constelab);
set_param_value('muwest',muwest);
set_param_value('mubss',mubss);

Rf_zlb=100*(-1 + (1 + gyest/100)*(betaest/100+1));

model(linear);

#gy = gyest/100;
#beta = (1 + betaest/100)^(-1);
#ssa = 1;
#ssR = (1-mubss)*(1+gy)/beta;
%#KY =(alpha*beta)/((1 + beta*(-1 + delta) + gy)*muin*muvar);
#KY = alpha/(muin*muvar*(ssR-1+delta)); 
%#IY = (alpha*beta*(delta + gy))/((1 + beta*(-1 + delta) + gy)*muin*muvar);
#IY = KY*(delta+gy);
#CY = 1 - GY - IY;
#ga = -1 + (1 + gy)^((1 - alpha)/(-1 + thetta));
#ssz = ((1 + ga + (-1 + lam)*(1 - obso))*ssa)/(lam*(1 - obso));
#sslabsr = ((ga + obso)/varphibar)^(1/rhoz);
#VAY = ((1 + ga)*(-1 + muin))/(muin*muvar*(1 + ga - beta*(1 - obso)));
#sslabsaz = ((-1 - ga + beta*(1 - obso))*rholambda*sslabsr^(1 - rhoz))/(beta*(-1 + rholambda)*varphibar);
#WSY = -((beta^2*lam*(1 - obso)*(-1 + rholambda)*sslabsr^(-1 + rhoz)*ssz*varphibar*VAY)/((1 + ga)*(1 + ga + beta*(1 - obso)*(-1 + lam - lam*rholambda))*ssa));
#JZY = -((beta*lam*(1 - obso)*(-1 + rholambda)*ssz*VAY)/((1 + ga + beta*(1 - obso)*(-1 + lam - lam*rholambda))*ssa));
#sslabs = sslabsaz + sslabsr - (ssa*sslabsaz)/ssz;
#muw = muwest+1;

%1 FOC labor demand
lvar - mcvar + w - y;

%2 FOC capital utilization
(1 - alpha)*lvar + mcvar - pi + tfp + (-1 + alpha - edp)*uvar + (-1 + thetta)*a(-1) + (-1 + alpha)*k(-1);

%3 FOC capital demand (definition of dividend)
alpha*beta*mcvar + d*(-1 + beta - gy)*KY*muin*muvar + (-(alpha*beta) + (1 - beta + gy)*KY*muin*muvar)*pi - alpha*beta*uvar + alpha*beta*y - alpha*beta*k(-1);

%4 Production function
(1 - alpha)*lvar + tfp + alpha*uvar - y + (-1 + thetta)*a(-1) + alpha*k(-1);

%5 Marginal utility of consumption
ctil*(-1 - gy + h)*(-1 - gy + beta*h) + c*((1 + gy)^2 + beta*h^2) - (1 + gy)*h*c(-1) - beta*(1 + gy)*h*c(+1);

%6 Euler equation
ctil - mub - rfvar - ctil(+1);

%7 Capital arbitrage condition
ctil*(1 + gy) + (1 + gy)*pi + (-1 - gy)*ctil(+1) + (-1 + beta - gy)*d(+1) - beta*pi(+1);

%8 Optimal investment (price of capital)
(-1 - beta/(1+gy))*ddac*invs + pi - pinv + ddac*invs(-1) + beta/(1+gy)*ddac*invs(+1);

%9 Capital law of motion
beta*invs*IY - beta*(1 + gy)*k*KY - (1 + beta*(-1 + delta) + gy)*KY*uvar - beta*(-1 + delta)*KY*k(-1);

%10 Taylor Rule
-mpvar + lvar*phi_y*(-1 + rho_smooth) + phi_m*pivar*(-1 + rho_smooth) + Rnomvar - rho_smooth*Rnomvar(-1);

%11 Fisher equation
rfvar - Rnomvar + pivar(+1);

%12 Tech LOM
chird*sslabsr^rhoz*varphibar + labsr*rhoz*sslabsr^rhoz*varphibar + (-1 - ga)*z + (1 + ga)*z(-1);

%13 FOC RD
chird - ctil + labsr*(-1 + rhoz) - ws - z + ctil(+1) + jz(+1) + z(-1);

%14 Value of Unadopted Intermediate
-((1 + ga)*jz*JZY*ssa) - a*beta*lam*(1 - obso)*ssz*VAY - (1 + ga)*labsaz*ssa*sslabsaz*WSY - (1 + ga)*ssa*sslabsaz*ws*WSY - ctil*(1 + ga)*ssa*(JZY + sslabsaz*WSY) + lamvar*ssa*(JZY*(1 + ga - beta*(1 - obso)) + (1 + ga)*sslabsaz*WSY) + (-((1 + ga)*JZY*ssa) + beta*lam*(1 - obso)*ssz*VAY - (1 + ga)*ssa*sslabsaz*WSY)*z + (1 + ga)*ssa*(JZY + sslabsaz*WSY)*ctil(+1) + ((1 + ga)*JZY*ssa - beta*lam*(1 - obso)*ssz*VAY + (1 + ga)*ssa*sslabsaz*WSY)*jz(+1) + beta*lam*(1 - obso)*ssz*VAY*va(+1) + (1 + ga)*ssa*(JZY + sslabsaz*WSY)*z(-1);

%15 Adoption Probability
-lamvar + labsaz*rholambda;

%16 Adopted LOM
-(a*(1 + ga)*ssa) + labsaz*(ga + obso)*rholambda*ssa - (-1 + lam)*(1 - obso)*ssa*a(-1) + lam*(1 - obso)*ssz*z(-1);

%17 Value of Adopted Intermediate
(-1 - ga)*mcvar*(-1 + muin) + a*beta*muin*muvar*(1 - obso)*VAY + beta*ctil*muin*muvar*(1 - obso)*VAY + (1 + ga)*muin*muvar*va*VAY + (-1 - ga)*(-1 + muin)*y - beta*muin*muvar*(1 - obso)*VAY*a(-1) - beta*muin*muvar*(1 - obso)*VAY*ctil(+1) - beta*muin*muvar*(1 - obso)*VAY*va(+1);

%18 FOC adoption
-(a*beta*lam*(1 - obso)*rholambda*ssz*VAY) - ctil*(1 + ga)*ssa*sslabsaz*WSY + (1 + ga)*labsaz*(-1 + rholambda)*ssa*sslabsaz*WSY - (1 + ga)*ssa*sslabsaz*ws*WSY + (beta*lam*(1 - obso)*rholambda*ssz*VAY - (1 + ga)*ssa*sslabsaz*WSY)*z + (1 + ga)*ssa*sslabsaz*WSY*ctil(+1) + (-(beta*lam*(1 - obso)*rholambda*ssz*VAY) + (1 + ga)*ssa*sslabsaz*WSY)*jz(+1) + beta*lam*(1 - obso)*rholambda*ssz*VAY*va(+1) + (1 + ga)*ssa*sslabsaz*WSY*z(-1);

%19 Market clearing consumption goods
c*CY + g*GY + invs*IY + IY*pinv - y;

%20 Market clearing research labor
labs*sslabs*ssz - labsr*sslabsr*ssz + labsaz*(-sslabs + sslabsr)*ssz + ssa*sslabsaz*a(-1) - ssa*sslabsaz*z(-1);

#kw=(1-epsw)*(1-beta*epsw)/(epsw*(1+beta))/(1+phi/(1+(1/muw)));
% wage setting
(1+kw)*w-1/(1+beta)*w(-1)-beta/(1+beta)*w(+1)+kw*(ctil-phi*lvar)-indw/(1+beta)*pivar(-1)+(1+beta*indw)/(1+beta)*pivar-(beta)/(1+beta)*pivar(+1)-muwvar;
(1+kw)*ws-1/(1+beta)*ws(-1)-beta/(1+beta)*ws(+1)+kw*(ctil-phi*labs)-indw/(1+beta)*pivar(-1)+(1+beta*indw)/(1+beta)*pivar-(beta)/(1+beta)*pivar(+1);

% philips curve
pivar=(indp/(1+beta*indp))*(pivar(-1))+(beta/(1+beta*indp))*(pivar(+1))+((1-beta*epsp)*(1-epsp)/(epsp*(1+beta*indp)))*mcvar+mu;   

% adoption and r&d expenditure
rd_exp = ws + labsr;
adopt_exp = ws + labsaz + ssa/(ssz-ssa)*(z(-1)-a(-1));

% shocks dynamics
tfp-rhotfp*tfp(-1)-etfp;    
pinv-rhopk*pinv(-1)-epinv;
g-rhog*g(-1)-eg;
mub-rhob*mub(-1)-eb;
mu-rhomu*mu(-1)-emu;
muwvar-rhow*muwvar(-1)-emuw;
mpvar-rhomp*mpvar(-1)-empvar;
chird-rhochi*chird(-1)-echi;

% measurement equations
Dy=gy*100+(y-y(-1));
Dc=gy*100+(c-c(-1));
Di=gy*100+(invs-invs(-1))+(pinv-pinv(-1));
lvarobs=lvar;
Dw=gy*100+w-w(-1);
Rnomobs = Rnomvar+((1-mubss)*(1+gy)/beta-1)*100;
pinfobs=pivar+piconst;
DrdNSF=gy*4*100+rd_exp-rd_exp(-4);

end;


shocks;
var eg; stderr std_eg;
var eb; stderr std_eb;
var emu; stderr std_emu;
var etfp; stderr std_etfp;
var empvar; stderr std_empvar;
var epinv; stderr std_epinv;
var emuw; stderr std_emuw;
var echi; stderr std_echi;
end;

varobs  Dw Dc Dy lvarobs Di Rnomobs pinfobs DrdNSF;
stoch_simul(order=1,irf=100,nograph);

@thorek1
Copy link
Owner

thorek1 commented Aug 22, 2024

The source of the error you got is still unknown to me but essentially the system of equations is not well defined. The error handling in the model and parameter blocks is still poor and I will try to improve the errors so that they are more informative.

In any case, you don't need to worry about that if you simply want to translate the model. You can use the function: translate_mod_file("name_of_mod_file.mod"). It should get you most of the way and in this case you get the # prefixed lines substituted into the model equations. I would recommend to remove the # prefix in the dynare file and run translate_mod_file("name_of_mod_file.mod")

What you would get then is the following model (which works):

@model model_translated_from_dynare begin
	lvar[0] - mcvar[0] + w[0] - y[0] = 0

	mcvar[0] + lvar[0] * (1 - alpha) - pi[0] + tfp[0] + (alpha - 1 - edp) * uvar[0] + (thetta - 1) * a[-1] + (alpha - 1) * k[-1] = 0

	mcvar[0] * alpha * beta + d[0] * (beta - 1 - gy) * KY * muin * muvar + pi[0] * (muvar * muin * KY * (gy + 1 - beta) - alpha * beta) - uvar[0] * alpha * beta + y[0] * alpha * beta - k[-1] * alpha * beta = 0

	(thetta - 1) * a[-1] + lvar[0] * (1 - alpha) + tfp[0] + alpha * uvar[0] - y[0] + alpha * k[-1] = 0

	ctil[0] * (( - 1) - gy + h) * (( - 1) - gy + beta * h) + c[0] * ((1 + gy) ^ 2 + beta * h ^ 2) - h * (1 + gy) * c[-1] - h * beta * (1 + gy) * c[1] = 0

	ctil[0] - mub[0] - rfvar[0] - ctil[1] = 0

	ctil[0] * (1 + gy) + pi[0] * (1 + gy) + (( - 1) - gy) * ctil[1] + (beta - 1 - gy) * d[1] - beta * pi[1] = 0

	pi[0] + (( - 1) - beta / (1 + gy)) * ddac * invs[0] - pinv[0] + ddac * invs[-1] + beta / (1 + gy) * ddac * invs[1] = 0

	beta * invs[0] * IY - KY * beta * (1 + gy) * k[0] - uvar[0] * KY * (gy + 1 + beta * (delta - 1)) - k[-1] * KY * beta * (delta - 1) = 0

	lvar[0] * phi_y * (rho_smooth - 1) - mpvar[0] + (rho_smooth - 1) * phi_m * pivar[0] + Rnomvar[0] - rho_smooth * Rnomvar[-1] = 0

	rfvar[0] - Rnomvar[0] + pivar[1] = 0

	chird[0] * sslabsr ^ rhoz * varphibar + varphibar * sslabsr ^ rhoz * rhoz * labsr[0] + (( - 1) - ga) * z[0] + (1 + ga) * z[-1] = 0

	z[-1] + ctil[1] + chird[0] - ctil[0] + labsr[0] * (rhoz - 1) - ws[0] - z[0] + jz[1] = 0

	( - ((1 + ga) * jz[0] * JZY * ssa)) - beta * a[0] * lam * (1 - obso) * ssz * VAY - ssa * (1 + ga) * labsaz[0] * sslabsaz * WSY - WSY * ws[0] * sslabsaz * (1 + ga) * ssa - ssa * ctil[0] * (1 + ga) * (JZY + sslabsaz * WSY) + ssa * lamvar[0] * (JZY * (1 + ga - beta * (1 - obso)) + WSY * (1 + ga) * sslabsaz) + z[0] * (VAY * ssz * (1 - obso) * beta * lam - ssa * (1 + ga) * JZY - WSY * sslabsaz * (1 + ga) * ssa) + ctil[1] * (1 + ga) * ssa * (JZY + sslabsaz * WSY) + jz[1] * (WSY * sslabsaz * (1 + ga) * ssa + ssa * (1 + ga) * JZY - VAY * ssz * (1 - obso) * beta * lam) + VAY * ssz * (1 - obso) * beta * lam * va[1] + z[-1] * (1 + ga) * ssa * (JZY + sslabsaz * WSY) = 0

	labsaz[0] * rholambda - lamvar[0] = 0

	ssa * rholambda * labsaz[0] * (ga + obso) - ssa * (1 + ga) * a[0] - a[-1] * ssa * (1 - obso) * (lam - 1) + z[-1] * ssz * lam * (1 - obso) = 0

	mcvar[0] * (( - 1) - ga) * (muin - 1) + VAY * (1 - obso) * muvar * muin * beta * a[0] + VAY * (1 - obso) * muvar * muin * beta * ctil[0] + VAY * muvar * muin * (1 + ga) * va[0] + y[0] * (( - 1) - ga) * (muin - 1) - a[-1] * VAY * (1 - obso) * muvar * beta * muin - ctil[1] * VAY * (1 - obso) * muvar * beta * muin - va[1] * VAY * (1 - obso) * muvar * beta * muin = 0

	( - (VAY * ssz * beta * a[0] * lam * (1 - obso) * rholambda)) - WSY * sslabsaz * ssa * ctil[0] * (1 + ga) + WSY * sslabsaz * ssa * (1 + ga) * labsaz[0] * (rholambda - 1) - WSY * ws[0] * sslabsaz * (1 + ga) * ssa + z[0] * (VAY * ssz * (1 - obso) * beta * lam * rholambda - WSY * sslabsaz * (1 + ga) * ssa) + ctil[1] * WSY * sslabsaz * (1 + ga) * ssa + jz[1] * (WSY * sslabsaz * (1 + ga) * ssa - VAY * ssz * (1 - obso) * beta * lam * rholambda) + va[1] * VAY * ssz * (1 - obso) * beta * lam * rholambda + z[-1] * WSY * sslabsaz * (1 + ga) * ssa = 0

	c[0] * CY + g[0] * GY + invs[0] * IY + pinv[0] * IY - y[0] = 0

	ssz * labs[0] * sslabs - ssz * sslabsr * labsr[0] + ssz * labsaz[0] * (sslabsr - sslabs) + a[-1] * ssa * sslabsaz - z[-1] * ssa * sslabsaz = 0

	pivar[0] * (1 + beta * indw) / (1 + beta) + w[0] * (1 + (1 - epsw) * (1 - beta * epsw) / (epsw * (1 + beta)) / (1 + phi / (1 + 1 / muw))) - 1 / (1 + beta) * w[-1] - beta / (1 + beta) * w[1] + (1 - epsw) * (1 - beta * epsw) / (epsw * (1 + beta)) / (1 + phi / (1 + 1 / muw)) * (ctil[0] - lvar[0] * phi) - indw / (1 + beta) * pivar[-1] - pivar[1] * beta / (1 + beta) - muwvar[0] = 0

	pivar[0] * (1 + beta * indw) / (1 + beta) + ws[0] * (1 + (1 - epsw) * (1 - beta * epsw) / (epsw * (1 + beta)) / (1 + phi / (1 + 1 / muw))) - 1 / (1 + beta) * ws[-1] - beta / (1 + beta) * ws[1] + (1 - epsw) * (1 - beta * epsw) / (epsw * (1 + beta)) / (1 + phi / (1 + 1 / muw)) * (ctil[0] - labs[0] * phi) - indw / (1 + beta) * pivar[-1] - pivar[1] * beta / (1 + beta) = 0

	pivar[0] = pivar[-1] * indp / (1 + beta * indp) + pivar[1] * beta / (1 + beta * indp) + mcvar[0] * (1 - beta * epsp) * (1 - epsp) / ((1 + beta * indp) * epsp) + mu[0]

	rd_exp[0] = labsr[0] + ws[0]

	adopt_exp[0] = ws[0] + labsaz[0] + ssa / (ssz - ssa) * (z[-1] - a[-1])

	tfp[0] - rhotfp * tfp[-1] - etfp[x] = 0

	pinv[0] - rhopk * pinv[-1] - epinv[x] = 0

	g[0] - rhog * g[-1] - eg[x] = 0

	mub[0] - rhob * mub[-1] - eb[x] = 0

	mu[0] - rhomu * mu[-1] - emu[x] = 0

	muwvar[0] - rhow * muwvar[-1] - emuw[x] = 0

	mpvar[0] - rhomp * mpvar[-1] - empvar[x] = 0

	chird[0] - rhochi * chird[-1] - echi[x] = 0

	Dy[0] = gy * 100 + y[0] - y[-1]

	Dc[0] = gy * 100 + c[0] - c[-1]

	Di[0] = gy * 100 + invs[0] - invs[-1] + pinv[0] - pinv[-1]

	lvarobs[0] = lvar[0]

	Dw[0] = w[0] + gy * 100 - w[-1]

	Rnomobs[0] = Rnomvar[0] + 100 * ((1 + gy) * (1 - mubss) / beta - 1)

	pinfobs[0] = pivar[0] + piconst

	DrdNSF[0] = rd_exp[0] + 100 * gy * 4 - AUX_ENDO_LAG_39_3[-1]

	AUX_ENDO_LAG_39_1[0] = rd_exp[-1]

	AUX_ENDO_LAG_39_2[0] = AUX_ENDO_LAG_39_1[-1]

	AUX_ENDO_LAG_39_3[0] = AUX_ENDO_LAG_39_2[-1]

end



@parameters model_translated_from_dynare  begin
    # liquidity premium in steady state 50 bps annually
    mubss = 0.005/4
    delta = 0.02
    GY = 0.16
    lam = 0.05
    muin = 1.18
    muvar = 1.1
    obso = 0.02
    rholambda = 0.925
    thetta = 1.35
    varphibar = 0.05
    rconst = 0.5
    piconst = 0.5
    constelab = 0.013
    ## NOTE estimated parameters (latter)
    std_eg = 2.5595
    std_epinv = 1.2712
    std_eb = 0.22225
    std_emu = 0.093091
    std_etfp = 0.48905
    std_empvar = 0.096492
    std_emuw = 0.28711
    std_echi = 2.2119
    rhotfp = 0.95092
    rhopk = 0.8999
    rhob = 0.92619
    rhomp = 0.47126
    rhomu = 0.40463
    rhog = 0.9681
    rhow = 0.27224
    rhochi = 0.80216
    rho_smooth = 0.82854
    phi_m = 1.5745
    phi_y = 0.38614
    phi = 2.7026
    ddac = 5.6386
    epsp = 0.9346
    epsw = 0.90568
    indp = 0.24118
    indw = 0.38198
    h = 0.48303
    edp = 4.0442
    rhoz = 0.37076
    alpha = 0.20068
    gyest = 0.45317
    betaest = 0.42122
    muwest = 0.15
    Rf_zlb = 100*(-1 + (1 + gyest/100)*(betaest/100+1))

    ## NOTE: Local-scope variables in dynare
    gy = gyest/100
    beta = (1 + betaest/100)^(-1)
    ssa = 1
    ssR = (1-mubss)*(1+gy)/beta
    KY = alpha/(muin*muvar*(ssR-1+delta))
    IY = KY*(delta+gy)
    CY = 1 - GY - IY
    ga = -1 + (1 + gy)^((1 - alpha)/(-1 + thetta))
    ssz = ((1 + ga + (-1 + lam)*(1 - obso))*ssa)/(lam*(1 - obso))
    sslabsr = ((ga + obso)/varphibar)^(1/rhoz)
    VAY = ((1 + ga)*(-1 + muin))/(muin*muvar*(1 + ga - beta*(1 - obso)))
    sslabsaz = ((-1 - ga + beta*(1 - obso))*rholambda*sslabsr^(1 - rhoz))/(beta*(-1 + rholambda)*varphibar)
    WSY = -((beta^2*lam*(1 - obso)*(-1 + rholambda)*sslabsr^(-1 + rhoz)*ssz*varphibar*VAY)/((1 + ga)*(1 + ga + beta*(1 - obso)*(-1 + lam - lam*rholambda))*ssa))
    JZY = -((beta*lam*(1 - obso)*(-1 + rholambda)*ssz*VAY)/((1 + ga + beta*(1 - obso)*(-1 + lam - lam*rholambda))*ssa))
    sslabs = sslabsaz + sslabsr - (ssa*sslabsaz)/ssz
    muw = muwest+1
    kw = (1-epsw)*(1-beta*epsw)/(epsw*(1+beta))/(1+phi/(1+(1/muw)))
end

The difference in equations between what you posted and what I posted should explain the error.

PS: Also with this model there is an extensive set of equations defining parameters (see last part in parameters block) which appears to lead to long run times for the derivatives. I will fix that in the next release.

@gpetrini
Copy link
Author

Thanks a lot!

Using this function worked. I'll inspect the two scripts to spot the differences between them.
A last question. How can I import the parameters into the @parameters macro? Is that possible?

I really appreciate your help!

@thorek1
Copy link
Owner

thorek1 commented Aug 22, 2024

The function will catch the plain vanilla case: a=1 but fails for setparams(...) and if there are any other matlab calls. I don't see a good way of supporting this. For now I would recommend rewriting the parameter definitions in the dynare mod file such that it is only: a=1; b=a/2;... statements

@gpetrini
Copy link
Author

I meant importing the parameter values using MacroModelling after the dynare translation.

Assume that I have a dictionary with the parameters as bellow:

params = Dict{Symbol, Float64}()

params[:mubss] = 0.005/4
params[:delta] = 0.02
params[:GY] = 0.16
params[:lam] = 0.05
params[:muin] = 1.18
params[:muvar] = 1.1
params[:obso] = 0.02
params[:rholambda] = 0.925
params[:thetta] = 1.35
params[:varphibar] = 0.05
params[:rconst] = 0.5
params[:piconst] = 0.5
params[:constelab] = 0.013
params[:std_eg] = 2.5595
params[:std_epinv] = 1.2712
params[:std_eb] = 0.22225
params[:std_emu] = 0.093091
params[:std_etfp] = 0.48905
params[:std_empvar] = 0.096492
params[:std_emuw] = 0.28711
params[:std_echi] = 2.2119
params[:rhotfp] = 0.95092
params[:rhopk] = 0.8999
params[:rhob] = 0.92619
params[:rhomp] = 0.47126
params[:rhomu] = 0.40463
params[:rhog] = 0.9681
params[:rhow] = 0.27224
params[:rhochi] = 0.80216
params[:rho_smooth] = 0.82854
params[:phi_m] = 1.5745
params[:phi_y] = 0.38614
params[:phi] = 2.7026
params[:ddac] = 5.6386
params[:epsp] = 0.9346
params[:epsw] = 0.90568
params[:indp] = 0.24118
params[:indw] = 0.38198
params[:h] = 0.48303
params[:edp] = 4.0442
params[:rhoz] = 0.37076
params[:alpha] = 0.20068
params[:gyest] = 0.45317
params[:betaest] = 0.42122
params[:muwest] = 0.15
params[:Rf_zlb] = 100*(-1 + (1 + params[:gyest]/100)*(params[:betaest]/100+1))

params[:gy] = params[:gyest]/100
params[:beta] = (1 + params[:betaest]/100)^(-1)
params[:ssa] = 1
params[:ssR] = (1-params[:mubss])*(1+params[:gy])/params[:beta]
params[:KY] = params[:alpha]/(params[:muin]*params[:muvar]*(params[:ssR]-1+params[:delta]))
params[:IY] = params[:KY]*(params[:delta]+params[:gy])
params[:CY] = 1 - params[:GY] - params[:IY]
params[:ga] = -1 + (1 + params[:gy])^((1 - params[:alpha])/(-1 + params[:thetta]))
params[:ssz] = ((1 + params[:ga] + (-1 + params[:lam])*(1 - params[:obso]))*params[:ssa])/(params[:lam]*(1 - params[:obso]))
params[:sslabsr] = ((params[:ga] + params[:obso])/params[:varphibar])^(1/params[:rhoz])
params[:VAY] = ((1 + params[:ga])*(-1 + params[:muin]))/(params[:muin]*params[:muvar]*(1 + params[:ga] - params[:beta]*(1 - params[:obso])))
params[:sslabsaz] = ((-1 - params[:ga] + params[:beta]*(1 - params[:obso]))*params[:rholambda]*params[:sslabsr]^(1 - params[:rhoz]))/(params[:beta]*(-1 + params[:rholambda])*params[:varphibar])
params[:WSY] = -((params[:beta]^2*params[:lam]*(1 - params[:obso])*(-1 + params[:rholambda])*params[:sslabsr]^(-1 + params[:rhoz])*params[:ssz]*params[:varphibar]*params[:VAY])/((1 + params[:ga])*(1 + params[:ga] + params[:beta]*(1 - params[:obso])*(-1 + params[:lam] - params[:lam]*params[:rholambda]))*params[:ssa]))
params[:JZY] = -((params[:beta]*params[:lam]*(1 - params[:obso])*(-1 + params[:rholambda])*params[:ssz]*params[:VAY])/((1 + params[:ga] + params[:beta]*(1 - params[:obso])*(-1 + params[:lam] - params[:lam]*params[:rholambda]))*params[:ssa]))
params[:sslabs] = params[:sslabsaz] + params[:sslabsr] - (params[:ssa]*params[:sslabsaz])/params[:ssz]
params[:muw] = params[:muwest]+1
params[:kw] = (1-params[:epsw])*(1-params[:beta]*params[:epsw])/(params[:epsw]*(1+params[:beta]))/(1+params[:phi]/(1+(1/params[:muw])))

By using this, I avoid hardcoding the parameters by hand (which would make the evaluation faster). The next stage is to feed other models with a bayesian estimation using a dictionary (or other object type). In the matlab code, the authors simply populate the global scope with the actual values of the parameters and continue the simulation. I was thinking in doing a similar stuff.

SInce this is a different topic, feel free to close the issue. If I came up with an idea, I'll write it down.

Thanks!

@thorek1
Copy link
Owner

thorek1 commented Aug 22, 2024

In the parameters block you have to define them using the name used in the model block and providing a value or relationship so that only using this information a steady state can be found.

For estimation and also for simply
plotting IRFs with different parameter values (or generating any other output) you don't need to rerun the model and parameter blocks. It is this second evaluation that is substantially faster compared to dynare.

Check out the tutorials for how to work with models and how to estimate them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants