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 implementation for a Bayesian Poisson Regression

A MCMC implementation for a Bayesian Poisson Regression

Model Setup

  • Data: Counts yi∈{0,1,2,… }y_i \in \{0, 1, 2, \dots\} and predictors XiX_i for i=1,…,ni = 1, \dots, n.

    yi∼Poisson(λi),log⁡λi=Xiβy_i \sim \text{Poisson}(\lambda_i), \quad \log \lambda_i = X_i \beta

    where XiX_i is the vector of predictors for observation ii, and β\beta is the vector of regression coefficients.


Likelihood

The likelihood for the observed data, given β\beta and XX, is based on the Poisson Probability Mass Function (PMF) since this is a discrete distribution:

p(y∣β,X)=∏i=1ne−λiλiyiyi!p(\mathbf{y} | \beta, X) = \prod_{i=1}^n \frac{e^{-\lambda_i} \lambda_i^{y_i}}{y_i!}

where λi=exp⁡(Xiβ)\lambda_i = \exp(X_i \beta).

The log-likelihood:

log⁡p(y∣β,X)=∑i=1n(yi(Xiβ)−exp⁡(Xiβ))+const\log p(\mathbf{y} | \beta, X) = \sum_{i=1}^n \left( y_i (X_i\beta) - \exp(X_i\beta) \right) + \text{const}

Prior

A multivariate normal (Gaussian) prior is placed on β\beta:

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

where:

  • μ0\mu_0 is the prior mean vector (often zeros),

  • Σ0\Sigma_0 is the prior covariance matrix.

The prior density 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)

Posterior

The posterior combines likelihood and prior:

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

Metropolis-Hastings Algorithm Steps in the Code

  1. Initialization (Frequentist/ML):

    • The chain starts at the MLE from a standard Poisson GLM fit.

  2. Proposal Step:

    • Propose β∗\beta^* from a multivariate normal centered at the current value:

      β∗∼N(β(t−1),δV)\beta^* \sim \mathcal{N}(\beta^{(t-1)}, \delta V)

      where VV is an estimate of the covariance matrix from the frequentist fit and δ\delta is a tuning parameter.

  3. Acceptance Ratio:

    • Compute the log of the Metropolis-Hastings acceptance ratio:

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

  4. Update Step:

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

    • Repeat for nIter iterations (with multiple chains if desired), discarding nBurnIn as burn-in.

Additional chains

The code below also implements multiple independent Markov Chains. This is standard practice in Bayesian estimation so parameter convergence can be validated across multiple chains besides inside a single chain.

Model Setup

  • Data: Counts yi∈{0,1,2,… }y_i \in \{0, 1, 2, \dots\} and predictors XiX_i for i=1,…,ni = 1, \dots, n.

  • Model: Poisson regression (log-linear model)

    yi∼Poisson(λi),log⁡λi=Xiβy_i \sim \text{Poisson}(\lambda_i), \quad \log \lambda_i = X_i \beta

    where XiX_i is the vector of predictors for observation ii, and β\beta is the vector of regression coefficients.


Likelihood

The likelihood for the observed data, given β\beta and XX, is based on the Poisson Probability Mass Function (PMF) since this is a discrete distribution:

p(y∣β,X)=∏i=1ne−λiλiyiyi!p(\mathbf{y} | \beta, X) = \prod_{i=1}^n \frac{e^{-\lambda_i} \lambda_i^{y_i}}{y_i!}

where λi=exp⁡(Xiβ)\lambda_i = \exp(X_i \beta).

The log-likelihood:

log⁡p(y∣β,X)=∑i=1n(yi(Xiβ)−exp⁡(Xiβ))+const\log p(\mathbf{y} | \beta, X) = \sum_{i=1}^n \left( y_i (X_i\beta) - \exp(X_i\beta) \right) + \text{const}

Prior

A multivariate normal (Gaussian) prior is placed on β\beta:

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

where:

  • μ0\mu_0 is the prior mean vector (often zeros),

  • Σ0\Sigma_0 is the prior covariance matrix.

The prior density 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)

Posterior

The posterior combines likelihood and prior:

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

Metropolis-Hastings Algorithm Steps in the Code

  1. Initialization (Frequentist/ML):

    • The chain starts at the MLE from a standard Poisson GLM fit.

  2. Proposal Step:

    • Propose β∗\beta^* from a multivariate normal centered at the current value:

      β∗∼N(β(t−1),δV)\beta^* \sim \mathcal{N}(\beta^{(t-1)}, \delta V)

      where VV is an estimate of the covariance matrix from the frequentist fit and δ\delta is a tuning parameter.

  3. Acceptance Ratio:

    • Compute the log of the Metropolis-Hastings acceptance ratio:

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

  4. Update Step:

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

    • Repeat for nIter iterations (with multiple chains if desired), discarding nBurnIn as burn-in.

Additional chains

The code below also implements multiple independent Markov Chains. This is standard practice in Bayesian estimation so parameter convergence can be validated across multiple chains besides inside a single chain.

<Figure size 1200x800 with 6 Axes>
  Parameter    Median        SD
0     alpha  1.887385  0.040348
1     beta1  0.049187  0.034894
2     beta2 -0.020226  0.139494