Announcement

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

  • Problem with optimize: error 3201

    Dear all
    Im starting to learn how to use Mata "optimize"
    For this, I'm trying to set a simple OLS estimator.
    This raises a question. For an example like this, is it better to send to the evaluator the matrix of data itself (yy,xx),
    or do you think is more efficient (memory and speed wise) to just send the pointer to those matrixes (as I did below)

    Thank you
    Code:
    sysuse auto, clear
    mata:
        mata clear
        yy=st_data(.,"price")
        xx=st_data(.,"mpg foreign"),J(rows(yy),1,1)
        xy=(&yy,&xx)
        void myxb(todo, b,  xy, ss, g, H) {
            ss= sum( ((*xy[1]):-(*xy[2])*b'):^2     )
             }
        S = optimize_init()
        optimize_init_evaluator(S, &myxb())
        optimize_init_which(S, "min")
        //optimize_init_evaluatortype(S, "d0")
        optimize_init_params(S, (1,1,1))
        optimize_init_argument(S, 1, xy)
        p = optimize(S)
    end
    PS. I started this with a "problem" but turn out into "best practices" question.
    Last edited by FernandoRios; 23 Apr 2021, 08:14.

  • #2
    I recall somewhere reading that all parameter passing in Mata is "call by reference," meaning that the address of the data object, not a copy of it, is given to the function, so my understanding would be that sending the data matrix to a function does not involve a copy of the data, so that both approaches should take the same time. Does a timing experiment with your data (best with an much expanded version of the input data) confirm this?

    Comment


    • #3
      You are right.
      I run a few exercises and seems that either option produces the same result. Meaning. No additional use of memory, and no speed gain or lose.
      Thank you!
      Fernando

      Comment

      Working...
      X