How it works¶
The Quickstart describes a run in words. This page runs one by hand, on a standard normal target in one dimension with chains started far from it: the paths of a few pairs and one replicate’s estimate taken apart; Unbiased on average shows the bias that the correction removes on average. Theory derives each step.
Pairs of chains¶
The target \(\pi\) is \(\mathcal N(0, 1)\), and the chains start from \(\mathcal N(5, 1)\),
five standard deviations away. The kernel is random-walk Metropolis with proposals
\(x' \sim \mathcal N(x, 2.38^2)\), its default for cov=[[1.0]], coupled by the
reflection-maximal coupling (Kernels and couplings).
telescopic.engine.run() runs replicates with given settings, and with
keep_paths=True it keeps the paths of both chains. Here the burn-in \(k\) is 5, the
run length \(m\) is 20 and the lag \(L\) is 1; the output is the meeting times \(\tau\) of sixteen
pairs:
import matplotlib.pyplot as plt
import numpy as np
import telescopic
from telescopic import engine, plotting
def log_prob(x):
return -0.5 * x[:, 0] ** 2
target = telescopic.Target(log_prob, vectorize=True)
kernel = telescopic.kernels.RandomWalk(cov=[[1.0]])
init = telescopic.initial.Normal(mean=[5.0], cov=[1.0])
k, m = 5, 20
replicates = engine.run(target, kernel, init, range(16), k=k, m=m, lag=1, rng=1, keep_paths=True)
replicates.meeting_time
array([ 3., 2., 2., 9., 4., 10., 4., 13., 7., 2., 3., 2., 2.,
6., 15., 4.])
telescopic.plotting.paths() draws the first pairs against time: \(X\) solid in
magenta and \(Y\) dashed in teal, shifted right by the lag, so that a pair meets where its
dashed line joins its solid one, marked with a dot. After a meeting only \(X\) runs, until
\(m\), in the dark ink of met chains. The MCMC average uses the shaded times, \(k\) to \(m\):
n = 4
ax = plotting.paths(replicates, n=n)
meeting = replicates.meeting_time[:n].astype(int)
position = np.array([replicates.paths[i][0][meeting[i], 0] for i in range(n)])
ax.plot(meeting, position, "o", color=plotting.MET)
ax.axvspan(k, m, color="gray", alpha=0.15)
ax.set(xlabel="$t$", ylabel="$X_t$ and $Y_{t-1}$");
The paths of the first four pairs against \(t\): \(X_t\) solid in magenta and \(Y_{t-1}\) dashed in teal, a dot where each pair meets, the pair in dark ink afterwards, and the times \(k\) to \(m\) that the MCMC average uses shaded.¶
Of the four pairs, 3 meet within three steps, and 3 meet more than three standard deviations from the target’s mean: a meeting does not mean that the chains have reached the target.
One replicate’s estimate¶
Each replicate’s estimate is a signed measure: atoms with weights, whose weighted sum of
a function \(h\) estimates its expectation \(\pi(h)\) under the target
(Theory). telescopic.Result holds the measures of
all replicates in its measure, a telescopic.SignedMeasure whose replicate
array gives each atom’s replicate. The first pair that met after \(k + L\), here \(k + 1\),
has a bias correction that is not zero:
result = telescopic.Result(replicates)
r = int(np.argmax(replicates.meeting_time > k + 1))
mine = result.measure.replicate == r
atoms, weights = result.measure.atoms[mine, 0], result.measure.weights[mine]
print(
f"replicate {r} met at tau = {replicates.meeting_time[r]:g}: {len(atoms)} atoms, "
f"{np.sum(weights < 0)} with negative weights; the weights sum to {weights.sum():.12f}"
)
replicate 3 met at tau = 9: 22 atoms, 3 with negative weights; the weights sum to 1.000000000000
The states \(X_k, \dots, X_m\) weigh \(1/(m - k + 1)\) each. For \(t\) from \(k + 1\) to \(\tau - 1\), the correction adds weight at \(X_t\) and removes the same weight at \(Y_{t-1}\), so the weights sum to 1 and some are negative. The weights of this replicate, summed over equal atoms, positive in blue and negative in gold:
points, which = np.unique(atoms, return_inverse=True)
summed = np.bincount(which, weights)
fig, ax = plt.subplots(figsize=(5, 3))
for sign, color, label in ((summed > 0, "C0", "positive"), (summed < 0, "C1", "negative")):
ax.stem(points[sign], summed[sign], linefmt=color, markerfmt=color, basefmt="none", label=label)
ax.axhline(0, color="gray", linewidth=0.8)
ax.legend()
ax.set(xlabel="atom", ylabel="weight");
The weights of one replicate’s atoms, summed over equal atoms, positive in blue and negative in gold: they sum to 1, and the bias correction makes some of them negative.¶
Its estimate of the mean, with \(h(x) = x\), is the MCMC average of \(X_k, \dots, X_m\) plus the bias correction, whose weights are \(\min(1, (t - k)/(m - k + 1))\) for \(L = 1\) (Theory):
x_path, y_path = replicates.paths[r]
tau = int(replicates.meeting_time[r])
mcmc = x_path[k : m + 1, 0].mean()
t = np.arange(k + 1, tau)
weight = np.minimum(1, (t - k) / (m - k + 1))
correction = np.sum(weight * (x_path[t, 0] - y_path[t - 1, 0]))
estimate = result.measure.per_replicate(lambda x: x[:, 0])[r]
print(f"MCMC average {mcmc:.6f} + bias correction {correction:.6f} = {mcmc + correction:.6f}")
print(f"signed measure: {estimate:.6f}")
MCMC average -0.001305 + bias correction 0.796290 = 0.794985
signed measure: 0.794985
One replicate’s correction can move its estimate toward the target’s mean, 0, or away from it: only the average over replicates is unbiased. For a pair that meets by \(k + L\) the correction is zero, and the estimate is the MCMC average alone.