From d973efae695f70642987edcb0edb85a0bebbb44e Mon Sep 17 00:00:00 2001 From: Matthew Pitkin <matthew.pitkin@ligo.org> Date: Thu, 2 Aug 2018 23:13:50 +0100 Subject: [PATCH] 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 --- tupak/core/likelihood.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/tupak/core/likelihood.py b/tupak/core/likelihood.py index e5b2dd053..26e91546b 100644 --- a/tupak/core/likelihood.py +++ b/tupak/core/likelihood.py @@ -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!") -- GitLab