KatInterview

Build a GLM from Scratch: Family, Link, and Optimizer

Machine LearningModel answer
HardModel answerMachine Learninggeneralized-linear-modelsmaximum-likelihoodnumerical-stability

Last updated

Design and implement a generalized linear model end-to-end, covering family and link selection, loss derivation, numerical stability, and prediction semantics.

Generalized Linear Models from First Principles

A generalized linear model extends ordinary linear regression by letting the response variable follow any distribution from the exponential family, and by connecting the linear predictor to the conditional mean through a link function. The result is a flexible framework that unifies logistic regression, Poisson regression, Gamma regression, and many other common models under one set of equations.

Your task

Explain how you would build a GLM trainer and predictor entirely from scratch. Your answer should cover:

  1. Model definition — state the distribution family and link function you will use as your working example, and write out the conditional mean as a function of the linear predictor.
  2. Objective function — derive the loss (typically negative log-likelihood) and explain why it is the right choice for your chosen family.
  3. Optimization — describe the algorithm you would use to minimize that loss, how you compute gradients or the update rule, and what numerical precautions are necessary.
  4. Prediction — clarify what the model outputs at inference time (linear predictor, conditional mean, or a thresholded decision) and under what circumstances each variant is appropriate.
  5. Implementation details — initialization strategy, stopping criterion, regularization hooks, and how to handle invalid inputs or boundary conditions.

Constraints and open decisions

The problem is intentionally under-specified. Before committing to a single implementation, call out which choices are open and how you would settle them in practice:

  • The response distribution and link are not fixed. Pick one explicitly and justify it.
  • Whether an intercept is included and whether regularization is applied are unspecified.
  • Numerical tolerances and convergence criteria are left to you.
  • Prediction semantics (raw score vs. probability vs. class label) depend on the use case.

Use your chosen family and link as a concrete illustration throughout; do not let the answer drift into discussing every possible GLM variant without grounding it in a specific example.

What a complete answer looks like

A strong response walks through the full pipeline — from the statistical model to runnable pseudocode or actual code — while clearly marking which decisions were made, why they were made, and what would change under different requirements.

Hints

Hint 1

Start by pinning down one specific family and link before writing a single line of math or code. Everything downstream — loss, gradient, inverse link — follows from that choice, so ambiguity here propagates into every subsequent step.

Hint 2

Numerical stability is not an afterthought. For the Bernoulli/logit example, compute log-likelihood using log-sum-exp or the numerically stable form log(1 + exp(−yη)) rather than evaluating σ(η) and then taking its log; for Poisson, clip the linear predictor before exponentiating to avoid overflow.

Hint 3

Prediction is not a single thing. The linear predictor η = Xβ, the conditional mean μ = g⁻¹(η), and a thresholded label are three distinct outputs. State which one your model returns by default and how a caller requests the others.