1. Extending Linear Models: The GLM Framework
Standard Ordinary Least Squares (OLS) regression requires that the target variable $y$ is continuous and unrestricted across $(-\infty, \infty)$.
However, in classification, count modeling, and survival analysis:
- Click-through rate / Churn: Binary $y \in {0, 1}$.
- API request counts: Discrete non-negative integers $y \in {0, 1, 2, \dots}$.
Nelder and Wedderburn (1972) unified these under Generalized Linear Models (GLMs).
text
2. Why Linear Regression Fails for Binary Outcomes
Attempting to model probabilities $p_i = P(y_i = 1 \mid \mathbf{x}_i)$ using linear regression ($p_i = \mathbf{x}_i^T \mathbf{\beta}$) causes fatal mathematical violations:
text
- Out-of-Bounds Predictions: Linear regression will predict $p < 0$ or $p > 1$ for extreme feature values.
- Heteroscedasticity: The variance of a Bernoulli variable is $\text{Var}(y_i) = p_i(1 - p_i)$, which varies with $\mathbf{x}_i$, violating OLS spherical error assumptions.
3. The Logistic (Logit) Model
Logistic regression maps the unbounded linear predictor $\eta \in (-\infty, \infty)$ into a valid probability $p \in (0, 1)$ via the Logistic Sigmoid Function:
$$p = \sigma(\eta) = \frac{1}{1 + e^{-\eta}} = \frac{1}{1 + e^{-\mathbf{x}^T \mathbf{\beta}}}$$
3.1 The Logit Link Function
Inverting the sigmoid yields the Logit function:
$$\text{logit}(p) = \ln\left( \frac{p}{1 - p} \right) = \mathbf{x}^T \mathbf{\beta}$$
The ratio $\frac{p}{1 - p}$ is the Odds of the event occurring. Taking the natural logarithm gives the Log-Odds.
3.2 Interpreting Coefficients via Odds Ratios
Unlike linear regression where $\beta_j$ represents additive change in $y$, in logistic regression $e^{\beta_j}$ represents the multiplicative change in odds for a 1-unit increase in $x_j$:
$$\text{Odds Ratio (OR)} = \frac{\text{Odds}(x_j + 1)}{\text{Odds}(x_j)} = e^{\beta_j}$$
- If $\beta_j = 0 \implies e^0 = 1.0$: Feature $x_j$ has zero impact on odds.
- If $\beta_j = 0.693 \implies e^{0.693} \approx 2.0$: Each 1-unit increase in $x_j$ doubles the odds of conversion ($100%$ increase).
- If $\beta_j = -0.51 \implies e^{-0.51} \approx 0.60$: Each 1-unit increase reduces odds by $40%$.
4. Maximum Likelihood Estimation & Log Loss
Because outcomes are binary Bernoulli variables ($y_i \in {0, 1}$), the likelihood function is:
$$L(\mathbf{\beta}) = \prod_{i=1}^n p_i^{y_i} (1 - p_i)^{1 - y_i}, \quad \text{where } p_i = \sigma(\mathbf{x}_i^T \mathbf{\beta})$$
The Log-Likelihood is:
$$\ell(\mathbf{\beta}) = \sum_{i=1}^n \left[ y_i \ln p_i + (1 - y_i) \ln(1 - p_i) \right]$$
Minimizing the negative log-likelihood defines the Binary Cross-Entropy Loss in machine learning:
$$\text{Loss}(\mathbf{\beta}) = -\frac{1}{n} \sum_{i=1}^n \left[ y_i \ln p_i + (1 - y_i) \ln(1 - p_i) \right]$$
5. Iteratively Reweighted Least Squares (IRLS)
Unlike OLS, $\nabla_\mathbf{\beta} \ell(\mathbf{\beta}) = \mathbf{0}$ has no closed-form solution. We use the Newton-Raphson method, known in statistics as Iteratively Reweighted Least Squares (IRLS).
5.1 Gradient (Score Vector)
$$\mathbf{g} = \nabla_\mathbf{\beta} \ell = \mathbf{X}^T (\mathbf{y} - \mathbf{p}) \in \mathbb{R}^{p+1}$$
5.2 Hessian Matrix
$$\mathbf{H} = \nabla^2_\mathbf{\beta} \ell = -\mathbf{X}^T \mathbf{W} \mathbf{X} \in \mathbb{R}^{(p+1) \times (p+1)}$$
where $\mathbf{W} = \text{diag}(p_1(1 - p_1), p_2(1 - p_2), \dots, p_n(1 - p_n))$ is a diagonal weight matrix.
Because $\mathbf{W}$ is strictly positive on its diagonal ($0 < p_i(1-p_i) \le 0.25$), the Hessian $\mathbf{H}$ is strictly negative definite, guaranteeing that the log-likelihood surface is strictly concave with a unique global maximum.
5.3 The Newton-Raphson Update Rule
$$\mathbf{\beta}^{(t+1)} = \mathbf{\beta}^{(t)} - \mathbf{H}^{-1} \mathbf{g} = \mathbf{\beta}^{(t)} + (\mathbf{X}^T \mathbf{W} \mathbf{X})^{-1} \mathbf{X}^T (\mathbf{y} - \mathbf{p})$$
IRLS converges quadratically to the exact global maximum within $5\text{--}8$ iterations!
6. Model Evaluation: ROC & AUC
In binary classification, evaluating models solely by raw accuracy is dangerously misleading when classes are imbalanced (e.g., $99%$ benign, $1%$ fraud).
text
The ROC (Receiver Operating Characteristic) Curve
Plots $\text{TPR}$ (Recall) on the y-axis against $\text{FPR}$ on the x-axis across every possible classification probability threshold $\tau \in [0, 1]$.
text
Area Under the Curve (AUC / AUROC)
- $\text{AUC} = 0.50$: No better than random coin flip.
- $\text{AUC} = 0.80\text{--}0.90$: Strong predictive discrimination.
- $\text{AUC} \ge 0.95$: Exceptional performance.
- Probabilistic Meaning: AUC equals the probability that the classifier will rank a randomly chosen positive instance higher than a randomly chosen negative instance.
7. Python Implementation: Logistic Regression via Newton-Raphson (IRLS)
python
8. Key Takeaways
- GLM Unification: The link function $g(\mu) = \mathbf{x}^T \mathbf{\beta}$ connects unbounded linear combinations to constrained response domains.
- Logit Link Interprets Odds: $e^{\beta_j}$ represents the multiplicative factor change in odds per unit change in feature $j$.
- Strict Concavity: The logistic regression log-likelihood Hessian is always negative definite; IRLS is guaranteed to reach the global optimum without local minima.