telescopic
Unbiased Markov chain Monte Carlo with coupled chains and telescoping sums.
telescopic runs pairs of coupled Markov chains until they meet, and turns each pair into
an exactly unbiased estimate of expectations under the target distribution (Glynn & Rhee
2014; Jacob, O'Leary & Atchadé 2020). Averages over independent pairs come with error
bars that are valid as the number of pairs grows, not as the chains grow, and the pairs
run in parallel without communication. Their meeting times also bound the distance of
the chains from stationarity.
telescopic is written in Python on NumPy and SciPy. It is in development; version 0.1 is
a first draft. The documentation is at https://joshspeagle.com/telescopic/.
Installation
Install telescopic from a clone of the repository:
pip install . # or pip install ".[all]" for every extra
It needs Python 3.11 or newer, NumPy 1.26 or newer, SciPy 1.12 or newer and
threadpoolctl 3.1 or newer. The extras are plot (matplotlib), progress (tqdm),
parallel (cloudpickle, which sends functions defined in notebooks to worker
processes), pandas (summaries as data frames), arviz (chains exported to
ArviZ 1.0 or newer, which needs Python 3.12 and NumPy 2) and all.
Example
The script below fits a straight line to data with Gaussian errors:
import numpy as np
from scipy import optimize
import telescopic
rng = np.random.default_rng(0)
x = np.sort(10 * rng.random(50))
yerr = 0.1 + 0.5 * rng.random(50)
y = -0.9 * x + 4.3 + yerr * rng.normal(size=50)
def log_prob(theta, x, y, yerr):
slope, intercept = theta
return -0.5 * np.sum(((y - slope * x - intercept) / yerr) ** 2)
# Start the chains from a Gaussian ten times wider than the best fit's errors.
fit = optimize.minimize(lambda theta: -log_prob(theta, x, y, yerr), [0.0, 0.0])
init = telescopic.initial.Normal(mean=fit.x, cov=100 * fit.hess_inv)
result = telescopic.sample(log_prob, init, args=(x, y, yerr), names=["slope", "intercept"],
rng=42)
print(result.summary()) # unbiased means, standard errors, quantiles
result.estimate(lambda t: t[:, 0] * t[:, 1]) # any function, after the run
result.plot_tv_bound() # with the plot extra: distance from stationarity
sample tunes a random-walk kernel on pilot chains, chooses the burn-in, the lag and the
length of each run from the meeting times of pilot pairs, runs 100 replicates, and warns
when the run shows signs that its error bars are unreliable. The chains should start
wider than the posterior, but not by orders of magnitude
(Initial distributions).
The Quickstart explains this example, and the
tutorials work through others.
What it guarantees
Each replicate's estimate is unbiased for any burn-in, lag and run length, and every
initial distribution from which the chain converges, provided the coupling keeps each
chain's law, the chains stay together once they meet, and the meeting time has enough
moments. With finite variance, the error bars are valid as the number of replicates
grows. Coupling does not make the chains mix faster, and a run whose initial
distribution or tuned kernel misses a mode can be confidently wrong. The
theory page states the conditions and the limits.
Citing
If you use telescopic, please cite the software (CITATION.cff) and the
methods it implements, which Citing lists.
License
MIT.