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
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
Comment