Announcement

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

  • can one use MATA within an ML program

    I am trying to write a customize likelihood function, where I want to numerically integrate using MATA within the ML framework. Is it possible to pass the arguments of the ML function as limits into the numerical integration command within STATA. here's what I have in mind : Right now I'm getting the error that the MATA function is either already defined OR that it doesn't exist when I try to drop it.


    capture program drop ML_program
    program define ML_program
    * specifiy the arguments for the program
    args lnf alpha m

    * declare temporary variables
    tempvar choice p1 p2 z1 z2 ce_x omega n_star result r
    **tempvar tmp alpha m mu

    quietly {

    /*
    ** define parameters
    generate double `alpha' = exp(`alphat')
    generate double `m' = exp(`mt')
    generate double `mu' = exp(`mut')
    */

    ** initialize the data
    generate int `choice' = $ML_y1
    generate double `p1' = $ML_y2
    generate double `p2' = $ML_y3
    generate double `z1' = $ML_y4
    generate double `z2' = $ML_y5
    generate double `ce_x' = $ML_y6
    generate double `omega' = $ML_y7
    generate double `n_star' = $ML_y8

    }



    cap mata: mata drop f_outer()
    cap mata: mata drop f_inner()

    mata:
    mata drop f_outer()
    mata drop f_inner()

    real rowvector f_outer(real rowvector x, real rowvector y)
    {
    return( exp(-(x:+exp(-x)) :- (y:+exp(-y))) )
    }

    real rowvector f_inner(real rowvector y)
    {
    for(i=1; i<=cols(y);i++) {
    if (i==1) f=integrate(&f_outer(), `omega', . , 40, y[i])
    else f = f, integrate(&f_outer(), `omega', . , 40, y[i])
    }
    return(f)
    }

    st_local("`result''", strofreal(integrate(&f_inner(), `alpha', .)))

    disp `result'


    * evaluate the likelihood
    replace `lnf' = `result' if `choice' ==1
    replace `lnf' = 1-`result' if `choice' ==0

    end




    Fix ML_program.


    - mata drop f_outer()
    f_outer() not found

  • #2
    I've always had errors when typing mata commands in a Stata program within a mata: end group, and have had to include many one line mata calls. You can always define mata functions outside the Stata program, and call them from within the Stata program. As a matter of fact I can't even see where the end for mata is. Also I can't see how you defined the integrate() function, but `alpha' and `omega' are variable names. If you're trying to pass a colvector with the variable you should be using st_data(), and if you're actually trying to pass a string with the name of the variable so that you get the vector within the function you should wrap your variable names within "".
    Alfonso Sanchez-Penalver

    Comment

    Working...
    X