Tutorials › Bayesian Clinical Trials › Random-Walk Metropolis For A Trial Effect

Posterior Computation · Tutorial 121 of 696

Random-Walk Metropolis For A Trial Effect

Work through random-walk Metropolis for a scalar clinical-trial effect, from constructing the log-posterior target to accepting or rejecting proposals.

Intermediate 12 min read

What You'll Learn

  • Define a scalar log-odds-ratio posterior for a binary clinical-trial endpoint.
  • Construct a random-walk proposal and identify when its proposal terms cancel.
  • Calculate log-posterior changes from likelihood and prior contributions.
  • Convert a log-posterior change into an acceptance probability and update the chain.
  • Recognize implementation errors involving proposal symmetry, constants, and rejected draws.
  • Compare random-walk Metropolis with direct one-dimensional posterior computation.

Why Use a Random Walk for a Trial Effect?

Some treatment-effect posteriors do not have a convenient closed-form distribution from which to draw independent samples. Random-walk Metropolis provides a general way to generate dependent draws from such a posterior: propose a nearby value, compare how well it is supported by the posterior, and either move to it or remain at the current value. The method is especially easy to implement for a single scalar parameter, where each proposed move and its contribution to the target can be inspected directly.

The focus here is the mechanics of the update, not a general treatment of Metropolis-Hastings acceptance probabilities. The earlier material on Metropolis-Hastings Acceptance Probabilities gives the general rule; below, it is specialized to a scalar trial effect and worked through numerically.

A Scalar Trial-Effect Posterior

Consider a binary endpoint and let \(\theta\) be the treatment log odds ratio. For a transparent illustration, condition on a specified reference response probability \(p_0=0.30\), treated here as known. The treatment response probability at effect \(\theta\) is

$$ p_T(\theta)=\operatorname{logit}^{-1}\{\operatorname{logit}(p_0)+\theta\}, \qquad \operatorname{logit}(p)=\log\left(\frac{p}{1-p}\right). $$

Suppose \(x\) responses are observed among \(n\) treated participants, with the working likelihood \(X\mid\theta\sim\operatorname{Binomial}(n,p_T(\theta))\). Assign the prior \(\theta\sim N(0,1)\). This prior is centered on no treatment effect, \(\theta=0\), and its standard deviation is one log-odds-ratio unit. The example is a deliberately reduced model: it conditions on the reference probability rather than estimating it from control-arm data. A real analysis must justify that choice or model baseline risk as well.

Definition: The random-walk Metropolis update for a scalar target \(\pi(\theta\mid x)\) proposes \(\theta'=\theta+\varepsilon\), where \(\varepsilon\) is drawn from a specified increment distribution. It accepts \(\theta'\) with probability \(\alpha(\theta,\theta')\); if the proposal is rejected, the next chain value is the current value \(\theta\).

For a normal random walk, take \(\varepsilon\sim N(0,s^2)\), independently at each iteration, where \(s>0\) is a chosen proposal scale. Its proposal density is symmetric: \(q(\theta'\mid\theta)=q(\theta\mid\theta')\). In this case the proposal-density terms cancel, so only the target posterior density is needed in the acceptance comparison.

Dropping constants that do not depend on \(\theta\), the log-posterior target is

$$ \ell(\theta) =x\{\operatorname{logit}(p_0)+\theta\} -n\log\left[1+\exp\{\operatorname{logit}(p_0)+\theta\}\right] -\frac{\theta^2}{2}. $$

The first two terms are the binomial log likelihood, and the final term is the log of the \(N(0,1)\) prior kernel. The binomial coefficient and prior normalizing constant can be omitted because they are the same at the current and proposed values. The likelihood is bounded above by one on the probability scale, and the normal prior is proper, so this posterior is proper.

Key update: Define \(\Delta=\ell(\theta')-\ell(\theta)\). For a symmetric proposal, \(\alpha(\theta,\theta')=\min\{1,\exp(\Delta)\}\). Draw \(u\sim\operatorname{Uniform}(0,1)\); accept the proposal if \(u<\alpha(\theta,\theta')\), and otherwise retain \(\theta\).

Why the Update Targets the Posterior

The random walk should not simply accept every proposal: doing so would generally produce a chain concentrated according to the proposal mechanism, not the posterior. Instead, the acceptance probability corrects for differences in posterior support. A proposed point with higher target density is accepted; a point with lower target density is accepted sometimes, allowing movement across the posterior rather than trapping the chain at its highest point.

Here is the justification for a symmetric proposal. Write \(\pi(\theta)\) for the normalized target density. The probability flow from \(\theta\) to a distinct point \(\theta'\), under the target distribution and accepted moves, is proportional to

$$ \pi(\theta)q(\theta'\mid\theta) \min\left\{1,\frac{\pi(\theta')}{\pi(\theta)}\right\} = q(\theta'\mid\theta)\min\{\pi(\theta),\pi(\theta')\}. $$

Because the proposal is symmetric, \(q(\theta'\mid\theta)=q(\theta\mid\theta')\). The expression is therefore unchanged if \(\theta\) and \(\theta'\) are exchanged: probability flow is balanced in both directions. This is detailed balance, which implies that the target distribution is invariant under the transition. For the normal random walk, the proposal has positive density between any two real values. Together with the positive posterior density on the real line, this allows movement throughout the target support; rejections provide self-transitions. These conditions support posterior sampling, though they do not guarantee that a finite run will explore the target well.

Worked Example: Calculate an Upward Proposal

Suppose \(x=18\) responses are observed among \(n=40\) treated participants. Use \(p_0=0.30\), the \(N(0,1)\) prior, and current value \(\theta=0\). Consider the proposed value \(\theta'=0.2\). The reference log odds is \(\operatorname{logit}(0.30)=\log(3/7)\approx-0.847298\).

The log-posterior difference can be calculated without evaluating either full posterior density:

$$ \begin{aligned} \Delta &=18(0.2)-40\left[ \log\{1+\exp(-0.847298+0.2)\} -\log\{1+\exp(-0.847298)\} \right] -\frac{0.2^2-0^2}{2}\\ &\approx 3.6-40(0.420983-0.356675)-0.02\\ &\approx 1.00768. \end{aligned} $$

Since \(\Delta>0\), the proposed value has higher posterior density than the current value. Thus \(\alpha=\min\{1,\exp(1.00768)\}=1\), so this proposal is accepted for every draw \(u\) in \((0,1)\). The positive prior contribution has not been assumed: its actual change is \(-0.02\), slightly penalizing the move away from zero, while the likelihood more than offsets it.

Worked Example: Reject a Less-Supported Proposal

Continue with the same hypothetical data, and suppose the current chain value is now \(\theta=0.2\). Propose \(\theta'=1.4\). The log-posterior difference is

$$ \begin{aligned} \Delta &=18(1.4-0.2) -40\left[ \log\{1+\exp(-0.847298+1.4)\} -\log\{1+\exp(-0.847298+0.2)\} \right]\\ &\qquad-\frac{1.4^2-0.2^2}{2}\\ &\approx 21.6-40(1.007207-0.420983)-0.96\\ &\approx -2.80895. \end{aligned} $$

The acceptance probability is \(\alpha=\exp(-2.80895)\approx0.0603\). If the generated uniform value is \(u=0.40\), then \(u>\alpha\), so reject the proposal and record \(0.2\) again as the next chain value. Rejection does not mean the proposal is discarded from the record and replaced by a missing value; the repeated current value is part of the Markov chain and affects the posterior-frequency summaries calculated from its draws.

Worked Example: Accept a Downward Proposal Sometimes

As a separate transition, return to current value \(\theta=0\) and propose \(\theta'=-0.2\). Using the same data and model,

$$ \begin{aligned} \Delta &=18(-0.2) -40\left[ \log\{1+\exp(-0.847298-0.2)\} -\log\{1+\exp(-0.847298)\} \right] -\frac{(-0.2)^2}{2}\\ &\approx -3.6-40(0.300760-0.356675)-0.02\\ &\approx -1.38339. \end{aligned} $$

This proposal is less supported than the current value, but its acceptance probability is not zero: \(\alpha=\exp(-1.38339)\approx0.2508\). If \(u=0.20\), accept and move to \(-0.2\); if \(u=0.30\), reject and remain at zero. Both outcomes are correct possibilities under this transition rule.

Implementing and Checking the Calculation

A basic iteration can be written as a short sequence: generate one increment, calculate the proposed value, evaluate the log-posterior change, generate a uniform random number, and either update or repeat the current value. The log-posterior should be evaluated at both values using the same definition of the target. The calculation can omit constants, but it cannot omit any term that changes with \(\theta\), including the prior contribution.

1
Propose.
Draw \(\varepsilon\sim N(0,s^2)\) and set \(\theta'=\theta+\varepsilon\).
2
Compare target support.
Calculate \(\Delta=\ell(\theta')-\ell(\theta)\), including both likelihood and prior terms.
3
Decide on the move.
Set \(\alpha=\min\{1,\exp(\Delta)\}\). Draw \(u\sim\operatorname{Uniform}(0,1)\); accept if \(u<\alpha\), otherwise keep \(\theta\).

For numerical stability, implementation can compare \(\log(u)\) with \(\min(0,\Delta)\) instead of calculating a very small \(\exp(\Delta)\). This is the same rule because \(u<\exp(\Delta)\) is equivalent to \(\log(u)<\Delta\) when \(\Delta<0\), and all proposals are accepted when \(\Delta\geq0\). In the log likelihood, the expression \(\log(1+\exp(z))\) is often evaluated using a stable softplus calculation, especially when \(z\) is very large or very small.

Several checks can catch mistakes. At an identical proposed and current value, \(\Delta\) must be zero and acceptance certain. A positive \(\Delta\) must also give acceptance probability one. A negative \(\Delta\) must give a probability strictly between zero and one. Recomputing a transition with the current and proposed values reversed should reverse the sign of \(\Delta\); this is a useful check on subtraction order.

Common Pitfalls, Alternatives, and Limitations

  • Using a symmetric-proposal formula for an asymmetric proposal. Symmetry is why the proposal-density ratio cancels here. If the proposal is not symmetric, the acceptance probability must include the appropriate proposal-density terms; simply using the target-density ratio is then generally wrong.
  • Dropping the prior or changing the log-posterior sign. The target is likelihood times prior, so the log target includes both. The difference is proposed minus current; reversing that subtraction reverses the acceptance decision.
  • Exponentiating too early. Extremely small target densities can underflow to zero even when their log values are representable. Subtract log targets and, when needed, compare \(\log(u)\) to the log acceptance threshold.
  • Treating a rejection as an absent draw. A rejection means the chain stays at its current value. Removing that iteration changes the chain’s distribution and biases summaries toward accepted moves.
  • Assuming the method guarantees good exploration. A valid invariant distribution does not ensure that a finite chain visits all important posterior regions often enough. Long runs with repeated values or little movement can signal poor exploration; acceptance alone is not a complete measure of sampling quality.

For this one-dimensional target, direct numerical integration or a sufficiently fine grid can be an attractive alternative: it can approximate the posterior density and summaries without Monte Carlo dependence. Such calculations become less convenient as dimension increases or when the target is difficult to integrate. When a model permits direct conjugate posterior simulation, those independent draws are simpler and typically more efficient than a random walk. Random-walk Metropolis is useful when direct simulation is unavailable, but its draws are correlated and its performance depends on the proposal scale. The reduced model also has substantive limitations: fixing \(p_0\) treats baseline risk as known, and the assumed binomial model may not represent all trial features. Computational convenience does not justify those modeling assumptions.

Key takeaway: For a scalar treatment effect and a symmetric random walk, the essential calculation is \(\Delta=\ell(\theta')-\ell(\theta)\). Keep every parameter-dependent likelihood and prior term, accept automatically when \(\Delta\geq0\), and accept a lower-density proposal with probability \(\exp(\Delta)\).

Check Your Understanding

Use the hypothetical binomial-logistic model and update rule above. Answers follow the questions.

  1. If \(\Delta=0.35\), what is the acceptance probability, and what happens for \(u=0.70\)?
  2. If \(\Delta=-2\), calculate the acceptance probability to three significant figures. Is a proposal accepted when \(u=0.10\)?
  3. Why can the binomial coefficient and prior normalizing constant be omitted from the log-posterior difference, while the quadratic prior term cannot?
  4. For a rejected proposal from current value \(\theta=0.5\), what value is recorded as the next chain value, and why?

Solutions. (1) Since \(\Delta>0\), \(\alpha=1\); \(u=0.70\) therefore leads to acceptance. (2) \(\alpha=\exp(-2)\approx0.135\); because \(0.10<0.135\), accept. (3) The binomial coefficient and prior normalizing constant do not depend on \(\theta\), so they cancel when subtracting the two log targets. The quadratic term changes with \(\theta\), so omitting it changes the posterior ratio and the acceptance probability. (4) Record \(0.5\) again: rejection leaves the chain at its current value, and that repeated value is a valid chain draw.

Clinical Biostats