From 1f9755469d513844529b295b58795b0736fdcd40 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 23 Mar 2021 08:44:56 +0000 Subject: [PATCH 1/5] added 3 evaluate_pointwise_loglikelihoods --- pints/_log_likelihoods.py | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/pints/_log_likelihoods.py b/pints/_log_likelihoods.py index fa4397bdb0..9efc6131e6 100644 --- a/pints/_log_likelihoods.py +++ b/pints/_log_likelihoods.py @@ -352,6 +352,26 @@ def __call__(self, parameters): return log_likelihood + def evaluate_pointwise_loglikelihoods(self, parameters): + """ + Returns a matrix of size nt x no containing the loglikelihood of each observation and at each time point + for the given parameters + """ + noise_parameters = np.asarray(parameters[-self._np:]) + sigma_base = noise_parameters[:self._no] + eta = noise_parameters[self._no:2 * self._no] + sigma_rel = noise_parameters[2 * self._no:] + + # Evaluate function and compute intermediate values + function_values = self._problem.evaluate(parameters[:-self._np]) + error = self._values - function_values + sigma_tot = sigma_base + sigma_rel * function_values**eta + + # Compute the pointwise log-likelihoods for each timepoint and observation + pointwise = -0.5 * np.log(2 * np.pi) - np.log(sigma_tot) - 0.5 * error**2/sigma_tot**2 + + return pointwise + def evaluateS1(self, parameters): r""" See :meth:`LogPDF.evaluateS1()`. @@ -739,6 +759,21 @@ def __call__(self, x): return np.sum(- self._logn - self._nt * np.log(sigma) - np.sum(error**2, axis=0) / (2 * sigma**2)) + def evaluate_pointwise_loglikelihoods(self, parameters): + """ + Returns a matrix of size nt x no containing the loglikelihood of each observation and at each time point + for the given parameters + """ + sigma = np.asarray(parameters[-self._no:]) + + # Compute intermediate values + error = self._values - self._problem.evaluate(parameters[:-self._no]) + + # Compute the pointwise log-likelihoods for each timepoint and observation + pointwise = -0.5 * np.log(2 * np.pi) - np.log(sigma) - error**2/ (2 * sigma**2) + + return pointwise + def evaluateS1(self, x): """ See :meth:`LogPDF.evaluateS1()`. """ sigma = np.asarray(x[-self._no:]) @@ -874,6 +909,24 @@ def __call__(self, x): return log_likelihood + def evaluate_pointwise_loglikelihoods(self, parameters): + """ + Returns a matrix of size nt x no containing the loglikelihood of each observation and at each time point + for the given parameters + """ + noise_parameters = np.asarray(parameters[-self._np:]) + eta = np.asarray(noise_parameters[0::2]) + sigma = np.asarray(noise_parameters[1::2]) + + # Compute intermediate values + function_values = self._problem.evaluate(parameters[:-self._np]) + error = self._values - function_values + sigma_tot = function_values**eta * sigma + + # Compute the pointwise log-likelihoods for each timepoint and observation + pointwise = -0.5 * np.log(2 * np.pi) - np.log(sigma_tot) - error**2/ (2 * sigma_tot**2) + return pointwise + class ScaledLogLikelihood(pints.ProblemLogLikelihood): """ From 6874c9a3ea99bbbc36446f74eace962028bcfc02 Mon Sep 17 00:00:00 2001 From: Rebecca-Rumney Date: Tue, 30 Mar 2021 08:29:11 +0100 Subject: [PATCH 2/5] Addded and tested evaluate_pointwise_logliklihoods --- pints/_log_likelihoods.py | 86 ++++- pints/_log_pdfs.py | 3 + pints/tests/test_log_likelihoods.py | 540 ++++++++++++++++++++++++++++ 3 files changed, 610 insertions(+), 19 deletions(-) diff --git a/pints/_log_likelihoods.py b/pints/_log_likelihoods.py index 9efc6131e6..65fe9e703b 100644 --- a/pints/_log_likelihoods.py +++ b/pints/_log_likelihoods.py @@ -93,7 +93,6 @@ def __call__(self, x): return np.sum(- self._logn - self._nt * np.log(sigma) - np.sum(autocorr_error**2, axis=0) / (2 * sigma**2)) - class ARMA11LogLikelihood(pints.ProblemLogLikelihood): r""" Calculates a log-likelihood assuming ARMA(1,1) errors. @@ -236,6 +235,24 @@ def __call__(self, x): - np.sum(np.log(1 + (error / sigma)**2), axis=0) ) + def evaluate_pointwise_loglikelihoods(self, x): + """ + Returns a matrix of size nt x no containing the loglikelihood of each observation and at + each time point for the given parameters + """ + m = self._no + + # problem parameters + problem_parameters = x[:-m] + error = self._values - self._problem.evaluate(problem_parameters) + + # Distribution parameters + sigma = np.asarray(x[-m:]) + + # Calculate + return - np.log(np.pi) - np.log(sigma) - np.log(1 + (error / sigma)**2) + + class ConstantAndMultiplicativeGaussianLogLikelihood( pints.ProblemLogLikelihood): @@ -354,14 +371,14 @@ def __call__(self, parameters): def evaluate_pointwise_loglikelihoods(self, parameters): """ - Returns a matrix of size nt x no containing the loglikelihood of each observation and at each time point - for the given parameters + Returns a matrix of size nt x no containing the loglikelihood of each observation and at + each time point for the given parameters """ noise_parameters = np.asarray(parameters[-self._np:]) sigma_base = noise_parameters[:self._no] eta = noise_parameters[self._no:2 * self._no] sigma_rel = noise_parameters[2 * self._no:] - + # Evaluate function and compute intermediate values function_values = self._problem.evaluate(parameters[:-self._np]) error = self._values - function_values @@ -662,8 +679,8 @@ def __init__(self, problem, sigma): raise ValueError('Standard deviation must be greater than zero.') # Pre-calculate parts - self._offset = -0.5 * self._nt * np.log(2 * np.pi) - self._offset -= self._nt * np.log(sigma) + self._offset_no_sum = -0.5 * np.log(2 * np.pi) - np.log(sigma) + self._offset = self._offset_no_sum * self._nt self._multip = -1 / (2.0 * sigma**2) # Pre-calculate S1 parts @@ -673,6 +690,15 @@ def __call__(self, x): error = self._values - self._problem.evaluate(x) return np.sum(self._offset + self._multip * np.sum(error**2, axis=0)) + + def evaluate_pointwise_loglikelihoods(self, x): + """ + Returns a matrix of size nt x no containing the loglikelihood of each observation and at + each time point for the given parameters + """ + error = self._values - self._problem.evaluate(x) + return self._offset_no_sum + self._multip * error**2 + def evaluateS1(self, x): """ See :meth:`LogPDF.evaluateS1()`. """ # Evaluate, and get residuals @@ -759,15 +785,15 @@ def __call__(self, x): return np.sum(- self._logn - self._nt * np.log(sigma) - np.sum(error**2, axis=0) / (2 * sigma**2)) - def evaluate_pointwise_loglikelihoods(self, parameters): + def evaluate_pointwise_loglikelihoods(self, x): """ - Returns a matrix of size nt x no containing the loglikelihood of each observation and at each time point - for the given parameters + Returns a matrix of size nt x no containing the loglikelihood of each observation and at + each time point for the given parameters """ - sigma = np.asarray(parameters[-self._no:]) - + sigma = np.asarray(x[-self._no:]) + # Compute intermediate values - error = self._values - self._problem.evaluate(parameters[:-self._no]) + error = self._values - self._problem.evaluate(x[:-self._no]) # Compute the pointwise log-likelihoods for each timepoint and observation pointwise = -0.5 * np.log(2 * np.pi) - np.log(sigma) - error**2/ (2 * sigma**2) @@ -909,20 +935,20 @@ def __call__(self, x): return log_likelihood - def evaluate_pointwise_loglikelihoods(self, parameters): + def evaluate_pointwise_loglikelihoods(self, x): """ - Returns a matrix of size nt x no containing the loglikelihood of each observation and at each time point - for the given parameters + Returns a matrix of size nt x no containing the loglikelihood of each observation and at + each time point for the given parameters """ - noise_parameters = np.asarray(parameters[-self._np:]) + noise_parameters = np.asarray(x[-self._np:]) eta = np.asarray(noise_parameters[0::2]) sigma = np.asarray(noise_parameters[1::2]) - + # Compute intermediate values - function_values = self._problem.evaluate(parameters[:-self._np]) + function_values = self._problem.evaluate(x[:-self._np]) error = self._values - function_values sigma_tot = function_values**eta * sigma - + # Compute the pointwise log-likelihoods for each timepoint and observation pointwise = -0.5 * np.log(2 * np.pi) - np.log(sigma_tot) - error**2/ (2 * sigma_tot**2) return pointwise @@ -1041,6 +1067,28 @@ def __call__(self, x): - 0.5 * (1 + nu) * np.sum(np.log(nu + (error / sigma)**2), axis=0) ) + def evaluate_pointwise_loglikelihoods(self, x): + """ + Returns a matrix of size nt x no containing the loglikelihood of each observation and at + each time point for the given parameters + """ + m = 2 * self._no + + # problem parameters + problem_parameters = x[:-m] + error = self._values - self._problem.evaluate(problem_parameters) + + # Distribution parameters + parameters = x[-m:] + nu = np.asarray(parameters[0::2]) + sigma = np.asarray(parameters[1::2]) + + # Calculate + return (0.5 * nu * np.log(nu) + - np.log(sigma) + - np.log(scipy.special.beta(0.5 * nu, 0.5)) + - 0.5 * (1 + nu) * np.log(nu + (error / sigma)**2)) + class UnknownNoiseLogLikelihood(GaussianLogLikelihood): """ diff --git a/pints/_log_pdfs.py b/pints/_log_pdfs.py index 98334d1491..e85903816b 100644 --- a/pints/_log_pdfs.py +++ b/pints/_log_pdfs.py @@ -336,6 +336,9 @@ def n_parameters(self): """ See :meth:`LogPDF.n_parameters()`. """ return self._n_parameters + def evaluate_pointwise_loglikelihoods(self, x): + raise NotImplementedError + class LogPosterior(LogPDF): """ diff --git a/pints/tests/test_log_likelihoods.py b/pints/tests/test_log_likelihoods.py index 67b66fb524..d37bd1edf7 100755 --- a/pints/tests/test_log_likelihoods.py +++ b/pints/tests/test_log_likelihoods.py @@ -277,6 +277,96 @@ def test_call_two_dim_array_multi(self): # Check that likelihood returns expected value self.assertEqual(score, -49.51182454195375) + def test_evaluate_pointwise_loglikelihoods_list(self): + # Convert data to list + values = self.data_single.tolist() + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.CauchyLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [0, 10] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): + # Convert data to array of shape (n_times,) + values = np.reshape(self.data_single, (self.n_times,)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.CauchyLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [0, 10] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): + # Convert data to array of shape (n_times, 1) + values = np.reshape(self.data_single, (self.n_times, 1)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.CauchyLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [0, 10] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): + # Create an object with links to the model and time series + problem = pints.MultiOutputProblem( + self.model_multi, self.times, self.data_multi) + + # Create log_likelihood + log_likelihood = pints.CauchyLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [ + 0, 0, 0, 0, 13, 8, 13.5, 10.5] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + class TestConstantAndMultiplicativeGaussianLogLikelihood(unittest.TestCase): @@ -448,6 +538,100 @@ def test_call_multiplicative_gaussian_log_likelihood_agrees_multi(self): multi_score = multi_log_likelihood(multi_test_parameters) self.assertAlmostEqual(score, multi_score) + def test_evaluate_pointwise_loglikelihoods_list(self): + # Convert data to list + values = self.data_single.tolist() + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( + problem) + + # Evaluate likelihood for test parameters + test_parameters = [2.0, 0.5, 1.1, 1.0] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): + # Convert data to array of shape (n_times,) + values = np.reshape(self.data_single, (self.n_times,)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( + problem) + + # Evaluate likelihood for test parameters + test_parameters = [2.0, 0.5, 1.1, 1.0] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): + # Convert data to array of shape (n_times, 1) + values = np.reshape(self.data_single, (self.n_times, 1)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( + problem) + + # Evaluate likelihood for test parameters + test_parameters = [2.0, 0.5, 1.1, 1.0] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): + # Create an object with links to the model and time series + problem = pints.MultiOutputProblem( + self.model_multi, self.times, self.data_multi) + + # Create log_likelihood + log_likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( + problem) + + # Evaluate likelihood for test parameters + test_parameters = [ + 2.0, 2.0, 2.0, 0.5, 0.5, 0.5, 1.1, 1.1, 1.1, 1.0, 1.0, 1.0] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + def test_evaluateS1_list(self): # Convert data to list values = self.data_single.tolist() @@ -999,6 +1183,95 @@ def test_call_two_dim_array_multi(self): # Check that likelihood returns expected value self.assertEqual(score, -196.9122623984561) + def test_evaluate_pointwise_loglikelihoods_list(self): + # Convert data to list + values = self.data_single.tolist() + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1.5) + + # Evaluate likelihood for test parameters + test_parameters = [-1] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): + # Convert data to array of shape (n_times,) + values = np.reshape(self.data_single, (self.n_times,)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1.5) + + # Evaluate likelihood for test parameters + test_parameters = [-1] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): + # Convert data to array of shape (n_times, 1) + values = np.reshape(self.data_single, (self.n_times, 1)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1.5) + + # Evaluate likelihood for test parameters + test_parameters = [-1] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): + # Create an object with links to the model and time series + problem = pints.MultiOutputProblem( + self.model_multi, self.times, self.data_multi) + + # Create log_likelihood + log_likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1) + + # Evaluate likelihood for test parameters + test_parameters = [0, 0, 0] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + def test_evaluateS1_list(self): # Convert data to list values = self.data_single.tolist() @@ -1243,6 +1516,95 @@ def test_call_two_dim_array_multi(self): # Check that likelihood returns expected value self.assertEqual(score, -50.75425117450455) + def test_evaluate_pointwise_loglikelihoods_list(self): + # Convert data to list + values = self.data_single.tolist() + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.GaussianLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [2, self.sigma] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): + # Convert data to array of shape (n_times,) + values = np.reshape(self.data_single, (self.n_times,)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.GaussianLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [2, self.sigma] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): + # Convert data to array of shape (n_times, 1) + values = np.reshape(self.data_single, (self.n_times, 1)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.GaussianLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [2, self.sigma] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): + # Create an object with links to the model and time series + problem = pints.MultiOutputProblem( + self.model_multi, self.times, self.data_multi) + + # Create log_likelihood + log_likelihood = pints.GaussianLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [0, 0, 0, 0.1, 0.1, 0.1] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + def test_evaluateS1_list(self): # Convert data to list values = self.data_single.tolist() @@ -1517,6 +1879,95 @@ def test_call_two_dim_array_multi(self): # Check that likelihood returns expected value self.assertEqual(score, -46.324126706784014) + def test_evaluate_pointwise_loglikelihoods_list(self): + # Convert data to list + values = self.data_single.tolist() + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [2.0, 2.0, 1.0] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): + # Convert data to array of shape (n_times,) + values = np.reshape(self.data_single, (self.n_times,)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [2.0, 2.0, 1.0] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): + # Convert data to array of shape (n_times, 1) + values = np.reshape(self.data_single, (self.n_times, 1)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [2.0, 2.0, 1.0] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): + # Create an object with links to the model and time series + problem = pints.MultiOutputProblem( + self.model_multi, self.times, self.data_multi) + + # Create log_likelihood + log_likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [2.0, 2.0, 2.0, 2.0, 1.0, 2.0, 1.0, 2.0, 1.0] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + class TestScaledLogLikelihood(unittest.TestCase): @@ -1884,6 +2335,95 @@ def test_call_two_dim_array_multi(self): # Check that scaled likelihood returns expected value self.assertEqual(score, -47.83720347766944) + def test_evaluate_pointwise_loglikelihoods_list(self): + # Convert data to list + values = self.data_single.tolist() + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.StudentTLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [0, 3, 10] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): + # Convert data to array of shape (n_times,) + values = np.reshape(self.data_single, (self.n_times,)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.StudentTLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [0, 3, 10] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): + # Convert data to array of shape (n_times, 1) + values = np.reshape(self.data_single, (self.n_times, 1)) + + # Create an object with links to the model and time series + problem = pints.SingleOutputProblem( + self.model_single, self.times, values) + + # Create log_likelihood + log_likelihood = pints.StudentTLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [0, 3, 10] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + + def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): + # Create an object with links to the model and time series + problem = pints.MultiOutputProblem( + self.model_multi, self.times, self.data_multi) + + # Create log_likelihood + log_likelihood = pints.StudentTLogLikelihood(problem) + + # Evaluate likelihood for test parameters + test_parameters = [0, 0, 0, 0, 2, 13, 1, 8, 2.5, 13.5, 3.4, 10.5] + pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + + # Check that the shape of the loglikelihoods match the shape of the data + self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + + # Check that the sum of the pointwise loglikelihoods agrees with call + # There are floating point deviations because the summation occurs at different points + # in __call__() + self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + if __name__ == '__main__': unittest.main() From 75a6db8fb0139064abfc3ead6cdb11d7f7e6b46e Mon Sep 17 00:00:00 2001 From: Rebecca-Rumney Date: Tue, 30 Mar 2021 09:19:16 +0100 Subject: [PATCH 3/5] doc string changes --- pints/_log_likelihoods.py | 36 +++++++++--------------------------- pints/_log_pdfs.py | 8 ++++++++ 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/pints/_log_likelihoods.py b/pints/_log_likelihoods.py index 65fe9e703b..0b520a8fa4 100644 --- a/pints/_log_likelihoods.py +++ b/pints/_log_likelihoods.py @@ -236,10 +236,7 @@ def __call__(self, x): ) def evaluate_pointwise_loglikelihoods(self, x): - """ - Returns a matrix of size nt x no containing the loglikelihood of each observation and at - each time point for the given parameters - """ + """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ m = self._no # problem parameters @@ -369,18 +366,15 @@ def __call__(self, parameters): return log_likelihood - def evaluate_pointwise_loglikelihoods(self, parameters): - """ - Returns a matrix of size nt x no containing the loglikelihood of each observation and at - each time point for the given parameters - """ - noise_parameters = np.asarray(parameters[-self._np:]) + def evaluate_pointwise_loglikelihoods(self, x): + """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ + noise_parameters = np.asarray(x[-self._np:]) sigma_base = noise_parameters[:self._no] eta = noise_parameters[self._no:2 * self._no] sigma_rel = noise_parameters[2 * self._no:] # Evaluate function and compute intermediate values - function_values = self._problem.evaluate(parameters[:-self._np]) + function_values = self._problem.evaluate(x[:-self._np]) error = self._values - function_values sigma_tot = sigma_base + sigma_rel * function_values**eta @@ -692,10 +686,7 @@ def __call__(self, x): def evaluate_pointwise_loglikelihoods(self, x): - """ - Returns a matrix of size nt x no containing the loglikelihood of each observation and at - each time point for the given parameters - """ + """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ error = self._values - self._problem.evaluate(x) return self._offset_no_sum + self._multip * error**2 @@ -786,10 +777,7 @@ def __call__(self, x): - np.sum(error**2, axis=0) / (2 * sigma**2)) def evaluate_pointwise_loglikelihoods(self, x): - """ - Returns a matrix of size nt x no containing the loglikelihood of each observation and at - each time point for the given parameters - """ + """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ sigma = np.asarray(x[-self._no:]) # Compute intermediate values @@ -936,10 +924,7 @@ def __call__(self, x): return log_likelihood def evaluate_pointwise_loglikelihoods(self, x): - """ - Returns a matrix of size nt x no containing the loglikelihood of each observation and at - each time point for the given parameters - """ + """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ noise_parameters = np.asarray(x[-self._np:]) eta = np.asarray(noise_parameters[0::2]) sigma = np.asarray(noise_parameters[1::2]) @@ -1068,10 +1053,7 @@ def __call__(self, x): ) def evaluate_pointwise_loglikelihoods(self, x): - """ - Returns a matrix of size nt x no containing the loglikelihood of each observation and at - each time point for the given parameters - """ + """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ m = 2 * self._no # problem parameters diff --git a/pints/_log_pdfs.py b/pints/_log_pdfs.py index e85903816b..9c2ff8904d 100644 --- a/pints/_log_pdfs.py +++ b/pints/_log_pdfs.py @@ -337,6 +337,14 @@ def n_parameters(self): return self._n_parameters def evaluate_pointwise_loglikelihoods(self, x): + """ + Evaluates the Log-likelihood at each observation for the given parameters, x. Returns a + numpy array of length no. timepoints if no. outputs = 1. Otherwise returns a 2d array of + size no. timepoints by no. outputs. + + *This is an optional method that is not always implemented.* + """ + raise NotImplementedError From 1ec76cb934fa2eca396e62cc7ea8ad62d587b086 Mon Sep 17 00:00:00 2001 From: Rebecca-Rumney Date: Tue, 30 Mar 2021 09:59:56 +0100 Subject: [PATCH 4/5] Minor changes for flake8 --- pints/_log_likelihoods.py | 45 +++++++++++++++++++++++++++------------ pints/_log_pdfs.py | 7 +++--- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/pints/_log_likelihoods.py b/pints/_log_likelihoods.py index 0b520a8fa4..04e528039a 100644 --- a/pints/_log_likelihoods.py +++ b/pints/_log_likelihoods.py @@ -93,6 +93,7 @@ def __call__(self, x): return np.sum(- self._logn - self._nt * np.log(sigma) - np.sum(autocorr_error**2, axis=0) / (2 * sigma**2)) + class ARMA11LogLikelihood(pints.ProblemLogLikelihood): r""" Calculates a log-likelihood assuming ARMA(1,1) errors. @@ -236,7 +237,9 @@ def __call__(self, x): ) def evaluate_pointwise_loglikelihoods(self, x): - """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ + """ + See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. + """ m = self._no # problem parameters @@ -250,7 +253,6 @@ def evaluate_pointwise_loglikelihoods(self, x): return - np.log(np.pi) - np.log(sigma) - np.log(1 + (error / sigma)**2) - class ConstantAndMultiplicativeGaussianLogLikelihood( pints.ProblemLogLikelihood): r""" @@ -367,7 +369,9 @@ def __call__(self, parameters): return log_likelihood def evaluate_pointwise_loglikelihoods(self, x): - """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ + """ + See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. + """ noise_parameters = np.asarray(x[-self._np:]) sigma_base = noise_parameters[:self._no] eta = noise_parameters[self._no:2 * self._no] @@ -378,8 +382,10 @@ def evaluate_pointwise_loglikelihoods(self, x): error = self._values - function_values sigma_tot = sigma_base + sigma_rel * function_values**eta - # Compute the pointwise log-likelihoods for each timepoint and observation - pointwise = -0.5 * np.log(2 * np.pi) - np.log(sigma_tot) - 0.5 * error**2/sigma_tot**2 + # Compute the pointwise log-likelihoods for each observation + pointwise = (- 0.5 * np.log(2 * np.pi) + - np.log(sigma_tot) + - 0.5 * error**2 / sigma_tot**2) return pointwise @@ -684,9 +690,10 @@ def __call__(self, x): error = self._values - self._problem.evaluate(x) return np.sum(self._offset + self._multip * np.sum(error**2, axis=0)) - def evaluate_pointwise_loglikelihoods(self, x): - """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ + """ + See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. + """ error = self._values - self._problem.evaluate(x) return self._offset_no_sum + self._multip * error**2 @@ -777,14 +784,18 @@ def __call__(self, x): - np.sum(error**2, axis=0) / (2 * sigma**2)) def evaluate_pointwise_loglikelihoods(self, x): - """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ + """ + See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. + """ sigma = np.asarray(x[-self._no:]) # Compute intermediate values error = self._values - self._problem.evaluate(x[:-self._no]) - # Compute the pointwise log-likelihoods for each timepoint and observation - pointwise = -0.5 * np.log(2 * np.pi) - np.log(sigma) - error**2/ (2 * sigma**2) + # Compute the pointwise log-likelihoods for each observation + pointwise = (-0.5 * np.log(2 * np.pi) + - np.log(sigma) + - error**2 / (2 * sigma**2)) return pointwise @@ -924,7 +935,9 @@ def __call__(self, x): return log_likelihood def evaluate_pointwise_loglikelihoods(self, x): - """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ + """ + See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. + """ noise_parameters = np.asarray(x[-self._np:]) eta = np.asarray(noise_parameters[0::2]) sigma = np.asarray(noise_parameters[1::2]) @@ -934,8 +947,10 @@ def evaluate_pointwise_loglikelihoods(self, x): error = self._values - function_values sigma_tot = function_values**eta * sigma - # Compute the pointwise log-likelihoods for each timepoint and observation - pointwise = -0.5 * np.log(2 * np.pi) - np.log(sigma_tot) - error**2/ (2 * sigma_tot**2) + # Compute the pointwise log-likelihoods for each observation + pointwise = (-0.5 * np.log(2 * np.pi) + - np.log(sigma_tot) + - error**2 / (2 * sigma_tot**2)) return pointwise @@ -1053,7 +1068,9 @@ def __call__(self, x): ) def evaluate_pointwise_loglikelihoods(self, x): - """ See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. """ + """ + See :meth:`ProblemLogLikelihood.evaluate_pointwise_loglikelihoods()`. + """ m = 2 * self._no # problem parameters diff --git a/pints/_log_pdfs.py b/pints/_log_pdfs.py index 9c2ff8904d..ce7f18b6a8 100644 --- a/pints/_log_pdfs.py +++ b/pints/_log_pdfs.py @@ -338,9 +338,10 @@ def n_parameters(self): def evaluate_pointwise_loglikelihoods(self, x): """ - Evaluates the Log-likelihood at each observation for the given parameters, x. Returns a - numpy array of length no. timepoints if no. outputs = 1. Otherwise returns a 2d array of - size no. timepoints by no. outputs. + Evaluates the Log-likelihood at each observation for the given + parameters, x. Returns a numpy array of length no. timepoints if no. + outputs = 1. Otherwise returns a 2d array of size no. timepoints by no. + outputs. *This is an optional method that is not always implemented.* """ From d7858d322831146bc27a47201909ed9a4a9ba05a Mon Sep 17 00:00:00 2001 From: Rebecca-Rumney Date: Wed, 31 Mar 2021 10:37:31 +0100 Subject: [PATCH 5/5] more flake8 corrections --- pints/tests/test_log_likelihoods.py | 387 ++++++++++++++-------------- 1 file changed, 193 insertions(+), 194 deletions(-) diff --git a/pints/tests/test_log_likelihoods.py b/pints/tests/test_log_likelihoods.py index d37bd1edf7..4432472da2 100755 --- a/pints/tests/test_log_likelihoods.py +++ b/pints/tests/test_log_likelihoods.py @@ -286,19 +286,19 @@ def test_evaluate_pointwise_loglikelihoods_list(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.CauchyLogLikelihood(problem) + likelihood = pints.CauchyLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [0, 10] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [0, 10] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): # Convert data to array of shape (n_times,) @@ -309,19 +309,19 @@ def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.CauchyLogLikelihood(problem) + likelihood = pints.CauchyLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [0, 10] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [0, 10] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): # Convert data to array of shape (n_times, 1) @@ -332,19 +332,19 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.CauchyLogLikelihood(problem) + likelihood = pints.CauchyLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [0, 10] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [0, 10] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): # Create an object with links to the model and time series @@ -352,20 +352,19 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): self.model_multi, self.times, self.data_multi) # Create log_likelihood - log_likelihood = pints.CauchyLogLikelihood(problem) + likelihood = pints.CauchyLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [ - 0, 0, 0, 0, 13, 8, 13.5, 10.5] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [0, 0, 0, 0, 13, 8, 13.5, 10.5] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, self.data_multi.shape) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) class TestConstantAndMultiplicativeGaussianLogLikelihood(unittest.TestCase): @@ -547,20 +546,20 @@ def test_evaluate_pointwise_loglikelihoods_list(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( + likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( problem) # Evaluate likelihood for test parameters - test_parameters = [2.0, 0.5, 1.1, 1.0] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [2.0, 0.5, 1.1, 1.0] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): # Convert data to array of shape (n_times,) @@ -571,20 +570,20 @@ def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( + likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( problem) # Evaluate likelihood for test parameters - test_parameters = [2.0, 0.5, 1.1, 1.0] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [2.0, 0.5, 1.1, 1.0] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): # Convert data to array of shape (n_times, 1) @@ -595,20 +594,20 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( + likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( problem) # Evaluate likelihood for test parameters - test_parameters = [2.0, 0.5, 1.1, 1.0] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [2.0, 0.5, 1.1, 1.0] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): # Create an object with links to the model and time series @@ -616,21 +615,21 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): self.model_multi, self.times, self.data_multi) # Create log_likelihood - log_likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( + likelihood = pints.ConstantAndMultiplicativeGaussianLogLikelihood( problem) # Evaluate likelihood for test parameters - test_parameters = [ + parameters = [ 2.0, 2.0, 2.0, 0.5, 0.5, 0.5, 1.1, 1.1, 1.1, 1.0, 1.0, 1.0] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, self.data_multi.shape) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluateS1_list(self): # Convert data to list @@ -1192,19 +1191,19 @@ def test_evaluate_pointwise_loglikelihoods_list(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1.5) + likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1.5) # Evaluate likelihood for test parameters - test_parameters = [-1] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [-1] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): # Convert data to array of shape (n_times,) @@ -1215,19 +1214,19 @@ def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1.5) + likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1.5) # Evaluate likelihood for test parameters - test_parameters = [-1] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [-1] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): # Convert data to array of shape (n_times, 1) @@ -1238,19 +1237,19 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1.5) + likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1.5) # Evaluate likelihood for test parameters - test_parameters = [-1] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [-1] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): # Create an object with links to the model and time series @@ -1258,19 +1257,19 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): self.model_multi, self.times, self.data_multi) # Create log_likelihood - log_likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1) + likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1) # Evaluate likelihood for test parameters - test_parameters = [0, 0, 0] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [0, 0, 0] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, self.data_multi.shape) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluateS1_list(self): # Convert data to list @@ -1525,19 +1524,19 @@ def test_evaluate_pointwise_loglikelihoods_list(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.GaussianLogLikelihood(problem) + likelihood = pints.GaussianLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [2, self.sigma] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [2, self.sigma] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): # Convert data to array of shape (n_times,) @@ -1548,19 +1547,19 @@ def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.GaussianLogLikelihood(problem) + likelihood = pints.GaussianLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [2, self.sigma] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [2, self.sigma] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): # Convert data to array of shape (n_times, 1) @@ -1571,19 +1570,19 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.GaussianLogLikelihood(problem) + likelihood = pints.GaussianLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [2, self.sigma] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [2, self.sigma] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): # Create an object with links to the model and time series @@ -1591,19 +1590,19 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): self.model_multi, self.times, self.data_multi) # Create log_likelihood - log_likelihood = pints.GaussianLogLikelihood(problem) + likelihood = pints.GaussianLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [0, 0, 0, 0.1, 0.1, 0.1] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [0, 0, 0, 0.1, 0.1, 0.1] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, self.data_multi.shape) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluateS1_list(self): # Convert data to list @@ -1879,6 +1878,7 @@ def test_call_two_dim_array_multi(self): # Check that likelihood returns expected value self.assertEqual(score, -46.324126706784014) + def test_evaluate_pointwise_loglikelihoods_list(self): # Convert data to list values = self.data_single.tolist() @@ -1888,19 +1888,19 @@ def test_evaluate_pointwise_loglikelihoods_list(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) + likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [2.0, 2.0, 1.0] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [2.0, 2.0, 1.0] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): # Convert data to array of shape (n_times,) @@ -1911,19 +1911,19 @@ def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) + likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [2.0, 2.0, 1.0] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [2.0, 2.0, 1.0] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): # Convert data to array of shape (n_times, 1) @@ -1934,19 +1934,19 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) + likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [2.0, 2.0, 1.0] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [2.0, 2.0, 1.0] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): # Create an object with links to the model and time series @@ -1954,20 +1954,19 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): self.model_multi, self.times, self.data_multi) # Create log_likelihood - log_likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) + likelihood = pints.MultiplicativeGaussianLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [2.0, 2.0, 2.0, 2.0, 1.0, 2.0, 1.0, 2.0, 1.0] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [2.0, 2.0, 2.0, 2.0, 1.0, 2.0, 1.0, 2.0, 1.0] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, self.data_multi.shape) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) - + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) class TestScaledLogLikelihood(unittest.TestCase): @@ -2344,19 +2343,19 @@ def test_evaluate_pointwise_loglikelihoods_list(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.StudentTLogLikelihood(problem) + likelihood = pints.StudentTLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [0, 3, 10] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [0, 3, 10] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): # Convert data to array of shape (n_times,) @@ -2367,19 +2366,19 @@ def test_evaluate_pointwise_loglikelihoods_one_dim_array(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.StudentTLogLikelihood(problem) + likelihood = pints.StudentTLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [0, 3, 10] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [0, 3, 10] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): # Convert data to array of shape (n_times, 1) @@ -2390,19 +2389,19 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_single(self): self.model_single, self.times, values) # Create log_likelihood - log_likelihood = pints.StudentTLogLikelihood(problem) + likelihood = pints.StudentTLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [0, 3, 10] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [0, 3, 10] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, (self.n_times,)) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, (self.n_times,)) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): # Create an object with links to the model and time series @@ -2410,19 +2409,19 @@ def test_evaluate_pointwise_loglikelihoods_two_dim_array_multi(self): self.model_multi, self.times, self.data_multi) # Create log_likelihood - log_likelihood = pints.StudentTLogLikelihood(problem) + likelihood = pints.StudentTLogLikelihood(problem) # Evaluate likelihood for test parameters - test_parameters = [0, 0, 0, 0, 2, 13, 1, 8, 2.5, 13.5, 3.4, 10.5] - pointwise_loglikelihoods = log_likelihood.evaluate_pointwise_loglikelihoods(test_parameters) + parameters = [0, 0, 0, 0, 2, 13, 1, 8, 2.5, 13.5, 3.4, 10.5] + pointwise = likelihood.evaluate_pointwise_loglikelihoods(parameters) - # Check that the shape of the loglikelihoods match the shape of the data - self.assertEqual(pointwise_loglikelihoods.shape, self.data_multi.shape) + # Check that the loglikelihoods match the shape of the data + self.assertEqual(pointwise.shape, self.data_multi.shape) # Check that the sum of the pointwise loglikelihoods agrees with call - # There are floating point deviations because the summation occurs at different points - # in __call__() - self.assertAlmostEqual(np.sum(pointwise_loglikelihoods), log_likelihood(test_parameters)) + # There are floating point deviations because the summation occurs at + # different points in __call__() + self.assertAlmostEqual(np.sum(pointwise), likelihood(parameters)) if __name__ == '__main__':