ML0133 GLMs & Count Regression

You’re modeling the number of purchases per user on an e-commerce app. Why is ordinary least squares (OLS) the wrong tool, how do Poisson and negative-binomial regression work, and what is a link function?

Two-panel line drawing. Left panel: histogram of purchases per user over 30 days, with a tall bar at zero holding almost half of all users, a short bar at one, and a long thin tail stretching out to 25 or more purchases. Right panel: four user timelines ending at today, each starting at a sign-up circle on a different day, with tick marks for purchases; user A has 3 purchases in 90 days, user B has 2 purchases in 7 days, user C has 0 purchases in 60 days, and user D has 10 purchases in 90 days

The Problem: Purchases per user pile up at zero and stretch into a long right tail, and users in the same table have been observed for very different lengths of time. The model has to produce a sensible expected count for every one of them.

Answer

Purchase counts are non-negative integers with a pile of zeros, a long right tail, and a spread that grows with the mean, and OLS ignores all three facts. It can predict negative purchases for light users, and its constant-variance assumption lets a few heavy buyers dominate the fit while corrupting every standard error. A generalized linear model (GLM) replaces the Gaussian with an exponential-family distribution, keeps a linear predictor \eta = x^\top\beta, and connects the mean to that predictor through a link function g(\mu) = \eta. Poisson regression uses the log link, so \mu = e^{\eta} stays positive and features act multiplicatively, but it assumes the variance equals the mean. Purchase data usually breaks that through overdispersion, so negative binomial regression adds a dispersion parameter \alpha with \mathrm{Var}(Y) = \mu + \alpha\mu^2. Both are fit by maximum likelihood and compared by deviance rather than R^2.

(1) Why OLS Fails: Gaussian errors put probability on negative and fractional purchases, and constant variance is false when variance scales with the mean. Regressing \log(y+1) does not rescue it, because the back-transformed prediction is biased downward and the “+1” is arbitrary.
(2) Three GLM Components: A random component (Poisson or negative binomial for counts), a systematic component \eta_i = x_i^\top\beta, and a link g(\mu_i) = \eta_i that maps the positive mean onto the whole real line.
(3) Log Link and Rate Ratios: The log is the canonical link for the Poisson, so e^{\beta_j} is a rate ratio. A coefficient of 0.405 means 1.5 times as many purchases, holding the other features fixed.
(4) Equidispersion Is the Weak Point: Poisson forces \mathrm{Var}(Y) = \mu. With a Pearson dispersion of 4, Poisson standard errors are too small by a factor of 2, even though the coefficients themselves stay consistent.
(5) Negative Binomial: Mixing a gamma-distributed per-user rate into a Poisson gives \mathrm{Var}(Y) = \mu + \alpha\mu^2. This absorbs unobserved heterogeneity between users and produces more zeros and a heavier tail at the same mean.
(6) Exposure Offset: Users observed for different windows enter through \log t_i with a coefficient fixed at 1, so the model learns a purchase rate per day instead of a raw total.
(7) Zeros and Deviance: Zero-inflated or hurdle models handle a separate “never buys” process. Deviance, AIC, and predicted versus observed zero shares decide among Poisson, NB, and their zero-modified versions.

Flow diagram. Top row: user features and exposure in days feed a systematic component eta equals x transpose beta plus log t, which feeds a link function log mu equals eta so mu equals e to the eta and is positive, which feeds a random component that is Poisson with variance mu or negative binomial with variance mu plus alpha mu squared; the last three boxes sit inside a dashed band labeled generalized linear model three components. Bottom row, right to left: fit by maximum likelihood with IRLS, diagnose the fit with Pearson dispersion, deviance and zero counts, a decision box asking whether dispersion is much greater than one or zeros exceed predictions, and a yes arrow to switch distribution to NB for overdispersion or ZINB or hurdle for zeros; a note under the decision reads no, keep the Poisson model

Figure 1: A count GLM as three components plus a diagnostic loop. Features and the exposure offset \log t_i form the linear predictor, the log link maps it to a positive mean, and the fitted Pearson dispersion and zero counts decide whether the Poisson must be swapped for a negative binomial or a zero-modified model.

Mathematical Formulation:
f(y;\theta,\phi) = \exp\left(\frac{y\theta - b(\theta)}{\phi} + c(y,\phi)\right)
\mu = b'(\theta)
\mathrm{Var}(Y) = \phi\, b''(\theta)
\log \mu_i = x_i^\top \beta + \log t_i
P(Y_i = y) = \frac{\mu_i^{y}\, e^{-\mu_i}}{y!}
\mathrm{Var}_{\mathrm{NB}}(Y_i) = \mu_i + \alpha \mu_i^2
D = 2\sum_{i=1}^{N} \left[ y_i \log\frac{y_i}{\hat\mu_i} - (y_i - \hat\mu_i) \right]

Where:

  • f(y;\theta,\phi) is the exponential-family density of the response y, with natural parameter \theta, dispersion \phi, cumulant function b(\theta), and normalizer c(y,\phi). For the Poisson, \theta = \log\mu, b(\theta) = e^{\theta}, and \phi = 1.
  • \mu = b'(\theta) is the mean and \phi\, b''(\theta) the variance function. Because b''(\theta) = e^{\theta} = \mu for the Poisson, its variance equals its mean, and its canonical link g = (b')^{-1} is the log.
  • \mu_i is the expected purchase count of user i, x_i its feature vector, \beta the coefficients, and t_i the exposure in days; \log t_i is an offset whose coefficient is fixed at 1 rather than estimated.
  • P(Y_i = y) is the Poisson probability of exactly y \in \{0, 1, 2, \ldots\} purchases, which puts zero mass on negative or fractional values.
  • \alpha \geq 0 is the negative binomial dispersion parameter; as \alpha \to 0 the NB collapses back to the Poisson.
  • D is the Poisson deviance, twice the log-likelihood gap to a saturated model, with \hat\mu_i the fitted means over N users and y_i \log(y_i/\hat\mu_i) taken as 0 when y_i = 0. For a Gaussian GLM it reduces to the residual sum of squares.

Worked Example:
Suppose the fitted model is \log\mu_i = -3.0 + 0.405\,x_{\mathrm{sub}} + \log t_i with t_i in days. The intercept gives a baseline rate of e^{-3.0} \approx 0.05 purchases per day, so a non-subscriber observed for 30 days has \mu = 1.5. Subscribing multiplies the rate by e^{0.405} = 1.5, giving \mu = 2.25 over 30 days and \mu = 0.75 for a subscriber observed only 10 days. For the non-subscriber, Poisson predicts P(Y=0) = e^{-1.5} \approx 0.22. An NB with \alpha = 2 and the same mean predicts P(Y=0) = (1 + \alpha\mu)^{-1/\alpha} = 4^{-1/2} = 0.50 and a variance of 1.5 + 2 \cdot 2.25 = 6.0. Same mean, very different shape: if half of your 30-day users never buy, the Poisson is rejected before any formal test.

PropertyPoisson GLMNegative Binomial GLMOLS
Support0, 1, 2, …0, 1, 2, …All real numbers, negatives included
Mean modellog mean = linear score (multiplicative effects)Same log link, same rate-ratio readingmean = linear score (additive effects)
VarianceEqual to the meanmean + alpha x mean squaredConstant for every user
Typical failure on purchasesOverdispersion makes p-values far too optimisticMisses a separate never-buyer process if one existsNegative predictions, heavy buyers dominate the fit
Fit and checkMLE via IRLS, deviance, Pearson dispersionMLE including alpha, LR test against Poisson, AICLeast squares, R squared

Login to view more content
Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *