Announcement

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

  • adding a covariate to moptimize_init_eq_indepvars inside an evaluator

    Hi,

    I am running maximum likelihood estimation with moptimize where I passed my independent variables to the evaluator through moptimize_init_eq_indepvars().

    I would like to add an extra covariate to those already passed such that this new covariate is defined inside the evaluator.

    My code would look like:

    mata
    y = mydepvar
    X=(myfirstindvar,mysecondindvar)

    function mle (transmorphic M, real vector b, real vector lnf)
    {
    real colvector y, xb,sigma,z

    y = moptimize_util_depvar(M, 1)
    xb = moptimize_util_xb(M, b, 1)
    sigma = moptimize_util_xb(M, b, 2)

    z=xb[.,1]:*xb[.,2]
    xb=(xb,z)

    lnf = ln(normalden(y-xb, 0, sigma))

    }
    M = moptimize_init()
    moptimize_init_evaluator(M, &mle())
    moptimize_init_depvar(M, 1, y)
    moptimize_init_eq_indepvars(M, 1,X)
    moptimize_init_eq_indepvars(M, 2,"")
    moptimize(M)
    end

    In this example, I tried to define the new covariate as the element by element product of the two independent variables:

    z=xb[.,1]:*xb[.,2]

    but it does not work. It seems to me that the evaluator considers moptimize_util_xb as a unique matrix that cannot be manipulated by columns. In particular,

    a) it does not seem possible to extract a specific column like xb[.,number of wanted column]. The evaluator considers the second index inside xb as the number of equation and not as column index.

    b) it does not seem possible to add a new column like xb=(xb,z).

    Any help will be greatly appreciated.

    Thanks,

    Simone

  • #2
    Simone --

    You are right about the structure of xb. It is in fact the linear index computed by multiplying the coefficients by the X's and hence has only one dimension. Is there a reason why you are not computing the interaction beforehand, and passing it as an additional argument? I.e.,

    Code:
    clear all
    sysuse auto
    gen fw = foreign*weight
    mata
        st_view(y=.,.,"price")
        st_view(X=.,.,"foreign weight fw")
    
    
    function mle (transmorphic M, real vector b, real vector lnf)
    { 
    real colvector y, xb,sigma,z
    
    y = moptimize_util_depvar(M, 1)
    xb = moptimize_util_xb(M,b,1)
    sigma = moptimize_util_xb(M, b, 2)
    
    lnf = ln(normalden(y-xb, 0, sigma))
    
    }
    M = moptimize_init()
    moptimize_init_evaluator(M, &mle())
    moptimize_init_depvar(M, 1, y)
    moptimize_init_userinfo(M,1,X)
    moptimize_init_eq_indepvars(M, 1,X)
    moptimize_init_eq_indepvars(M, 2,"")
    moptimize(M)
    end
    If you must pass the matrix of X values to the program, you could always do it by adding some user information. That is, using the moptimize_init_userinfo() command you can pass just about anything into the likelihood. Inside the likelihood, it can be retrieved using moptimize_util_userinfo().

    Hope that helps!

    Matt Baker

    Comment


    • #3
      Matt,

      I need to compute the interaction term inside the evaluator because my log-likelihood function is the summation of two log-likelihoods, where one covariate of the second likelihood function is a messy expected value from the coefficients defined in the first log-likelihood.

      moptimize_util_userinfo() is very useful, but I do not see how it can fix this problem.

      Probably I need to use optmize() and exploit the fact that it allows to extract specific columns of X.

      Thank you very much for your feed-back.

      Simone Angioloni

      Comment


      • #4
        It will be more helpful if you gave us an example which is closer to the likelihood you have in mind. From what you explain in your last post Matthew's solution is not what you want and the example you give cannot work. You should specify which parameters are to be estimated and which parameters enters in which of the two parts of the likelihood.

        Has your likelihood the following structure?

        Code:
        lnf1 = some function of xb
        
        lnf2 = some function of lfn1
        
        lnf = lnf1 + lnf2
        and does lnf2 involves parameters you are trying to estimate others than those involved in xb? From your initial example it is not clear that it is the case.

        i.e. is the likelihood you have in mind something like this

        lnf = f1(y|x;b1) + f2(xb1;b2)

        or simply

        lnf = f1(y|x;b1) + f2(xb1) ?

        In the first case you have to specify additional equations.


        But maybe what you need is this. According to this post and a reply of Jeff Pitblado (StataCorp) you can access the matrix of independent variables of the first equation with

        Code:
        moptimize_util_indepvars(M,1)
        According to Jeff Pitblado this function is undocumented (see link to the post I mention). The availability of the function might depend on your version of Stata.
        Hope this helps
        Last edited by Christophe Kolodziejczyk; 05 Mar 2016, 08:32.

        Comment


        • #5
          Christophe --

          Thanks for that - I was unaware of that command as well. Does that help with the problem Simone?

          Best,

          Matt

          Comment


          • #6
            Matthew J. Baker It is unclear whether it helps, as Simone's problem is not clearly defined to me. I guessed that one possibility that Simone wants to access the matrix of covariates and make some non-trivial computations with them. If it is the case then it should help. It is also sligthly more efficient than moptimize_util_info() in this case. But this function is also useful in other contexts.
            I think that in the case where one wants to include interactions then I find your solution the most efficient (IMHO).

            Comment


            • #7
              Christophe,

              I wrote my first post to simplify the problem, but probably it did not help.

              I have one dependent variable and a set of independent variables for each equation. In addition, but only in the second equation, one covariate, E[y], is a messy function of SOME x1 and b1 from the first equation. My likelihood looks like:

              lnf = f1(y1|x1;b1) + f2(y2|Z;b2)

              Z = (x2,E[y]))

              Unfortunately, I read your post after I fixed the problem with optimize.

              However, I will double check the post you suggested. If it works, it will save a lot of time.

              Many thanks.

              Simone

              Comment


              • #8
                In order to keep moptimize() I think you have to define a third equation to isolate the parameter relevant for E[y] and treat it as a constant.

                I define b3 as the parameter for E[y]

                Code:
                
                
                void mle(transmorphic M, real vector b, real vector lnf)
                {
                ...
                
                xb1  = moptimize_util_xb(M,1)
                xb2  = moptimize_util_xb(M,2)
                vecb3    = moptimize_util_xb(M,3) // this is in order to access b3.  vecb3 becomes a constant vector with b3
                
                Ey = .... // some function of X1 and b1
                b3Ey = vecb3:*Ey
                
                lnf1 = ...
                lnf2 = ... // some function of xb2 and b3*E[y]
                
                lnf = lnf1 + lnf2
                
                }
                
                moptimize_init_depvar(M, 1, y1)
                moptimize_init_depvar(M, 2, y2)
                moptimize_init_eq_indepvars(M, 1,X1)
                moptimize_init_eq_indepvars(M, 2,X2)
                moptimize_init_eq_indepvars(M, 3,"")

                Comment


                • #9
                  Christophe,

                  I worked. Very nice solution.

                  Many thanks.

                  Simone

                  Comment

                  Working...
                  X