Announcement

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

  • st_view in optimization

    Hi all

    I have a column vector (let call that X) that contains rows(X) i.i.d variables with Geometric distribution in Stata. I try to use optimize to find the parameter (let call that p) that maximizes the likelihood function of the sample.

    Here is my codes:
    Code:
    mata mata clear
    mata:
        x = st_view(x=0,.,"X",0)
        void MLE_geo_dtype(todo,p,x,ll,g,H)
        {
            ll = rows(x)*ln(p)+(sum(x)-rows(x))*ln(1-p)
            if (todo>=1) {
                g = rows(x)/p-(sum(x)-rows(x))/(1-p)
                if (todo==2) {
                    H = (-rows(x)/p^2)-(sum(x)-rows(x))/((1-p)^2)
                }
            }
        }
        S = optimize_init()
        optimize_init_evaluator(S, &MLE_geo_dtype())
        optimize_init_evaluatortype(S,"d2")
        optimize_init_params(S,0.5)
        optimize_init_argument(S,1,x)
        p = optimize(S)
    end
    I know in advance that the sample was created with p = 0.3 (I checked the mean and variance of the sample, and it surely was created by Geometric distribution with the chance of success is 0.3). What surprised me are: first, the
    Code:
    p=optimize(S)
    returns the true parameter for p is 0.5 (which is the initial value set for p and it is wrong). Second, the column vector x, which is a view on X, contains no value despite the fact that X has not changed. I know this is the setup of g-type optimization, but I want to learn whether d-type could work with extra parameter like the g-type.

    Can anyone help me to explain this.

    Thank you

  • #2
    I'm unfamiliar with the syntax:
    Code:
    x = st_view(x=0,.,"X",0)
    and it seems to return an empty matrix. The help file says that st_view() is a subroutine (void function).

    Try running this to see what I mean:
    Code:
    version 17.0
    
    clear *
    
    // seedem
    set seed 1058123613
    
    quietly set obs 250
    
    generate byte x = rgeometric(0.3)
    
    mata:
    mata set matastrict on
    
    // Unfamiliar syntax
    X2 = st_view(X2=0, ., "x", 0)
    isview(X2)
    eltype(X2)
    orgtype(X2)
    nonmissing(X2)
    rows(X2)
    cols(X2)
    
    // Familiar syntax
    st_view(X=(.), ., "x")
    isview(X)
    orgtype(X)
    orgtype(X)
    nonmissing(X)
    rows(X)
    cols(X)
    
    void function geodist(
        real scalar todo,
        real scalar p,
        real matrix X,
        real scalar ll,
        real rowvector g,
        real matrix H) {
    
        assert(isview(X))
    
        ll = rows(X) * ln(p) + ( sum(X) - rows(X) )  * ln(1-p)
    
        if (todo >= 1) {
            g = rows(X) / p - ( sum(X) - rows(X) ) / (1-p)
            if (todo == 2) {
                H = ( -rows(X) / p^2 ) - ( sum(X) - rows(X) ) / ( (1-p)^2 )
            }
        }
    }
    
    S = optimize_init()
    optimize_init_evaluator(S, &geodist())
    optimize_init_evaluatortype(S,"d2")
    optimize_init_params(S, 0.5)
    optimize_init_argument(S, 1, X)
    p = optimize(S)
    
    p
    
    end
    
    exit

    Comment


    • #3
      Thank you Joseph, it worked.

      I am quite new to mata and sometimes get confused with the syntax.

      May I ask one more question related to your function geodist? If I want to redefined the mata function geodist or remove it after using optimize. What should I do because when I code mata mata drop geodist(), I cannot drop the function as Mata said that it is in use. So it leaves me the one option to clear the entire memory of mata using mata clear.

      Comment


      • #4
        You have to drop S first.

        S is described in the help file as a "problem-description handle", which implies that it's persistent.

        Because S has a pointer to your evaluator function, Mata balks at dropping your evaluator function until the pointer to it is set to NULL, which will happen during destruction of S.

        So, after your done with things, you can
        Code:
        mata drop S // First, before dropping the evaluation functioni
        mata drop geodist()

        Comment


        • #5
          Originally posted by Nhan Nguyen View Post
          If I want to redefined the mata function geodist
          If you want to keep S otherwise intact, then as an alternative to what I show above you can just change out the pointer that S has to a placeholder (dummy) evaluator.

          That will allow you to drop your evaluator function, rewrite it, and then swap it back in for the placeholder.

          Like the following:
          Code:
          void function dummy() {}
          
          optimize_init_evaluator(S, &dummy())
          
          mata drop geodist()

          Comment


          • #6
            Thank you for your explanation Joseph.

            It is really helpful. Those are not written in the help file of optimize function so I kept messing around.

            Comment

            Working...
            X