## How to write a finite sum in dynare

This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location where you will have to reset your password.
Forum rules
This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location (https://forum.dynare.org) where you will have to reset your password.

### 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?

Sb= cp*m + cp*m*m(+1) + cp*m*m(+1)*m(+2) + m*m(+1)*m(+2);
parantap

Posts: 31
Joined: Tue Oct 01, 2013 11:58 pm

### Re: How to write a finite sum in dynare

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.
Université du Maine, GAINS and DynareTeam

Posts: 429
Joined: Wed Jan 05, 2005 4:24 pm
Location: Paris, France.

### Re: How to write a finite sum in dynare

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.
------------
Johannes Pfeifer
University of Cologne
jpfeifer

Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

### Re: How to write a finite sum in dynare

Thank you. Let me try this.
parantap

Posts: 31
Joined: Tue Oct 01, 2013 11:58 pm

### Re: How to write a finite sum in dynare

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
+cp
+ m
@# endfor
@# endfor
@# endfor
;
parantap

Posts: 31
Joined: Tue Oct 01, 2013 11:58 pm

### Re: How to write a finite sum in dynare

It should be
Code: Select all
`@#define J=2Sb=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;`
------------
Johannes Pfeifer
University of Cologne
jpfeifer

Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

### Re: How to write a finite sum in dynare

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.
parantap

Posts: 31
Joined: Tue Oct 01, 2013 11:58 pm

### Re: How to write a finite sum in dynare

When you install Dynare, there will be a doc-folder that contains a macroprocessor.pdf
------------
Johannes Pfeifer
University of Cologne
jpfeifer

Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

### Who is online

Users browsing this forum: No registered users and 11 guests