From fd4c866fddaeff295edb67a8a62f3a34fb759e07 Mon Sep 17 00:00:00 2001 From: Simon Kuberski Date: Mon, 17 Oct 2022 14:38:23 +0200 Subject: [PATCH] fix: bug in automatic window for irregular chains fixed --- pyerrors/obs.py | 19 +++++++++++++------ tests/obs_test.py | 10 ++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pyerrors/obs.py b/pyerrors/obs.py index 24ef3720..481b71a8 100644 --- a/pyerrors/obs.py +++ b/pyerrors/obs.py @@ -279,14 +279,20 @@ class Obs: tmp = self.e_rho[e_name][i + 1:w_max] + np.concatenate([self.e_rho[e_name][i - 1::-1], self.e_rho[e_name][1:w_max - 2 * i]]) - 2 * self.e_rho[e_name][i] * self.e_rho[e_name][1:w_max - i] self.e_drho[e_name][i] = np.sqrt(np.sum(tmp ** 2) / e_N) - _compute_drho(1) + # detect regular step size in irregular MC chain + gapsize = 1 + for r_name in e_content[e_name][:1]: + if not isinstance(self.idl[r_name], range): + gapsize = np.min(np.diff(self.idl[r_name])) + + _compute_drho(gapsize) if self.tau_exp[e_name] > 0: texp = self.tau_exp[e_name] # Critical slowing down analysis if w_max // 2 <= 1: raise Exception("Need at least 8 samples for tau_exp error analysis") - for n in range(1, w_max // 2): - _compute_drho(n + 1) + for n in range(gapsize, w_max // 2, gapsize): + _compute_drho(n + gapsize) if (self.e_rho[e_name][n] - self.N_sigma[e_name] * self.e_drho[e_name][n]) < 0 or n >= w_max // 2 - 2: # Bias correction hep-lat/0306017 eq. (49) included self.e_tauint[e_name] = self.e_n_tauint[e_name][n] * (1 + (2 * n + 1) / e_N) / (1 + 1 / e_N) + texp * np.abs(self.e_rho[e_name][n + 1]) # The absolute makes sure, that the tail contribution is always positive @@ -305,12 +311,13 @@ class Obs: self.e_windowsize[e_name] = 0 else: # Standard automatic windowing procedure - tau = self.S[e_name] / np.log((2 * self.e_n_tauint[e_name][1:] + 1) / (2 * self.e_n_tauint[e_name][1:] - 1)) - g_w = np.exp(- np.arange(1, w_max) / tau) - tau / np.sqrt(np.arange(1, w_max) * e_N) + tau = self.S[e_name] / np.log((2 * self.e_n_tauint[e_name][gapsize::gapsize] + 1) / (2 * self.e_n_tauint[e_name][gapsize::gapsize] - 1)) + g_w = np.exp(- np.arange(1, len(tau) + 1) / tau) - tau / np.sqrt(np.arange(1, len(tau) + 1) * e_N) for n in range(1, w_max): if n < w_max // 2 - 2: - _compute_drho(n + 1) + _compute_drho(gapsize * n + gapsize) if g_w[n - 1] < 0 or n >= w_max - 1: + n *= gapsize self.e_tauint[e_name] = self.e_n_tauint[e_name][n] * (1 + (2 * n + 1) / e_N) / (1 + 1 / e_N) # Bias correction hep-lat/0306017 eq. (49) self.e_dtauint[e_name] = self.e_n_dtauint[e_name][n] self.e_dvalue[e_name] = np.sqrt(2 * self.e_tauint[e_name] * e_gamma[e_name][0] * (1 + 1 / e_N) / e_N) diff --git a/tests/obs_test.py b/tests/obs_test.py index d58933c9..d996e065 100644 --- a/tests/obs_test.py +++ b/tests/obs_test.py @@ -628,9 +628,19 @@ def test_gamma_method_irregular(): ao = pe.Obs([[carr[i] for i in range(len(carr)) if i % 2 == 1]], ['a'], idl=[[i for i in range(len(carr)) if i % 2 == 1]]) ao.gamma_method() + arrt = [carr[i] for i in range(len(carr)) if i % 2 == 1] + idlt = [i for i in range(len(carr)) if i % 2 == 1] + for el in [int(e) for e in N * np.random.uniform(size=10)]: + arrt = arrt[:el] + arrt[el + 1:] + idlt = idlt[:el] + idlt[el + 1:] + ai = pe.Obs([arrt], ['a'], idl=[idlt]) + ai.gamma_method() + assert(ae.e_tauint['a'] < a.e_tauint['a']) assert((ae.e_tauint['a'] - 4 * ae.e_dtauint['a'] < ao.e_tauint['a'])) assert((ae.e_tauint['a'] + 4 * ae.e_dtauint['a'] > ao.e_tauint['a'])) + assert((ai.e_tauint['a'] - 4 * ai.e_dtauint['a'] < ao.e_tauint['a'])) + assert((ai.e_tauint['a'] + 4 * ai.e_dtauint['a'] > ao.e_tauint['a'])) a = pe.pseudo_Obs(1, .1, 'a', samples=10) a.idl['a'] = range(4, 15)