Poisson Regression › Formula
Machine Learning from ScratchAll chapters 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 events when events occur independently at a constant average rate .
The probability mass function (PMF) of the Poisson distribution, which forms the basis of Poisson regression, is given by:
where:
- is a discrete random variable representing the count of events (the outcome variable we observe)
- is a non-negative integer () representing the observed count (the specific value taken by )
- is the rate parameter (the expected number of events in a given interval, where ; also equal to both the mean and variance of the distribution)
- denotes the factorial of (i.e., , with by definition)
- is Euler's number (approximately 2.71828; the base of the natural logarithm)
This formula expresses the probability of observing exactly events in a fixed interval of time or space, assuming events occur independently and at a constant average rate .
In the context of Poisson regression, 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 . This parameter represents the mean number of occurrences for a given set of predictors. Because 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 . This is known as the log-link function, and it is a defining feature of Poisson regression.
The model is specified as follows:
where:
- is the expected count (rate parameter) for observation (where ; represents the mean of the Poisson distribution for observation )
- is the intercept (the log of the expected count when all predictors equal zero)
- are the coefficients for each predictor (representing the change in log-count per unit change in the predictor, holding all other predictors constant)
- are the values of the predictors for observation (the feature values for the -th observation)
- is the number of predictor variables in the model (total count of features excluding the intercept)
- indexes the observations from to , where is the total number of observations
This logarithmic transformation guarantees that the predicted rate is positive, regardless of the values of the predictors or coefficients. By exponentiating both sides, we see that:
where:
- is the exponential function (the inverse of the natural logarithm; maps any real number to a positive value)
- is guaranteed to be positive for any values of and
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 multiplies by , holding other variables constant.
For computational efficiency and clarity, we often rewrite the model in matrix notation. Let be the design matrix containing all predictor variables (with a column of ones for the intercept), and let be the vector of coefficients. Then, for all observations:
where:
- is the vector of rate parameters for all observations: (the expected counts for each observation)
- is the design matrix where each row represents one observation and each column represents a predictor (the first column contains all ones for the intercept term)
- is the vector of coefficients: (includes the intercept and all predictor coefficients)
- is the number of observations in the dataset (total number of data points)
- denotes element-wise application of the logarithm to each element of
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 independent observations, the likelihood is:
where:
- is the likelihood function (the probability of observing the entire dataset given the parameter vector )
- is the observed count for observation (a non-negative integer; the actual data value we observed)
- is the expected count for observation , as determined by the predictors and coefficients:
- is the feature vector for observation (includes 1 for the intercept and the predictor values)
- is the vector of coefficients we are estimating:
- denotes the product over all 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:
where:
- is the log-likelihood function (the natural logarithm of the likelihood function)
- denotes the sum over all observations (converting the product in the likelihood to a sum)
- is the natural logarithm (base ; the inverse of the exponential function)
- comes from
- comes from
- comes from the factorial term in the denominator
The term does not depend on the model parameters , so it can be omitted during the optimization process (it acts as a constant additive term that doesn't affect which maximizes the function). This simplifies the log-likelihood to:
Maximum Likelihood Estimation
To find the values of the coefficients 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 .
First, recall the log-likelihood:
where:
- is the expected count for observation (a function of the coefficients we're optimizing)
- is the linear predictor for observation (the sum of all coefficients times their corresponding predictor values; the argument to the exponential function)
- is the feature vector for observation (the first element is 1 for the intercept term)
- indexes the coefficients from to , where corresponds to the intercept
Take the partial derivative with respect to :
where:
- is the partial derivative of the log-likelihood with respect to coefficient (the gradient component for the -th parameter)
- The first term comes from differentiating
- The second term comes from differentiating using the chain rule
Now, compute using the chain rule:
where:
- is the partial derivative of the expected count with respect to coefficient (how changes when changes)
- The first step uses the chain rule:
- because , and only the -th term depends on
- is the -th element of the feature vector (the value of predictor for observation )
- For , (the intercept term)
- For , is the value of the -th predictor variable for observation
Substitute this result back into the derivative:
where:
- In the first line, we substitute into both terms
- In the second line, we simplify (the terms cancel)
- In the third line, we factor out from both terms to get the final form
To find the maximum likelihood estimates, set the derivative equal to zero and solve for :
where:
- This equation must hold for each coefficient (including the intercept when )
- represents the residual (observed count minus expected count) for observation (the prediction error)
- The equation states that the weighted sum of residuals must equal zero, where weights are the predictor values
- This is the first-order condition for a maximum (the gradient equals zero)
This yields a system of equations (one for each coefficient, including the intercept), which are solved numerically to obtain the maximum likelihood estimates .
These equations are nonlinear in and generally do not have a closed-form solution. Therefore, we solve them numerically using iterative optimization algorithms. The most common approaches are:
- Newton-Raphson method: Uses both first and second derivatives (the Hessian matrix) to find the optimal coefficients
- 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., , where is a vector of zeros)
- Computing predicted values using the current coefficient estimates: , where denotes the iteration number
- Updating coefficients using gradient information (the partial derivatives): , where is the update step computed from the gradient
- Repeating until convergence (when for some small tolerance , typically or )
When the algorithm converges, we obtain the maximum likelihood estimates , , , . 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 represents the condition that the residuals 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.


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 :
where:
- is the expected value (mean) of the count variable
- is the variance of the count variable
- 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 simply shifts the corresponding coefficient by . If , then such that .
Coefficient interpretation: The coefficients have a natural interpretation as log-rate ratios. When we exponentiate a coefficient , we get the rate ratio:
This represents the multiplicative effect on the expected count for a one-unit increase in , 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.