Tutorials › Bayesian Clinical Trials › Metropolis-Within-Gibbs For Logistic Trial Models

Posterior Computation · Tutorial 124 of 696

Metropolis-Within-Gibbs For Logistic Trial Models

Learn how to update logistic-regression coefficients conditionally with Metropolis steps and evaluate each proposed move using the trial likelihood and prior.

Intermediate 11 min read

What You'll Learn

  • Write the Bernoulli logistic-regression posterior kernel for a clinical trial.
  • Construct a conditional target for one regression coefficient while holding the others fixed.
  • Calculate a coordinate-wise Metropolis acceptance probability on the log scale.
  • Understand why a sequential sweep of valid coordinate updates preserves the joint posterior.
  • Identify model and computational problems that can undermine the sampler.
  • Compare coordinate-wise updates with conjugate Gibbs and joint Metropolis approaches.

Why Logistic Trial Models Need Metropolis-Within-Gibbs

A binary trial endpoint is often modeled by letting a participant’s probability of response depend on treatment assignment and baseline characteristics. With a logistic link, the likelihood is straightforward to evaluate, but the resulting coefficient posteriors generally do not have conjugate full conditional distributions. A practical option is to update one coefficient at a time with a Metropolis step, while treating the remaining coefficients as fixed during that step. Repeating this for every coefficient creates a Metropolis-within-Gibbs sampler.

The method combines two familiar ideas: the conditional updating structure of Gibbs sampling and the accept-or-reject correction of Metropolis-Hastings. The key implementation detail is to evaluate the logistic likelihood at the proposed coefficient value, using the most recently available values of all the other coefficients. In particular, a treatment coefficient changes the linear predictor only for participants whose treatment indicator is one.

Model and Posterior Kernel

Definition: For participant \(i=1,\ldots,n\), let \(Y_i\in\{0,1\}\) indicate a binary outcome, let \(x_i\) be a vector of recorded covariates, and let \(\beta\) be the corresponding regression-coefficient vector. The logistic regression model assumes conditional independence of outcomes given the covariates and coefficients, with $$ Y_i\mid x_i,\beta\sim\operatorname{Bernoulli}(p_i),\qquad \operatorname{logit}(p_i)=x_i^\mathsf{T}\beta,\qquad p_i=\frac{\exp(x_i^\mathsf{T}\beta)}{1+\exp(x_i^\mathsf{T}\beta)}. $$

For example, a simple two-arm trial can use \(x_i=(1,A_i)^\mathsf{T}\), where \(A_i=1\) denotes assignment to the treatment arm and \(A_i=0\) denotes control. Then \(\beta=(\beta_0,\beta_1)^\mathsf{T}\), the control-arm log odds are \(\beta_0\), and the treatment-versus-control log odds contrast is \(\beta_1\). This interpretation is conditional on the specified model and any covariates included in it.

Assume for this tutorial that the coefficients have independent proper normal priors, \(\beta_j\sim N(m_j,s_j^2)\), with \(s_j^2>0\). Conditional on the observed trial data, the posterior density is proportional to the likelihood times the prior density. For computation, it is useful to work with its log kernel, omitting only constants that do not depend on \(\beta\).

$$ \ell(\beta) = \sum_{i=1}^n \left[ Y_i x_i^\mathsf{T}\beta -\log\{1+\exp(x_i^\mathsf{T}\beta)\} \right] -\frac12\sum_j\frac{(\beta_j-m_j)^2}{s_j^2}. $$

The first term follows because \(P(Y_i\mid x_i,\beta)=p_i^{Y_i}(1-p_i)^{1-Y_i}\). Taking logarithms gives \(Y_i\log(p_i)+(1-Y_i)\log(1-p_i)=Y_i x_i^\mathsf{T}\beta-\log\{1+\exp(x_i^\mathsf{T}\beta)\}\). The second term is the log density of the independent normal priors, with constants omitted. A proper prior makes the posterior proper for this finite binary dataset, including cases in which the likelihood alone has separation.

Worked Example: Writing the Trial Log Posterior

Suppose a hypothetical trial has 8 control participants, of whom 3 respond, and 6 treatment participants, of whom 4 respond. Use the model \(\operatorname{logit}(p_i)=\beta_0+\beta_1A_i\), with independent \(N(0,4)\) priors for both coefficients. The data can be grouped by arm because all participants within an arm have the same linear predictor.

For a group with \(y\) responses out of \(n\) participants and linear predictor \(\eta\), the log likelihood, omitting the binomial coefficient, is \(y\eta-n\log(1+\exp(\eta))\). Thus, with \(\eta_C=\beta_0\) and \(\eta_T=\beta_0+\beta_1\), the log posterior kernel is

$$ \ell(\beta_0,\beta_1) = 3\beta_0-8\log(1+\exp(\beta_0)) +4(\beta_0+\beta_1)-6\log(1+\exp(\beta_0+\beta_1)) -\frac{\beta_0^2+\beta_1^2}{8}. $$

The prior contribution has denominator 8 because a normal variance of 4 gives the log-density quadratic term \(-\beta_j^2/(2\cdot4)\). The likelihood terms use the observed response counts and arm sizes; the prior and likelihood together, rather than the likelihood alone, determine the posterior target.

Coordinate-Wise Metropolis Updates

A sweep updates coefficients in a fixed order. When updating coefficient \(\beta_j\), hold all other coefficients at their current values and propose a candidate \(z\). With independent normal priors, the conditional target for this step is proportional to the likelihood as a function of \(\beta_j\) times the prior density for \(\beta_j\). Prior factors for the other coefficients cancel from the acceptance ratio.

For a symmetric random-walk proposal, such as \(z\sim N(\beta_j^{\mathrm{cur}},\tau_j^2)\), the forward and reverse proposal densities are equal. The log acceptance ratio is therefore the difference between the conditional log-posterior kernels:

$$ r=\ell(\beta_1,\ldots,\beta_{j-1},z,\beta_{j+1},\ldots) -\ell(\beta_1,\ldots,\beta_j^{\mathrm{cur}},\ldots). $$

Accept the proposal with probability \(\min\{1,\exp(r)\}\). Equivalently, draw \(U\sim\operatorname{Uniform}(0,1)\) and accept when \(\log(U)<r\). If the proposal is not symmetric, its reverse-to-forward proposal-density ratio must also be included. A proposal that is accepted becomes the current value used in subsequent coefficient updates during that sweep.

Worked Example: Updating a Treatment Effect

Return to the hypothetical trial with 3 responses among 8 control participants and 4 among 6 treatment participants. The current values are \(\beta_0=-0.4\) and \(\beta_1=-0.2\). Propose \(\beta_1=0.4\), keeping \(\beta_0=-0.4\). Suppose the prior for \(\beta_1\) is \(N(0,1.5^2)\), and use a symmetric proposal. The control-arm predictor stays at \(-0.4\), so its likelihood cancels. The treatment predictor changes from \(-0.4+(-0.2)=-0.6\) to \(-0.4+0.4=0\).

For the treatment arm, \(y=4\) and \(n=6\). At the current predictor, the log-likelihood contribution is

$$ 4(-0.6)-6\log\{1+\exp(-0.6)\} \approx -2.4-6(0.43749) \approx -5.02493. $$

At the proposed predictor, it is

$$ 4(0)-6\log\{1+\exp(0)\} =-6\log(2) \approx -4.15888. $$

The likelihood change is approximately \(0.86604\). The prior log-density change, with constants canceled, is

$$ -\frac{0.4^2}{2(1.5^2)} +\frac{(-0.2)^2}{2(1.5^2)} = -\frac{0.16-0.04}{4.5} \approx -0.02667. $$

Therefore \(r\approx0.86604-0.02667=0.83938\), and the acceptance probability is \(\min\{1,\exp(0.83938)\}=1\). This proposal is accepted. The positive log ratio means that the proposed value has a higher conditional posterior density than the current value; it does not mean that the value itself has posterior probability one.

Why the Sequential Sampler Targets the Joint Posterior

Each coordinate update is a Metropolis-Hastings transition whose target is the corresponding full conditional distribution under the joint posterior. If the other coordinates are held fixed, the acceptance rule corrects for the proposal so that this conditional distribution is invariant for that update. Consequently, when the current vector has the joint posterior distribution, updating one coordinate leaves the joint distribution unchanged. Applying the same reasoning successively to every coordinate shows that a complete sweep also preserves the joint posterior.

This establishes the target distribution, not the speed of convergence from an arbitrary starting point. With proper normal priors and nonzero-variance normal random-walk proposals, each coefficient can move across the real line, supporting exploration of the posterior. Nonetheless, high posterior dependence can make coordinate moves inefficient: changing one coefficient at a time may require many small accepted steps to follow a narrow, tilted region of high posterior density.

1
Choose a coefficient.
Use a fixed order, and retain the most recently available values of all other coefficients.
2
Propose a candidate.
For a random-walk update, draw the candidate from a distribution centered at the current coefficient value.
3
Evaluate the conditional log ratio.
Recompute the affected likelihood contributions and the prior contribution for this coefficient; add a proposal correction if the proposal is not symmetric.
4
Accept or retain.
Accept when \(\log(U)<r\); otherwise keep the current coefficient. Continue until every coefficient has been updated.

Worked Example: A Proposal That Is Rejected

Consider a hypothetical intercept-only model with 2 responses among 5 participants and prior \(\beta_0\sim N(0,1)\). The current intercept is 0, and a symmetric random-walk proposal produces 1. The current log likelihood is \(2(0)-5\log(2)\approx-3.46574\). The proposed log likelihood is

$$ 2(1)-5\log(1+\exp(1)) \approx -4.56631. $$

The likelihood change is approximately \(-1.10057\). The prior log-density change is \(-1^2/2-0^2/2=-0.5\), so \(r\approx-1.60057\) and the acceptance probability is \(\exp(-1.60057)\approx0.2018\). If the auxiliary draw is \(U=0.30\), then \(\log(0.30)\approx-1.2040\) is not less than \(-1.60057\), so the proposal is rejected and the intercept remains 0 for this update.

Implementation Checks, Alternatives, and Limitations

Evaluate the log posterior rather than exponentiating a likelihood that may be extremely small. For numerical stability, calculate \(\log(1+\exp(\eta))\) with a stable log-one-plus-exponential routine when \(\eta\) could be large. For grouped binomial data, use \(y\eta-n\log(1+\exp(\eta))\); using \(n-y\) in place of \(n\), or omitting the failures’ contribution to the denominator, gives the wrong likelihood. Check the design matrix, arm coding, response counts, and the predictor affected by each proposed coefficient.

A sampler can be mathematically correct yet computationally ineffective. Many consecutive rejections suggest proposals may be too large for the local posterior scale; extremely small moves can be accepted frequently but explore the posterior slowly. Strong posterior dependence can also make single-coordinate movement inefficient. These patterns are reasons to inspect the chain and reconsider the update strategy, not grounds for changing the target density. A proper prior helps address posterior propriety, but it does not repair a misspecified link, unmodeled dependence, incorrect outcome coding, or an inappropriate trial design model.

When full conditional distributions are available in standard forms, ordinary Gibbs sampling draws directly from them and needs no accept-or-reject step; logistic regression with normal coefficient priors generally does not provide those conjugate conditionals. A joint random-walk Metropolis update proposes the entire coefficient vector at once and uses one acceptance decision, which can account for coordinated movement but may be sensitive to the joint proposal. Metropolis-within-Gibbs is a convenient compromise when coordinate-wise conditional targets are easy to evaluate. Its main limitation is that changing coefficients one at a time can mix slowly when posterior dependence is strong.

A practical diagnostic is to compare the proposed and accepted values, not just the acceptance frequency: verify that a hand-calculated update agrees with the code for a small dataset. Also check that rejected proposals leave the coefficient unchanged and that each update uses the latest values from the same sweep. Convergence and mixing require assessment from generated chains; a correct acceptance formula alone does not establish that posterior summaries are reliable.

Common pitfall: When proposing a treatment coefficient, it is tempting to change the treatment-arm predictor without recomputing it from the current intercept and proposed treatment coefficient. The predictor is \(\beta_0+\beta_1\), so the current value must use the current pair and the proposed value must use the proposed pair. Recompute each affected linear predictor explicitly before evaluating the likelihood.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions below.

  1. In a two-arm model with predictor \(\beta_0+\beta_1A_i\), which participants’ likelihood contributions change when proposing a new \(\beta_1\)?
  2. For a symmetric random-walk proposal, what terms enter the log acceptance ratio for a single coefficient under independent normal priors?
  3. A proposed coefficient has log acceptance ratio \(r=-2\). What is its acceptance probability, to three significant figures?
  4. Why can a proper prior be useful when the trial data exhibit separation?
  5. State one reason a coordinate-wise sampler might explore the posterior slowly even when its acceptance calculations are correct.

Solutions. (1) Only participants with \(A_i=1\) have predictors changed by \(\beta_1\); control participants have \(A_i=0\), so their predictors remain \(\beta_0\). (2) Use the change in the affected likelihood contributions plus the change in the updated coefficient’s log-prior density. Other independent prior factors cancel, as do symmetric proposal densities. (3) The probability is \(\min\{1,\exp(-2)\}=\exp(-2)\approx0.135\). (4) Separation can make the likelihood continue to increase as a coefficient moves toward an extreme value; a proper prior can yield a proper posterior and regularize such coefficients. (5) Strong posterior dependence can make one-coordinate-at-a-time moves inefficient, because the chain may need many successive updates to move along a correlated region of high posterior density.

Clinical Biostats