Poisson Regression › Formula

Michael BrenndoerferJune 21, 202515 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 39Poisson Regression › Formula
Reading section 2 of 8 · Formula · 15 minSection 2 of 8 · 15 min

Book contents

Section 2 of 8 · 15 min read

Poisson Regression › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The foundation of Poisson regression lies in the Poisson distribution, which describes the probability of observing exactly kk events when events occur independently at a constant average rate λ\lambda.

The probability mass function (PMF) of the Poisson distribution, which forms the basis of Poisson regression, is given by:

P(Y=k∣λ)=e−λλkk!P(Y = k \mid \lambda) = \frac{e^{-\lambda} \lambda^k}{k!}

where:

  • YY is a discrete random variable representing the count of events (the outcome variable we observe)
  • kk is a non-negative integer (k=0,1,2,…k = 0, 1, 2, \ldots) representing the observed count (the specific value taken by YY)
  • λ\lambda is the rate parameter (the expected number of events in a given interval, where λ>0\lambda > 0; also equal to both the mean and variance of the distribution)
  • k!k! denotes the factorial of kk (i.e., k!=k×(k−1)×⋯×2×1k! = k \times (k-1) \times \cdots \times 2 \times 1, with 0!=10! = 1 by definition)
  • ee is Euler's number (approximately 2.71828; the base of the natural logarithm)

This formula expresses the probability of observing exactly kk events in a fixed interval of time or space, assuming events occur independently and at a constant average rate λ\lambda.

In the context of Poisson regression, λ\lambda is not a fixed value but is modeled as a function of predictor variables, allowing us to understand how different factors influence the expected event count. This distribution naturally handles count data because it only assigns positive probability to non-negative integers.

In Poisson regression, our goal is to model the relationship between a set of predictor variables and the expected rate at which events occur, denoted by the parameter λ\lambda. This parameter represents the mean number of occurrences for a given set of predictors. Because λ\lambda needs to be positive (since you cannot have a negative rate of occurrence), we use a logarithmic link function to relate the linear combination of predictors to λ\lambda. This is known as the log-link function, and it is a defining feature of Poisson regression.

The model is specified as follows:

log⁡(λi)=β0+β1xi1+β2xi2+⋯+βpxip\log(\lambda_i) = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} + \cdots + \beta_p x_{ip}

where:

  • λi\lambda_i is the expected count (rate parameter) for observation ii (where λi>0\lambda_i > 0; represents the mean of the Poisson distribution for observation ii)
  • β0\beta_0 is the intercept (the log of the expected count when all predictors equal zero)
  • β1,…,βp\beta_1, \ldots, \beta_p are the coefficients for each predictor (representing the change in log-count per unit change in the predictor, holding all other predictors constant)
  • xi1,…,xipx_{i1}, \ldots, x_{ip} are the values of the predictors for observation ii (the feature values for the ii-th observation)
  • pp is the number of predictor variables in the model (total count of features excluding the intercept)
  • ii indexes the observations from 11 to nn, where nn is the total number of observations

This logarithmic transformation guarantees that the predicted rate λi\lambda_i is positive, regardless of the values of the predictors or coefficients. By exponentiating both sides, we see that:

λi=exp⁡(β0+β1xi1+⋯+βpxip)\lambda_i = \exp(\beta_0 + \beta_1 x_{i1} + \cdots + \beta_p x_{ip})

where:

  • exp⁡(⋅)\exp(\cdot) is the exponential function (the inverse of the natural logarithm; maps any real number to a positive value)
  • λi\lambda_i is guaranteed to be positive for any values of β0,β1,…,βp\beta_0, \beta_1, \ldots, \beta_p and xi1,…,xipx_{i1}, \ldots, x_{ip}

The exponential function maps any real number to a positive value, which is exactly what we need for modeling count data. This also means that the effect of each predictor is multiplicative on the expected count: a one-unit increase in xijx_{ij} multiplies λi\lambda_i by eβje^{\beta_j}, holding other variables constant.

For computational efficiency and clarity, we often rewrite the model in matrix notation. Let X\mathbf{X} be the design matrix containing all predictor variables (with a column of ones for the intercept), and let β\boldsymbol{\beta} be the vector of coefficients. Then, for all nn observations:

log⁡(λ)=Xβ\log(\boldsymbol{\lambda}) = \mathbf{X}\boldsymbol{\beta}

where:

  • λ\boldsymbol{\lambda} is the n×1n \times 1 vector of rate parameters for all observations: λ=[λ1,λ2,…,λn]T\boldsymbol{\lambda} = [\lambda_1, \lambda_2, \ldots, \lambda_n]^T (the expected counts for each observation)
  • X\mathbf{X} is the n×(p+1)n \times (p+1) design matrix where each row represents one observation and each column represents a predictor (the first column contains all ones for the intercept term)
  • β\boldsymbol{\beta} is the (p+1)×1(p+1) \times 1 vector of coefficients: β=[β0,β1,…,βp]T\boldsymbol{\beta} = [\beta_0, \beta_1, \ldots, \beta_p]^T (includes the intercept and all predictor coefficients)
  • nn is the number of observations in the dataset (total number of data points)
  • log⁡(λ)\log(\boldsymbol{\lambda}) denotes element-wise application of the logarithm to each element of λ\boldsymbol{\lambda}

This matrix formulation allows us to efficiently compute predictions for all data points and to fit the model using numerical optimization algorithms.

Likelihood and Log-Likelihood

The Poisson regression model is typically estimated using maximum likelihood estimation (MLE). The likelihood function for the entire dataset is the product of the individual Poisson probabilities for each observation. For nn independent observations, the likelihood is:

L(β)=∏i=1ne−λiλiyiyi!L(\boldsymbol{\beta}) = \prod_{i=1}^{n} \frac{e^{-\lambda_i} \lambda_i^{y_i}}{y_i!}

where:

  • L(β)L(\boldsymbol{\beta}) is the likelihood function (the probability of observing the entire dataset given the parameter vector β\boldsymbol{\beta})
  • yiy_i is the observed count for observation ii (a non-negative integer; the actual data value we observed)
  • λi\lambda_i is the expected count for observation ii, as determined by the predictors and coefficients: λi=exp⁡(xiTβ)\lambda_i = \exp(\mathbf{x}_i^T \boldsymbol{\beta})
  • xi\mathbf{x}_i is the (p+1)×1(p+1) \times 1 feature vector for observation ii (includes 1 for the intercept and the pp predictor values)
  • β\boldsymbol{\beta} is the (p+1)×1(p+1) \times 1 vector of coefficients we are estimating: β=[β0,β1,…,βp]T\boldsymbol{\beta} = [\beta_0, \beta_1, \ldots, \beta_p]^T
  • ∏i=1n\prod_{i=1}^{n} denotes the product over all nn observations (assuming observations are independent)

Because products of many small probabilities can be numerically unstable, we usually work with the log-likelihood, which is the sum of the logarithms of the individual probabilities:

ℓ(β)=∑i=1n[−λi+yilog⁡(λi)−log⁡(yi!)]\ell(\boldsymbol{\beta}) = \sum_{i=1}^{n} \left[ -\lambda_i + y_i \log(\lambda_i) - \log(y_i!) \right]

where:

  • ℓ(β)\ell(\boldsymbol{\beta}) is the log-likelihood function (the natural logarithm of the likelihood function)
  • ∑i=1n\sum_{i=1}^{n} denotes the sum over all nn observations (converting the product in the likelihood to a sum)
  • log⁡(⋅)\log(\cdot) is the natural logarithm (base ee; the inverse of the exponential function)
  • −λi-\lambda_i comes from log⁡(e−λi)=−λi\log(e^{-\lambda_i}) = -\lambda_i
  • yilog⁡(λi)y_i \log(\lambda_i) comes from log⁡(λiyi)=yilog⁡(λi)\log(\lambda_i^{y_i}) = y_i \log(\lambda_i)
  • log⁡(yi!)\log(y_i!) comes from the factorial term in the denominator

The term log⁡(yi!)\log(y_i!) does not depend on the model parameters β\boldsymbol{\beta}, so it can be omitted during the optimization process (it acts as a constant additive term that doesn't affect which β\boldsymbol{\beta} maximizes the function). This simplifies the log-likelihood to:

ℓ(β)=∑i=1n[−λi+yilog⁡(λi)]\ell(\boldsymbol{\beta}) = \sum_{i=1}^{n} \left[ -\lambda_i + y_i \log(\lambda_i) \right]

Maximum Likelihood Estimation

To find the values of the coefficients β\boldsymbol{\beta} that best fit the data, we maximize the log-likelihood function. This involves taking the partial derivatives of the log-likelihood with respect to each coefficient and setting them equal to zero.

Let's expand the steps for computing the derivative of the log-likelihood with respect to βj\beta_j.

First, recall the log-likelihood:

ℓ(β)=∑i=1n[−λi+yilog⁡(λi)]\ell(\boldsymbol{\beta}) = \sum_{i=1}^n \left[ -\lambda_i + y_i \log(\lambda_i) \right]

where:

  • λi=exp⁡(xiTβ)\lambda_i = \exp(\mathbf{x}_i^T \boldsymbol{\beta}) is the expected count for observation ii (a function of the coefficients we're optimizing)
  • xiTβ=∑j=0pxijβj\mathbf{x}_i^T \boldsymbol{\beta} = \sum_{j=0}^p x_{ij} \beta_j is the linear predictor for observation ii (the sum of all coefficients times their corresponding predictor values; the argument to the exponential function)
  • xi=[1,xi1,xi2,…,xip]T\mathbf{x}_i = [1, x_{i1}, x_{i2}, \ldots, x_{ip}]^T is the (p+1)×1(p+1) \times 1 feature vector for observation ii (the first element is 1 for the intercept term)
  • jj indexes the coefficients from 00 to pp, where j=0j=0 corresponds to the intercept β0\beta_0

Take the partial derivative with respect to βj\beta_j:

∂ℓ∂βj=∑i=1n[−∂λi∂βj+yi1λi∂λi∂βj]\begin{aligned} \frac{\partial \ell}{\partial \beta_j} &= \sum_{i=1}^n \left[ -\frac{\partial \lambda_i}{\partial \beta_j} + y_i \frac{1}{\lambda_i} \frac{\partial \lambda_i}{\partial \beta_j} \right] \end{aligned}

where:

  • ∂ℓ∂βj\frac{\partial \ell}{\partial \beta_j} is the partial derivative of the log-likelihood with respect to coefficient βj\beta_j (the gradient component for the jj-th parameter)
  • The first term −∂λi∂βj-\frac{\partial \lambda_i}{\partial \beta_j} comes from differentiating −λi-\lambda_i
  • The second term yi1λi∂λi∂βjy_i \frac{1}{\lambda_i} \frac{\partial \lambda_i}{\partial \beta_j} comes from differentiating yilog⁡(λi)y_i \log(\lambda_i) using the chain rule

Now, compute ∂λi∂βj\frac{\partial \lambda_i}{\partial \beta_j} using the chain rule:

λi=exp⁡(xiTβ)∂λi∂βj=exp⁡(xiTβ)⋅∂(xiTβ)∂βj=exp⁡(xiTβ)⋅xij=λixij\begin{aligned} \lambda_i &= \exp(\mathbf{x}_i^T \boldsymbol{\beta}) \\ \frac{\partial \lambda_i}{\partial \beta_j} &= \exp(\mathbf{x}_i^T \boldsymbol{\beta}) \cdot \frac{\partial (\mathbf{x}_i^T \boldsymbol{\beta})}{\partial \beta_j} \\ &= \exp(\mathbf{x}_i^T \boldsymbol{\beta}) \cdot x_{ij} \\ &= \lambda_i x_{ij} \end{aligned}

where:

  • ∂λi∂βj\frac{\partial \lambda_i}{\partial \beta_j} is the partial derivative of the expected count with respect to coefficient βj\beta_j (how λi\lambda_i changes when βj\beta_j changes)
  • The first step uses the chain rule: ddxef(x)=ef(x)⋅f′(x)\frac{d}{dx}e^{f(x)} = e^{f(x)} \cdot f'(x)
  • ∂(xiTβ)∂βj=xij\frac{\partial (\mathbf{x}_i^T \boldsymbol{\beta})}{\partial \beta_j} = x_{ij} because xiTβ=∑k=0pxikβk\mathbf{x}_i^T \boldsymbol{\beta} = \sum_{k=0}^p x_{ik} \beta_k, and only the jj-th term depends on βj\beta_j
  • xijx_{ij} is the jj-th element of the feature vector xi\mathbf{x}_i (the value of predictor jj for observation ii)
  • For j=0j=0, xi0=1x_{i0} = 1 (the intercept term)
  • For j>0j > 0, xijx_{ij} is the value of the jj-th predictor variable for observation ii

Substitute this result back into the derivative:

∂ℓ∂βj=∑i=1n[−λixij+yi1λiλixij]=∑i=1n[−λixij+yixij]=∑i=1nxij(yi−λi)\begin{aligned} \frac{\partial \ell}{\partial \beta_j} &= \sum_{i=1}^n \left[ -\lambda_i x_{ij} + y_i \frac{1}{\lambda_i} \lambda_i x_{ij} \right] \\ &= \sum_{i=1}^n \left[ -\lambda_i x_{ij} + y_i x_{ij} \right] \\ &= \sum_{i=1}^n x_{ij} (y_i - \lambda_i) \end{aligned}

where:

  • In the first line, we substitute ∂λi∂βj=λixij\frac{\partial \lambda_i}{\partial \beta_j} = \lambda_i x_{ij} into both terms
  • In the second line, we simplify yi1λiλixij=yixijy_i \frac{1}{\lambda_i} \lambda_i x_{ij} = y_i x_{ij} (the λi\lambda_i terms cancel)
  • In the third line, we factor out xijx_{ij} from both terms to get the final form

To find the maximum likelihood estimates, set the derivative equal to zero and solve for βj\beta_j:

∑i=1nxij(yi−λi)=0for all j=0,1,…,p\sum_{i=1}^n x_{ij} (y_i - \lambda_i) = 0 \quad \text{for all } j = 0, 1, \ldots, p

where:

  • This equation must hold for each coefficient βj\beta_j (including the intercept β0\beta_0 when j=0j=0)
  • yi−λiy_i - \lambda_i represents the residual (observed count minus expected count) for observation ii (the prediction error)
  • The equation states that the weighted sum of residuals must equal zero, where weights are the predictor values xijx_{ij}
  • This is the first-order condition for a maximum (the gradient equals zero)

This yields a system of (p+1)(p+1) equations (one for each coefficient, including the intercept), which are solved numerically to obtain the maximum likelihood estimates β^0,β^1,…,β^p\hat{\beta}_0, \hat{\beta}_1, \ldots, \hat{\beta}_p.

These equations are nonlinear in β\boldsymbol{\beta} and generally do not have a closed-form solution. Therefore, we solve them numerically using iterative optimization algorithms. The most common approaches are:

  1. Newton-Raphson method: Uses both first and second derivatives (the Hessian matrix) to find the optimal coefficients
  2. Iteratively reweighted least squares (IRLS): Recasts each iteration as a weighted least squares problem

The numerical solution process involves:

  • Starting with initial guesses for the coefficients (e.g., β(0)=0\boldsymbol{\beta}^{(0)} = \mathbf{0}, where 0\mathbf{0} is a vector of zeros)
  • Computing predicted values λi\lambda_i using the current coefficient estimates: λi(t)=exp⁡(xiTβ(t))\lambda_i^{(t)} = \exp(\mathbf{x}_i^T \boldsymbol{\beta}^{(t)}), where tt denotes the iteration number
  • Updating coefficients using gradient information (the partial derivatives): β(t+1)=β(t)+Δβ\boldsymbol{\beta}^{(t+1)} = \boldsymbol{\beta}^{(t)} + \Delta\boldsymbol{\beta}, where Δβ\Delta\boldsymbol{\beta} is the update step computed from the gradient
  • Repeating until convergence (when ∥β(t+1)−β(t)∥<ϵ\|\boldsymbol{\beta}^{(t+1)} - \boldsymbol{\beta}^{(t)}\| < \epsilon for some small tolerance ϵ\epsilon, typically ϵ=10−4\epsilon = 10^{-4} or 10−610^{-6})

When the algorithm converges, we obtain the maximum likelihood estimates β^0\hat{\beta}_0, β^1\hat{\beta}_1, …\ldots, β^p\hat{\beta}_p. These are the values that make the observed data most probable under the Poisson regression model.

The key insight is that the system of equations ∑i=1nxij(yi−λi)=0\sum_{i=1}^{n} x_{ij} (y_i - \lambda_i) = 0 represents the condition that the residuals (yi−λi)(y_i - \lambda_i) are orthogonal to each predictor variable, weighted by the predictor values. This ensures that the model captures the relationship between predictors and outcomes in a way that maximizes the likelihood of observing the actual data.

Note: The use of the log-link function and the exponential mean structure distinguishes Poisson regression from ordinary linear regression. This approach ensures that predictions are non-negative and that the model is well-suited for count data, where the variance typically increases with the mean.

Visualizing the Optimization Process

To better understand how maximum likelihood estimation works in practice, let's visualize the iterative optimization process. The following plots show how the algorithm searches for the optimal coefficients by iteratively improving the log-likelihood.

Out[3]:
Visualization
Contour plot showing log-likelihood surface with optimization path.
Log-likelihood surface and optimization path for two coefficients (β₀ and β₁). The contour lines represent different log-likelihood values, with warmer colors indicating higher likelihood. The highlighted path shows how the optimization algorithm iteratively moves from an initial guess toward the maximum likelihood estimates (marked by the star). Each step improves the log-likelihood, and the algorithm converges when the gradient becomes negligibly small. This visualization demonstrates why we need iterative methods.the surface is complex and has no closed-form solution.
Line plot showing log-likelihood convergence across iterations.
Convergence of the log-likelihood value across iterations. Starting from an initial guess, the algorithm rapidly increases the log-likelihood in early iterations, then gradually converges to the optimal value. The curve flattens as we approach the maximum, indicating convergence. The dashed red line marks the final maximum log-likelihood. This pattern is typical of Newton-Raphson and IRLS algorithms, which use gradient information to efficiently find the optimal coefficients.

The optimization process demonstrates several key insights about maximum likelihood estimation in Poisson regression. First, the log-likelihood surface is smooth and unimodal (has a single peak), which ensures that iterative algorithms reliably converge to the global maximum regardless of the starting point. This is a desirable property that makes Poisson regression computationally stable.

Second, the algorithm makes rapid progress in early iterations when far from the optimum, then slows down as it approaches the maximum. This is characteristic of gradient-based methods like Newton-Raphson and IRLS, which use information about both the direction and curvature of the log-likelihood surface to move efficiently toward the solution.

Finally, convergence is achieved when the gradient (rate of change) of the log-likelihood becomes negligibly small, indicating that we've reached a point where no small adjustment to the coefficients will improve the fit. In practice, algorithms stop when the change in coefficients or log-likelihood between iterations falls below a specified tolerance threshold.

Mathematical properties

The Poisson regression model has several important mathematical properties that make it well-suited for count data.

Equidispersion property: For a Poisson distribution, the mean and variance are both equal to λ\lambda:

E[Y]=Var(Y)=λE[Y] = \text{Var}(Y) = \lambda

where:

  • E[Y]E[Y] is the expected value (mean) of the count variable YY
  • Var(Y)\text{Var}(Y) is the variance of the count variable YY
  • λ\lambda is the rate parameter (constrained to be positive)

This means that as the expected count increases, so does the variability.a realistic assumption for many count processes.

Scale invariance: The model is scale-invariant under the log-link: multiplying a predictor by a constant cc simply shifts the corresponding coefficient by log⁡(c)\log(c). If xj∗=c⋅xjx_j^* = c \cdot x_j, then βj∗=βj/c\beta_j^* = \beta_j / c such that βj∗xj∗=βjxj\beta_j^* x_j^* = \beta_j x_j.

Coefficient interpretation: The coefficients have a natural interpretation as log-rate ratios. When we exponentiate a coefficient βj\beta_j, we get the rate ratio:

Rate Ratio=eβj\text{Rate Ratio} = e^{\beta_j}

This represents the multiplicative effect on the expected count for a one-unit increase in xjx_j, holding all other variables constant. This multiplicative structure is often more appropriate for count data than the additive structure of linear regression, as it naturally handles the non-negative constraint and allows for proportional changes in rates.