Page 1 of 1

How to write a finite sum in dynare

PostPosted: Fri Jul 14, 2017 2:57 pm
by parantap
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= cp*m + cp*m*m(+1) + cp*m*m(+1)*m(+2) + m*m(+1)*m(+2);

Re: How to write a finite sum in dynare

PostPosted: Fri Jul 14, 2017 5:59 pm
by StephaneAdjemian
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.

Re: How to write a finite sum in dynare

PostPosted: Fri Jul 14, 2017 6:15 pm
by jpfeifer
Try
Code: Select all
Sb=cp*m
@# 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
Code: Select all
onlymacro savemacro=macro_output.mod nolinemacro

This will generate a new mod-file with the result.

Re: How to write a finite sum in dynare

PostPosted: Fri Jul 14, 2017 8:29 pm
by parantap
Thank you. Let me try this.

Re: How to write a finite sum in dynare

PostPosted: Fri Jul 14, 2017 11:08 pm
by parantap
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+m*m(+1)+m*m(+1)*m(+2) while I want to compute m*m(+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
;

Re: How to write a finite sum in dynare

PostPosted: Sat Jul 15, 2017 7:22 am
by jpfeifer
It should be
Code: Select all
@#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
;

Re: How to write a finite sum in dynare

PostPosted: Sat Jul 15, 2017 8:08 am
by parantap
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.

Re: How to write a finite sum in dynare

PostPosted: Sat Jul 15, 2017 8:36 am
by jpfeifer
When you install Dynare, there will be a doc-folder that contains a macroprocessor.pdf