Search code examples
pythonscipyscipy-optimizescipy-optimize-minimize

scipy.optimize.minimize returning [inf]


I am trying to call scipy.optimize.minimize to minimize a function poissonNegLogLikelihood, which is defined as follows:

def poissonNegLogLikelihood(lam, y):

  Computes the negative log-likelihood for a Poisson random variable.

  Inputs:
  lam - float or array.  Parameter for the poisson distribution.
  y - float or array.  Observed data.

  Outputs:
  log_lik - float.  The negative log-likelihood for the data (y) with parameter (lam).

  Y = np.atleast_1d(y)
  LAM = np.atleast_1d(lam)
  log_lik = -np.sum(np.multiply(Y,np.log(LAM)) - gammaln(Y+1) - LAM)
  return log_lik

This function works fine, but when I try to use it as input to scipy.optimize.minimize it return [inf]. This is how I'm passing it:

data = np.array([1.0])
betas = np.array([0])
minimize(poissonNegLogLikelihood,betas,args=(data),jac=False)

Am I using the scipy.optimize.minimize function wrong?


Solution

  • You are computing np.log(LAM = beats = [0]) in poissonNegLogLikelihood() and log(0) is -inf. Therefore, it seems to me that your initial guess betas is the problem. You should test with adequate values.