Metadata-Version: 2.4
Name: gnm
Version: 1.1.0
Summary: Rock n Rolling awesome affine-invariant MCMC Sampler (Dynamic Gauss-Newton Metropolis)
Home-page: https://github.com/mugurbil/gnm
Author: Mehmet Ugurbil
Author-email: Mehmet Ugurbil <mugurbil@gmail.com>
License: MIT
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE.rst
License-File: AUTHORS.rst
Requires-Dist: numpy>=1.20
Provides-Extra: scipy
Requires-Dist: scipy>=1.7; extra == "scipy"
Provides-Extra: fast
Requires-Dist: numba>=0.57; extra == "fast"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: scipy>=1.7; extra == "dev"
Requires-Dist: matplotlib>=3.5; extra == "dev"
Requires-Dist: numba>=0.57; extra == "dev"
Dynamic: author
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-python

# `gnm`: Dynamic Gauss-Newton Metropolis MCMC Sampler

**A fast, strictly affine-invariant Markov chain Monte Carlo (MCMC) sampler for nonlinear inverse problems and stiff Bayesian posteriors.**

[![Paper](https://img.shields.io/badge/arXiv-2001.03530-b31b1b.svg)](https://arxiv.org/abs/2001.03530)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE.rst)
[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)

`gnm` implements the **Dynamic Gauss-Newton Metropolis (GNM)** algorithm ([Ugurbil & Goodman, arXiv:2001.03530](https://arxiv.org/abs/2001.03530)) for sampling target distributions induced by nonlinear least-squares residuals $f: \mathbb{R}^n \to \mathbb{R}^m$ with an optional Gaussian prior $\mathcal{N}(m, H^{-1})$:

$$\pi(x) \propto \exp\!\left(-\frac{1}{2}\|f(x)\|_2^2 - \frac{1}{2}(x - m)^T H (x - m)\right)$$

This structure arises throughout science and engineering whenever parameters $x \in \mathbb{R}^n$ are inferred from noisy nonlinear observations $y = g(x) + \epsilon$ via standardized residuals $f_i(x) = (g_i(x) - y_i)/\sigma_i$.

---

## Why `gnm`?

Standard MCMC algorithms struggle when the posterior landscape has strong parameter correlations, curved "banana" valleys, or parameters that vary across many orders of magnitude:
- **Random-Walk Metropolis (RWM)** and **MALA / HMC** with isotropic mass matrices get trapped whenever the local Hessian condition number $\kappa$ is large.
- **Riemannian HMC** adapts to local curvature but requires expensive **second-order derivatives** (Hessian tensors) and implicit symplectic integrators.
- **Ensemble samplers (`emcee`)** are affine-invariant to *global* linear transformations, but their stretch moves along chords between walkers frequently overshoot curved nonlinear manifolds and degrade in higher dimensions.

`gnm` solves these challenges using **only first-order Jacobians $J(x) = \nabla f(x)$**:
1. **Local Curvature Preconditioning without Second Derivatives**: At each state $x$, `gnm` forms the local Gauss-Newton precision matrix $P(x) = J(x)^T J(x) + H$, automatically aligning proposals with curved valleys and multi-scale coordinates.
2. **Strict Affine Invariance Across $10^{12}$ Condition Numbers**: Rescaling parameters by $10^6\times$ ($\kappa = 10^{12}$) leaves `gnm`'s acceptance rate and autocorrelation time completely unchanged, whereas unpreconditioned MALA and HMC collapse to `0.000` acceptance at $\kappa \ge 10^4$.
3. **Delayed-Rejection Back-Off (`static` & `dynamic`)**: When a full Gauss-Newton step overshoots a curved valley, `gnm` does not discard the directional information—it backs off along the proposal ray using either geometric scaling (`static`) or **cubic Hermite interpolation** (`dynamic`) while preserving exact detailed balance.
4. **Numba `@njit`-Accelerated Native Speed**: Fused LLVM kernels compile Cholesky factorization, triangular solves, and recursive delayed-rejection probabilities to machine code—achieving **`9.7–27.4 µs/eval`** and the **highest effective sample throughput (`ESS/sec`)** across benchmarks.

---

## Installation

```bash
# Install from PyPI (with Numba JIT acceleration and SciPy utilities)
pip install "gnm[fast,scipy]"

# Minimal installation (pure NumPy fallback)
pip install gnm

# Or install from source for development & benchmarking
git clone https://github.com/mugurbil/gnm.git
cd gnm
pip install -e ".[dev]"
```

---

## 30-Second Quickstart

```python
import numpy as np
import gnm

# 1. Define residual vector f(x) and Jacobian J(x)
#    Return signature: (in_domain_flag, f_x, J_x)
def well_model(x, args):
    y, s = args["y"], args["s"]
    f = [(x[0] ** 2 - y) / s]
    J = [[(2.0 * x[0]) / s]]
    return 1, f, J

# 2. Initialize sampler and optional Gaussian prior N(m, H^{-1})
sampler = gnm.sampler(x=[0.5], model=well_model, args={"y": 2.0, "s": 1.0})
sampler.prior(m=[0.0], H=[[1.0]])

# 3. Verify analytical Jacobian against finite differences (returns 0 on pass)
assert sampler.Jtest(x_min=[-3.0], x_max=[3.0]) == 0

# 4. Configure Dynamic cubic-Hermite back-off (up to k_max=2 back-off stages)
sampler.dynamic(max_steps=2)

# 5. Draw samples and discard burn-in
sampler.sample(12000)
chain = sampler.burn(2000)

print(f"Acceptance rate:      {sampler.accept_rate:.3f}")
print(f"Autocorrelation time: {sampler.acor()}")
print(f"Posterior E[x^2]:     {np.mean(chain[:, 0] ** 2):.4f}")
```

---

## Flexible Workflows & Cookbook

### 1. Black-Box Models (Automatic Finite-Difference Jacobians)
Don't want to derive or code $J(x)$ by hand? Simply return the residual vector $f(x)$ (with or without `args`). `gnm` automatically computes central finite-difference Jacobians:

```python
def blackbox_residual(x):
    # Just return the residual vector f(x)
    return [1.0 - x[0], 10.0 * (x[1] - x[0] ** 2)]

sampler = gnm.sampler([0.0, 0.0], blackbox_residual)
sampler.dynamic(2)
sampler.sample(5000)
```

You can also return a 2-tuple `(f_x, J_x)` (domain validity is inferred automatically from finiteness) or pass a separate Jacobian callable via `gnm.sampler(x0, residual_fn, jac=jacobian_fn)`.

### 2. Warm-Starting at the MAP / MLE Estimate (`from_least_squares`)
Eliminate burn-in on stiff nonlinear problems by initializing the Markov chain directly at the Maximum A Posteriori (MAP) or Maximum Likelihood (MLE) point using `scipy.optimize.least_squares`:

```python
sampler = gnm.sampler.from_least_squares(
    rosenbrock_model,
    x0=[-1.5, 2.0],
    args={"a": 1.0, "b": 100.0},
    prior_mean=[1.0, 1.0],
    prior_precision=np.eye(2),
)
sampler.dynamic(2)
sampler.sample(10000)
```

### 3. High-Dimensional Step-Size Scaling (`r0="auto"` & `sampler.tune()`)
In higher dimensions ($n \ge 6$), a full unscaled stage-0 step ($r_0 = 1$) on a non-Gaussian target often requires multiple back-offs per step. Use `r0="auto"` ($r_0 = \min(1, 2/n)$) or run Robbins-Monro burn-in adaptation via `sampler.tune()`, which adapts $r_0 \in (0, 1]$ during warmup and freezes it during `sample()` so detailed balance holds strictly:

```python
sampler = gnm.sampler(x0, high_dim_model, r0="auto")
sampler.dynamic(1)
tuned_r0 = sampler.tune(n_tune=1000, target_accept=0.45)  # Warmup & freeze r0
sampler.sample(20000)
```

### 4. Rank-Deficient & Underdetermined Models ($m < n$)
When the number of observations $m$ is smaller than the number of parameters $n$ (or at critical points where $J(x) = 0$), $J(x)^T J(x)$ is singular. `gnm` automatically applies adaptive Levenberg-Marquardt regularization (or user-specified baseline damping via `gnm.sampler(..., reg=1e-3)`) so sampling proceeds smoothly even without a prior.

---

## Benchmarks vs. State of the Art

We benchmarked `gnm` (Static and Dynamic) against four standard MCMC baselines under equal function-evaluation budgets:
1. **Random-Walk Metropolis (RWM)**
2. **MALA** (Metropolis-Adjusted Langevin Algorithm — 1st-order gradient)
3. **HMC** (Hamiltonian Monte Carlo with $L=10$ leapfrog steps — 1st-order gradient)
4. **`emcee`** (Affine-Invariant Ensemble Stretch Sampler)

---

### 1. Exact Posterior Recovery & Affine-Invariance Stress Test ($\kappa = 1$ to $10^{12}$)

![Verification and Affine Invariance](https://raw.githubusercontent.com/mugurbil/gnm/master/benchmarks/figures/fig1_verification_and_invariance.png)

- **Panel (A) — 1D Bimodal Double-Well ($f(x) = x^2 - 2$)**: Both `gnm` Static and Dynamic recover the exact bimodal posterior within Monte Carlo error bars (Total Variation distance $\text{TV} = 0.016$, exact $\mathbb{E}[x^2] = 1.246$ vs. `gnm` $1.251$) despite $J(0) = 0$ at the barrier between modes.
- **Panel (B) — Back-Off Ablation on Stiff Rosenbrock ($b = 100$)**: Dynamic cubic-Hermite back-off increases acceptance from `17.2%` ($k_{\max}=0$) to **`43.7%`** ($k_{\max}=3$) and increases total ESS by **55%**.
- **Panel (C) — Coordinate Scaling Stress Test ($x_0 = S \tilde{x}_0, x_1 = \tilde{x}_1 / S$)**:

| Scale Factor $S$ (Condition Number $\kappa = S^2$) | `gnm` (Dynamic) Accept | `gnm` (Dynamic) $\tau_{\text{int}}$ | MALA Accept | MALA $\tau_{\text{int}}$ | HMC Accept | HMC $\tau_{\text{int}}$ |
| :--- | :---: | :---: | :---: | :---: | :---: | :---: |
| **$S = 10^0$ ($\kappa = 10^0$)** | **0.475** | **105.3** | 0.805 | 650.4 | 0.993 | 82.1 |
| **$S = 10^2$ ($\kappa = 10^4$)** | **0.475** | **105.3** | 0.001 | 962.3 | 0.000 | Stuck |
| **$S = 10^4$ ($\kappa = 10^8$)** | **0.475** | **105.3** | 0.000 | Stuck | 0.000 | Stuck |
| **$S = 10^6$ ($\kappa = 10^{12}$)** | **0.472** | **118.1** | 0.000 | Stuck | 0.000 | Stuck |

---

### 2. Statistical Efficiency on Ill-Posed Inverse Problems & High-Dimensional Targets

![SOTA Statistical Efficiency Comparison](https://raw.githubusercontent.com/mugurbil/gnm/master/benchmarks/figures/fig2_sota_comparison.png)

- **4D Ill-Posed Multi-Exponential Time Series ($y(t) = w_1 e^{-\lambda_1 t} + w_2 e^{-\lambda_2 t}$)**: Multi-exponential fitting is notoriously ill-conditioned due to strong collinearity between $(w_1, \lambda_1)$ and $(w_2, \lambda_2)$. `gnm` (Dynamic) achieves **19.26 ESS per 1,000 evaluations** ($\tau_{\text{int}} = 22.6$)—**2.2x more sample-efficient than `emcee`** (`8.87`), **15x better than RWM** (`1.26`), **32x better than HMC** (`0.59`), and **48x better than MALA** (`0.40`).
- **16D Anisotropic Coupled Rosenbrock ($\kappa = 10^8$)**: With `sampler.tune()`, `gnm` achieves the **lowest Posterior Mean RMSE (`0.37`) and lowest Posterior Std RMSE (`0.33`)** of all methods tested, outperforming both `emcee` (`0.51` / `0.89`) and HMC (`0.71` / `1.90`).

---

### 3. Wall-Clock Running Time & Effective Sample Throughput (`ESS / sec`)

![Wall-Clock Runtime and Throughput Comparison](https://raw.githubusercontent.com/mugurbil/gnm/master/benchmarks/figures/fig3_runtime_comparison.png)

With fused `@numba.njit` kernels ([`gnm/_numba_kernels.py`](https://github.com/mugurbil/gnm/blob/master/gnm/_numba_kernels.py)), `gnm` achieves both the **lowest wall-clock time per 20,000 evaluations** and the **highest effective sample throughput (`ESS/sec`)** across all four benchmarks:

| Benchmark | Sampler | Time / 20k Evals (s) | Cost ($\mu\text{s}$ / Eval) | **Throughput (ESS / sec)** | **Time per 100 ESS (s)** |
| :--- | :--- | :---: | :---: | :---: | :---: |
| **1D Double-Well** | RWM | 0.382 | 19.1 | 1145.2 | 0.09 |
| | MALA | 0.582 | 29.1 | 269.2 | 0.37 |
| | HMC ($L=10$) | 0.348 | 17.4 | 366.1 | 0.27 |
| | `emcee` | 0.943 | 47.1 | 424.3 | 0.24 |
| | **`gnm` (Static)** | **0.204** | **10.2** | **1205.5** | **0.08** |
| | **`gnm` (Dynamic)** | **0.194** | **9.7** | **1132.7** | **0.09** |
| **2D Rosenbrock ($b=100$)** | RWM | 0.294 | 14.7 | 47.2 | 2.12 |
| | MALA | 0.502 | 25.1 | 25.4 | 3.94 |
| | HMC ($L=10$) | 0.285 | 14.2 | 55.5 | 1.80 |
| | `emcee` | 0.887 | 44.4 | 94.5 | 1.06 |
| | **`gnm` (Static)** | **0.195** | **9.8** | 76.9 | 1.30 |
| | **`gnm` (Dynamic)** | **0.193** | **9.7** | **141.2** | **0.71** |
| **4D Exp Time Series** | RWM | 0.703 | 35.2 | 33.9 | 2.95 |
| | MALA | 0.934 | 46.7 | 7.8 | 12.83 |
| | HMC ($L=10$) | 0.681 | 34.0 | 20.8 | 4.80 |
| | `emcee` | 1.387 | 69.3 | 168.5 | 0.59 |
| | **`gnm` (Static)** | **0.557** | **27.9** | **671.5** | **0.15** |
| | **`gnm` (Dynamic)** | **0.547** | **27.4** | **644.8** | **0.16** |
| **6D Anisotropic Rosenbrock** | RWM | 0.362 | 18.1 | 22.4 | 4.46 |
| | MALA | 0.543 | 27.1 | 14.1 | 7.09 |
| | HMC ($L=10$) | 0.346 | 17.3 | 23.2 | 4.31 |
| | `emcee` | 0.745 | 37.3 | 214.6 | 0.47 |
| | **`gnm` (Static, $k_{\max}=1, r_0=0.7$)** | **0.274** | **13.7** | **220.1** | **0.45** |
| | **`gnm` (Dynamic, $k_{\max}=1, r_0=0.7$)** | **0.274** | **13.7** | **165.3** | **0.60** |

### Reproducing the Benchmarks
```bash
# Run the full verification & SOTA comparison suite
python benchmarks/benchmark_sota.py

# Regenerate all benchmark figures in benchmarks/figures/
python benchmarks/plot_benchmarks.py

# Run the unit test suite
pytest -v
```

---

## When Should You Use `gnm`?

| Problem Regime | Recommended Configuration | Why |
| :--- | :--- | :--- |
| **Nonlinear least-squares & curve/ODE fitting** ($n \le 50$, collinear parameters) | `gnm` (`dynamic(2)` or `static(2, 0.25)`) | Gauss-Newton metric $J^T J + H$ preconditions collinear directions automatically; **4x–32x higher ESS/sec** than `emcee` and HMC. |
| **Multi-scale / poorly scaled parameters** ($\kappa = 10^4 \text{ to } 10^{12}$) | `gnm` (`dynamic(1)` + `tune()`) | Strictly affine-invariant; zero manual parameter rescaling or mass-matrix tuning required. |
| **Stiff curved valleys** (e.g. Rosenbrock banana, $n \le 10$) | `gnm` (`dynamic(2)`) | Cubic-Hermite back-off recovers overshot proposals along curved valleys (`141.2 ESS/sec`). |
| **Higher-dimensional nonlinear models** ($n = 8 \text{ to } 50$) | `gnm` (`r0="auto"` + `tune()` + `dynamic(1)`) | Adapts initial step size $r_0$ during burn-in so stage-0 proposals maintain optimal acceptance without excessive back-offs. |

---

## API Reference Overview

| Method / Property | Description |
| :--- | :--- |
| `gnm.sampler(x, model, args=None, jac=None, reg=0.0, r0=1.0)` | Initialize sampler at `x` with `model` returning `(chi, f, J)`, `(f, J)`, or `f`. |
| `sampler.from_least_squares(model, x0, ...)` | Class method: find MAP/MLE via `scipy.optimize.least_squares` and initialize sampler at the solution. |
| `sampler.prior(m, H)` | Set Gaussian prior with mean `m` ($n$) and positive-definite precision matrix `H` ($n \times n$). |
| `sampler.Jtest(x_min, x_max, ...)` | Validate analytical Jacobian against central finite differences over `[x_min, x_max]`. Returns `0` on pass. |
| `sampler.static(max_steps, step_size, r0=None)` | Use static geometric back-off $r_k = q^k r_0$ with up to `max_steps` back-offs. |
| `sampler.dynamic(max_steps, opts=None, r0=None)` | Use dynamic cubic-Hermite interpolation back-off with up to `max_steps` back-offs. |
| `sampler.tune(n_tune=1000, target_accept=0.45)` | Adapt initial step size `r0` during burn-in via Robbins-Monro stochastic approximation, then freeze `r0`. |
| `sampler.sample(n_samples, divs=1, visual=False, safe=False)` | Draw `n_samples` MCMC samples. |
| `sampler.burn(n_burned)` | Discard the first `n_burned` samples from `sampler.chain` and return the burned chain. |
| `sampler.acor(k=5)` | Compute integrated autocorrelation time $\tau_{\text{int}}$ for each parameter via FFT (Sokal's window). |
| `sampler.error_bars(n_bins, d_min, d_max)` | Compute 1D or projected $n$-D posterior histogram density and standard error bars. |
| `sampler.save(path)` / `sampler.load(path)` | Save or restore sampler state and chain to/from JSON. |
| `sampler.chain` / `sampler.accept_rate` / `sampler.step_count` / `sampler.call_count` | Access the `(n_samples, n)` sample array, acceptance rate, stage acceptance counts, and total model evaluations. |

---

## Citation & Attribution

If you use `gnm` in your research, please cite:

```bibtex
@article{ugurbil2020dynamic,
  title   = {Dynamic Gauss-Newton Metropolis Algorithm},
  author  = {Ugurbil, Mehmet and Goodman, Jonathan},
  journal = {arXiv preprint arXiv:2001.03530},
  year    = {2020},
  url     = {https://arxiv.org/abs/2001.03530}
}
```

- **Authors**: [Mehmet Ugurbil](https://mugurbil.github.io/) and [Jonathan Goodman](http://www.math.nyu.edu/faculty/goodman/)
- **License**: [MIT License](LICENSE.rst)
