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

Postby parantap » Fri Jul 14, 2017 2:57 pm

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);
parantap
 
Posts: 31
Joined: Tue Oct 01, 2013 11:58 pm

Re: How to write a finite sum in dynare

Postby StephaneAdjemian » Fri Jul 14, 2017 5:59 pm

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.
Stéphane Adjemian
Université du Maine, GAINS and DynareTeam
https://stepan.adjemian.eu
StephaneAdjemian
 
Posts: 429
Joined: Wed Jan 05, 2005 4:24 pm
Location: Paris, France.

Re: How to write a finite sum in dynare

Postby jpfeifer » Fri Jul 14, 2017 6:15 pm

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
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: How to write a finite sum in dynare

Postby parantap » Fri Jul 14, 2017 8:29 pm

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

Postby parantap » Fri Jul 14, 2017 11:08 pm

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
;
parantap
 
Posts: 31
Joined: Tue Oct 01, 2013 11:58 pm

Re: How to write a finite sum in dynare

Postby jpfeifer » Sat Jul 15, 2017 7:22 am

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
;
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: How to write a finite sum in dynare

Postby parantap » Sat Jul 15, 2017 8:08 am

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

Postby jpfeifer » Sat Jul 15, 2017 8:36 am

When you install Dynare, there will be a doc-folder that contains a macroprocessor.pdf
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany


Return to Dynare help

Who is online

Users browsing this forum: No registered users and 11 guests