Announcement

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

  • Dropping a mata function

    Hi all,

    I have the following the optimization function in mata:
    Code:
    mata
    mata set matastrict off
    
    void mvopt(todo, p, fml, g, H) {
    fml=253*($m1*invlogit(p[1])+$m2*invlogit(p[2])+$m3*((1-invlogit(p[1])-invlogit(p[2]))))/*
    */-sqrt(253)*sqrt($s11*(invlogit(p[1]))^2+$s22*(invlogit(p[2]))^2+$s33*(1-invlogit(p[1])-invlogit(p[2]))^2 /*
    */+2*$s12*invlogit(p[1])*invlogit(p[2])/*
    */+2*$s13*invlogit(p[1])*(1-invlogit(p[1])-invlogit(p[2]))/*
    */+2*$s23*invlogit(p[2])*(1-invlogit(p[1])-invlogit(p[2])))
    }
    
    S = optimize_init()
    optimize_init_evaluator(S, &mvopt())
    optimize_init_which(S, "max")
    optimize_init_params(S, (.3, .3))
    (void) optimize(S)
    p = optimize_result_params(S)
    st_matrix("w",p)
    
    end

    After completing the optimization I would like to drop the function using mata drop myopt() but I receive the error:
    Code:
    mvopt() in use (nothing dropped)
    I can't seem to figure out why it would say the function is in use. Is there a way I can drop this function after running it?

    Thank you!
    George

    Last edited by George D; 23 Apr 2014, 13:31.

  • #2
    George, I am guessing, but there must be a reference somewhere to the pointer you supplied:
    &mvopt() Which needs to be dropped/cleared first. Best, Sergiy Radyakin

    Comment


    • #3
      Sergiy is right. S is holding a reference to your function. You can see this with a simpler example taken from the documentation:
      Code:
      mata
      
      void myeval(todo, x,  y, g, H)
      {
          y = exp(-x^2 + x - 3)
      }
      
      S = optimize_init()
      optimize_init_evaluator(S, &myeval())
      
      end
      After running the above
      Code:
      : optimize_init_evaluator(S)
        0x21c69c0
      
      : &myeval()
        0x21c69c0
      Thus you have to drop S before you can drop your function.

      Comment


      • #4
        Many thanks James and Sergiy! I dropped S and it works now.

        I am now trying to impose a constraint using optimize_init_constraints(S,Cc) and I was wondering if it is possible to impose an inequality constraint. I know how to impose constraints such as P1 = P2 but I don't know how to do something like P1 >= 0.

        Thank you.
        George

        Comment


        • #5
          George, start a new thread since this is a totally different question.

          If you want to minimize F(x) subject to P1>=0, then you can minimize F(x)+(P1<0)*10^200 unconditionally. See 'Penalty method'.

          There is likely a more efficient method. So start a new thread and wait for the crowd.

          Best, Sergiy

          Comment


          • #6
            Thanks Sergiy. I moved this to another thread.

            Comment

            Working...
            X