Source code for py_core.bin_size

from .metropolis import *
from math import isclose
from math import floor
import tqdm


[docs] def get_uncorr_n_partitions(rwalk, Fx, **kwargs): """Estimate the number of partitions needed for uncorrelated measurements of an observable. Parameters ---------- rwalk : Rwalk Rwalk object with a sampled MCMC chain. Fx : callable The observable functional to be measured. **kwargs Additional keyword arguments forwarded to :meth:`Rwalk.get_Fx_avg_partitioned`. Returns ------- int Number of partitions such that partitioned averages are statistically consistent (ratio of successive sigmas within 10% of 1). Raises ------ ValueError If the sample size is less than 2. """ divide_by = 1.5 n_partitions = rwalk.get_paths().shape[0] ratio = 0. if n_partitions < 2: raise ValueError("The sample size must be at least 2.") # compute the maximum number of partitions for the given sample size and division step pbar = tqdm.tqdm( total=floor(np.emath.logn(1.5, rwalk.get_paths().shape[0])) + 1, desc="Finding uncorrelated partitions", leave=False ) while (not isclose(ratio, 1., abs_tol=0.1)) and n_partitions > 2: n_partitions = int(n_partitions // divide_by) sample1 = rwalk.get_Fx_avg_partitioned(n_partitions, Fx, **kwargs) sample2 = rwalk.get_Fx_avg_partitioned(int(n_partitions // divide_by), Fx, **kwargs) sigma1 = np.std(sample1, ddof=1) / np.sqrt(sample1.shape[0]) sigma2 = np.std(sample2, ddof=1) / np.sqrt(sample2.shape[0]) ratio = sigma2 / sigma1 pbar.update(1) return n_partitions