Metadata-Version: 2.5
Name: telescopic
Version: 0.1.0
Summary: Unbiased Markov chain Monte Carlo with coupled chains and telescoping sums
Project-URL: Documentation, https://joshspeagle.com/telescopic/
Project-URL: Repository, https://github.com/joshspeagle/telescopic
Project-URL: Changelog, https://github.com/joshspeagle/telescopic/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/joshspeagle/telescopic/issues
Author: Joshua S. Speagle
License-Expression: MIT
License-File: LICENSE
Keywords: Bayesian inference,MCMC,astronomy,couplings,unbiased estimation
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Scientific/Engineering :: Astronomy
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: numpy>=1.26
Requires-Dist: scipy>=1.12
Requires-Dist: threadpoolctl>=3.1
Provides-Extra: all
Requires-Dist: arviz>=1.0; (python_version >= '3.12') and extra == 'all'
Requires-Dist: cloudpickle>=3.0; extra == 'all'
Requires-Dist: matplotlib>=3.8; extra == 'all'
Requires-Dist: pandas>=2.1; extra == 'all'
Requires-Dist: tqdm>=4.66; extra == 'all'
Provides-Extra: arviz
Requires-Dist: arviz>=1.0; (python_version >= '3.12') and extra == 'arviz'
Provides-Extra: pandas
Requires-Dist: pandas>=2.1; extra == 'pandas'
Provides-Extra: parallel
Requires-Dist: cloudpickle>=3.0; extra == 'parallel'
Provides-Extra: plot
Requires-Dist: matplotlib>=3.8; extra == 'plot'
Provides-Extra: progress
Requires-Dist: tqdm>=4.66; extra == 'progress'
Description-Content-Type: text/markdown

# 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:

```bash
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:

```python
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](docs/guide/initial-distribution.md#choosing-one)).
The [Quickstart](docs/start/quickstart.md) explains this example, and the
[tutorials](docs/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](docs/theory.md) states the conditions and the limits.

## Citing

If you use telescopic, please cite the software ([`CITATION.cff`](CITATION.cff)) and the
methods it implements, which [Citing](docs/citing.md) lists.

## License

MIT.
