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

  • Limitation of arguments in mata

    Cheers together,

    currently, I am trying to find the internal rate of return or cost of equity for companies over time. So I have many companies and for each company a time series of data. Hence, I am trying to find the cost of equity for each year for a given company. This requires to solve an equation like: Price = function(cost of equity, earnings0, earnings1, earnings2,.....) . Here the cost of equity is the only unkonwn paramter, while I have 13 input paramters.

    In this context I wrote some mata code which loops over all observations and determines the cost of equity (here k). Nevertheles, the command optimize allows only for 9 Input paramters....

    Does anyone have an idea how to increase the number of arguments or can recommand any other command that allows for at least 13 arguments?

    Thanks a lof for your help and consideration!


    HTML Code:
    mata: mata clear  
    P = st_data(.,("me"))
    GL = st_data(.,("g_l"))
    B0 = st_data(.,("be"))
    B1 = st_data(.,("be_1"))
    B2 = st_data(.,("be_2"))
    B3 = st_data(.,("be_3"))
    B4 = st_data(.,("be_4"))
    B5 = st_data(.,("be_5"))
    E1 = st_data(.,("E_1"))
    E2 = st_data(.,("E_2"))
    E3 = st_data(.,("E_3"))
    E4 = st_data(.,("E_4"))
    E5 = st_data(.,("E_5"))
    void eval0(todo, k, p, gl, b0, b1, b2, b3, b4, b5, e1, e2, e3, e4, e5, v, g, H)  {
        v =  (p :- b0 :- ((e1 :- k :* b0) :/ (1 :+ k)) :- ((e2 :- k :* b1) :/ (1 :+ k)^2) :- ((e3 :- k :* b2) :/ (1 :+ k)^3) :- ((e4 :- k :* b3) :/ (1 :+ k)^4) :- ((e5 :- k :* b4) :/ (1 :+ k)^5) :- (((e5 :- k :* b4) :* (1+gl)) :/ ((1 :+ k)^5) :* (k-gl)))^2                                    
        S = optimize_init()
        optimize_init_which(S,  "min")
        optimize_init_conv_ptol(S, 1e-12)
        optimize_init_conv_vtol(S, 1e-12)
        optimize_init_evaluator(S, &eval0())
        optimize_init_params(S, (0))
        for(i=1;i<=235313;i++) {
            p =P[i..i,1..1]
            gl =GL[i..i,1..1]
            b0 =B0[i..i,1..1]
            b1 =B1[i..i,1..1]
            b2 =B2[i..i,1..1]
            b3 =B3[i..i,1..1]
            b4 =B4[i..i,1..1]
            b5 =B5[i..i,1..1]
            e1 =E1[i..i,1..1]
            e2 =E2[i..i,1..1]
            e3 =E3[i..i,1..1]
            e4 =E4[i..i,1..1]
            e5 =E5[i..i,1..1]
            optimize_init_argument(S, 1, p)
            optimize_init_argument(S, 2, gl)
            optimize_init_argument(S, 3, b0)
            optimize_init_argument(S, 4, b1)
            optimize_init_argument(S, 5, b2)
            optimize_init_argument(S, 6, b3)
            optimize_init_argument(S, 7, b4)
            optimize_init_argument(S, 8, b5)
            optimize_init_argument(S, 9, E1)
            optimize_init_argument(S, 10, E2)
            optimize_init_argument(S, 11, E3)
            optimize_init_argument(S, 12, E4)
            optimize_init_argument(S, 13, E5)
            k= optimize(S)
            ri = k
            if (i == 1) R = st_matrix( "r1")
            if (i >= 2) R = R \ ri

  • #2
    I have not dealt with this particular issue in -optimize-, so at the risk of giving irrelevant advice, I'll offer some thoughts. First of all, can you indicate where you found the documentation about this limitation? I ask because several things occur to me: 1) Perhaps this is only the limit on *unknown* parameters, and to my understanding, you're only looking for one parameter. I even find it hard to think that there's a 9 parameter limit: What about e.g. a regression model with 10 predictors? 2) What about using parameters that are matrices or structures? You could certainly put all of your parameters into a matrix or two, if that would help. My apologies if you've been through this and found my thoughts to be off-base.


    • #3
      Thanks a lot for your thoughts. To me its also a bit puzzeling why one would limit this command to 9 arguments. Here is the stata manual which I studied carefully:

      Click image for larger version

Name:	Unbenannt.JPG
Views:	1
Size:	83.5 KB
ID:	1561228

      Unfortunately, I can't combine all these Input parameters in one vector, because I need to multiply some of them with k others with k2 and so on. In case you have any suggestions which command would allow to solve an equation dependent on at least 13 changing Input parameters, just let me know. And also note that the equation I need to solve repeatedly is just a "plain" equation with 1x1 matrices.

      Thanks for your consideration!


      • #4
        The short answer is that the parameters whose values are to be determined by the optimize routine are passed to the evaluator function you write as a vector; this does not mean that their use is limited to matrix arithmetic. Within the evaluator function you can pull individual elements out of the vector, do some computations, and replace the values in the vector with updated values. The arguments you are looking at are not how the optimization parameters are passed to the evaluator function.

        The longer answer is that in the Mata Reference Manual PDF documentation for optimize() you need to carefully review the Remarks and examples section to obtain a more complete understanding of the optimize() set of functions than the syntax diagrams alone will give you.


        • #5
          Thanks a lot for your answer! I am glad to hear that it is just my - by now - limited understanding of the command.

          I see, that makes sense and I will try to change the paramenters within the evaluator function.


          • #6
            I want to note that in post #4 I wrote "... and replace the values in the vector with updated values". On reflection, I don't think that part is right. The role of the evaluator program is to evaluate the function at the values of the parameters passed to it in the parameters vector and return that value to the calling program. I don't think the intent is to alter values in the parameter vector. At least, not based on my limited understanding of optimize(). Blame that answer on the caffeine not having gotten my brain fully started at the time I was writing post #4.