I want to use the maximum likelihood method to estimate the parameters of a function which involves integration. However, when i tried to run it, i got a message that "non-finite function value".
Here are the codes:
library("maxLik")
integrand <− function(x,para)
{
# print(para)
# dv, dv_T, dn, dn_T are the actual input data, here only gives one set of value for easy debugging
dv <- -1.4527280
dv_T <- 0.2038909
dn <- 17.5174383
dn_T <- 26.6993828
beta10 = para[1];
beta11 = para[2];
beta12 = para[3];
alpha1 = para[4];
beta20 = para[5];
beta21 = para[6];
beta22 = para[7];
alpha2 = para[8];
U_Cur = exp(beta10 + beta11*dv + beta12*dn + alpha1*x);
U_Tgt = exp(beta20 + beta21*dv_T + beta22*dn_T + alpha2*x);
# Update Start - 03/10/2021
if (is.infinite(U_Cur))
{
if (U_Cur<0)
{U_Cur=-2^1000}
else
{U_Cur=2^1000}
}
if (is.infinite(U_Tgt))
{
if (U_Tgt <0)
{U_Tgt =-2^1000}
else
{U_Tgt =2^1000}
}
# Update End - 03/10/2021
# print(U_Cur)
# print(U_Tgt)
P <- (U_Cur/(U_Cur+U_Tgt))
# print(P)
return(P)
}
integrand2 <− function(para1) {integrate( integrand, lower = -2 , upper = 2, para=para1)$value }
MLE<-maxLik(logLik=integrand2,start=c(0.5, 0.2, 0.1, 0.3, -0.5, 0.2, 0.1, 0.6))
After debug, i find the reason of the error is the value of U_Cur or U_Tgt sometimes may be 0 or Inf.
I have no idea about how to deal with that, I really appreciate any related proposal.
===================================
EDIT:
I have update the value of U_Cur, U_Tgt, if they are (-)infinite, set their value as (-)2^1000 (the code is added in the function body above, between “# Update - 03/10/2021”).
I could be misreading your code, but it looks like you are maximizing the following (log)likelihood:
LL = int^2_{-2} exp(5+x+theta) dx = exp(5+theta)int^2_{-2} exp(x) dx.
That likelihood will be maximized by sending theta to infinity. Is that the log likelihood you want?