From 8be9b27b5497bd0f357c44baa834be3af9b6d646 Mon Sep 17 00:00:00 2001
From: Colm Talbot <colm.talbot@ligo.org>
Date: Mon, 17 Sep 2018 21:54:20 +1000
Subject: [PATCH] move log_likelihood methods

---
 tupak/core/likelihood.py | 59 ++++++++++++++++++++--------------------
 1 file changed, 30 insertions(+), 29 deletions(-)

diff --git a/tupak/core/likelihood.py b/tupak/core/likelihood.py
index 1ac787460..460ae7c55 100644
--- a/tupak/core/likelihood.py
+++ b/tupak/core/likelihood.py
@@ -153,15 +153,15 @@ class GaussianLikelihood(Analytical1DLikelihood):
         if self.sigma is None:
             self.parameters['sigma'] = None
 
-    def __repr__(self):
-        return self.__class__.__name__ + '(x={}, y={}, func={}, sigma={})'\
-            .format(self.x, self.y, self.func.__name__, self.sigma)
-
     def log_likelihood(self):
         log_l = np.sum(- (self.residual / self.sigma)**2 / 2
                        - np.log(2 * np.pi * self.sigma) / 2)
         return log_l
 
+    def __repr__(self):
+        return self.__class__.__name__ + '(x={}, y={}, func={}, sigma={})' \
+            .format(self.x, self.y, self.func.__name__, self.sigma)
+
     @property
     def sigma(self):
         """
@@ -205,6 +205,20 @@ class PoissonLikelihood(Analytical1DLikelihood):
 
         Analytical1DLikelihood.__init__(self, x=x, y=y, func=func)
 
+    def log_likelihood(self):
+        rate = self.func(self.x, **self.model_parameters)
+        if not isinstance(rate, np.ndarray):
+            raise ValueError(
+                "Poisson rate function returns wrong value type! "
+                "Is {} when it should be numpy.ndarray".format(type(rate)))
+        elif np.any(rate < 0.):
+            raise ValueError(("Poisson rate function returns a negative",
+                              " value!"))
+        elif np.any(rate == 0.):
+            return -np.inf
+        else:
+            return np.sum(-rate + self.y * np.log(rate) - gammaln(self.y + 1))
+
     def __repr__(self):
         return Analytical1DLikelihood.__repr__(self)
 
@@ -222,19 +236,6 @@ class PoissonLikelihood(Analytical1DLikelihood):
             raise ValueError("Data must be non-negative integers")
         self.__y = y
 
-    def log_likelihood(self):
-        rate = self.func(self.x, **self.model_parameters)
-        if not isinstance(rate, np.ndarray):
-            raise ValueError("Poisson rate function returns wrong value type! "
-                             "Is {} when it should be numpy.ndarray".format(type(rate)))
-        elif np.any(rate < 0.):
-            raise ValueError(("Poisson rate function returns a negative",
-                              " value!"))
-        elif np.any(rate == 0.):
-            return -np.inf
-        else:
-            return np.sum(-rate + self.y * np.log(rate)) - np.sum(gammaln(self.y + 1))
-
 
 class ExponentialLikelihood(Analytical1DLikelihood):
     def __init__(self, x, y, func):
@@ -255,6 +256,12 @@ class ExponentialLikelihood(Analytical1DLikelihood):
         """
         Analytical1DLikelihood.__init__(self, x=x, y=y, func=func)
 
+    def log_likelihood(self):
+        mu = self.func(self.x, **self.model_parameters)
+        if np.any(mu < 0.):
+            return -np.inf
+        return -np.sum(np.log(mu) + (self.y / mu))
+
     def __repr__(self):
         return Analytical1DLikelihood.__repr__(self)
 
@@ -271,12 +278,6 @@ class ExponentialLikelihood(Analytical1DLikelihood):
             raise ValueError("Data must be non-negative")
         self.__y = y
 
-    def log_likelihood(self):
-        mu = self.func(self.x, **self.model_parameters)
-        if np.any(mu < 0.):
-            return -np.inf
-        return -np.sum(np.log(mu) + (self.y / mu))
-
 
 class StudentTLikelihood(Analytical1DLikelihood):
     def __init__(self, x, y, func, nu=None, sigma=1.):
@@ -317,6 +318,12 @@ class StudentTLikelihood(Analytical1DLikelihood):
         if self.nu is None:
             self.parameters['nu'] = None
 
+    def log_likelihood(self):
+        if self.__get_nu() <= 0.:
+            raise ValueError("Number of degrees of freedom for Student's t-likelihood must be positive")
+
+        return self.__summed_log_likelihood(self.__get_nu())
+
     def __repr__(self):
         return self.__class__.__name__ + '(x={}, y={}, func={}, nu={}, sigma={})'\
             .format(self.x, self.y, self.func.__name__, self.nu, self.sigma)
@@ -326,12 +333,6 @@ class StudentTLikelihood(Analytical1DLikelihood):
         """ Converts 'scale' to 'precision' """
         return 1. / self.sigma ** 2
 
-    def log_likelihood(self):
-        if self.__get_nu() <= 0.:
-            raise ValueError("Number of degrees of freedom for Student's t-likelihood must be positive")
-
-        return self.__summed_log_likelihood(self.__get_nu())
-
     def __get_nu(self):
         """ This checks if nu or sigma have been set in parameters. If so, those
         values will be used. Otherwise, the attribute nu is used. The logic is
-- 
GitLab