From 7af784293c4da14a80b8c73224c6863015a9573d Mon Sep 17 00:00:00 2001 From: Davis Rens <166565828+davrens101@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:38:54 -0800 Subject: [PATCH] Exclude lag-0 autocorrelation from effective sample size denominator Signed-off-by: Davis Rens <166565828+davrens101@users.noreply.github.com> --- pints/_diagnostics.py | 2 +- pints/tests/test_diagnostics.py | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pints/_diagnostics.py b/pints/_diagnostics.py index 7332011d1..fe41c4d80 100644 --- a/pints/_diagnostics.py +++ b/pints/_diagnostics.py @@ -41,7 +41,7 @@ def effective_sample_size_single_parameter(x): rho = autocorrelation(x) T = _autocorrelate_negative(rho) n = len(x) - ess = n / (1 + 2 * np.sum(rho[0:T])) + ess = n / (1 + 2 * np.sum(rho[1:T])) return ess diff --git a/pints/tests/test_diagnostics.py b/pints/tests/test_diagnostics.py index dad293938..6722cf384 100755 --- a/pints/tests/test_diagnostics.py +++ b/pints/tests/test_diagnostics.py @@ -44,13 +44,19 @@ def test_effective_sample_size_single_parameter(self): # For case with negative elements in x x = np.array([1, 2, 3, 4, -1, -1]) y = pints._diagnostics.effective_sample_size_single_parameter(x) - self.assertAlmostEqual(y, 1.75076, 5) + self.assertAlmostEqual(y, 4.20437956, 5) # Case with positive elements only in x x = np.array([1, 2, 3, 4, 1, 1]) self.assertAlmostEqual( pints._diagnostics.effective_sample_size_single_parameter(x), - 1.846154, 6) + 4.8, 6) + + # IID normal draws: ESS should be approximately equal to sample size n + rng = np.random.default_rng(0) + x_iid = rng.normal(size=20000) + ess_iid = pints._diagnostics.effective_sample_size_single_parameter(x_iid) + self.assertAlmostEqual(ess_iid / 20000, 1.0, delta=0.05) def test_effective_sample_size(self): # Tests ess for a matrix of parameters @@ -59,8 +65,8 @@ def test_effective_sample_size(self): x = np.transpose(np.array([[1.0, 1.1, 1.4, 1.3, 1.3], [1.0, 2.0, 3.0, 4.0, 5.0]])) y = pints._diagnostics.effective_sample_size(x) - self.assertAlmostEqual(y[0], 1.439232, 6) - self.assertAlmostEqual(y[1], 1.315789, 6) + self.assertAlmostEqual(y[0], 3.391959798994974, 6) + self.assertAlmostEqual(y[1], 2.7777777777777777, 6) # Bad calls self.assertRaisesRegex(