Autocorrelation-adjusted effective sample size of a 1-D series.
ESS = n / (1 + 2 * sum_k rho_k) over Geyer's initial positive sequence of the
autocorrelations. A strongly autocorrelated series of length n carries far
fewer than n independent observations; ESS is the count to use in an analytic
standard error.
See Also
block_bootstrap_ci : Block bootstrap using this idea.
Examples:
>>> import numpy as np
>>> smooth = np.sin(np.linspace(0, 20, 200)) # autocorrelated -> ESS < n
>>> bool(effective_sample_size(smooth) < 200)
True
Source code in src/gbfs_toolkit/analytics/resampling.py
| def effective_sample_size(series) -> float:
"""Autocorrelation-adjusted effective sample size of a 1-D series.
``ESS = n / (1 + 2 * sum_k rho_k)`` over Geyer's initial positive sequence of the
autocorrelations. A strongly autocorrelated series of length ``n`` carries far
fewer than ``n`` independent observations; ESS is the count to use in an analytic
standard error.
See Also
--------
[`block_bootstrap_ci`][gbfs_toolkit.block_bootstrap_ci] : Block bootstrap using this idea.
Examples
--------
>>> import numpy as np
>>> smooth = np.sin(np.linspace(0, 20, 200)) # autocorrelated -> ESS < n
>>> bool(effective_sample_size(smooth) < 200)
True
"""
x = np.asarray(series, dtype="float64")
x = x[np.isfinite(x)]
n = x.size
if n < 2:
return float(n)
x = x - x.mean()
denom = float((x**2).sum())
if denom == 0:
return float(n)
acf_full = np.correlate(x, x, mode="full")[n - 1 :]
acf = acf_full / acf_full[0]
s = 0.0
for k in range(1, n):
if acf[k] <= 0: # initial positive sequence
break
s += acf[k]
ess = n / (1.0 + 2.0 * s)
return float(min(max(ess, 1.0), n))
|