Announcement

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

  • Two identical functions, but only one can be optimised.

    Dear Statalisters,

    I’m learning MATA function optimisation, and run into a problem.
    In short, I have two identical functions, but MATA can only optimise one of them.
    I demonstrate the problem by a simple example.

    MATA works well with a function like this:
    Code:
    mata:
       mata clear
       void FN1(todo, b, lnf, g, H) {
          lnf = ln( binomialp(189, 59, invlogit(b) ))
       }
       S1 = optimize_init()
       optimize_init_evaluator(S1, &FN1())
       optimize_init_params(S1, (0))
       optimize(S1)
    end
    However, MATA doesn't work with a function like this:
    Code:
    mata:
       mata clear
       n = 189
       r = 59
       void FN2(todo, b, lnf, g, H) {
          lnf = ln( binomialp(n, r, invlogit(b) ))
       }
       S2 = optimize_init()
       optimize_init_evaluator(S2, &FN2())
       optimize_init_params(S2, (0))
       optimize(S2)
    end
    The only difference between the two functions is that n=189 and r=59 are defined outside the function 2.
    I've tried to deal with this problem by using optimize_init_argument, but MATA still returns the same error message: "initial values not feasible."
    Code:
    mata:
       mata clear
       n = 189
       r = 59
       void FN2(todo, b, lnf, n, r, g, H) {
          lnf = ln( binomialp(n, r, invlogit(b) ))
       }
       S2 = optimize_init()
       optimize_init_evaluator(S2, &FN2())
       optimize_init_params(S2, (0))
       optimize_init_argument(S2, 1, n)
       optimize_init_argument(S2, 2, r)
       optimize(S2)
    end
    I'd like to know why this difference matters, and how to deal with this problem, if I really need to define something outside the function. Thank you very much.

    Best,

    Kirin


    Last edited by Kirin_Guess; 07 Sep 2014, 06:08.

  • #2
    Dear Kirin You need to define your evaluator as Void FN2(todo, b, n, r, lnf, g, H) { } More generally lnf, g and H have to be the last arguments of your evaluator. Best Christophe

    Comment


    • #3
      Dear Christophe,
      Many thanks for the correction. It works.
      Best,
      Kirin

      Comment

      Working...
      X