Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • IRR in Mata

    Hi,

    I'm trying to use mata to calculate a bond yield. I have seen this post http://www.stata.com/statalist/archi.../msg01113.html which explains how to calculate an IRR when the formula is stationary (that is, there are a set number of periods, in that case just two). In my data, I have different maturities for all of my bonds. Can I write a mata function that sums up (from t=1 to floor(years to bond maturity))?

    I want be able to use mm_root to solve for

    C/(1+r)+C/(1+r)^2+...C/(1+r)^t+F/(1+r)^t where t varies for each of the rows in my data, and am not sure how to do this within the mata function.

    Thanks!

  • #2
    Hi
    It is quite easy to implement npv calculations in Mata:
    Code:
    : function npv(real vector c, real scalar r, real vector t) return(sum(c :* (J(rows(c),cols(c),1) :+ r) :^ -t))
    
    : npv((10, 10, 110), .1, 1..3)
      100
    However I would use Newton Raphson for finding IRR.
    Make a function dnpv for the derivative function of npv() to do it yourself
    Kind regards

    nhb

    Comment


    • #3
      Hi Niels,

      I guess I don't see how to use Newton Raphson or just the basic root calculation in my case because the equation will take a different form depending on how many periods there are (and my periods vary over my observations -- i.e. one bond has 14.5 years to maturity, and another has 2 years) so I'm not sure how to write this function because it's not a static one. Any help would be greatly appreciated!

      Comment


      • #4
        mm_root are for root finding using Brent's method, NR is in this case a better method.
        All I'm saying is that speed of convergence favors NR in this case (You double the number of right decimals for each iteration in the sense that if the old iteration value has 3 decimals in common with the new iteration then 6 decimals are actually right in the new iteration estimate).
        As I recall chosing a IRR starting point around 0 is good.

        My npv function above handles differences in maturities without problems
        Kind regards

        nhb

        Comment

        Working...
        X