
//*********************************************************
/* Endo. Variables */
 
var mcf zcapf rkf kf pkf muf cf invef yf labf pinff wf pf emplf rrf effortf
    rf mc zcap rk k pk mu c inve y lab pinf w p empl ww effort pinf4 r dr 
    pinfLAG1 pinfLAG2 ygap a as b g ls qs ps ms scons sinv sy slab spinf sw 
    kpf habf kp hab one
 
//*********************************************************
// Modelbase Variables  
                                                 
        interest interestq inflation outputgap output; 
                  
//*********************************************************
//* Exo. Variables */
 
varexo ea eb y_ els eqs eps eas econs einv ey elab epinf ew em
 
//*********************************************************
// Modelbase Shocks 
                                                            
       interest_ fiscal_;   
                                             
//*********************************************************
 //* IDENTIFICATION OF MODEL PARAMETERS*/

parameters calfa ctou cbeta chab ccs cinvs crelwage chabw clabeff chlab csadjcost 
           csigma chabb cprobw clandaw csigl cprobp cindw cindp cfc cinvdyn 
           czcap csadjlab crpi crdpi crr cry crdy crhoa crhoas crhob crhog 
           crhols crhoqs crhops crhoms crhoy crhocons crhoinv crhopinf crhow 
           crholab cscaleea cscaleeas cscaleeb cscaleeg cscaleels cscaleeqs 
           cscaleeps cscaleem cscaleecons cscaleeinv cscaleey cscaleelab 
           cscaleepinf cscaleew 
 
//*********************************************************
//* Modelbase Parameters                                                  
                                                                         
        cofintintb1 cofintintb2 cofintintb3 cofintintb4                  
        cofintinf0 cofintinfb1 cofintinfb2 cofintinfb3 cofintinfb4       

        cofintinff1 cofintinff2 cofintinff3 cofintinff4                  
        cofintout cofintoutb1 cofintoutb2 cofintoutb3 cofintoutb4        

        cofintoutf1 cofintoutf2 cofintoutf3 cofintoutf4   
               
        std_r_ std_r_quart;                            
                  
//*******************************************************                                                                         
// load Modelbase Monetary Policy Parameters  
                          
//* Policy Rule 

cofintintb1 =  0.87^3; 
cofintintb2 = 0; 
cofintintb3 = 0; 
cofintintb4 = 0; 
cofintinf0 = (1-0.87^3)*1.93; 
cofintinfb1 = 0; 
cofintinfb2 = 0; 
cofintinfb3 = 0; 
cofintinfb4 = 0; 
cofintinff1 = 0; 
cofintinff2 = 0; 
cofintinff3 = 0; 
cofintinff4 = 0;
cofintout = (1-0.87^3)*0.28; 
cofintoutb1 = 0; 
cofintoutb2 = 0; 
cofintoutb3 = 0; 
cofintoutb4 = 0; 
cofintoutf1 = 0; 
cofintoutf2 = 0; 
cofintoutf3 = 0; 
cofintoutf4 = 0;
std_r_ =  1;        //1.0124*1.0238;
std_r_quart = 0.0808*.25*4*3.1479; 
                                                                                                                                               
//*********************************************************
 
/*The parameter values ??were taken from the following sources: J.P Medina and Claudio Soto (2007) Raphael Bergoeing et al. 2001, cuentas Nacionales Chilenas, Michael Kumhof and Douglas Laxton (2009)*/

calfa       = 0.47; //labor share in production 
ctou        = 0.058; //depreciation rate 
cbeta       = 0.99; //discount factor
chab        = 0;
ccs         = 0.6;// steady state consumption in output 
cinvs       = 0.22;//steady state investment
crelwage    = 0;
chabw       = 0;
clabeff     = 0;
chlab       = 0;
 
csadjcost   = 6.7711; //investment adjustment cost
csigma      = 1.3533; // elasticity of intertemporal substitution
chabb       = 0.5732; // habit persistence 
cprobw      = 0.7367;//calvo parameter labor market
clandaw     = 1.5; // SS markup labor market	
csigl       = 2.3995;// inverse of the elasticity of labor supply 
cprobp      = 0.9082;//calvo parameter goods market
cindw       = 0.7627;//indexation labor market
cindp       = 0.4694;//indexation goods market
cfc         = 1.4077;
cinvdyn     = 1;
czcap       = 0.1690; //capital utilization
csadjlab    = 0.5990;
 
// reaction function

crpi        = 1.6841; //Taylor rule reaction to inflation
crdpi       = 0.1398;
crr         = 0.9613;//Taylor rule interest rate 
cry         = 0.0988;//Taylor rule long run reaction to output gap
crdy        = 0.1586;//Taylor rule short run reaction to output gap
 
// AR(1) shocks - To simulate Shocks

crhoa       = 0.8232;/* productivity shock/*
crhoas      = 0.9238;
crhob       = 0.8545; /*cons. preference shock*/
crhog       = 0.9493; /*government spending shock*/
crhols      = 0.8894; /*labor supply shock*/
crhoqs      = 0.9273; 
crhops      = 0;
crhoms      = 0;
crhoy       = 0;
crhocons    = 0;
crhoinv     = 0;
crhopinf    = 0;
crhow       = 0;
crholab     = 0;
 
// scaling factor of the innovations

cscaleea    = 0.5978; /* productivity/*
cscaleeas   = 0.0165;
cscaleeb    = 0.3361; /*cons. preference*/
cscaleeg    = 0.3247; /*government spending*/
cscaleels   = 3.5197; /*labor supply*/
cscaleeqs   = 0.0851; 
cscaleeps   = 0;
cscaleem    = 0.0808; 
cscaleecons = 0;
cscaleeinv  = 0.6043; /*investment*/
cscaleey    = 0;
cscaleelab  = 0;
cscaleepinf = 0.1602; /*inflation*/
cscaleew    = 0.2892; /*wage*/

//********************************************************* 
// Model Definition

model(linear);
 
//*********************************************************

// Modelbase Variables                                                   

interest   = interestq * 4;                                              

interestq  = r;                                                          

inflation  = pinf4;                                                      

outputgap  = ygap;                                                       

output     = y;                                                          
                                                                         
// Monetary Policy Rule
                                                           
interestq =   cofintintb1*interestq(-1)                                   
            + cofintintb2*interestq(-2)                                  
            + cofintintb3*interestq(-3)                                  
            + cofintintb4*interestq(-4)                                  
            + cofintinf0/4*inflation                                     
            + cofintinfb1/4*inflation(-1)                                
            + cofintinfb2/4*inflation(-2)                                
            + cofintinfb3/4*inflation(-3)                                
            + cofintinfb4/4*inflation(-4)                                
            + cofintinff1/4*inflation(+1)                                
            + cofintinff2/4*inflation(+2)                                
            + cofintinff3/4*inflation(+3)                                
            + cofintinff4/4*inflation(+4)                                
            + cofintout/4*outputgap                                      
            + cofintoutb1/4*outputgap(-1)                                
            + cofintoutb2/4*outputgap(-2)                                
            + cofintoutb3/4*outputgap(-3)                                
            + cofintoutb4/4*outputgap(-4)                                
            + cofintoutf1/4*outputgap(+1)                                
            + cofintoutf2/4*outputgap(+2)                                
            + cofintoutf3/4*outputgap(+3)                                
            + cofintoutf4/4*outputgap(+4)                                
            + std_r_quart * interest_; 
                                  
//*********************************************************

// Original Model Code:

// Original monetary policy rule (Smets and Wouters, 2003)

// r = crr*r(-1) + (1-crr)*(crpi*pinf+cry*ygap)+crdpi*(pinf-pinf(-1))+crdy*(ygap-ygap(-1))+interest_ //

//************** Flexible Economy
mcf      =   calfa*rkf + (1-calfa)*wf - a;
zcapf    =   (1/czcap)*rkf;
rkf      =   wf + labf - kf;
kf       =   kpf(-1) + zcapf;
//************** Investment Euler Equation
invef    =   (1/(1+cinvdyn*cbeta))*((cinvdyn*invef(-1) + cbeta*invef(+1)) + (1/csadjcost)*pkf) + 0*sinv;
pkf      = - muf - 1*b + muf(+1) + (1-cbeta*(1-ctou))*rkf(+1) + 0*(1-cbeta*(1-ctou))*zcapf(+1) + cbeta*(1-ctou)*pkf(+1) + qs;
muf      =   muf(+1) + rf - pinff(+1) - b;
muf      = - csigma*((1-chab)/(1-chab-chabb))*cf + csigma*(chabb/(1-chab-chabb))*habf;
// ************* Aggregate Resource constraint
yf       =   ccs*cf + cinvs*invef + g;
// ************* Aggregate Production function
yf       =   cfc*(calfa*kf + (1-calfa)*labf + a);
mcf      = - 0*ps - 0*(1+cbeta*cindp)*(1/((1-cprobp)*(1-cbeta*cprobp)/(cprobp)))*spinf;
wf       = - 1*muf - 1*ls + csigl*labf;
pf       =   0*pf(-1) + pinff;
emplf    =   emplf(-1) + 1*emplf(+1)- 1*emplf + ((1-csadjlab)*(1-csadjlab)/csadjlab)*effortf;
rrf      =   rf - pinff(+1);
effortf  =   labf - emplf;
pinff    =   0;
//************ Sticky Price - wage economy
//************ Marginal Cost
mc       =   calfa*rk+(1-calfa)*w - a - slab;
//************ Capital Utilization 
zcap     =   (1/czcap)*rk - 0*(1/czcap)*pk;
//************ Rental Rate of Capital
rk       =   w + lab - k;
//************ Capital installed used one period later in production 
k        =   kp(-1) + zcap;
//************ Investment Euler equation
inve     =   (1/(1+cinvdyn*cbeta))*((cinvdyn*inve(-1) + cbeta*inve(+1)) + (1/csadjcost)*pk) + 1*sinv;
//************ Arbitrage equation for Value of Capital
pk       = - mu - 1*b - 0*(1-crhols)*ls - 0*scons+mu(+1) + 0*b(+1) + (1-cbeta*(1-ctou))*rk(+1) + 0*(1-cbeta*(1-ctou))*zcap(+1)
             + cbeta*(1-ctou)*pk(+1) + qs + 0*sinv;
mu       =   mu(+1) + r - pinf(+1) - b + 0*b(+1) - scons - 0*(1-crhols)*ls;
mu       = - csigma*((1-chab)/(1-chab-chabb))*c + csigma*(chabb/(1-chab-chabb))*hab;
y        =   ccs*c + cinvs*inve + g + sy;
//************ Aggregate Production function
y        =   cfc*(calfa*k + (1-calfa)*lab + a + slab);
//************ Phillips Curve
pinf     =   0*as + (1/(1+cbeta*cindp))*((cbeta)*(pinf(+1) - 0*as(+1)) + (cindp)*(pinf(-1) - 0*as(-1)) 
             + ((1-cprobp)*(1-cbeta*cprobp)/(cprobp))*(mc+ps) + 0*0.1*ps)+ spinf;
w        =   ((1/(((1+cbeta)*cprobw*((clandaw/(1-clandaw))*csigl-1+crelwage)/(1-cprobw))+crelwage+crelwage*cprobw*cbeta*(chabw-1))))
             *(((cprobw*((clandaw/(1-clandaw))*csigl-1+crelwage))/(1-cprobw)+crelwage+crelwage*(chabw-1))*w(-1)
             + (cbeta)*((cprobw*((clandaw/(1-clandaw))*csigl-1+crelwage))/(1-cprobw))*w(+1)
             + (cindw)*(cprobw/(1-cprobw))*((clandaw/(1-clandaw))*csigl-1+crelwage)*(pinf(-1)-0*as(-1))
             - (cindw*cbeta*cprobw*(cprobw/(1-cprobw))*((clandaw/(1-clandaw))*csigl-1+crelwage)
             + (cprobw/(1-cprobw))*((clandaw/(1-clandaw))*csigl-1+crelwage)
             + cprobw*cbeta*cindw*((clandaw/(1-clandaw))*csigl-1))*(pinf - 0*as)
             + (cbeta*cprobw)*(((cprobw/(1-cprobw))*((clandaw/(1-clandaw))*csigl-1+crelwage))
             + ((clandaw/(1-clandaw))*csigl-1))*(pinf(+1) - 0*as(+1))
             + (1-cbeta*cprobw)*(w + 1*mu + 1*ls - clabeff*effort - csigl*(1/(1-chlab))*lab + csigl*(chlab/(1-chlab))*lab(-1)))
             + 0*(1/(1+cbeta))*1*ls + 1*sw;
p        =   0*p(-1) + pinf;
empl     =   empl(-1) + 1*empl(+1) - 1*empl - 0*r + 0*pinf(+1) + 0*csadjlab*effort + ((1-csadjlab)*(1-csadjlab)/csadjlab)*effort
             + 0.0*(a(-1) + cbeta*a(+1) - (1+cbeta)*a);
ww       =   w + 0*(lab - empl);
effort   =   lab - empl;
dr       =   r - r(-1);
pinfLAG1 =   pinf(-1);
pinfLAG2 =   pinfLAG1(-1);
pinf4    =   pinf + pinfLAG1 + pinfLAG2 + pinfLAG2(-1);
ygap     =   y - yf;
a        =   crhoa*a(-1) + cscaleea*ea;
as       =   crhoas*as(-1) + cscaleeas*eas;
b        =   crhob*b(-1) + cscaleeb*eb;
//g        =   crhog*g(-1) + cscaleeg*fiscal_;
//************ exogenous spending (also including net exports)
g        =   crhog*g(-1) + fiscal_;
qs       =   crhoqs*qs(-1) + cscaleeqs*eqs;
ms       =   crhoms*ms(-1) + cscaleem*em; 
ls       =   crhols*ls(-1) + cscaleels*els;
ps       =   crhops*ps(-1) + cscaleeps*eps;
scons    =   crhocons*scons(-1) + cscaleecons*econs;
sinv     =   crhoinv*sinv(-1) + cscaleeinv*einv;
sy       =   crhoy*sy(-1) + cscaleey*ey;
slab     =   crholab*slab(-1) + cscaleelab*elab;
          //cost push shock
spinf    =   crhopinf*spinf(-1) + cscaleepinf*epinf;
sw       =   crhow*sw(-1) + cscaleew*ew;
//************ Accumulation of installed capital
kpf      =   (1-ctou)*kpf(-1) + ctou*invef(-1);
habf     =   chab*habf(-1) + (1-chab)*cf(-1);
//************ accumulation of installed capital
kp       =   (1-ctou)*kp(-1) + ctou*inve(-1);
hab      =   chab*hab(-1) + (1-chab)*c(-1);
one      =   0*one(-1);
end;
  
 
shocks;
//************ productivity shock
var ea       = 1;
var eas      = 1;
//************ wedge between FFR  and return on assets
var eb       = 1;
var fiscal_  = 1; //eg
var interest_ = 1; // set equal to one for IRF
var els      = 1;
//************ investment specific technology
var eqs      = 1;
//************ monetary policy shock
var em       = 1;
var econs    = 1;
var einv     = 1;
var ey       = 1;
var elab     = 1;
var epinf    = 1;
var ew       = 1;

end;
check;
steady;
 
//*********************************************************** 
//* stoch_simul (irf = 200, ar=0, nograph, noprint); //*
 
//*stoch_simul (irf = 200, ar=0, nograph, noprint) inflation outputgap interest;//*
//************************************************************

// BAYESIAN ESTIMATION //

estimated_params;
 
csigl, inv_gamma_pdf, 1, 3;
chabb, beta_pdf, 0.5, 0.25;
cprobw, beta_pdf, 0.75, 0.2;
cindw, beta_pdf, 0.5, 0.25;
cprobp, beta_pdf, 0.75, 0.2;
cindp, beta_pdf, 0.5, 0.25;

stderr eb, inv_gamma_pdf, 1, 3;
stderr fiscal_, inv_gamma_pdf, 1, 3;
stderr els, inv_gamma_pdf, 1, 3;
stderr epinf, inv_gamma_pdf, 1, 3;
stderr einv, inv_gamma_pdf, 1, 3;
  
end;
 
estimated_params_init;
 
csigl, 0.5;
chabb, 0.5;
cprobw, 0.5;
cindw, 0.5;
cprobp, 0.5;
cindp, 0.5;

stderr eb, 0.1;
stderr fiscal_, 0.1;
stderr els, 0.1;
stderr epinf, 0.1;
stderr einv,0.1;

end;
 
varobs ygap inve cf w;
 
identification;  dynare_sensitivity(identification=1); 
 
estimation(datafile=Input7,mode_check,
first_obs=2,nobs=69,mode_compute=4,mh_replic=3000,
mh_jscale=1.5,mh_nblocks=1,bayesian_irf,irf=24, forecast=12) ygap inve cf w interestq pinf empl;

 
/**********************************************************************************/
 
 
 


