Skip to content

Commit

Permalink
verify negative binomial objective function
Browse files Browse the repository at this point in the history
  • Loading branch information
twallema committed Sep 6, 2024
1 parent af4dc05 commit e55afc9
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/pySODM/optimization/objective_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,13 @@ def ll_negative_binomial(ymodel, ydata, alpha):
ll: float
Loglikelihood belonging to the comparison of the data points and the model prediction.
"""

# Expand first dimensions on 'alpha' to match the axes
alpha = np.array(alpha)[np.newaxis, ...]
# Convert datatype to float
ymodel = ymodel.astype('float64')
ydata = ydata.astype('float64')
# Raise ymodel or ydata if there are negative values present
if ((np.min(ymodel) < 0) | (np.min(ydata) < 0)):
offset_value = (-1 - 1e-6)*np.min([np.min(ymodel), np.min(ydata)])
ymodel += offset_value
ydata += offset_value
#warnings.warn(f"One or more values in the prediction were negative thus the prediction was offset, minimum predicted value: {min(ymodel)}")
elif ((np.min(ymodel) == 0) | (np.min(ydata) == 0)):
ymodel += 1e-6
ydata += 1e-6

# Convert datatype to float and add one
ymodel = ymodel.astype('float64') + 1
ydata = ydata.astype('float64') + 1

return np.sum(ydata*np.log(ymodel)) - np.sum((ydata + 1/alpha)*np.log(1+alpha*ymodel)) + np.sum(ydata*np.log(alpha)) + np.sum(gammaln(ydata+1/alpha)) - np.sum(gammaln(ydata+1))- np.sum(ydata.shape[0]*gammaln(1/alpha))

Expand Down

0 comments on commit e55afc9

Please sign in to comment.