*! version 1.1.0 Jesper Sorensen 050598
program define zinb_ll
    version 5.0
    local f `1'
    local xbeta `2'   /* negative bin equation */
    local zgamma `3'  /* logit equation */
    local alpha `4'   /* dispersion paramter */
qui {
    tempvar rr a L1 L2 zg mu amu m xx
    gen double `a' = exp(`alpha')
    gen double `zg' = exp(`zgamma')
    gen double `mu' = exp(`xbeta')
    gen double `amu' = `a'*`mu'
    gen double `m' = 1/`a'
#delimit ;
    gen double `xx' = 1/(1+`amu')^`m';
    gen double `L1' = (`zg'/(1+`zg'))+(1/(1+`zg'))*
            (1/(1+`amu'))^`m' if $S_mldepn==0;
    replace `L1' = log(`L1');
    gen double `L2' = log(1/(1+`zg'))+
                lngamma($S_mldepn+1/`a')-lngamma($S_mldepn+1)
                - lngamma(1/`a') + $S_mldepn*ln(`amu'/(1+`amu'))-
                ln(1+`amu')/`a' if $S_mldepn>0;
#delimit cr
    replace `f' = cond($S_mldepn==0,`L1',`L2')
}
end