Poisson Regression › Example
Machine Learning from ScratchAll chapters Poisson Regression › ExampleOne section · Sections in this chapter4 / 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
Let's work through a concrete example to understand how Poisson regression operates step by step. Suppose we're modeling the number of customer complaints received per day at a retail store, and we have two predictors: the number of customers served (in hundreds) and whether it's a weekend (1 for weekend, 0 for weekday).
Our dataset contains the following observations:
| Day | Customers (X₁) | Weekend (X₂) | Complaints (Y) |
|---|---|---|---|
| 1 | 2.5 | 0 | 3 |
| 2 | 3.0 | 0 | 4 |
| 3 | 1.8 | 1 | 2 |
| 4 | 4.2 | 0 | 6 |
| 5 | 2.1 | 1 | 1 |
The Poisson regression model assumes that the logarithm of the expected number of complaints is linearly related to our predictors:
where:
- is the expected number of complaints for day (the mean of the Poisson distribution for observation ; constrained to be positive)
- is the intercept (the log of the expected count when all predictors equal zero; baseline log-count)
- is the coefficient for customer count (the change in log-count for each additional hundred customers, holding weekend status constant)
- is the coefficient for weekend indicator (the change in log-count when comparing weekends to weekdays, holding customer count constant)
- is the number of customers (in hundreds) for day (a continuous predictor variable)
- is 1 if day is a weekend, 0 otherwise (a binary indicator variable)
This means the expected count is:
where:
- is the exponential function (ensures for any values of the coefficients and predictors)
- This is the inverse transformation of the log-link, converting from the log scale back to the count scale
Let's say our fitted model produces the following coefficients: , , and . Now we can calculate the expected number of complaints for each day.
-
For Day 1:
where: , (weekday), so the linear predictor is
-
For Day 2:
where: , (weekday), so the linear predictor is
-
For Day 3:
where: , (weekend), so the linear predictor is
-
For Day 4:
where: , (weekday), so the linear predictor is
-
For Day 5:
where: , (weekend), so the linear predictor is
Now let's interpret these coefficients. The coefficient means that for every additional hundred customers, the expected number of complaints increases by a factor of . In other words, each additional hundred customers more than doubles the expected complaints.
Calculation:
This multiplicative effect means that if we have 200 customers instead of 100, we expect as many complaints, holding weekend status constant.
The coefficient means that weekends have times as many complaints as weekdays, holding customer count constant. This suggests that weekends have about 39% fewer complaints than weekdays, possibly because weekend customers are more relaxed or have different expectations.
Calculation:
The intercept represents the log of the expected complaints when both predictors are zero (no customers on a weekday), which gives us expected complaints.a baseline level that makes sense for a retail environment.
Calculation:
The intercept in Poisson regression represents the log of the expected count when all predictors equal zero. In this example, with zero customers on a weekday, we expect about 1.22 complaints. However, be cautious when interpreting intercepts, especially when zero values for predictors may not be meaningful in your specific context (e.g., zero customers might not be a realistic scenario).
Determining coefficients
In the example above, we simply stated the coefficients as , , and for illustration. But how do we determine these values from data?
As previously discussed, to estimate the coefficients in Poisson regression, we use maximum likelihood estimation (MLE). The idea is to find the values of , , and that make the observed data most probable under the Poisson model. This involves the following steps:
-
Specify the likelihood function: For each observation , the probability of observing complaints given predictors is:
where:
- is the random variable representing the complaint count for observation (a Poisson-distributed random variable)
- is the observed complaint count for observation (the actual data value we observed; a non-negative integer)
- represents the predictor values for observation (in our example, customer count and weekend indicator)
- is the expected count given the predictors (the mean parameter of the Poisson distribution for observation )
- is Euler's number (approximately 2.71828)
-
Write the log-likelihood: The log-likelihood for all observations is:
where:
- is the log-likelihood function (the natural logarithm of the likelihood; easier to maximize than the likelihood itself)
- is the vector of coefficients we want to estimate (the parameters of our model)
- is the total number of observations (5 days in our example)
- denotes summing over all observations (converting the product of probabilities to a sum of log-probabilities)
- This function measures how well a given set of coefficients explains the observed data (higher values indicate better fit)
-
Maximize the log-likelihood: We seek the values of , , and that maximize . Mathematically, we solve:
where:
- denotes the maximum likelihood estimates (the optimal coefficient values)
- means "the argument that maximizes" (the value of that gives the highest log-likelihood)
There is no closed-form solution, so we use numerical optimization algorithms (such as Newton-Raphson or iteratively reweighted least squares) to find the best-fitting coefficients.
-
Interpret the fitted coefficients: Once the optimization converges, the resulting coefficients are the maximum likelihood estimates , , and . These are the values that would be reported by statistical software or machine learning libraries when fitting a Poisson regression model to the data.
Note: In practice, you do not need to perform these calculations by hand. Statistical software (such as
statsmodelsorscikit-learn, or specialized packages) will fit the model and return the estimated coefficients, along with measures of uncertainty and model fit.
In summary, the coefficients in Poisson regression are determined by finding the values that maximize the likelihood of observing the data, given the model. This process ensures that the fitted model best captures the relationship between the predictors and the count outcome according to the Poisson distribution.