Skip to content
Snippets Groups Projects
Commit d973efae authored by Matthew David Pitkin's avatar Matthew David Pitkin
Browse files

likelihood.py: some fixes to PoissonLikelihood

 - change from using numpy bincount to check for non-negative
   values, as large values can cause a memory error
 - make sure log likelihood returns -inf if any rate values
   are zero
 - refs Monash/tupak!132
parent dd4dda13
No related branches found
No related tags found
1 merge request!132Add Poisson likelihood to core likelihood functions
......@@ -143,12 +143,14 @@ class PoissonLikelihood(Likelihood):
# check values are non-negative integers
if isinstance(self.counts, int):
# convert to numpy array if passing a single integer
self.counts = np.array(self.counts)
self.counts = np.array([self.counts])
try:
# use bincount to check all values are non-negative integers
_ = np.bincount(self.counts)
except ValueError:
# check array is an integer array
if self.counts.dtype.kind not in 'ui':
raise ValueError("Data must be non-negative integers")
# check for non-negative integers
if np.any(self.counts < 0):
raise ValueError("Data must be non-negative integers")
# save sum of log factorial of counts
......@@ -187,16 +189,22 @@ class PoissonLikelihood(Likelihood):
raise ValueError(("Poisson rate function returns a negative ",
"value!"))
# Return the summed log likelihood
return (-self.N*rate + np.sum(self.counts*np.log(rate))
- self.sumlogfactorial)
if rate == 0.:
return -np.inf
else:
# Return the summed log likelihood
return (-self.N*rate + np.sum(self.counts*np.log(rate))
-self.sumlogfactorial)
elif isinstance(rate, np.ndarray):
# check rates are positive
if np.any(rate < 0.):
raise ValueError(("Poisson rate function returns a negative",
" value!"))
return (np.sum(-rate + self.counts*np.log(rate))
- self.sumlogfactorial)
if np.any(rate == 0.):
return -np.inf
else:
return (np.sum(-rate + self.counts*np.log(rate))
-self.sumlogfactorial)
else:
raise ValueError("Poisson rate function returns wrong value type!")
\ No newline at end of file
raise ValueError("Poisson rate function returns wrong value type!")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment