Announcement

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

  • ml programming conditional logit gradient

    Hello,

    I'm trying to build a maximum likelihood estimator (mle) for conditional logit which would include gradient calculations (i.e. method d1 evaluator). In the code below, I have included a method d0 program but unfortunately I'm stuck with developing a method d1 program.I would appreciate if anyone will be willing to help me out with coding the gradient for clogit.

    My code includes a discrete choice model of household labour supply. Each household i (idhh_11 is an identifier variable for the household) can choose among j alternatives (each household could make a choice between 36 alternatives (rows)). The dependent variable is choice which is coded as 1 if the option is chosen and 0 otherwise. The independent variables are consumption (C), male's leisure (LM), female's leisure (LF) and a set of socio-demographic variables (Z).

    The log-likelihood function is:

    LL(b) = {sum_i}{sum_j}{y_ij}*ln{p_ij} , where p_ij = {exp({x_ij}b)} / {sum_k}{exp({x_ik}b)}

    The derivative of LL(b) with respect to b can be written as:
    d{LL(b)} / d{b} = {sum_i}{sum_j}{y_ij}*({x_ij}-{x_i_bar}) , where x_i_bar = {sum_k}{p_ik}{x_ik}

    The expressions above can be found in Cameron, A. C., & Trivedi, P. K. (2005). Microeconometrics: Methods and applications, page 548.

    This is the code I have so far

    Code:
    // programminf ML evaluator (d1 method)
    *******************************************
    cap prog drop myclogit
    prog myclogit
        version 14.2
        args todo b lnf g   // todo is subsequently ignored as it is always =0
        tempvar  xb numer sum denom L
        local d "$ML_y1"
        mleval `xb' = `b', eq(1)
        qui gen double `numer' = exp(`xb')
        qui by idhh_11: gen double `sum' = sum(`numer')
        qui by idhh_11: gen double `denom' = `sum'[_N]
        qui gen double `L' = `numer' / `denom'
        mlsum `lnf' = ln(`L') if `d' == 1
        if (`todo' == 0 | `lnf' >= .) exit
        // compute the gradient
        tempvar
        tempname d1
                                                      // this is where I should write the expression which I have derived  
        mlvecsum `lnf' `d1' = , eq(1) // this is where I should include the calculations (formula) //    
        matrix g = (`d1')
    
    
    // checking evaluator program and running estimation
    *********************************************************
    glo X "C LM LF Z"
    ml model d1 myclogit (choice = $X , nocons)
    ml check
    ml maximize
    end

    Thank you for your help.

    Marko
Working...
X