Tutorials › Generalized Linear Mixed Models (GLMM) › Pólya-Gamma Sampling for Logistic Random-Effects Models

Bayesian GLMM and Prior Specification for Variance Components · Tutorial 271 of 698

Pólya-Gamma Sampling for Logistic Random-Effects Models

Learn how Pólya-Gamma latent variables turn the logistic likelihood into a quadratic form, allowing joint Gaussian updates for regression coefficients and random effects.

Advanced 12 min read

What You'll Learn

  • Derive the Pólya-Gamma identity from the binomial logistic likelihood.
  • Distinguish the likelihood kernel from the full likelihood, including its constants.
  • Construct a joint conditional Gaussian update for fixed and random effects.
  • Work through grouped-binomial and random-intercept calculations.
  • Identify what remains nonconjugate and how to detect implementation errors.

Why Pólya-Gamma Augmentation Helps

In a logistic random-effects model, the log likelihood is not quadratic in the coefficients. A Gaussian prior therefore does not produce a Gaussian posterior conditional on the observed outcomes alone. Pólya-Gamma augmentation introduces latent positive variables that make the likelihood quadratic in the linear predictors. Conditional on those variables, the fixed effects and random effects have a multivariate Gaussian distribution and can be updated jointly.

This is a conditional simplification, not a claim that the marginal posterior is Gaussian. The latent variables must be sampled, and variance and correlation parameters generally still require their own updates. The construction applies to the usual logistic link and is particularly useful when Gaussian priors are specified for regression coefficients and random effects.

Model and the Pólya-Gamma Identity

Suppose there are \(m\) conditionally independent binomial observations. For observation \(i\), \(y_i\) of \(n_i\) trials are successes, and the success probability is \(p_i\). Conditional on the model parameters,

$$ y_i\mid p_i \sim \operatorname{Binomial}(n_i,p_i), \qquad \operatorname{logit}(p_i)=\eta_i=o_i+\mathbf c_i^\mathsf{T}\boldsymbol\theta . $$

Here \(o_i\) is a known offset, \(\mathbf c_i^\mathsf{T}\) is row \(i\) of a design matrix \(\mathbf C\), and \(\boldsymbol\theta\) collects the fixed-effect coefficients and random effects. The binomial observations are independent conditional on these quantities. For a GLMM, the random effects have a Gaussian prior conditional on variance and correlation parameters. In the derivations below, those parameters are held fixed, and the resulting prior for \(\boldsymbol\theta\) is written \(N(\mathbf m,\mathbf Q^{-1})\), with positive-definite precision matrix \(\mathbf Q\).

Set \(\kappa_i=y_i-n_i/2\). The binomial likelihood for observation \(i\) is

$$ \binom{n_i}{y_i} \frac{\exp(y_i\eta_i)}{(1+\exp(\eta_i))^{n_i}}. $$

The Pólya-Gamma distribution \(\operatorname{PG}(b,c)\) is a distribution on positive values. The identity needed here is its Laplace transform:

$$ \mathbb E_{\omega\sim\operatorname{PG}(b,0)} \left[\exp\left(-\frac{\omega\psi^2}{2}\right)\right] = \cosh^{-b}(\psi/2). $$

Since \(1+\exp(\psi)=2\exp(\psi/2)\cosh(\psi/2)\), the logistic likelihood can be rewritten using this transform. The full binomial likelihood, not just its kernel, satisfies

$$ \binom{n_i}{y_i} \frac{\exp(y_i\eta_i)}{(1+\exp(\eta_i))^{n_i}} = \binom{n_i}{y_i}2^{-n_i}\exp(\kappa_i\eta_i) \mathbb E_{\omega_i\sim\operatorname{PG}(n_i,0)} \left[\exp\left(-\frac{\omega_i\eta_i^2}{2}\right)\right]. $$
Definition: Pólya-Gamma augmentation for a binomial logistic observation introduces a latent variable \(\omega_i\) whose prior distribution in the mixture representation is \(\operatorname{PG}(n_i,0)\). Conditional on the linear predictor, its full conditional distribution is \(\operatorname{PG}(n_i,\eta_i)\). The likelihood kernel has the mixture representation with factor \(2^{-n_i}\); the full likelihood also includes \(\binom{n_i}{y_i}\).

The conditional distribution follows because the joint density of \(\omega_i\) and the data, as a function of \(\omega_i\) given \(\eta_i\), is proportional to \(\exp(-\omega_i\eta_i^2/2)p(\omega_i\mid n_i,0)\), the exponential tilting that defines \(\operatorname{PG}(n_i,\eta_i)\). Thus the augmentation has two alternating steps: draw each \(\omega_i\) given the current \(\eta_i\), then draw the coefficient vector given the \(\omega_i\)'s.

Worked Example: Checking the Full Binomial Identity

Consider a hypothetical observation with \(n=5\), \(y=3\), and linear predictor \(\eta=\log 2\). The success probability is \(2/3\), so the original likelihood is

$$ \binom{5}{3}\left(\frac{2}{3}\right)^3\left(\frac{1}{3}\right)^2 = 10\cdot\frac{8}{243} = \frac{80}{243} \approx 0.3292. $$

Here \(\kappa=3-5/2=1/2\). The mixture representation gives

$$ 10\cdot 2^{-5}\exp\left(\frac{\log 2}{2}\right) \mathbb E_{\omega\sim\operatorname{PG}(5,0)} \left[\exp\left(-\frac{\omega(\log 2)^2}{2}\right)\right]. $$

By the Laplace transform, the expectation is \(\cosh^{-5}((\log 2)/2)\). Substitution into the expression gives \(10\cdot 2^{-5}\exp((\log 2)/2)\cosh^{-5}((\log 2)/2)=80/243\). The factor \(2^{-5}\) is essential; omitting it would not give the likelihood. The binomial coefficient is also needed for the full likelihood, although it is constant in \(\eta\) and can be dropped when forming a parameter-dependent kernel.

Deriving the Gaussian Conditional Update

Write \(\boldsymbol\kappa=(\kappa_1,\ldots,\kappa_m)^\mathsf{T}\), \(\boldsymbol\omega=(\omega_1,\ldots,\omega_m)^\mathsf{T}\), and \(\boldsymbol\Omega=\operatorname{diag}(\omega_1,\ldots,\omega_m)\). Conditional on the latent variables, the part of the joint likelihood that depends on \(\boldsymbol\theta\) is

$$ \exp\left\{ \boldsymbol\kappa^\mathsf{T}(\mathbf o+\mathbf C\boldsymbol\theta) -\frac{1}{2}(\mathbf o+\mathbf C\boldsymbol\theta)^\mathsf{T} \boldsymbol\Omega(\mathbf o+\mathbf C\boldsymbol\theta) \right\}. $$

Multiplying by the Gaussian prior density adds the exponent \(-(\boldsymbol\theta-\mathbf m)^\mathsf{T}\mathbf Q(\boldsymbol\theta-\mathbf m)/2\). Collecting the terms that involve \(\boldsymbol\theta\) gives a quadratic form with precision and precision-weighted mean

$$ \mathbf Q_\ast=\mathbf Q+\mathbf C^\mathsf{T}\boldsymbol\Omega\mathbf C, \qquad \mathbf h_\ast=\mathbf Q\mathbf m+ \mathbf C^\mathsf{T}(\boldsymbol\kappa-\boldsymbol\Omega\mathbf o). $$

Completing the square therefore gives the conditional distribution

$$ \boldsymbol\theta\mid\boldsymbol\omega,\mathbf y \sim N\left(\mathbf Q_\ast^{-1}\mathbf h_\ast,\ \mathbf Q_\ast^{-1}\right). $$

This derivation requires a Gaussian prior with a positive-definite precision matrix conditional on the variance and correlation parameters, and the logistic binomial likelihood specified above. Because every \(\omega_i>0\), the matrix \(\mathbf C^\mathsf{T}\boldsymbol\Omega\mathbf C\) is positive semidefinite. Adding it to positive-definite \(\mathbf Q\) leaves \(\mathbf Q_\ast\) positive definite, so the Gaussian conditional is well-defined even if the design matrix alone is rank deficient.

1
Update the latent variables.
For each observation, draw \(\omega_i\sim\operatorname{PG}(n_i,\eta_i)\) using the current coefficients and random effects.
2
Form the Gaussian parameters.
Calculate \(\mathbf Q_\ast\) and \(\mathbf h_\ast\) from the prior, design matrix, offsets, outcomes, and latent draws.
3
Draw the coefficient vector.
Sample \(\boldsymbol\theta\) from \(N(\mathbf Q_\ast^{-1}\mathbf h_\ast,\mathbf Q_\ast^{-1})\), then recompute the linear predictors for the next latent-variable update.

Worked Example: A One-Coefficient Gaussian Update

Suppose a hypothetical intercept-only model has \(n=4\), \(y=3\), and prior \(\beta\sim N(0,4)\). Conditional on a latent draw \(\omega=0.8\), the likelihood kernel in \(\beta\) is proportional to \(\exp(\kappa\beta-\omega\beta^2/2)\), where \(\kappa=3-4/2=1\). The prior precision is \(1/4=0.25\).

The conditional precision is \(0.25+0.8=1.05\), so the conditional variance is \(1/1.05=0.9524\). The precision-weighted mean is \(0+1=1\), giving conditional mean \(1/1.05=0.9524\). Therefore

$$ \beta\mid\omega,y \sim N(0.9524,\ 0.9524), \qquad SD(\beta\mid\omega,y)=\sqrt{1/1.05}=0.9759. $$

This is the coefficient update conditional on the specified latent draw, not the marginal posterior for \(\beta\). In an actual sampler, \(\omega\) is itself drawn from \(\operatorname{PG}(4,\beta)\) at the current value of \(\beta\).

Including Random Effects

The Gaussian update applies to fixed and random effects together, provided their joint prior is Gaussian conditional on the variance and correlation parameters. For example, let \(\boldsymbol\theta=(\boldsymbol\beta^\mathsf{T},\mathbf b^\mathsf{T})^\mathsf{T}\), where \(\mathbf b\) contains group-level effects. The design matrix \(\mathbf C\) then contains both fixed-effect and random-effect columns. Its cross-products automatically include the dependence induced by observations sharing a group. A random effect is not independently updated observation by observation; its posterior information accumulates across every observation to which it contributes.

Worked Example: Joint Update for Two Random Intercepts

Consider hypothetical data from two groups, with \(n_1=n_2=2\), \(y_1=2\), and \(y_2=0\). Let \(\eta_j=\beta+b_j\). Use independent priors \(\beta\sim N(0,4)\) and \(b_j\sim N(0,1)\). For illustration, condition on latent draws \(\omega_1=\omega_2=0.5\). These are inputs to this conditional update; in a sampling iteration they would be generated from the appropriate Pólya-Gamma full conditionals.

Take \(\boldsymbol\theta=(\beta,b_1,b_2)^\mathsf{T}\). The design matrix has rows \((1,1,0)\) and \((1,0,1)\), and \(\boldsymbol\kappa=(2-1,0-1)^\mathsf{T}=(1,-1)^\mathsf{T}\). The prior precision is \(\operatorname{diag}(0.25,1,1)\). Thus

$$ \mathbf Q_\ast= \begin{pmatrix} 1.25&0.5&0.5\\ 0.5&1.5&0\\ 0.5&0&1.5 \end{pmatrix}, \qquad \mathbf h_\ast= \begin{pmatrix}0\\1\\-1\end{pmatrix}. $$

Solving \(\mathbf Q_\ast\boldsymbol\mu_\ast=\mathbf h_\ast\) gives \(\beta=0\), \(b_1=2/3\), and \(b_2=-2/3\): the first equation is satisfied because \(0.5(2/3)+0.5(-2/3)=0\), and the other two give \(1.5(2/3)=1\) and \(1.5(-2/3)=-1\). The conditional update is therefore

$$ \boldsymbol\theta\mid\boldsymbol\omega,\mathbf y \sim N\left( \begin{pmatrix}0\\2/3\\-2/3\end{pmatrix}, \mathbf Q_\ast^{-1} \right). $$

The observations pull the group effects in opposite directions, while the shared intercept and prior structure are accounted for jointly. The values \(2/3\) and \(-2/3\) are conditional means, not estimates that incorporate uncertainty in the latent variables or variance parameters.

What the Method Does—and Does Not—Solve

Pólya-Gamma sampling is an exact augmentation: integrating out each \(\omega_i\) recovers the original logistic likelihood. Conditional Gaussianity makes the coefficient update convenient, but it does not eliminate all computational work. The \(\omega_i\)'s must be generated from their tilted distributions, and random-effect variance and correlation parameters do not generally become conjugate merely because the logistic likelihood has been augmented. Their updates must still respect their specified priors and the model's hierarchical structure.

Compared with the latent-variable augmentation for binary GLMMs, which commonly represents binary outcomes through thresholded latent variables and a probit link, Pólya-Gamma augmentation preserves the logistic link directly. It also differs from a random-walk Metropolis update for coefficients: here the coefficient block has an exact Gaussian conditional, rather than a proposal accepted or rejected using a likelihood ratio. Neither comparison means that the augmentation is universally faster; performance depends on model dimension, data, and the quality of the latent-variable sampler.

Several practical checks help identify errors. Verify that \(\kappa_i=y_i-n_i/2\), that the Pólya-Gamma shape is \(n_i\) for a binomial observation, and that the offset term enters as \(-\mathbf C^\mathsf{T}\boldsymbol\Omega\mathbf o\) in \(\mathbf h_\ast\). Check that \(\mathbf Q_\ast\) is symmetric positive definite and that simulated \(\omega_i\)'s are positive. Comparing a small model's likelihood-based posterior calculations with the augmented calculation can expose missing constants or incorrect signs.

A common mistake is to treat the identity as an exact representation of the likelihood kernel without its multiplier, or to say that the likelihood is exactly \(\exp(\kappa_i\eta_i-\omega_i\eta_i^2/2)\) mixed over \(\operatorname{PG}(n_i,0)\). The correct mixture for the kernel includes \(2^{-n_i}\); the full likelihood additionally includes \(\binom{n_i}{y_i}\). Another mistake is to call the marginal posterior Gaussian. Only the conditional distribution of the coefficient vector, given the latent variables and variance parameters, is Gaussian. The marginal distribution averages over those latent variables and is generally not Gaussian.

Key takeaway: Conditional on \(\omega_i\sim\operatorname{PG}(n_i,\eta_i)\), the logistic binomial likelihood contributes the quadratic kernel \(\exp\{\boldsymbol\kappa^\mathsf{T}\boldsymbol\eta-\boldsymbol\eta^\mathsf{T}\boldsymbol\Omega\boldsymbol\eta/2\}\). Its mixture representation for the likelihood kernel includes \(2^{-\sum_i n_i}\), and the full likelihood also includes \(\prod_i\binom{n_i}{y_i}\). With a Gaussian prior, fixed and random effects have a Gaussian conditional update.

Check Your Understanding

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

  1. For \(n_i=6\) and \(y_i=4\), what is \(\kappa_i\), and what is the shape parameter of the Pólya-Gamma full conditional?
  2. Why does the Gaussian coefficient update remain well-defined when \(\mathbf C\) is rank deficient, assuming \(\mathbf Q\) is positive definite?
  3. With no offset, state the conditional precision and precision-weighted mean for prior \(N(\mathbf m,\mathbf Q^{-1})\).
  4. Does Pólya-Gamma augmentation make the marginal posterior of the fixed and random effects Gaussian? Explain.

Solutions. (1) \(\kappa_i=4-6/2=1\), and the full conditional is \(\operatorname{PG}(6,\eta_i)\). (2) \(\mathbf C^\mathsf{T}\boldsymbol\Omega\mathbf C\) is positive semidefinite, so its sum with positive-definite \(\mathbf Q\) remains positive definite. (3) The precision is \(\mathbf Q+\mathbf C^\mathsf{T}\boldsymbol\Omega\mathbf C\), and the precision-weighted mean is \(\mathbf Q\mathbf m+\mathbf C^\mathsf{T}\boldsymbol\kappa\). (4) No. Gaussianity holds conditional on the latent variables and variance parameters; averaging over them generally produces a non-Gaussian marginal posterior.

Clinical Biostats