Announcement

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

  • mm_root function to solve for unknows -

    I am trying to solve the unkown IRR using mm_root function of the moremata package. Here is my example data set
    Code:
    clear
    input float(eps1 eps2 dps1) double prccd float IRR
     .33  .46  .02   7.66 .
      .3  .41  .08    6.2 .
     .38  .49  .13   7.71 .
     .38   .5  .35    9.9 .
     6.8  5.7  3.1   1330 .
     1.7  3.2    1    440 .
     9.7   11  2.6    999 .
    10.6 11.4  2.8   1200 .
    1.04 1.18  .21    183 .
     1.1 1.23  .22  36.25 .
    1.35 1.54  .25   20.5 .
    1.05 1.21  .25  10.99 .
      .9  1.1  .27  19.55 .
     1.2 1.36  .33  24.25 .
    1.56 1.76  .42   27.8 .
     .77  .83  .27   10.7 .
    1.22  1.4  .43  19.99 .
    1.17 1.32  .42 18.165 .
      .9 1.12  .29     14 .
     8.9  9.4    .   1508 .
    28.8 30.9 10.2    434 .
    end
    And the code that I am trying use is
    Code:
      mata
      mata clear
      end
    
      
      generate IRR=.
      mata
      z=J(1,1,.)
      st_view(z,., "IRR prccd eps1 eps2 dps1")
      function s(x,p,f1,f2,dv) {
          
          return(-p+(f2-f1+(dv*x))/x^2)
      }
      for (i=1;i<=rows(z);i++) {
          r=mm_root(IRR=.,&s(),z[i,9]+epsilon(1),1-epsilon(1),1e-9,1000,z[i,2],z[i,3],z[i,4],z[i,5], z[i,6],z[i,7],z[i,8],z[i,9],z[i,10] )
          z[i,1]=IRR
      }
      end
    I receive the followng error message

    <istmt>: 3301 subscript invalid
    r(3301);

  • #2
    From the way you created the dataset and the way you created the Mata view of it, z will have five columns. In your call to the user-written Mata function, you make reference to up to 10 columns of the view. From the error message, it sounds like it's trying to tell you that column subscripts beyond five are out-of-range.

    Comment


    • #3
      I tried to delete the following and many other combination, but still I receive the same message.
      z[i,6],z[i,7],z[i,8],z[i,9],z[i,10]

      Comment


      • #4
        Joseph is right, Your st_view creates a view onto five varaibles, while the mm_root looks for 10 columns which do not exist. See the following code
        Code:
        mata
          mata clear
          end
        
          
          generate IRR=.
          mata
          z=J(1,1,.)
          st_view(z,., "IRR prccd eps1 eps2 dps1")
          function s(x,p,f1,f2,dv) {
              
              return(-p+(f2-f1+(dv*x))/x^2)
          }
          for (i=1;i<=rows(z);i++) {
              r=mm_root(IRR=.,&s(),z[i,5]+epsilon(1),1-epsilon(1),1e-9,1000,z[i,2],z[i,3],z[i,4],z[i,5])
              z[i,1]=IRR
          }
          end
        Last edited by Attaullah Shah; 19 Jun 2016, 17:29.
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #5
          Originally posted by Saeed Sardar View Post
          I tried to delete the following and many other combination, but still I receive the same message.
          z[i,6],z[i,7],z[i,8],z[i,9],z[i,10]
          See here, especially the first two sentences under the first subsection.

          Comment


          • #6
            Thanks everyone, the code provided by Attaullah Shah worked for me.

            Comment

            Working...
            X