Search code examples
pythonnumpydata-sciencelog-likelihood

Pythonic way to calculate cumulative sum with complex math over numpy array


I'm performing Data Science and am calculating the Log Likelihood of a Poisson Distribution of arrival times.

def LogLikelihood(arrival_times, _lambda):
  """Calculate the likelihood that _lambda predicts the arrival_times well."""
  ll = 0
  for t in arrival_times:
    ll += -(_lambda) + t*np.log(_lambda) - np.log(factorial(t))
  return ll

Mathematically, the expression is on the last line:

Text

Is there a more pythonic way to perform this sum? Perhaps in one line?


Solution

  • Seems perfectly Pythonic to me; but since numpy is already here, why not to vectorize the whole thing?

    return (
        -_lambda
        + arrival_times * np.log(_lambda)
        - np.log(np.vectorize(np.math.factorial)(arrival_times))
    ).sum()