Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Logistic Regression

The linear predictor can take any real value, while a probability must stay between 0 and 1. A link function connects these two scales.

Linearity

The equations below define the link and convert the linear predictor back to a probability.

logit(p)=log⁡p1−p\text{logit}(p) = \log\frac{p}{1-p}
logit(p)=β0+β1x1+...+βpxp\text{logit}(p) = \beta_0 + \beta_1x_1 + ... + \beta_px_p
  • Log-odds (logit) allow transformation from range (0,1)(0,1) to (−∞,∞)(-\infty, \infty).

  • Hence, logit(p)\text{logit}(p) outputs a number on the entire real line.

    p=eβ0+β1x1+eβ0+β1xp = \frac{e^{\beta_0+\beta_1x}}{1+e^{\beta_0+\beta_1x}}

    p=expit(β0+β1x)p = \text{expit}(\beta_0+\beta_1x)
  • Classifying a new observation ii:

    yi^={1if pi≥0.5 (Or any threshold)0otherwise\hat{y_i} = \begin{cases} 1 & \text{if } p_i \geq 0.5 \space \text{(Or any threshold)}\\ 0 & \text{otherwise} \end{cases}

Odds

odds⁡=p1−p=P(Y=1∣X)P(Y=0∣X)\operatorname{odds}=\frac{p}{1-p}=\frac{P(Y=1\mid X)}{P(Y=0\mid X)}

Interpreting Coefficients

Assume that the two equations below differ by 1 unit of xix_i.

logpi1−pi=β0+β1(x1+1)+...+βpxp\text{log}\frac{p_i}{1-p_i} = \beta_0 + \beta_1(x_1+1) + ... + \beta_p x_p

logpj1−pj=β0+β1x1+...+βpxp\text{log}\frac{p_j}{1-p_j} = \beta_0 + \beta_1x_1 + ... + \beta_p x_p

And,

logpi1−pi−logpj1−pj=β1\text{log}\frac{p_i}{1-p_i} - \text{log}\frac{p_j}{1-p_j} = \beta_1

logpi1−pipj1−pj=β1\text{log}\frac{\frac{p_i}{1-p_i}}{\frac{p_j}{1-p_j}}= \beta_1

β1=logoddsioddsj\beta_1 = \text{log}\frac{\text{odds}_i}{\text{odds}_j}

exp(β1)=oddsioddsj\text{exp}(\beta_1) = \frac{\text{odds}_i}{\text{odds}_j}

We can rearrange it as,

oddsj×exp(β1)=oddsi\text{odds}_j \times \text{exp}(\beta_1) = \text{odds}_i

We can state that increasing xix_i by 1, the odds of y=1y=1 increase by factor of exp⁡(β1)\exp(\beta_1).

Odds are not probabilities

An odds ratio multiplies the odds, not the probability. If p=0.2p=0.2, the odds are 0.2/0.8=0.250.2/0.8=0.25. Doubling those odds gives 0.5, so the new probability is 0.5/(1+0.5)=1/30.5/(1+0.5)=1/3, not 0.4.

For a change of Δ\Delta units in xjx_j, the odds ratio is eβjΔe^{\beta_j\Delta}, holding the other predictors fixed. This interpretation assumes that xjx_j enters without interactions or nonlinear terms. With an interaction, its effect also depends on the interacting variable.

For a simple linear predictor, the probability slope is

∂p∂xj=βjp(1−p).\frac{\partial p}{\partial x_j}=\beta_jp(1-p).

The same coefficient therefore produces different probability changes at different starting probabilities. The intercept gives the log-odds when all predictors are zero; that baseline may be outside the observed data unless the predictors have been centered.

Estimation

L(β)=∏i=1N[p(xi;β)]yi⋅[1−p(xi;β)]1−yiL(\beta) = \prod_{i=1}^N \left[p(x_i;\beta)\right]^{y_i} \cdot \left[1 - p(x_i;\beta)\right]^{1 - y_i}
  • The likelihood is maximized over the coefficients by Maximum Likelihood, where p(x;β)=ex′β1+ex′βp(x; \beta) = \frac{e^{x'\beta}}{1+e^{x'\beta}}, and where yiy_i is 0 or 1, and xix_i is a vector.

Properties of MLE

PropertyMeaning
ConsistencyUnder regularity conditions, estimates converge to the true parameter as the sample size grows: β^→β\hat\beta\to\beta.
Asymptotic normalityIn large samples, the estimator has an approximately normal sampling distribution under regularity conditions.
EfficiencyMLE is asymptotically efficient under regularity conditions; this is not a universal finite-sample guarantee.
InvarianceIf θ^\hat\theta is an MLE of θ\theta, then g(θ^)g(\hat\theta) is an MLE of g(θ)g(\theta).

An unbiased estimator satisfies E[θ^]=θE[\hat\theta]=\theta: across repeated samples, its average equals the true parameter. This is a separate property; an MLE need not be unbiased in a finite sample.

From likelihood to loss

Each observation contributes pip_i if yi=1y_i=1 and 1−pi1-p_i if yi=0y_i=0. Taking logs turns the product into

ℓ(β)=∑i=1n{yilog⁡pi+(1−yi)log⁡(1−pi)}.\ell(\beta)=\sum_{i=1}^n\{y_i\log p_i+(1-y_i)\log(1-p_i)\}.

Maximizing this expression is equivalent to minimizing log loss, −ℓ(β)/n-\ell(\beta)/n. A confident wrong prediction receives a large penalty. Predicting p=0.01p=0.01 for an observation with y=1y=1 contributes −log⁡(0.01)≈4.61-\log(0.01)\approx4.61, compared with −log⁡(0.5)≈0.69-\log(0.5)\approx0.69 at p=0.5p=0.5.

Separation

If a predictor or combination of predictors perfectly separates the classes, increasingly large coefficients can keep improving the unpenalized likelihood. A finite MLE may not exist. Large standard errors and extreme fitted probabilities can be symptoms of this problem.

Regularization limits coefficient growth. It is also useful with many correlated predictors. Approximate normal inference for the unpenalized MLE needs a sufficiently informative sample and regularity conditions; it is not an exact finite-sample result.

Usage

  • Logistic regression is less prone to overfitting.

    • Due to linearity making the fit highly inflexible, with no penalty tuning parameter in the unregularized formulation.

    • Cannot bend or wiggle to fit noise.

    • With only raw predictors, captures a linear log-odds relationship; derived features and interactions allow richer patterns.

  • Excels at explaining how individual variables relate to the outcome.

  • Use for binary outcomes and if we want to capture probabilities.

  • Suggested dataset size is approximately medium.

  • Simple, stable, and well-regularized. Hence performs well on real classification problems.

  • Has low variance, so does not overfit easily.

    • Since logistic regression models (log⁡(p1−p)\log(\frac{p}{1-p})) are linear in features, and the decision boundary cannot adapt too much to random noise. Cannot bend to fit small fluctuations.

  • Can produce useful probability estimates when the model is appropriate; calibration should still be checked.

Limitations

  • When the data relationship is highly nonlinear, with complex interactions and nonlinear boundaries.

    • Instead, explore gradient boosting or random forest.

  • High-dimensional sparse data needs regularization; regularized logistic regression can work well in this setting.

  • When classes are extremely imbalanced.

    • Can still use logistic regression, but needs a proper class weighing and threshold tuning.

    • This is because the model is trained to minimize log loss, not to treat both classes equally.

    • When there is a class majorly dominating the other, logistic regression model can predict everything as major class and still achieve higher accuracy.

    • Class weighing assigns higher penalty to mistakes on minority class.


LASSO Regression

Regularization

Criterion

  • Criterion: MSE + Penalty.

    • Least squares estimator: MSE=RSSn\text{MSE} = \frac{\text{RSS}}{n}, where RSS is the Residual Sum Squares, and is RSS=∑i=1n(yi−βtx)2\text{RSS} = \sum_{i=1}^n(y_i-\beta^tx)^2.

    • The Least Square Estimator finds the coefficients that minimize the sum of squared errors between observed and predicted values.

    • Hence, Criterion=RSSn+Penalty\text{Criterion} = \frac{\text{RSS}}{n} + \text{Penalty} for Least Squares Estimator.

L2 Ridge Regression

  • L2 penalty is defined as:

Criterion=RSSn+1n∥β∥2, where ∥β∥=∑i=1pβi2\text{Criterion} = \frac{\text{RSS}}{n} + \frac{1}{n}\|\beta \|^2\text{,} \space \text{where } \| \beta \| = \sqrt{\sum_{i=1}^p\beta_i^2}
  • ∥β∥\| \beta \| is the L2 or Euclidean norm of coefficients.

  • This penalty favours smaller squared coefficients; strongly penalizes large coefficients.

  • No penalty when all coefficients equal zero.

  • Dividing by nn can be skipped.

  • Can be simplified to:

    Criterion=RSSn+1n∑j=1pβj2\text{Criterion} = \frac{\text{RSS}}{n} + \frac{1}{n}\sum_{j=1}^p{\beta_j^2}
    • Ridge uses the squared L2 norm from the start. Removing a square root from only the penalty would change the objective and generally its minimizer.

  • With regularization parameter λ\lambda that decides on how large a penalty should be,

    Criterion=RSSn+λn∑j=1pβj2 ; λ>0\text{Criterion} = \frac{\text{RSS}}{n} + \frac{\lambda}{n}\sum_{j=1}^p{\beta_j^2}\text{ ; }\lambda> 0
  • Coefficients do not shrink to zero. When variables are highly correlated, L2 regulazation performs better.

Methods for the best λ\lambda

  1. Estimate λ\lambda via cross validation.

  2. Find the β\beta’s in the linear model that minimize the criterion above.

  3. Rely on AIC or BIC.

Different Notation

Can also be written as...

min⁡βRSSsuch that∑j=1pβj2≤s\min_{\beta}\text{RSS} \quad \text{such that} \quad \sum_{j=1}^p \beta_j^2 \le s

Here, errors are minimized subject to a constraint.

  • ss: constraint on total coefficient size.

  • For every λ\lambda, there exists some ss.

  • For every ss, there exists some λ\lambda.

  • If there are two xx-variables, then β12+β22≤s\beta_1^2+\beta_2^2 \leq s.

L1 LASSO Regression

  • When we don’t want to penalize large coefficients as strongly.

Criterion=RSSn+λn∑j=1p∣βj∣ ; λ>0\text{Criterion} = \frac{\text{RSS}}{n} + \frac{\lambda}{n}\sum_{j=1}^p{|\beta_j|}\text{ ; }\lambda> 0
  • Here, coefficients are shrinked to zero. Used for variable selection.

  • Whether lasso or ridge predicts better depends on the data; compare them using validation error.

  • Large λ\lambda means selecting fewer variables.

Different Notation

min⁡βRSSsuch that∑j=1p∣βj∣≤s\min_{\beta}\text{RSS} \quad \text{such that} \quad \sum_{j=1}^p |\beta_j| \le s
  • ss: constraint on total coefficient size.

  • If there are two xx-variables, then ∣β1∣+∣β2∣≤s|\beta_1|+|\beta_2| \leq s.

  • Every corners of the diamond correspond to one of β1\beta_1 and β2\beta_2 being 0.

PropertyL1 (Lasso)L2 (Ridge)
Penalty∑∣βj∣\sum \lvert\beta_j\rvert∑βj2\sum \beta_j^2
Feature selectionYes (sets some coefficients to 0)No (keeps all features)
SparsityProduces sparse modelsNo sparsity
Correlated featuresTends to pick oneDistributes weights across them
StabilityLess stableMore stable
Best use caseHigh-dimensional, few relevant varsMany relevant, correlated features
InterpretabilityHighModerate

Regularized Logistic Regression

  • From the regularization criterion above, replace RSS\text{RSS} with log⁡likelihood\log \text{likelihood}.

    • Instead of RSS minimization, likelihood must be maximized. (Or, minimize the negative likelihood.)

Criterion=−log⁡likelihood+λn∑j=1pβj2 ; λ>0\text{Criterion} = -\log\text{likelihood} + \frac{\lambda}{n}\sum_{j=1}^p{\beta_j^2}\text{ ; }\lambda> 0
Criterion=−log⁡likelihood+λn∑j=1pβj2⋅Var(xj)\text{Criterion} = -\log\text{likelihood} + \frac{\lambda}{n}\sum_{j=1}^p{\beta_j^2}\cdot\text{Var}(x_j)
  • Sometimes, Var(xj)\text{Var}(x_j) is included by taking into account the different scales of xx-variables.

    • Because, if left as is, βj2\beta_j^2 assigns smaller coefficient to features with large scales. To make this fair across all features, apply variance-weighted penalty.

    • In this case, no need to standardize xx-variables.

Keeping the penalty convention consistent

A convenient convention is average negative log-likelihood plus a penalty:

min⁡β0,β[−ℓ(β0,β)n+λP(β)],\min_{\beta_0,\beta}\left[-\frac{\ell(\beta_0,\beta)}{n}+\lambda P(\beta)\right],

where P(β)=∑j∣βj∣P(\beta)=\sum_j|\beta_j| for lasso and P(β)=12∑jβj2P(\beta)=\tfrac12\sum_j\beta_j^2 for ridge. The intercept is usually left unpenalized. Factors such as nn and 1/21/2 change the numerical meaning of λ\lambda, so compare formulas before comparing penalty values.

Standardize continuous predictors before applying a common penalty. Otherwise, changing a variable from metres to centimetres changes the coefficient size and therefore its penalty, even though the underlying information is unchanged. Choose λ\lambda using validation data or cross-validation, with scaling estimated within each training fold.

Lasso can set coefficients exactly to zero; ridge usually shrinks them continuously. Neither gives a universal prediction advantage. With correlated predictors, several different lasso selections may give similar predictions, so selection alone should not be treated as evidence that one variable is uniquely important.

SVM Regularization

  • SVM tries to maximize the margin between classes while allowing some errors (ϵi\epsilon_i).

  • SVM is another regularized optimization problem with a different loss function.

    • Loss function: Measure of how wrong the model’s predictions are. Smaller the better.

    • Loss = error between y and y^\text{Loss = error between } y \text{ and } \hat{y}.

  • Find a boundary, f(x)=β0+βTxf(x) = \beta_0 + \beta^T x, that can make correct classifications with a large margin.

    min⁡β∑i=1nmax⁡(0,1−yif(xi))+λ∑j=1pβj2\min_{\beta} \sum_{i=1}^n \max(0, 1 - y_i f(x_i)) + \lambda \sum_{j=1}^p \beta_j^2
  • We are minimizng the loss and penalty added together.

    • Loss: ∑i=1nmax⁡(0,1−yif(xi))\sum_{i=1}^n \max(0, 1 - y_i f(x_i)). This is hinge loss.

    • yi∈{−1,+1}y_i \in \{-1, +1\}; True label classes are either encoded as -1 or +1.

    • In SVM, +1 refers to one class, and -1 refers to other class.

CaseValue
Correct and far away0
Correct but closesmall penalty
Wronglarge penalty