Skip to content

Commit

Permalink
Remove adaptive from this branch
Browse files Browse the repository at this point in the history
  • Loading branch information
edknock committed Jan 30, 2025
1 parent f8e5abb commit 53b7c65
Showing 1 changed file with 0 additions and 87 deletions.
87 changes: 0 additions & 87 deletions samplers.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -107,93 +107,6 @@ This autocorrelation function can be interpreted that we have to wait almost 100

### Adaptive Metropolis-Hastings Sampler

**Overview**
This sampler extends the basic (non-adaptive) random-walk Metropolis-Hastings algorithm by dynamically updating its proposal distribution as the chain progresses. In a standard random-walk MCMC, one must guess a single variance-covariance matrix (VCV) up front. If the target distribution is high-dimensional or has strong correlations, it can be challenging to find a VCV that allows the chain to explore efficiently.

With this **adaptive** approach, the VCV and the overall scaling factor evolve according to the observed samples. The sampler “learns” the covariance structure of the target distribution on-the-fly, leading to (potentially) much lower autocorrelation, better mixing, and more efficient exploration. Formally, this adaptivity modifies the Markov property, so care must be taken to ensure the adaptation “diminishes” over time (as this code does).

**Main ideas**

1. **Adaptive Covariance**
- After each iteration, the algorithm updates an **empirical covariance** using the newly accepted parameter values.
- Early samples are gradually *forgotten* (depending on `forget_rate` and `forget_end`) so that the empirical covariance primarily reflects the more recent part of the chain—helpful if the chain starts far from the posterior bulk.

2. **Adaptive Scaling**
- The sampler also updates a **global scaling factor** (multiplied by $2.38^2 / d$ in the proposal) to target a desired acceptance rate (`acceptance_target`).
- If acceptance is too high, the sampler increases the step size. If acceptance is too low, it decreases it. Doing this in log-space is often more stable (`log_scaling_update = TRUE`).

3. **Boundaries**
- Proposals that fall outside the domain can be handled in one of three ways:
- *Reflect*: Mirror them back into the domain (the default).
- *Reject*: Assign $-\infty$ log-density and reject.
- *Ignore*: Accept them anyway (if your model supports out-of-domain evaluations).

4. **Adaptation Stopping**
- Eventually, the sampler stops adapting (`adapt_end`), freezing the proposal distribution for the remainder of the run. This helps restore formal MCMC convergence properties, as purely adaptive samplers can break standard theory if they keep adapting indefinitely.

**Citation**
This implementation is inspired by an “accelerated shaping” adaptive algorithm from Spencer (2021):

> Spencer SEF (2021) Accelerating adaptation in the adaptive Metropolis–Hastings random walk algorithm. *Australian & New Zealand Journal of Statistics*, 63:468–484.
---

### Example: Adaptive vs. Non-adaptive Metropolis-Hastings

Below is a simple demonstration showing how to use the adaptive sampler on a multivariate Gaussian example, comparing it against the basic random-walk approach. The target Gaussian has some correlation in its variance-covariance matrix, so the adaptive sampler’s ability to learn this correlation on the fly can be advantageous.

```{r}
# 2. Adaptive sampler:
sampler_adaptive <- monty_sampler_adaptive(
initial_vcv = vcv_initial, # start from the same guess
initial_vcv_weight = 1000, # weight for the initial guess
acceptance_target = 0.234, # desired acceptance rate
forget_rate = 0.2, # regularly forget oldest samples
adapt_end = 300, # stop adapting after 300 iterations
boundaries = "reflect" # default reflection at domain edges
)
# Run both samplers for 1,000 iterations
res_adaptive <- monty_sample(m, sampler_adaptive, n_steps = 1000)
# Compare the chain density traces
plot(drop(res_rw$density), type = "l", col = 2, xlab = "Iteration",
ylab = "Log density", main = "Comparison of RWMH vs. Adaptive M-H")
lines(drop(res_adaptive$density), type = "l", col = 4)
legend("bottomright", legend = c("Random-Walk", "Adaptive"),
col = c(2, 4), lty = 1, bty = "n")
```

In the above code:

- **Random-Walk Metropolis** uses a fixed variance-covariance matrix (`vcv_initial`) throughout the chain.
- **Adaptive Metropolis-Hastings** starts with the same matrix but actively refines it. You should see that the adaptive sampler often converges to a better proposal distribution, leading to improved mixing and typically higher log densities (indicating that the chain is exploring the posterior more effectively).

You can also inspect the final estimated variance-covariance matrix from the adaptive sampler:

```{r}
# Show the final estimate of the VCV from the adaptive sampler
# (s_adaptive$details is updated after each chain)
est_vcv <- sampler_adaptive$details[[1]]$vcv
print(est_vcv)
```

Over many iterations, this matrix will reflect the empirical correlation structure of the target distribution—showing how the chain has “learned” the shape of the posterior.

---

**Summary**
- This **Adaptive Metropolis-Hastings** sampler is especially helpful in moderate to high dimensions or when the posterior exhibits nontrivial correlations.
- By updating the proposal’s scale and shape in real time, it can achieve more stable acceptance rates and faster convergence compared to a fixed random-walk approach.
- The user has fine-grained control via parameters like `acceptance_target`, `forget_rate`, and `adapt_end`, allowing adaptation to be tailored to the problem at hand.

Feel free to experiment with different settings—particularly the forgetting and adaptation window parameters—to see how they affect convergence and mixing for your particular model.

```{r}
acf(res_adaptive$pars[1,,1], 200)
```

### Nested models

### Hamiltonian Monte Carlo
Expand Down

0 comments on commit 53b7c65

Please sign in to comment.