Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

A MCMC example for a Bayesian multiple logistic regression

A MCMC example for a Bayesian multiple logistic regression

This algorithm samples from the posterior distribution of the regression coefficients β\beta in a Bayesian logistic regression model with a flat (uniform) prior.


Model Setup

  • Likelihood: For binary data yi∈{0,1}y_i \in \{0, 1\}, and predictors XiX_i, the model is:

    yi∼Bernoulli(pi),pi=11+exp⁡(−ηi),ηi=Xiβy_i \sim \mathrm{Bernoulli}(p_i), \quad p_i = \frac{1}{1 + \exp(-\eta_i)}, \quad \eta_i = X_i \beta

    The likelihood for all data is:

    p(y∣β,X)=∏i=1npiyi(1−pi)1−yip(\mathbf{y} | \beta, X) = \prod_{i=1}^n p_i^{y_i} (1 - p_i)^{1-y_i}
  • Log-Likelihood (as implemented in logit_loglik):

    log⁡p(y∣β,X)=∑i=1nyilog⁡(pi)+(1−yi)log⁡(1−pi)\log p(\mathbf{y} | \beta, X) = \sum_{i=1}^n y_i \log(p_i) + (1 - y_i) \log(1 - p_i)

    In code:

    • log⁡(pi)=log⁡(11+exp⁡(−ηi))\log(p_i) = \log\left(\frac{1}{1 + \exp(-\eta_i)}\right)

    • log⁡(1−pi)=log⁡(11+exp⁡(ηi))\log(1 - p_i) = \log\left(\frac{1}{1 + \exp(\eta_i)}\right)

  • Prior:

    • Flat prior: p(β)∝1p(\beta) \propto 1

    • The posterior is proportional to the likelihood alone.

  • Posterior:

    p(β∣y,X)∝p(y∣β,X)p(\beta | \mathbf{y}, X) \propto p(\mathbf{y} | \beta, X)

Algorithm Steps and Equations

  1. Proposal Step:

    • Propose β∗∼N(β(t−1), δ V)\beta^* \sim \mathcal{N}(\beta^{(t-1)},\, \delta\,V) where VV is the estimated covariance from the frequentist fit.

  2. Acceptance Ratio:

    • With a flat prior, the Metropolis-Hastings acceptance ratio simplifies to:

      R=p(y∣β∗,X)p(y∣β(t−1),X)R = \frac{p(\mathbf{y} | \beta^*, X)}{p(\mathbf{y} | \beta^{(t-1)}, X)}

      The code works on the log scale for stability:

      log⁡R=log⁡p(y∣β∗,X)−log⁡p(y∣β(t−1),X)\log R = \log p(\mathbf{y} | \beta^*, X) - \log p(\mathbf{y} | \beta^{(t-1)}, X)
    • Accept β∗\beta^* with probability min⁡(1,R)\min(1, R).

  3. Sampling:

    • If accepted, set β(t)=β∗\beta^{(t)} = \beta^*. Otherwise, set β(t)=β(t−1)\beta^{(t)} = \beta^{(t-1)}.

    • Repeat for nIter iterations, discarding the first nBurnIn as burn-in.


<Figure size 1200x800 with 6 Axes>
  Parameter    Median        SD
0     alpha  0.381343  0.132908
1     beta1  1.099688  0.172224
2     beta2 -0.514806  0.142406

Adding a prior distribution

A Multivariate Normal Prior

A common choice for the prior on β\beta is the multivariate normal (Gaussian) distribution:

β∼N(μ0,Σ0)\beta \sim \mathcal{N}(\mu_0, \Sigma_0)

where:

  • μ0\mu_0 is the prior mean vector (typically a vector of zeros for a weakly informative or “neutral” prior).

  • Σ0\Sigma_0 is the prior covariance matrix (often diagonal, with large values for weakly informative priors, or small values for strong regularization).

The density function is:

p(β)=1(2π)p/2∣Σ0∣1/2exp⁡(−12(β−μ0)⊤Σ0−1(β−μ0))p(\beta) = \frac{1}{(2\pi)^{p/2} |\Sigma_0|^{1/2}} \exp\left(-\frac{1}{2} (\beta - \mu_0)^\top \Sigma_0^{-1} (\beta - \mu_0)\right)

Role in Posterior Sampling

In the Metropolis-Hastings algorithm, the prior is combined with the likelihood to determine the posterior (in this case, the unormalized posterior):

p(β∣y,X)∝p(y∣β,X)⋅p(β)p(\beta | \mathbf{y}, X) \propto p(\mathbf{y} | \beta, X) \cdot p(\beta)

This means that higher prior density values for certain β\beta make those values more likely to be sampled, all else equal. A normal prior shrinks coefficients toward the prior mean and helps regularize the model, especially when data are sparse or predictors are highly correlated.

<Figure size 1200x800 with 6 Axes>
  Parameter    Median        SD
0     alpha  0.592558  0.308010
1     beta1 -0.750312  0.338694
2     beta2  2.097528  0.437506