How to write a finite sum in dynare

Hi, I am trying to write a bond pricing formula in dynare. Sb is the bond price and cp is coupon with one as the face value. m is the stochastic discount factor. I would like to write this formula for a maturity K. This requires writing a finite sum. I have done it manually up to 2 period. If I like to do this for 30 periods, how do I write it? Any help?

I write the formula below for your perusal. Thanks in advance for your help.

Sb= cpm + cpmm(+1) + cpm*m(+1)m(+2) + mm(+1)*m(+2);

Hi,

There is macro language in Dynare with a devoted section in the reference manual. You can write loops, also conditional statements, with macros.

Best,
Stéphane.

Try

@# for lead in 1:30
    +cp
    @#for prod_lead in 0:lead
        *m(@{prod_lead})
    @# endfor   
@# endfor
;

You can verify this via the command line with the options

onlymacro savemacro=macro_output.mod nolinemacro
This will generate a new mod-file with the result.

Thank you. Let me try this.

Dear Jpfeifer,
I think you missed the last term which does not involve cp. i am struggling to code this last term, i tried the following for k = 2 for which I know the steady state result. Still in a blind alley. I think the last loop term s computing m+mm(+1)+mm(+1)m(+2) while I want to compute mm(+1)*m(+2). I don’t know the syntax for it, I guess.

Sb=cp*m
@# for lead in 1:2
+cp
@#for prod_lead in 0:lead
*m(@{prod_lead})
@# for lead in 1:2
+ m
@#for prod_lead in 0:lead
*m(@{prod_lead})
@# endfor
@# endfor
@# endfor
;

It should be

@#define J=2
Sb=cp*m
@# for lead in 1:J
    +cp
    @#for prod_lead in 0:lead
        *m(@{prod_lead})
    @# endfor   
@# endfor
+ m
@# for prod_lead in 1:J
        *m(@{prod_lead})
@# endfor
;
1 Like

Dear Jpfeifer
It worked. You are great! Where do I learn this macro syntax? In the reference manual, there is not much. It is not quite matlab.

When you install Dynare, there will be a doc-folder that contains a macroprocessor.pdf

Hi, I am new to writing macros in dynare and I am trying to write the following equation

y = (1 - normcdf(z_1-sigma))*x(-1)q(-1) + (1 - normcdf(z_2-sigma))(1-delta)*x(-2)*q(-2);

using the for-loop because I then want to generalize it to larger time periods.

The way I did it is as follows:

y =

@# for j in 1:M

+((1 - normcdf(z_@{j}-sigma))*(1-delta)^(-1+@{j})*x(-@{j})*q(-@{j}))

@# endfor
;

Am I doing anything wrong?

I get the following error message:

Error using print_info (line 83)
Impossible to find the steady state. Either the model doesn’t have a steady state, there are an infinity of steady states, or the guess
values are too far from the solution

Error in check (line 76)
print_info(info, 0, options);

Error in gwloop (line 344)
oo_.dr.eigval = check(M_,options_,oo_);

Error in dynare (line 223)
evalin(‘base’,fname) ;

In this code I am trying to replicate the Putty-Clay non-linear model of Glichrist-Williams with zero-growth and first setting M=2.

Thanks,
Adelia

This seems like a problem unrelated to the Macro-processor. Does the model work if you hardcode the equation for M=2, i.e. before moving to a generalizable code?

Hi,

Thanks for your response.

Yes the model works when I hardcode the equation for M=2. If I keep the same steady state system (as in the hardcode) and just generalize the model, I should still get the code running as long as M=2, correct? I seem to fail there, which makes me think that maybe I didn’t write the macro correctly…

Attached are my two models (hardcode and generalized)

Thanks,
gw_nogrow.mod (2.9 KB)
gwloop.mod (3.0 KB)

Use

dynare gwloop nolinemacro savemacro=temp.mod

to write the generated mod-file to a file called called temp.mod. That allows for easy debugging. One difference seems to be that

@#for i in 1:M

z1_@{j} = z_ss;

z2_@{j} =  z_ss;

@#endfor

has the wrong index, i.e. j instead of i

Thank you so much. That seemed to be the issue. It is giving me identical results now as the hardcoded one :slight_smile: