This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · lib/stats/glm

Generalized linear models by iteratively reweighted least squares

glm_irls(y, X, w, offset, family, link, ...) is R's glm.fit step for step: the same starting means, the same working response and weights, each weighted least-squares step by lib/stats/linmod.viv's pivoted QR (R's tolerance min(10−7, epsilon / 1000)), the same clamps in the link functions (logit at |eta| = 30, probit at Phi^-1(eps), the log link's mean at least eps), and R's stopping rule |dev - dev_old| / (|dev| + 0.1) < epsilon.

The families and their links: binomial (logit, probit), Poisson (log), Gamma (inverse, log), Gaussian (identity). For binomial, y is the proportion of successes and w the number of trials.

The covariance it returns is R's: (X^T W X)^-1 from the last step's factors, W the working weights (Fisher's expected information). glm_newton is the slot's other method: Newton steps with the observed information, from one IRLS step. glm_observed_cov gives the inverse of the observed information instead: the negative Hessian of the log-likelihood, X^T diag(-d s / d eta) X, with the derivative of each observation's score s(eta) taken by deriv (forward-mode, exact). For the canonical links (logit, log for Poisson, inverse for Gamma, identity) the two are the same matrix.

Used with from stats.glm import ....

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 28–304