From 8a3ba828b66c7834f95970fe3020b8516b1b4d5b Mon Sep 17 00:00:00 2001 From: Anton Soloviev Date: Mon, 19 Oct 2026 14:46:33 -0700 Subject: [PATCH] Use censoring thresholds in Gaussian noise sensitivities --- CHANGELOG.md | 1 + pints/_log_likelihoods.py | 5 ++- pints/tests/test_log_likelihoods.py | 48 +++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 142e81ded..2e212ac70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. ### Deprecated ### Removed ### Fixed +- Fixed censored Gaussian noise sensitivities when data values lie beyond the censoring limits. - [#1746](https://github.com/pints-team/pints/pull/1746) Corrected the function signature in the docstring for `OptimisationController.set_callback`. diff --git a/pints/_log_likelihoods.py b/pints/_log_likelihoods.py index 025aea6c2..274130c98 100644 --- a/pints/_log_likelihoods.py +++ b/pints/_log_likelihoods.py @@ -508,13 +508,13 @@ def evaluateS1(self, x): # Calculate derivative wrt sigma lower_dsigma_inner_val = (lower_pdf.T * - (self._values - y).T) / (lower_cdf.T) + (self._a - y).T) / (lower_cdf.T) lower_censored_dsigma = - sigma**(-2) *\ np.sum(lower_dsigma_inner_val.T, where=self._lower_condition, axis=0).T upper_dsigma_inner_val = (upper_pdf.T * - (self._values - y).T) / (1 - upper_cdf.T) + (self._b - y).T) / (1 - upper_cdf.T) upper_censored_dsigma = sigma**(-2) *\ np.sum(upper_dsigma_inner_val.T, where=self._upper_condition, axis=0).T @@ -1813,4 +1813,3 @@ def __init__(self, problem): 'The class `pints.UnknownNoiseLogLikelihood` is deprecated.' ' Please use `pints.GaussianLogLikelihood` instead.') super().__init__(problem) - diff --git a/pints/tests/test_log_likelihoods.py b/pints/tests/test_log_likelihoods.py index e10a43dc3..7c909022a 100755 --- a/pints/tests/test_log_likelihoods.py +++ b/pints/tests/test_log_likelihoods.py @@ -240,6 +240,54 @@ def setUpClass(cls): [1.1, 0.0, -5.5]]) cls.sigma = 0.1 + def test_evaluateS1_values_beyond_limits(self): + # Censored values contribute their threshold probability, regardless + # of how far beyond the threshold the recorded value lies. + times = np.array([0., 1., 2., 3.]) + data = np.array([[-0.4, 0.4, -0.2], [0.6, 1.2, 0.5], + [1.4, 1.8, 1.5], [2.2, 3.2, 2.5]]) + for n_outputs in (1, 2, 3): + for lower, upper in ((None, None), (0.5, None), + (None, 1.6), (0.5, 1.6)): + with self.subTest(n_outputs=n_outputs, + lower=lower, upper=upper): + model = pints.toy.ConstantModel(n_outputs) + values = data[:, :n_outputs] + clipped = np.clip(values, + -np.inf if lower is None else lower, + np.inf if upper is None else upper) + if n_outputs == 1: + problem = pints.SingleOutputProblem( + model, times, values[:, 0]) + clipped_problem = pints.SingleOutputProblem( + model, times, clipped[:, 0]) + else: + problem = pints.MultiOutputProblem( + model, times, values) + clipped_problem = pints.MultiOutputProblem( + model, times, clipped) + likelihood = pints.CensoredGaussianLogLikelihood( + problem, lower, upper) + clipped_likelihood = pints.CensoredGaussianLogLikelihood( + clipped_problem, lower, upper) + parameters = np.r_[[0.7, 0.4, 0.3][:n_outputs], + [0.9, 1.1, 1.3][:n_outputs]] + with np.errstate(divide='ignore', invalid='ignore'): + score, gradient = likelihood.evaluateS1(parameters) + clipped_score, clipped_gradient = ( + clipped_likelihood.evaluateS1(parameters)) + self.assertEqual(score, clipped_score) + np.testing.assert_allclose(gradient, clipped_gradient) + expected = np.empty_like(parameters) + for j in range(len(parameters)): + step = np.zeros_like(parameters) + step[j] = 1e-5 + expected[j] = (likelihood(parameters + step) + - likelihood(parameters - step) + ) / (2 * step[j]) + np.testing.assert_allclose(gradient, expected, + rtol=1e-5, atol=1e-5) + def test_call_list_none_censored(self): values = self.data_single.tolist() problem = pints.SingleOutputProblem(